-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
206 lines (170 loc) · 5.22 KB
/
app.js
File metadata and controls
206 lines (170 loc) · 5.22 KB
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
const RAY_BEGIN_A = 10;
const RAY_BEGIN_B = 11;
const MIRROR_BEGIN_A = 12;
const MIRROR_BEGIN_B = 13;
const BEAM_BEGIN_A = 14;
const BEAM_BEGIN_B = 15;
const NOTHING = 20;
class App {
constructor() {
w.cv = d.getElementById("cv");
w.ctx = cv.getContext("2d");
this.resize();
w.t0 = Date.now();
this.mode = NOTHING;
this.ray_added_id = 0;
this.beam_added_id = 0;
this.mirror_added_id = 0;
w.omp = new Vec2(0, 0);
w.mp = new Vec2(0, 0);
w.msdown = 0;
this.init_cb();
w.rays = [];
w.beams = [];
w.mirrors = []
w.lens = [];
w.rays.push(new Ray(new Vec2(-100, 100), new Vec2(-90, 100), true));
w.mirrors.push(new Mirror(new Vec2(0, 175), new Vec2(175, 0), 1));
w.fps = new FPS();
w.grid = new Grid(25, 25, 8, 'rgb(200, 0, 255)', 'rgb(255, 0, 0)');
}
init_cb() {
w.cv.addEventListener('mousedown', x => {
w.ptime = Date.now();
w.msdown = true;
});
w.cv.addEventListener('mouseup', x => {
w.ltime = Date.now() - w.ptime;
w.msdown = false;
});
/* Buttons */
d.getElementById('rb0').addEventListener('click', x => {
this.mode = RAY_BEGIN_A;
});
d.getElementById('mb0').addEventListener('click', x => {
this.mode = MIRROR_BEGIN_A;
});
d.getElementById('bb0').addEventListener('click', x => {
this.mode = BEAM_BEGIN_A;
});
d.getElementById('nb0').addEventListener('click', x => {
this.mode = this.MODE_NONE;
});
w.cv.addEventListener('mousemove', this.mousemove);
w.cv.addEventListener('click', this.click.bind(this));
w.addEventListener('resize', this.resize, false);
}
resize() {
cv.width = w.innerWidth - 4;
cv.height = w.innerHeight - $("#pan0").height() - 5;
}
mousemove(evt) {
var rect = cv.getBoundingClientRect();
w.omp = new Vec2( (evt.clientX - rect.left),
(evt.clientY - rect.top) );
w.mp = new Vec2(-(cv.width/2.0 - (evt.clientX - rect.left)),
(cv.height/2.0 - (evt.clientY - rect.top)) );
}
// Click on canvas callback
click(evt) {
if(this.mode == RAY_BEGIN_A) {
w.rays.push(new Ray(mp, mp));
this.ray_added_id = w.rays.length - 1;
this.mode = RAY_BEGIN_B;
}
else if(this.mode == RAY_BEGIN_B) {
w.rays[this.ray_added_id].dir = mp;
this.mode = NOTHING;
}
else if(this.mode == MIRROR_BEGIN_A) {
w.mirrors.push(new Mirror(mp, mp));
this.mirror_added_id = w.mirrors.length - 1;
this.mode = MIRROR_BEGIN_B;
}
else if(this.mode == MIRROR_BEGIN_B) {
w.mirrors[this.mirror_added_id].dir = mp;
this.mode = NOTHING;
}
else if(this.mode == BEAM_BEGIN_A) {
w.beams.push(new Beam(mp, mp, 10));
this.beam_added_id = w.beams.length - 1;
this.mode = BEAM_BEGIN_B;
}
else if(this.mode == BEAM_BEGIN_B) {
w.beams[this.beam_added_id].dir = mp;
this.mode = NOTHING;
}
}
// Just update all rays
update_all_rays() {
for(let i=0;i<w.rays.length;++i) {
w.rays[i].need_update = true;
}
}
// If adding mirror or ray
// animate it when moving mouse
update_ray_add() {
if(this.mode == RAY_BEGIN_B) {
w.rays[this.ray_added_id].dir = mp;
w.rays[this.ray_added_id].generate_points();
w.rays[this.ray_added_id].need_update = true;
}
else if(this.mode == BEAM_BEGIN_B) {
let b = w.beams[this.beam_added_id];
b.dir = mp;
b.brays = [];
b.init_rays(b.density);
}
else if(this.mode == MIRROR_BEGIN_B) {
w.mirrors[this.mirror_added_id].dir = mp;
this.update_all_rays();
}
}
/* Just render app */
render() {
w.t = Date.now() - w.t0;
fps.begin();
// Clear canvas
ctx.clearRect(0, 0, cv.width, cv.height);
this.update_ray_add();
w.rid = 0;
/*
* Draw all rays
*/
for(let i=0;i<w.rays.length;++i) {
let r = w.rays[i];
r.draw(++rid);
}
/*
* ... all beams
*/
for(let i=0;i<w.beams.length;++i) {
let b = w.beams[i];
b.draw();
for(let j=0;j<b.brays.length;++j)
b.brays[j].draw(++rid);
}
/*
* And all mirrors
*/
for(let i=0;i<w.mirrors.length;++i) {
let m = w.mirrors[i];
m.draw();
}
for(let i=0;i<w.lens.length;++i) {
let l = w.lens[i];
l.draw();
}
/*
* Draw grid and fps
* nothing important
*/
grid.draw();
fps.draw();
//throw '';
requestAnimationFrame(this.render.bind(this));
}
run() {
this.render();
}
}