-
Notifications
You must be signed in to change notification settings - Fork 0
/
vector-subtraction.html
127 lines (107 loc) · 3.48 KB
/
vector-subtraction.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<title>Vector Subtraction</title>
<meta name="description" content="" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link rel="stylesheet" href="content-frame-styles.css" />
</head>
<body>
<script type="module">
import {
World,
System,
TagComponent
} from "//unpkg.com/ecsy@0.2.1/build/ecsy.module.js";
import Vector from "./vector.js";
// Initialize canvas
let canvas = document.querySelector("canvas");
let canvasWidth = (canvas.width = window.innerWidth);
let canvasHeight = (canvas.height = window.innerHeight);
let ctx = canvas.getContext("2d");
window.addEventListener(
"resize",
() => {
canvasWidth = canvas.width = window.innerWidth;
canvasHeight = canvas.height = window.innerHeight;
},
false
);
let mouseX, mouseY;
window.addEventListener("mousemove", e => {
mouseX = e.offsetX;
mouseY = e.offsetY;
});
//----------------------
// Components
//----------------------
// Position component
class Position {
constructor() {
this.x = this.y = 0;
}
}
// Line component
class Line {
constructor() {
this.color = "#000";
this.thickness = 4;
}
}
// Renderable component
class Renderable extends TagComponent {}
//----------------------
// Systems
//----------------------
class RendererSystem extends System {
execute(delta, time) {
ctx.globalAlpha = 1;
ctx.fillStyle = "#fff";
ctx.setTransform(1, 0, 0, 1, 0, 0);
ctx.fillRect(0, 0, canvasWidth, canvasHeight);
// Iterate through all the entities on the query
this.queries.renderables.results.forEach(entity => {
const position = entity.getMutableComponent(Position);
const line = entity.getMutableComponent(Line);
const center = new Vector(canvasWidth / 2, canvasHeight / 2);
const mouse = new Vector(mouseX, mouseY);
mouse.sub(center);
ctx.translate(center.x, center.y);
ctx.strokeStyle = line.color;
ctx.lineWidth = line.thickness;
ctx.beginPath();
ctx.moveTo(position.x, position.y);
ctx.lineTo(mouse.x, mouse.y);
ctx.stroke();
});
}
}
RendererSystem.queries = {
renderables: { components: [Renderable, Line, Position] }
};
// Create world and register the systems on it
const world = new World();
world.registerSystem(RendererSystem);
world
.createEntity()
.addComponent(Line)
.addComponent(Position)
.addComponent(Renderable);
// Run!
function run() {
// Compute delta and elapsed time
const time = performance.now();
const delta = time - lastTime;
// Run all the systems
world.execute(delta, time);
lastTime = time;
requestAnimationFrame(run);
}
let lastTime = performance.now();
run();
</script>
<canvas></canvas>
</body>
</html>