Skip to content

Commit

Permalink
refactor: use builtin decode method to await image
Browse files Browse the repository at this point in the history
  • Loading branch information
robertrosman committed May 9, 2024
1 parent 211f1f1 commit ea85775
Showing 1 changed file with 12 additions and 14 deletions.
26 changes: 12 additions & 14 deletions src/utils/toCanvas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,16 @@ interface Options {
export async function toCanvas({ svg, canvas, crop }: Options) {
const image64 = toImgSrc({ svg, crop })

return new Promise<void>((resolve, reject) => {
// set it as the source of the img element
const img = new Image()
img.onload = () => {
// draw the image onto the canvas once loaded
const unreffedCanvas = unref(canvas)
unreffedCanvas.height = img.height
unreffedCanvas.width = img.width
unreffedCanvas.getContext('2d')?.drawImage(img, 0, 0);
resolve()
}
img.onerror = () => reject(img);
img.src = image64;
});
const img = new Image()
img.src = image64;

// Wait for image to load
await img.decode()

// draw the image onto the canvas once loaded
const unreffedCanvas = unref(canvas)
unreffedCanvas.height = img.height
unreffedCanvas.width = img.width
unreffedCanvas.getContext('2d')?.drawImage(img, 0, 0);

}

0 comments on commit ea85775

Please sign in to comment.