-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
481 lines (419 loc) · 14.9 KB
/
main.js
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
//import CanvasSVG from "canvas-svg/trunk/canvas-getsvg";
document.addEventListener("DOMContentLoaded", function(event) {
let mode;
let brightness = 50;
let first_step_obstacle = false;
let density = 7;
let point_sources = [];
let flat_mirrors = [];
let obstacles = [];
let beams = [];
let rays = [];
let point_buffer = [];
let mousePosition = {
x: 0,
y: 0
};
let density_value = document.getElementById('density_value');
let density_slider = document.getElementById('density_slider');
let brightness_value = document.getElementById('brightness_value');
let brightness_slider = document.getElementById('brightness_slider');
let canvas_wrapper = document.getElementsByClassName('canvas');
let layer1 = document.getElementById('layer1');
let layer2 = document.getElementById('layer2');
let ctx1 = layer1.getContext("2d");
let ctx2 = layer2.getContext("2d");
let ray_btn = document.getElementById("ray");
let beam_btn = document.getElementById("beam");
let point_btn = document.getElementById("point_source");
let flat_mirror_btn = document.getElementById("flat_mirror");
let coll_lens_btn = document.getElementById("coll_lens");
let diff_lens_btn = document.getElementById("diff_lens");
let obstacle_btn = document.getElementById("obstacle");
let save_btn = document.getElementById("saveButton");
ray_btn.addEventListener("mouseup", (event)=>{mode=1});
beam_btn.addEventListener("mouseup", (event)=>{mode=2});
point_btn.addEventListener("mouseup", (event)=>{mode=3});
flat_mirror_btn.addEventListener("mouseup", (event)=>{mode=4});
coll_lens_btn.addEventListener("mouseup", (event)=>{mode=5});
diff_lens_btn.addEventListener("mouseup", (event)=>{mode=6});
obstacle_btn.addEventListener("mouseup", (event)=>{mode=7});
save_btn.addEventListener("mouseup", (event)=>{
//download(createSvg(), "image", "image/svg+xml");
download(createSvg(), "image", "text/html");
});
let canvas_offset = layer1.getBoundingClientRect();
let cs = getComputedStyle(canvas_wrapper[0]);
let width = parseInt(cs.getPropertyValue('width'), 10);
let height = parseInt(cs.getPropertyValue('height'), 10);
layer1.width = width;
layer1.height = height;
layer2.width = width;
layer2.height = height;
let ctxSVG = new C2S(width,height);
add_borders();
density_slider.addEventListener("input",(event) => {
density = event.target.value;
density_value.innerHTML = density;
renderAll();
});
brightness_slider.addEventListener("input",(event) => {
brightness = event.target.value;
brightness_value.innerHTML = `${brightness}%`;
renderAll()
});
clear(ctx2);
layer1.addEventListener("mousemove", (event) => {
mousePosition = {
x: event.pageX - canvas_offset.x,
y: event.pageY - canvas_offset.y
}
ctx2.beginPath();
clear(ctx2);
draw_point(mousePosition.x, mousePosition.y, ctx2);
ctx2.stroke();
ctx1.strokeStyle = `rgba(255, 255, 255, ${brightness/100})`;
ctxSVG.strokeStyle = `rgba(255, 255, 255, ${brightness/100})`;
ctx2.strokeStyle = `rgba(255, 255, 255, ${brightness/100})`;
if(first_step_obstacle) {
ctx2.beginPath();
draw_point(point_buffer[0].x, point_buffer[0].y, ctx2);
ctx2.moveTo(point_buffer[0].x, point_buffer[0].y);
ctx2.lineTo(mousePosition.x, mousePosition.y);
ctx2.stroke();
}
});
layer1.addEventListener("mouseup", (event) => {
switch (mode) {
case 1:
add_ray();
break;
case 2:
add_beam();
break;
case 3:
add_point_source(event);
break;
case 4:
add_flat_mirror();
break;
case 5:
add_coll_lens();
break;
case 6:
add_diff_lens();
break;
case 7:
add_obstacle(event);
break;
}
renderAll();
});
function add_two_point_entity (arr) {
if(!first_step_obstacle) {
first_step_obstacle = true;
point_buffer.push(mousePosition);
}
else {
point_buffer.push(mousePosition);
arr.push(point_buffer);
first_step_obstacle = false;
point_buffer = [];
}
}
function add_ray() {
add_two_point_entity(rays);
}
function add_flat_mirror() {
add_two_point_entity(flat_mirrors);
}
function add_obstacle() {
add_two_point_entity(obstacles);
}
function add_beam() {
add_two_point_entity(beams);
}
function add_borders() {
obstacles.push([
{x:-1,y:-1},
{x:-1,y:layer1.height}]);
obstacles.push([
{x:-1,y:-1},
{x:layer1.width,y:-1}]);
obstacles.push([
{x:-1,y:layer1.height},
{x:layer1.width,y:layer1.height}]);
obstacles.push([
{x:layer1.width,y:-1},
{x:layer1.width,y:layer1.height}]);
}
function add_point_source(event) {
point_sources.push([mousePosition.x, mousePosition.y]);
}
function render_obstacles() {
ctx1.beginPath();
ctx1.strokeStyle = `red`;
obstacles.forEach((obstacle)=>{
draw_point(obstacle[0].x, obstacle[0].y, ctx1);
draw_point(obstacle[1].x, obstacle[1].y, ctx1);
ctx1.moveTo(obstacle[0].x, obstacle[0].y);
ctx1.lineTo(obstacle[1].x, obstacle[1].y);
ctx1.stroke();
});
ctxSVG.beginPath();
ctxSVG.strokeStyle = `red`;
obstacles.forEach((obstacle)=>{
draw_point(obstacle[0].x, obstacle[0].y, ctxSVG);
draw_point(obstacle[1].x, obstacle[1].y, ctxSVG);
ctxSVG.moveTo(obstacle[0].x, obstacle[0].y);
ctxSVG.lineTo(obstacle[1].x, obstacle[1].y);
ctxSVG.stroke();
});
}
function render_flat_mirrors() {
ctx1.beginPath();
ctx1.strokeStyle = `blue`;
flat_mirrors.forEach((mirror)=>{
draw_point(mirror[0].x, mirror[0].y, ctx1);
draw_point(mirror[1].x, mirror[1].y, ctx1);
ctx1.moveTo(mirror[0].x, mirror[0].y);
ctx1.lineTo(mirror[1].x, mirror[1].y);
ctx1.stroke();
});
ctxSVG.beginPath();
ctxSVG.strokeStyle = `blue`;
flat_mirrors.forEach((mirror)=>{
draw_point(mirror[0].x, mirror[0].y, ctxSVG);
draw_point(mirror[1].x, mirror[1].y, ctxSVG);
ctxSVG.moveTo(mirror[0].x, mirror[0].y);
ctxSVG.lineTo(mirror[1].x, mirror[1].y);
ctxSVG.stroke();
});
}
function draw_point(x, y, ctx) {
ctx.fillStyle = "red";
ctx.fillRect(x - 2, y - 2, 4, 4);
}
function clear(ctx) {
ctx.fillStyle = "black";
ctx.fillRect(0, 0, layer1.width, layer1.height);
}
function update_sources() {
ctx1.strokeStyle = `rgba(255, 255, 255, ${brightness/100})`;
ctxSVG.strokeStyle = `rgba(255, 255, 255, ${brightness/100})`;
point_sources.forEach((source)=>{
let step = 2 * Math.PI / parseInt(density_value.innerHTML, 10);
for (let i = 0; i < 2 * Math.PI; i += step) {
renderRay([
{x: source[0], y: source[1]},
{x: source[0] + Math.sin(i)*5000, y: source[1] + Math.cos(i)*5000}
]);
}
});
rays.forEach((source)=>{
ctx1.strokeStyle = `rgba(255, 255, 255, ${brightness/100})`;
draw_point(source[0].x, source[0].y, ctx1);
ctxSVG.strokeStyle = `rgba(255, 255, 255, ${brightness/100})`;
draw_point(source[0].x, source[0].y, ctxSVG);
renderRay(source);
});
beams.forEach((source)=>{
draw_point(source[0].x, source[0].y, ctx1);
draw_point(source[1].x, source[1].y, ctx1);
ctx1.strokeStyle = "gray"
ctx1.moveTo(source[0].x, source[0].y);
ctx1.lineTo(source[1].x, source[1].y);
ctx1.stroke();
ctx1.strokeStyle = `rgba(255, 255, 255, ${brightness/100})`;
draw_point(source[0].x, source[0].y, ctxSVG);
draw_point(source[1].x, source[1].y, ctxSVG);
ctxSVG.strokeStyle = "gray"
ctxSVG.moveTo(source[0].x, source[0].y);
ctxSVG.lineTo(source[1].x, source[1].y);
ctxSVG.stroke();
ctxSVG.strokeStyle = `rgba(255, 255, 255, ${brightness/100})`;
let numOfRays = parseInt(density_value.innerHTML, 10);
let vector = {
x: (source[1].x - source[0].x) / numOfRays,
y: (source[1].y - source[0].y) / numOfRays
}
let length = lengthVec(vector);
let step = length / numOfRays;
let normal = getNormalVector(vector);
let checkNormalDirection = normal.x * vector.y - normal.y * vector.x;
if (checkNormalDirection > 0) {
normal = {
x: -normal.x,
y: -normal.y
};
}
for (let i = 0; i < numOfRays; i ++) {
let start = {
x: source[0].x + vector.x*i,
y: source[0].y + vector.y*i
};
let end = {
x: start.x + normal.x,
y: start.y + normal.y
}
renderRay([
start,
end
]);
}
});
}
function renderAll() {
ctx1.clearRect(0, 0, layer1.width, layer2.height);
ctxSVG.clearRect(0, 0, layer1.width, layer2.height);
update_sources();
render_flat_mirrors();
render_obstacles();
}
function renderRay(ray) {
let minDistance = Infinity;
let minIntersect;
let currentMirror;
obstacles.forEach((obstacle)=>{
let intersect = checkIntersect(obstacle, ray);
if (intersect) {
let dist = distance(ray[0].x, ray[0].y, intersect.x, intersect.y);
if (minDistance > dist) {
minDistance = dist;
minIntersect = intersect;
}
}
});
flat_mirrors.forEach((mirror)=>{
let intersect = checkIntersect(mirror, ray);
if (intersect) {
let dist = distance(ray[0].x, ray[0].y, intersect.x, intersect.y);
if (minDistance > dist) {
minDistance = dist;
minIntersect = intersect;
currentMirror = mirror;
}
}
});
ctx1.beginPath();
ctxSVG.beginPath();
if(minIntersect) {
ctx1.moveTo(ray[0].x, ray[0].y);
ctx1.lineTo(minIntersect.x, minIntersect.y);
ctx1.stroke();
ctxSVG.moveTo(ray[0].x, ray[0].y);
ctxSVG.lineTo(minIntersect.x, minIntersect.y);
ctxSVG.stroke();
if(currentMirror) {
let n = getNormalVector({
x:currentMirror[1].x - currentMirror[0].x,
y:currentMirror[1].y - currentMirror[0].y
});
let l = {
x: (ray[0].x - ray[1].x),
y: (ray[0].y - ray[1].y)
};
let ln = dotProduct(l,n);
if(ln < 0) {
n = {
x: -n.x,
y: -n.y
};
ln = -ln;
}
let resVector = normalize({
x: -l.x + 2 * n.x * ln,
y: -l.y + 2 * n.y * ln
});
renderRay([
{
x: minIntersect.x + resVector.x*5,
y: minIntersect.y + resVector.y*5
},
{
x: resVector.x*10 + minIntersect.x,
y: resVector.y*10 + minIntersect.y
}
]);
}
}
else {
ctx1.moveTo(ray[0].x, ray[0].y);
ctx1.lineTo(ray[1].x, ray[1].y);
ctx1.stroke();
ctxSVG.moveTo(ray[0].x, ray[0].y);
ctxSVG.lineTo(ray[1].x, ray[1].y);
ctxSVG.stroke();
}
}
function normalize(vec) {
let length = lengthVec(vec);
return({
x: vec.x / length,
y: vec.y / length
});
}
function dotProduct(vec1, vec2) {
return vec1.x*vec2.x + vec1.y*vec2.y;
}
function getNormalVector(vec) {
let n = {
x: -vec.y/vec.x,
y: 1
}
return (normalize(n));
}
function distance(x1, y1, x2, y2) {
return Math.sqrt((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1));
}
function lengthVec(vec) {
return Math.sqrt(dotProduct(vec,vec));
}
function checkIntersect(obstacle, ray) {
const x1 = obstacle[0].x;
const y1 = obstacle[0].y;
const x2 = obstacle[1].x;
const y2 = obstacle[1].y;
const x3 = ray[0].x;
const y3 = ray[0].y;
const x4 = ray[1].x;
const y4 = ray[1].y;
const den = (x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4);
if (den == 0) {
return;
}
const t = ((x1 - x3) * (y3 - y4) - (y1 - y3) * (x3 - x4)) / den;
const u = -((x1 - x2) * (y1 - y3) - (y1 - y2) * (x1 - x3)) / den;
if (t > 0 && t < 1 && u > 0) {
let intersect = {
x: x1 + t * (x2 - x1),
y: y1 + t * (y2 - y1)
}
return intersect;
} else {
return;
}
}
function download(data, filename, type) {
let file = new Blob([data], {type: type});
if (window.navigator.msSaveOrOpenBlob) // IE10+
window.navigator.msSaveOrOpenBlob(file, filename);
else { // Others
let a = document.createElement("a"),
url = URL.createObjectURL(file);
a.href = url;
a.download = filename;
document.body.appendChild(a);
a.click();
setTimeout(function() {
document.body.removeChild(a);
window.URL.revokeObjectURL(url);
}, 0);
}
}
function createSvg() {
var mySerializedSVG = ctxSVG.getSerializedSvg();
var svg = ctxSVG.getSvg();
return svg.outerHTML.slice(0, 5) + "style=\"background-color:black\"" + svg.outerHTML.slice(4);
}
});