Skip to content

Commit

Permalink
feat: add destroy method
Browse files Browse the repository at this point in the history
  • Loading branch information
drawcall committed Sep 10, 2021
1 parent 6fe846c commit d4ade61
Show file tree
Hide file tree
Showing 5 changed files with 319 additions and 306 deletions.
2 changes: 2 additions & 0 deletions src/render/BaseRenderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ export default class BaseRenderer {

destroy() {
this.remove();
this.pool.destroy();
this.element = null;
}

remove(proton) {
Expand Down
36 changes: 8 additions & 28 deletions src/render/PixelRenderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,27 +20,13 @@ export default class PixelRenderer extends BaseRenderer {
}

createImageData(rectangle) {
this.rectangle = rectangle
? rectangle
: new Rectangle(0, 0, this.element.width, this.element.height);
this.imageData = this.context.createImageData(
this.rectangle.width,
this.rectangle.height
);
this.context.putImageData(
this.imageData,
this.rectangle.x,
this.rectangle.y
);
this.rectangle = rectangle ? rectangle : new Rectangle(0, 0, this.element.width, this.element.height);
this.imageData = this.context.createImageData(this.rectangle.width, this.rectangle.height);
this.context.putImageData(this.imageData, this.rectangle.x, this.rectangle.y);
}

onProtonUpdate() {
this.context.clearRect(
this.rectangle.x,
this.rectangle.y,
this.rectangle.width,
this.rectangle.height
);
this.context.clearRect(this.rectangle.x, this.rectangle.y, this.rectangle.width, this.rectangle.height);
this.imageData = this.context.getImageData(
this.rectangle.x,
this.rectangle.y,
Expand All @@ -50,11 +36,7 @@ export default class PixelRenderer extends BaseRenderer {
}

onProtonUpdateAfter() {
this.context.putImageData(
this.imageData,
this.rectangle.x,
this.rectangle.y
);
this.context.putImageData(this.imageData, this.rectangle.x, this.rectangle.y);
}

onParticleCreated(particle) {}
Expand All @@ -63,20 +45,18 @@ export default class PixelRenderer extends BaseRenderer {
if (this.imageData) {
this.setPixel(
this.imageData,
Math.floor(particle.p.x - this.rectangle.x),
Math.floor(particle.p.y - this.rectangle.y),
(particle.p.x - this.rectangle.x) >> 0,
(particle.p.y - this.rectangle.y) >> 0,
particle
);
}
}

setPixel(imagedata, x, y, particle) {
const rgb = particle.rgb;
if (x < 0 || x > this.element.width || y < 0 || y > this.elementwidth)
return;
if (x < 0 || x > this.element.width || y < 0 || y > this.elementwidth) return;

const i = ((y >> 0) * imagedata.width + (x >> 0)) * 4;

imagedata.data[i] = rgb.r;
imagedata.data[i + 1] = rgb.g;
imagedata.data[i + 2] = rgb.b;
Expand Down
33 changes: 15 additions & 18 deletions src/render/PixiRenderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ export default class PixiRenderer extends BaseRenderer {
setPIXI(PIXI) {
try {
PIXIClass = PIXI || { Sprite: {} };
this.createFromImage =
PIXIClass.Sprite.from || PIXIClass.Sprite.fromImage;
this.createFromImage = PIXIClass.Sprite.from || PIXIClass.Sprite.fromImage;
} catch (e) {}
}

Expand Down Expand Up @@ -64,19 +63,6 @@ export default class PixiRenderer extends BaseRenderer {
particle.body = null;
}

destroy(particles) {
super.destroy();
this.pool.destroy();

let i = particles.length;
while (i--) {
let particle = particles[i];
if (particle.body) {
this.element.removeChild(particle.body);
}
}
}

transform(particle, target) {
target.x = particle.p.x;
target.y = particle.p.y;
Expand All @@ -96,9 +82,7 @@ export default class PixiRenderer extends BaseRenderer {
}

createSprite(body) {
const sprite = body.isInner
? this.createFromImage(body.src)
: new PIXIClass.Sprite(body);
const sprite = body.isInner ? this.createFromImage(body.src) : new PIXIClass.Sprite(body);

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

return graphics;
}

destroy(particles) {
super.destroy();
this.pool.destroy();

let i = particles.length;
while (i--) {
let particle = particles[i];
if (particle.body) {
this.element.removeChild(particle.body);
}
}
}
}
Loading

0 comments on commit d4ade61

Please sign in to comment.