Skip to content

Commit

Permalink
fix(module:watermark): cleanup event listeners once settled (#8862)
Browse files Browse the repository at this point in the history
  • Loading branch information
arturovt authored Nov 12, 2024
1 parent 40d466d commit decd477
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions components/water-mark/water-mark.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,10 @@ export class NzWaterMarkComponent implements AfterViewInit, OnInit, OnChanges, O

if (this.nzImage) {
const img = new Image();
img.onload = () => {

const onLoad = (): void => {
cleanup();

ctx.drawImage(img, drawX, drawY, drawWidth, drawHeight);

/** Draw interleaved pictures after rotation */
Expand All @@ -270,7 +273,10 @@ export class NzWaterMarkComponent implements AfterViewInit, OnInit, OnChanges, O
ctx.drawImage(img, alternateDrawX, alternateDrawY, drawWidth, drawHeight);
this.appendWatermark(canvas.toDataURL(), markWidth);
};
img.onerror = () =>

const onError = (): void => {
cleanup();

this.drawText(
canvas,
ctx,
Expand All @@ -284,6 +290,16 @@ export class NzWaterMarkComponent implements AfterViewInit, OnInit, OnChanges, O
alternateDrawY,
markWidth
);
};

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

img.addEventListener('load', onLoad);
img.addEventListener('error', onError);

img.crossOrigin = 'anonymous';
img.referrerPolicy = 'no-referrer';
img.src = this.nzImage;
Expand Down

0 comments on commit decd477

Please sign in to comment.