Skip to content

Commit ce6ac3e

Browse files
committed
feat: add type sprite on canvas engine
1 parent dce2101 commit ce6ac3e

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

src/scripts/canvas.js

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ class CanvasEngine extends Engine {
1111
this.canvas.innerHTML = '';
1212
window.cancelAnimationFrame(this.request);
1313

14+
if (this.type === 'sprite') {
15+
this.sprite = new Image();
16+
this.sprite.src = '/sprite.png';
17+
}
18+
1419
// Particle creation
1520
const particles = new Array(this.count);
1621
const rnd = [1, -1];
@@ -45,10 +50,17 @@ class CanvasEngine extends Engine {
4550
else if (r.y + r.size < 0) r.dy *= -1;
4651
if (r.x > this.width) r.dx *= -1;
4752
else if (r.y > this.height) r.dy *= -1;
48-
this.ctx.beginPath();
49-
this.ctx.arc(r.x, r.y, r.size, 0, 2 * Math.PI);
50-
if (this.type === 'fill') this.ctx.fill();
51-
if (this.type != 'sprite') this.ctx.stroke();
53+
if (this.type === 'sprite') {
54+
if (this.sprite.complete) {
55+
this.ctx.beginPath();
56+
this.ctx.drawImage(this.sprite, r.x, r.y);
57+
}
58+
} else {
59+
this.ctx.beginPath();
60+
this.ctx.arc(r.x, r.y, r.size, 0, 2 * Math.PI);
61+
if (this.type === 'fill') this.ctx.fill();
62+
if (this.type != 'sprite') this.ctx.stroke();
63+
}
5264
}
5365

5466
this.fpsmeter.tick();

0 commit comments

Comments
 (0)