|
| 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