Skip to content

Commit d4ade61

Browse files
committed
feat: add destroy method
1 parent 6fe846c commit d4ade61

File tree

5 files changed

+319
-306
lines changed

5 files changed

+319
-306
lines changed

src/render/BaseRenderer.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ export default class BaseRenderer {
6363

6464
destroy() {
6565
this.remove();
66+
this.pool.destroy();
67+
this.element = null;
6668
}
6769

6870
remove(proton) {

src/render/PixelRenderer.js

Lines changed: 8 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -20,27 +20,13 @@ export default class PixelRenderer extends BaseRenderer {
2020
}
2121

2222
createImageData(rectangle) {
23-
this.rectangle = rectangle
24-
? rectangle
25-
: new Rectangle(0, 0, this.element.width, this.element.height);
26-
this.imageData = this.context.createImageData(
27-
this.rectangle.width,
28-
this.rectangle.height
29-
);
30-
this.context.putImageData(
31-
this.imageData,
32-
this.rectangle.x,
33-
this.rectangle.y
34-
);
23+
this.rectangle = rectangle ? rectangle : new Rectangle(0, 0, this.element.width, this.element.height);
24+
this.imageData = this.context.createImageData(this.rectangle.width, this.rectangle.height);
25+
this.context.putImageData(this.imageData, this.rectangle.x, this.rectangle.y);
3526
}
3627

3728
onProtonUpdate() {
38-
this.context.clearRect(
39-
this.rectangle.x,
40-
this.rectangle.y,
41-
this.rectangle.width,
42-
this.rectangle.height
43-
);
29+
this.context.clearRect(this.rectangle.x, this.rectangle.y, this.rectangle.width, this.rectangle.height);
4430
this.imageData = this.context.getImageData(
4531
this.rectangle.x,
4632
this.rectangle.y,
@@ -50,11 +36,7 @@ export default class PixelRenderer extends BaseRenderer {
5036
}
5137

5238
onProtonUpdateAfter() {
53-
this.context.putImageData(
54-
this.imageData,
55-
this.rectangle.x,
56-
this.rectangle.y
57-
);
39+
this.context.putImageData(this.imageData, this.rectangle.x, this.rectangle.y);
5840
}
5941

6042
onParticleCreated(particle) {}
@@ -63,20 +45,18 @@ export default class PixelRenderer extends BaseRenderer {
6345
if (this.imageData) {
6446
this.setPixel(
6547
this.imageData,
66-
Math.floor(particle.p.x - this.rectangle.x),
67-
Math.floor(particle.p.y - this.rectangle.y),
48+
(particle.p.x - this.rectangle.x) >> 0,
49+
(particle.p.y - this.rectangle.y) >> 0,
6850
particle
6951
);
7052
}
7153
}
7254

7355
setPixel(imagedata, x, y, particle) {
7456
const rgb = particle.rgb;
75-
if (x < 0 || x > this.element.width || y < 0 || y > this.elementwidth)
76-
return;
57+
if (x < 0 || x > this.element.width || y < 0 || y > this.elementwidth) return;
7758

7859
const i = ((y >> 0) * imagedata.width + (x >> 0)) * 4;
79-
8060
imagedata.data[i] = rgb.r;
8161
imagedata.data[i + 1] = rgb.g;
8262
imagedata.data[i + 2] = rgb.b;

src/render/PixiRenderer.js

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@ export default class PixiRenderer extends BaseRenderer {
2020
setPIXI(PIXI) {
2121
try {
2222
PIXIClass = PIXI || { Sprite: {} };
23-
this.createFromImage =
24-
PIXIClass.Sprite.from || PIXIClass.Sprite.fromImage;
23+
this.createFromImage = PIXIClass.Sprite.from || PIXIClass.Sprite.fromImage;
2524
} catch (e) {}
2625
}
2726

@@ -64,19 +63,6 @@ export default class PixiRenderer extends BaseRenderer {
6463
particle.body = null;
6564
}
6665

67-
destroy(particles) {
68-
super.destroy();
69-
this.pool.destroy();
70-
71-
let i = particles.length;
72-
while (i--) {
73-
let particle = particles[i];
74-
if (particle.body) {
75-
this.element.removeChild(particle.body);
76-
}
77-
}
78-
}
79-
8066
transform(particle, target) {
8167
target.x = particle.p.x;
8268
target.y = particle.p.y;
@@ -96,9 +82,7 @@ export default class PixiRenderer extends BaseRenderer {
9682
}
9783

9884
createSprite(body) {
99-
const sprite = body.isInner
100-
? this.createFromImage(body.src)
101-
: new PIXIClass.Sprite(body);
85+
const sprite = body.isInner ? this.createFromImage(body.src) : new PIXIClass.Sprite(body);
10286

10387
sprite.anchor.x = 0.5;
10488
sprite.anchor.y = 0.5;
@@ -120,4 +104,17 @@ export default class PixiRenderer extends BaseRenderer {
120104

121105
return graphics;
122106
}
107+
108+
destroy(particles) {
109+
super.destroy();
110+
this.pool.destroy();
111+
112+
let i = particles.length;
113+
while (i--) {
114+
let particle = particles[i];
115+
if (particle.body) {
116+
this.element.removeChild(particle.body);
117+
}
118+
}
119+
}
123120
}

0 commit comments

Comments
 (0)