]> git.r.bdr.sh - rbdr/lissajous/blob - lib/main.js
6a2ea8f9cc427f38372ec488f8298be956c4c6a7
[rbdr/lissajous] / lib / main.js
1 import Serpentity from '@serpentity/serpentity';
2 import WebGLRenderer from './systems/webgl_renderer';
3 import LissajousPositionUpdater from './systems/lissajous_position_updater';
4 import { lissajousCurve } from './factories/curves';
5 import { canvasId, fps } from './config';
6
7 const internals = {
8
9 // State variables
10
11 started: false,
12 currentTime: 0,
13 engine: null,
14
15 load() {
16 // Create entities
17 const engine = new Serpentity();
18
19 // Add Systems
20 engine.addSystem(new LissajousPositionUpdater(canvasId));
21 engine.addSystem(new WebGLRenderer(canvasId));
22
23 // Add Entities
24 engine.addEntity(lissajousCurve());
25
26 internals.engine = engine;
27 },
28
29 unload() {
30 internals.engine = null;
31 },
32
33 run(time) {
34
35 const delta = time - internals.currentTime;
36 const interval = 1000 / fps;
37
38 if (delta >= interval) {
39 internals.engine.update(delta);
40 internals.currentTime = time;
41 }
42
43 internals.started && window.requestAnimationFrame(internals.run);
44 },
45
46 start() {
47 if (!internals.started) {
48 internals.started = true;
49 internals.load();
50 window.requestAnimationFrame(internals.run);
51 }
52 },
53
54 stop() {
55 if (internals.started) {
56 internals.started = false;
57 internals.unload();
58 }
59 }
60 };
61
62 internals.start();