From 48ce5e97128f108e55f198b93d16bc3fd1d51398 Mon Sep 17 00:00:00 2001 From: Anton Lavrevov Date: Wed, 12 Mar 2025 07:48:47 -0700 Subject: [PATCH] fix memory leak. close #1896 --- src/shapes/Image.ts | 23 +++++++++++--- test/sandbox.html | 75 ++++++++++++++++++++++----------------------- 2 files changed, 55 insertions(+), 43 deletions(-) diff --git a/src/shapes/Image.ts b/src/shapes/Image.ts index ae509530..a091b3ec 100644 --- a/src/shapes/Image.ts +++ b/src/shapes/Image.ts @@ -40,9 +40,16 @@ export interface ImageConfig extends ShapeConfig { * imageObj.src = '/path/to/image.jpg' */ export class Image extends Shape { + private _loadListener: () => void; + constructor(attrs: ImageConfig) { super(attrs); - this.on('imageChange.konva', () => { + this._loadListener = () => { + this._requestDraw(); + }; + + this.on('imageChange.konva', (props: any) => { + this._removeImageLoad(props.oldVal); this._setImageLoad(); }); @@ -59,11 +66,19 @@ export class Image extends Shape { return; } if (image && image['addEventListener']) { - image['addEventListener']('load', () => { - this._requestDraw(); - }); + image['addEventListener']('load', this._loadListener); } } + _removeImageLoad(image: any) { + if (image && image['removeEventListener']) { + image['removeEventListener']('load', this._loadListener); + } + } + destroy() { + this._removeImageLoad(this.image()); + super.destroy(); + return this; + } _useBufferCanvas() { const hasCornerRadius = !!this.cornerRadius(); const hasShadow = this.hasShadow(); diff --git a/test/sandbox.html b/test/sandbox.html index 82bf29e9..bf961d4d 100644 --- a/test/sandbox.html +++ b/test/sandbox.html @@ -17,25 +17,9 @@ padding: 0; margin: 0; } - - .test { - position: absolute; - color: red; - font-size: 20px; - font-family: Arial; - border: 0; - background-color: transparent; - outline: none; - resize: none; - overflow: hidden; - line-height: 1; - padding: 0px; - letter-spacing: 20px; - width: 500px; - text-align: center; - } + @@ -52,36 +36,49 @@