Skip to content

Commit

Permalink
fix(module:qrcode): remove event listeners once settled (#8861)
Browse files Browse the repository at this point in the history
  • Loading branch information
arturovt authored Nov 12, 2024
1 parent 472bb34 commit 40d466d
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions components/qr-code/qrcode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,11 @@ export function drawCanvas(
iconImg.crossOrigin = 'anonymous';
iconImg.width = iconSize * (canvas.width / size);
iconImg.height = iconSize * (canvas.width / size);
iconImg.onload = () => {

const onLoad = (): void => {
cleanup();
drawCanvasBackground(ctx, canvas.width, canvas.height, scale, backgroundColor);
drawCanvasColor(ctx, value, scale, formattedPadding, backgroundColor, color);
drawCanvasColor(ctx, value!, scale, formattedPadding, backgroundColor, color);
const iconCoordinate = canvas.width / 2 - (iconSize * (canvas.width / size)) / 2;

ctx.fillRect(iconCoordinate, iconCoordinate, iconSize * (canvas.width / size), iconSize * (canvas.width / size));
Expand All @@ -75,10 +77,20 @@ export function drawCanvas(
iconSize * (canvas.width / size)
);
};
iconImg.onerror = () => {

const onError = (): void => {
cleanup();
drawCanvasBackground(ctx, canvas.width, canvas.height, scale, backgroundColor);
drawCanvasColor(ctx, value, scale, formattedPadding, backgroundColor, color);
};

const cleanup = (): void => {
iconImg.removeEventListener('load', onLoad);
iconImg.removeEventListener('error', onError);
};

iconImg.addEventListener('load', onLoad);
iconImg.addEventListener('error', onError);
}
}

Expand Down

0 comments on commit 40d466d

Please sign in to comment.