mirror of
https://github.com/konvajs/konva.git
synced 2025-06-28 04:42:02 +08:00
fix memory leak. close #1896
This commit is contained in:
parent
3e8f0dff8f
commit
48ce5e9712
@ -40,9 +40,16 @@ export interface ImageConfig extends ShapeConfig {
|
|||||||
* imageObj.src = '/path/to/image.jpg'
|
* imageObj.src = '/path/to/image.jpg'
|
||||||
*/
|
*/
|
||||||
export class Image extends Shape<ImageConfig> {
|
export class Image extends Shape<ImageConfig> {
|
||||||
|
private _loadListener: () => void;
|
||||||
|
|
||||||
constructor(attrs: ImageConfig) {
|
constructor(attrs: ImageConfig) {
|
||||||
super(attrs);
|
super(attrs);
|
||||||
this.on('imageChange.konva', () => {
|
this._loadListener = () => {
|
||||||
|
this._requestDraw();
|
||||||
|
};
|
||||||
|
|
||||||
|
this.on('imageChange.konva', (props: any) => {
|
||||||
|
this._removeImageLoad(props.oldVal);
|
||||||
this._setImageLoad();
|
this._setImageLoad();
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -59,11 +66,19 @@ export class Image extends Shape<ImageConfig> {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (image && image['addEventListener']) {
|
if (image && image['addEventListener']) {
|
||||||
image['addEventListener']('load', () => {
|
image['addEventListener']('load', this._loadListener);
|
||||||
this._requestDraw();
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
_removeImageLoad(image: any) {
|
||||||
|
if (image && image['removeEventListener']) {
|
||||||
|
image['removeEventListener']('load', this._loadListener);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
destroy() {
|
||||||
|
this._removeImageLoad(this.image());
|
||||||
|
super.destroy();
|
||||||
|
return this;
|
||||||
|
}
|
||||||
_useBufferCanvas() {
|
_useBufferCanvas() {
|
||||||
const hasCornerRadius = !!this.cornerRadius();
|
const hasCornerRadius = !!this.cornerRadius();
|
||||||
const hasShadow = this.hasShadow();
|
const hasShadow = this.hasShadow();
|
||||||
|
@ -17,25 +17,9 @@
|
|||||||
padding: 0;
|
padding: 0;
|
||||||
margin: 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;
|
|
||||||
}
|
|
||||||
</style>
|
</style>
|
||||||
<!-- <script src="https://cdn.rawgit.com/hammerjs/touchemulator/master/touch-emulator.js"></script> -->
|
<!-- <script src="https://cdn.rawgit.com/hammerjs/touchemulator/master/touch-emulator.js"></script> -->
|
||||||
|
<script src="https://unpkg.com/gifler@0.1.0/gifler.min.js"></script>
|
||||||
<script>
|
<script>
|
||||||
// TouchEmulator();
|
// TouchEmulator();
|
||||||
</script>
|
</script>
|
||||||
@ -52,36 +36,49 @@
|
|||||||
<script type="module">
|
<script type="module">
|
||||||
import Konva from '../src/index.ts';
|
import Konva from '../src/index.ts';
|
||||||
|
|
||||||
var stageWidth = window.innerWidth;
|
var width = window.innerWidth;
|
||||||
var stageHeight = window.innerHeight;
|
var height = window.innerHeight;
|
||||||
|
|
||||||
Konva._fixTextRendering = true;
|
|
||||||
|
|
||||||
var stage = new Konva.Stage({
|
var stage = new Konva.Stage({
|
||||||
container: 'container',
|
container: 'container',
|
||||||
width: stageWidth,
|
width: width,
|
||||||
height: stageHeight,
|
height: height,
|
||||||
});
|
});
|
||||||
|
|
||||||
Konva._fixTextRendering = true;
|
var layer = new Konva.Layer();
|
||||||
|
|
||||||
const layer = new Konva.Layer();
|
|
||||||
stage.add(layer);
|
stage.add(layer);
|
||||||
|
|
||||||
const shape = new Konva.Text({
|
var canvas = document.createElement('canvas');
|
||||||
x: 50,
|
// use external library to parse and draw gif animation
|
||||||
y: 50,
|
function onDrawFrame(ctx, frame) {
|
||||||
text: 'Hello',
|
// update canvas size
|
||||||
fontSize: 20,
|
canvas.width = frame.width;
|
||||||
fontFamily: 'Arial',
|
canvas.height = frame.height;
|
||||||
letterSpacing: 20,
|
// update canvas that we are using for Konva.Image
|
||||||
align: 'center',
|
ctx.drawImage(frame.buffer, 0, 0);
|
||||||
width: 500,
|
// redraw the layer
|
||||||
});
|
layer.draw();
|
||||||
layer.add(shape);
|
}
|
||||||
|
|
||||||
text.style.top = shape.y() + 'px';
|
gifler('https://konvajs.org/assets/yoda.gif').frames(canvas, onDrawFrame);
|
||||||
text.style.left = shape.x() + 'px';
|
|
||||||
|
function testKonvaImage() {
|
||||||
|
setInterval(() => {
|
||||||
|
const image = new Konva.Image({
|
||||||
|
image: canvas,
|
||||||
|
x: Math.random() * width,
|
||||||
|
y: Math.random() * height,
|
||||||
|
});
|
||||||
|
layer.add(image);
|
||||||
|
|
||||||
|
setTimeout(() => {
|
||||||
|
image.image(canvas);
|
||||||
|
image.destroy();
|
||||||
|
}, 500);
|
||||||
|
}, 10);
|
||||||
|
}
|
||||||
|
|
||||||
|
testKonvaImage();
|
||||||
</script>
|
</script>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
Loading…
Reference in New Issue
Block a user