Skip to content

Commit bc70263

Browse files
authored
Merge pull request #4 from luizbills/litecanvas
Add Litecanvas
2 parents efc77e5 + 3e9f327 commit bc70263

File tree

4 files changed

+100
-0
lines changed

4 files changed

+100
-0
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
"kaboom": "^2000.2.10",
2222
"kaplay": "^3001.0.19",
2323
"kontra": "^10.0.2",
24+
"litecanvas": "0.66.4",
2425
"melonjs": "^17.4.0",
2526
"phaser": "^3.90.0",
2627
"pixi.js": "^7.4.3",

src/litecanvas.html

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
{{#> head }} Canvas — JS Game Rendering Benchmark {{/head}}
4+
<body>
5+
{{> header }}
6+
<main>
7+
{{> container }}
8+
<canvas id="canvas" class="canvas"></canvas>
9+
</main>
10+
{{> footer }}
11+
<script type="module" src="/scripts/litecanvas.js"></script>
12+
</body>
13+
</html>

src/partials/header.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
<a href="./phaser.html">Phaser</a>
1212
<a href="./three.html">Three.js</a>
1313
<a href="./pixi.html">Pixi.js</a>
14+
<a href="./litecanvas.html">Litecanvas</a>
1415
<a href="./canvas.html">Canvas</a>
1516
<a href="./dom.html">DOM</a>
1617
</nav>

src/scripts/litecanvas.js

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
import Engine from './engine.js';
2+
import litecanvas from 'litecanvas';
3+
4+
function init() {}
5+
6+
class LitecanvasEngine extends Engine {
7+
constructor() {
8+
super();
9+
}
10+
init() {
11+
super.init();
12+
13+
// Clear the canvas
14+
this.canvas.innerHTML = '';
15+
window.cancelAnimationFrame(this.request);
16+
17+
// Start the Litecanvas
18+
if (!this.engine) {
19+
this.engine = litecanvas({
20+
canvas: this.canvas,
21+
width: this.width,
22+
height: this.height,
23+
autoscale: false,
24+
25+
// disable the built-in game loop
26+
animate: false,
27+
loop: {},
28+
global: false,
29+
});
30+
}
31+
32+
// Load the sprite
33+
if (this.type === 'sprite') {
34+
this.sprite = new Image();
35+
this.sprite.src = 'sprite.png';
36+
}
37+
38+
// Particle creation
39+
const particles = new Array(this.count);
40+
const rnd = [1, -1];
41+
for (let i = 0; i < this.count; i++) {
42+
const size = 10 + Math.random() * 80;
43+
const x = Math.random() * this.width;
44+
const y = Math.random() * (this.height - size);
45+
const [dx, dy] = [
46+
3 * Math.random() * rnd[Math.floor(Math.random() * 2)],
47+
3 * Math.random() * rnd[Math.floor(Math.random() * 2)],
48+
];
49+
particles[i] = { x, y, size: size, dx, dy };
50+
}
51+
this.particles = particles;
52+
}
53+
render() {
54+
// Clear the canvas
55+
this.engine.cls();
56+
57+
// Particle animation
58+
const particles = this.particles;
59+
for (let i = 0; i < this.count; i++) {
60+
const r = particles[i];
61+
r.x -= r.dx;
62+
r.y -= r.dy;
63+
if (r.x + r.size < 0) r.dx *= -1;
64+
else if (r.y + r.size < 0) r.dy *= -1;
65+
if (r.x > this.width) r.dx *= -1;
66+
else if (r.y > this.height) r.dy *= -1;
67+
if (this.type === 'sprite') {
68+
if (this.sprite.complete) {
69+
this.engine.image(r.x, r.y, this.sprite);
70+
}
71+
} else {
72+
if (this.type === 'fill') this.engine.circfill(r.x, r.y, r.size, 3);
73+
if (this.type != 'sprite') this.engine.circ(r.x, r.y, r.size, 1);
74+
}
75+
}
76+
77+
this.fpsmeter.tick();
78+
this.request = window.requestAnimationFrame(() => this.render());
79+
}
80+
}
81+
82+
document.addEventListener('DOMContentLoaded', () => {
83+
const engine = new LitecanvasEngine();
84+
engine.render();
85+
});

0 commit comments

Comments
 (0)