-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscript.js
1363 lines (1208 loc) · 38.3 KB
/
script.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
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import {changeToolBarStyle, translation } from './helper.js'; // Import the translation function from helper.js
const canvas = document.getElementById("canvas");
const bgCanvas = document.getElementById("bgCanvas");
const context = canvas.getContext("2d");
const bgContext = bgCanvas.getContext("2d");
const downloadCanvas = document.createElement('canvas');
const canvasMoveScale = document.getElementById("canvasMoveScale");
const moveScaleImg = document.getElementById("moveScaleImg");
const undoButton = document.getElementById("undo");
const redoButton = document.getElementById("redo");
const link = document.getElementById("downloadLink");
const loadImgButton = document.getElementById("loadImg");
const downloadButton = document.getElementById("downloadGrid");
const clearButton = document.getElementById("clearCanvas");
const toolsLeft = document.getElementById("toolsLeft");
const toolsRight = document.getElementById("toolsRight");
const ifaceColor = document.getElementById("ifaceColor");
const ifaceNeutral = document.getElementById("ifaceNeutral");
const colors1 = document.getElementById("colors1");
const colors2 = document.getElementById("colors2");
const createVPButton = document.getElementById("createVP");
const vp1Button = document.getElementById("vp1");
const vp2Button = document.getElementById("vp2");
const vp3Button = document.getElementById("vp3");
const horizonButton = document.getElementById("horizon");
const vp1Image = document.getElementById("vp1Image");
const vp2Image = document.getElementById("vp2Image");
const vp3Image = document.getElementById("vp3Image");
const hImage = document.getElementById("hImage");
const moreButton = document.getElementById("more");
const lessButton = document.getElementById("less");
const alertWindow = document.getElementById("alert");
const deleteVPButton = document.getElementById("deleteVP");
const slider = document.getElementById("slider");
const sliderLabel = document.getElementById("sliderLabel");
const maxRadialLines = 360;
const minRadialLines = 8;
const defaultSettings = {
hand: 'left',
iface: 'colorful',
colors: 'pyb',
transparency: '0'
};
const STATE = {
Init: -1,
FirstDottedLine: 0,
FirstLine: 1,
SecondDottedLine: 2,
SecondLine: 3,
VP: 4,
}
let state = STATE.FirstDottedLine;
let currentVP = 0; //when creating them
let selectedVP = -1; //when manipulating them
let radialLines = [20, 20, 20];
let colors = ["HotPink", "Gold", "RoyalBlue"];
let touches = 0;
let isDrawing = false;
let x = 0;
let y = 0;
let offsetX;
let offsetY;
let moveX;
let isMoving = false;
let bgImage;
let bgImageHistory = [];
let dottedLine;
let firstLine;
let secondLine;
let vanishingPoints = [];
let horizonLine;
let aspectRatioWidth = 0.85; //possibly change them in the future to adapt for mobile phones
let asepctRatioHeight = 0.85;
let cameraOffset = { x: window.innerWidth * aspectRatioWidth / 2, y: window.innerHeight * asepctRatioHeight / 2 }
let cameraZoom = 1;
let MAX_ZOOM = 20;
let MIN_ZOOM = 0.05;
let SCROLL_SENSITIVITY = 0.0005;
let mouseWorldPos;
let isDragging = false;
let dragStart = { x: 0, y: 0 };
let initialPinchDistance = null;
let lastZoom = cameraZoom;
let matrix = [1, 0, 0, 1, 0, 0]; // normal matrix
let invMatrix = [1, 0, 0, 1]; // inverse matrix
let timeStart;
let previousDistance;
let isMobile = false;
let pinchPan = 0;
let previousActions = [];
let nextActions = [];
let settings;
/********************************
*** INITIALIZATION ***
********************************/
/**
* Initialize the application.
*/
document.addEventListener("DOMContentLoaded", () => {
if ("ontouchstart" in document.documentElement) { //is mobile device
isMobile = true;
setupTouchEvents();
}
else { //Computer
setupMouseEvents();
}
getLocalSettings();
setupEventListeners();
resetButtons();
state = STATE.Init;
draw();
});
/**
* Set up touch event listeners.
*/
function setupTouchEvents() {
canvas.addEventListener("touchstart", handleTouchStart);
canvas.addEventListener("touchmove", handleTouchMove);
canvas.addEventListener("touchend", handleTouchEnd);
}
/**
* Set up mouse event listeners.
*/
function setupMouseEvents() {
canvas.addEventListener("mousedown", onPointerDown);
canvas.addEventListener("mouseup", onPointerUp);
canvas.addEventListener("mousemove", onPointerMove);
canvas.addEventListener("mouseleave", onPointerLeave);
canvas.addEventListener("wheel", (e) => { adjustZoom(-e.deltaY * SCROLL_SENSITIVITY); });
document.body.addEventListener("keydown", handleKeyDown);
}
/**
* Set up general event listeners.
*/
function setupEventListeners() {
canvasMoveScale.addEventListener("click", switchZoomPan);
undoButton.addEventListener("click", (e) => { undo(); manageVPButtons(-1); });
redoButton.addEventListener("click", (e) => { redo(); manageVPButtons(-1); });
downloadButton.addEventListener("click", (e) => { downloadGrid(); manageVPButtons(-1); });
clearButton.addEventListener("click", (e) => { deleteScene(); manageVPButtons(-1); });
toolsLeft.addEventListener("change", changeSettings);
toolsRight.addEventListener("change", changeSettings);
ifaceColor.addEventListener("change", changeSettings);
ifaceNeutral.addEventListener("change", changeSettings);
colors1.addEventListener("change", changeSettings);
colors2.addEventListener("change", changeSettings);
slider.addEventListener("input", changeBgImgTransparency);
createVPButton.addEventListener("click", (e) => { createVP(); });
vp1Button.addEventListener("click", (e) => { manageVPButtons(0); });
vp2Button.addEventListener("click", (e) => { manageVPButtons(1); });
vp3Button.addEventListener("click", (e) => { manageVPButtons(2); });
horizonButton.addEventListener("click", (e) => { manageVPButtons(3); });
moreButton.addEventListener("click", (e) => { more(); });
lessButton.addEventListener("click", (e) => { less(); });
deleteVPButton.addEventListener("click", (e) => { deleteVP(); });
document.addEventListener("click", (e) => { if (e.target.id == "body") manageVPButtons(-1); });
loadImgButton.addEventListener("change", loadBGImage); //background
}
/**
* Reset button states.
*/
function resetButtons() {
createVPButton.disabled = true;
downloadButton.disabled = true;
vp1Button.hidden = true;
vp2Button.hidden = true;
vp3Button.hidden = true;
horizonButton.hidden = true;
moreButton.hidden = true;
lessButton.hidden = true;
deleteVPButton.hidden = true;
}
/********************************
*** EVENT HANDLERS ***
********************************/
/**
* Handle PointerDown events.
*/
function onPointerDown(e) {
if (selectedVP != -1 && (e.button == 0 || touches != 0)) //moving a VP
{
isMoving = true;
if (e.button == 0) {
if (selectedVP == 3) //horizon line
{
saveAction({ n: "MoveHorizonS", p1: { x: vanishingPoints[0].x, y: vanishingPoints[0].y }, p2: { x: vanishingPoints[1].x, y: vanishingPoints[1].y } });
moveX = (getEventLocation(e).x - cameraOffset.x) / cameraZoom;
vanishingPoints[0].y = (getEventLocation(e).y - cameraOffset.y) / cameraZoom;
vanishingPoints[1].y = (getEventLocation(e).y - cameraOffset.y) / cameraZoom;
createHorizonLine();
}
else {
saveAction({ n: "MoveVPS", p1: { x: vanishingPoints[selectedVP].x, y: vanishingPoints[selectedVP].y }, p2: selectedVP });
vanishingPoints[selectedVP] = { x: (getEventLocation(e).x - cameraOffset.x) / cameraZoom, y: (getEventLocation(e).y - cameraOffset.y) / cameraZoom };
if (vanishingPoints.length > 1) createHorizonLine();
}
}
else {
if (selectedVP == 3) //horizon line
{
moveX = (getEventLocation(e).x - offsetX - cameraOffset.x) / cameraZoom;
vanishingPoints[0].y = (getEventLocation(e).y - offsetY - cameraOffset.y) / cameraZoom;
vanishingPoints[1].y = (getEventLocation(e).y - offsetY - cameraOffset.y) / cameraZoom;
createHorizonLine();
}
else {
vanishingPoints[selectedVP] = { x: (getEventLocation(e).x - offsetX - cameraOffset.x) / cameraZoom, y: (getEventLocation(e).y - offsetY - cameraOffset.y) / cameraZoom };
if (vanishingPoints.length > 1) createHorizonLine();
}
}
return;
}
else if (vanishingPoints.length == 3 && (e.button == 0 || touches == 1)) { //trying to create a new VP
alert(translation("threeVPsAlert"));
return;
}
else if (isDrawing && dottedLine != undefined) //still drawing but the pointer was realeased outside canvas
{
//don't do anything...
}
//Drawing
else if (e.button == 0) {
isDrawing = true;
mouseWorldPos = toWorld(getEventLocation(e).x, getEventLocation(e).y);
x = mouseWorldPos.x;
y = mouseWorldPos.y;
}
else if (touches == 1) { //one finger
isDrawing = true;
mouseWorldPos = toWorld(getEventLocation(e).x - offsetX, getEventLocation(e).y - offsetY);
x = mouseWorldPos.x;
y = mouseWorldPos.y;
}
else { //Dragging with three fingers or right/middle click
isDragging = true;
if (touches == 0) {
dragStart.x = getEventLocation(e).x / cameraZoom - cameraOffset.x;
dragStart.y = getEventLocation(e).y / cameraZoom - cameraOffset.y;
}
else {
dragStart.x = (e.changedTouches[0].clientX - offsetX) / cameraZoom - cameraOffset.x;
dragStart.y = (e.changedTouches[0].clientY - offsetY) / cameraZoom - cameraOffset.y;
}
}
}
/**
* Handle PointerMove events.
*/
function onPointerMove(e) {
if (isMoving) {
if (e.button == 0) {
if (selectedVP == 3) //horizon line
{
const offset = (getEventLocation(e).x - cameraOffset.x) / cameraZoom - moveX;
vanishingPoints[0].x += offset;
vanishingPoints[1].x += offset;
moveX = (getEventLocation(e).x - cameraOffset.x) / cameraZoom;
vanishingPoints[0].y = (getEventLocation(e).y - cameraOffset.y) / cameraZoom;
vanishingPoints[1].y = (getEventLocation(e).y - cameraOffset.y) / cameraZoom;
createHorizonLine();
}
else {
vanishingPoints[selectedVP] = { x: (getEventLocation(e).x - cameraOffset.x) / cameraZoom, y: (getEventLocation(e).y - cameraOffset.y) / cameraZoom };
if (vanishingPoints.length > 1) createHorizonLine();
}
}
else {
if (selectedVP == 3) //horizon line
{
const offset = (getEventLocation(e).x - offsetX - cameraOffset.x) / cameraZoom - moveX;
vanishingPoints[0].x += offset;
vanishingPoints[1].x += offset;
moveX = (getEventLocation(e).x - offsetX - cameraOffset.x) / cameraZoom;
vanishingPoints[1].y = (getEventLocation(e).y - offsetY - cameraOffset.y) / cameraZoom;
vanishingPoints[0].y = (getEventLocation(e).y - offsetY - cameraOffset.y) / cameraZoom;
createHorizonLine();
}
else {
vanishingPoints[selectedVP] = { x: (getEventLocation(e).x - offsetX - cameraOffset.x) / cameraZoom, y: (getEventLocation(e).y - offsetY - cameraOffset.y) / cameraZoom };
if (vanishingPoints.length > 1) createHorizonLine();
}
}
}
else if (isDrawing) {
if (e.button == 0) {
mouseWorldPos = toWorld(getEventLocation(e).x, getEventLocation(e).y);
dottedLine = { x1: x, y1: y, x2: mouseWorldPos.x, y2: mouseWorldPos.y };
}
else {
mouseWorldPos = toWorld(getEventLocation(e).x - offsetX, getEventLocation(e).y - offsetY);
dottedLine = { x1: x, y1: y, x2: mouseWorldPos.x, y2: mouseWorldPos.y };
}
switch (state) {
case STATE.Init:
state = STATE.FirstDottedLine;
break;
case STATE.FirstLine:
state = STATE.SecondDottedLine;
break;
case STATE.SecondLine: //repaint second line
state = STATE.SecondDottedLine;
break;
case STATE.VP:
state = STATE.FirstDottedLine;
break;
}
}
else if (isDragging) {
if (touches == 0) {
cameraOffset.x = getEventLocation(e).x / cameraZoom - dragStart.x;
cameraOffset.y = getEventLocation(e).y / cameraZoom - dragStart.y;
cameraOffset.x = Math.min(cameraOffset.x, canvas.width);
cameraOffset.x = Math.max(cameraOffset.x, 0);
cameraOffset.y = Math.min(cameraOffset.y, canvas.height);
cameraOffset.y = Math.max(cameraOffset.y, 0);
}
else {
cameraOffset.x = (e.changedTouches[0].clientX - offsetX) / cameraZoom - dragStart.x;
cameraOffset.y = (e.changedTouches[0].clientY - offsetY) / cameraZoom - dragStart.y;
}
}
}
/**
* Handle PointerUp events.
*/
function onPointerUp(e) {
if (isMoving) {
isMoving = false;
if (vanishingPoints.length > 1) createHorizonLine();
if (selectedVP == 3) //horizon line
saveAction({ n: "MoveHorizon", p1: { x: vanishingPoints[0].x, y: vanishingPoints[0].y }, p2: { x: vanishingPoints[1].x, y: vanishingPoints[1].y } });
else
saveAction({ n: "MoveVP", p1: { x: vanishingPoints[selectedVP].x, y: vanishingPoints[selectedVP].y }, p2: selectedVP });
}
else if (isDrawing) {
if (e.button == 0) {
mouseWorldPos = toWorld(getEventLocation(e).x, getEventLocation(e).y);
createLine({ x1: x, y1: y, x2: mouseWorldPos.x, y2: mouseWorldPos.y });
}
else {
mouseWorldPos = toWorld(getEventLocation(e).x - offsetX, getEventLocation(e).y - offsetY);
createLine({ x1: x, y1: y, x2: mouseWorldPos.x, y2: mouseWorldPos.y });
}
}
else if (isDragging) {
isDragging = false;
}
}
/**
* Handle PointerLeave events.
*/
function onPointerLeave(e) {
isDragging = false;
isMoving = false;
}
/**
* Touch Start
*/
function handleTouchStart(e) {
touches = e.touches.length;
if (touches > 1) e.preventDefault();
switch (touches) {
case 1: onPointerDown(e); break;
case 2:
timeStart = Date.now();
handlePinchPan(e);
break;
case 3:
if (nextActions.length > 0) redo();
break;
case 4: deleteScene(); break;
}
}
/**
* Touch Move
*/
function handleTouchMove(e) {
if (touches > 1) e.preventDefault();
switch (touches) {
case 1: onPointerMove(e); break;
case 2: if ((Date.now() - timeStart) > 60) handlePinchPan(e); break;
}
}
/**
* Touch End
*/
function handleTouchEnd(e) {
if (touches > 1) e.preventDefault();
switch (touches) {
case 1: onPointerUp(e); break;
case 2:
if ((Date.now() - timeStart) < 60) {
if (previousActions.length > 0) undo();
} else {
initialPinchDistance = null;
lastZoom = cameraZoom;
}
break;
}
touches = 0;
}
/**
* Handle key down events.
*/
function handleKeyDown(e) {
if (e.key == "Enter" && !createVPButton.disabled) {
createVP();
manageVPButtons(-1);
}
if (e.key == "Escape" || e.key == "Esc") {
if (state == STATE.FirstDottedLine || state == STATE.SecondDottedLine) {
isDrawing = false;
dottedLine = { x, y };
}
manageVPButtons(-1);
isDragging = false;
isMoving = false;
}
if (e.key == "1" && vanishingPoints.length > 0) {
manageVPButtons(0);
}
if (e.key == "2" && vanishingPoints.length > 1) {
manageVPButtons(1);
}
if (e.key == "3" && vanishingPoints.length > 2) {
manageVPButtons(2);
}
if (e.key == "h" && vanishingPoints.length > 1) {
manageVPButtons(3);
}
if (e.ctrlKey && e.key == "z") {
undo();
}
if (e.ctrlKey && e.key == "y") {
redo();
}
if (e.key == "Backspace" || e.key == "Delete") {
deleteScene();
}
if (e.key == "+") {
if (selectedVP > -1 && selectedVP < 3) {
more();
}
}
if (e.key == "-") {
if (selectedVP > -1 && selectedVP < 3) {
less();
}
}
}
/**
* Get settings from the local storage
*/
function getLocalSettings() {
const savedSettings = localStorage.getItem('userSettings');
if (savedSettings) {
settings = JSON.parse(savedSettings);
} else {
settings = defaultSettings;
}
updateSettingsButtons(); //Make sure the right radio buttons are checked
showCustomizedInterface();
}
/**
* Update the radio buttons
*/
function updateSettingsButtons() {
toolsLeft.checked = settings.hand === "left";
toolsRight.checked = settings.hand === "right";
ifaceColor.checked = settings.iface === "colorful";
ifaceNeutral.checked = settings.iface === "neutral";
colors1.checked = settings.colors === "pyb";
colors2.checked = settings.colors === "rgb";
slider.value = settings.transparency * 100;
sliderLabel.innerHTML = `${slider.value}%`
}
/**
* Save settings to local storage
*/
function saveSettings() {
localStorage.setItem('userSettings', JSON.stringify(settings));
}
/**
* Show updated settings in the webpage
*/
function showCustomizedInterface() {
changeToolBarStyle(isMobile, settings.hand === "left");
colors = settings.colors === "pyb" ? ["deeppink", "gold", "slateblue"] : ["limegreen", "blue", "orangered"];
canvas.style.backgroundColor = `rgba(255, 255, 255, ${settings.transparency}`;
}
/**
* Change settings based on user input.
*/
function changeSettings(e) {
switch (e.target.value) {
case "left":
settings.hand = "left";
break;
case "right":
settings.hand = "right";
break;
case "colorful":
settings.iface = "colorful";
break;
case "neutral":
settings.iface = "neutral";
break;
case "pyb":
settings.colors = "pyb";
break;
case "rgb":
settings.colors = "rgb";
break;
}
saveSettings();
showCustomizedInterface();
manageVPButtons(-1);
}
/**
* Change image background transparency
*/
function changeBgImgTransparency(e)
{
settings.transparency = e.target.value/100;
sliderLabel.innerHTML = ` ${e.target.value}%`;
saveSettings();
showCustomizedInterface();
manageVPButtons(-1);
}
/**
* Switch between zoom and pan modes.
*/
function switchZoomPan(e) {
if (pinchPan == 0) {
moveScaleImg.src = "./images/pan.png";
pinchPan = 1;
}
else {
moveScaleImg.src = "./images/pinch.png";
pinchPan = 0;
}
}
/********************************
*** DRAWING FUNCTIONS ***
********************************/
/**
* Drawing loop.
*/
function draw() {
canvas.width = window.innerWidth * aspectRatioWidth;
canvas.height = window.innerHeight * asepctRatioHeight;
bgCanvas.width = window.innerWidth * aspectRatioWidth;
bgCanvas.height = window.innerHeight * asepctRatioHeight;
const m = matrix;
context.setTransform(1, 0, 0, 1, 0, 0);
context.clearRect(0, 0, canvas.width, canvas.height);
createMatrix(cameraOffset.x, cameraOffset.y, cameraZoom, 0);
context.setTransform(m[0], m[1], m[2], m[3], m[4], m[5]);
bgContext.setTransform(1, 0, 0, 1, 0, 0);
bgContext.clearRect(0, 0, bgCanvas.width, bgCanvas.height);
bgContext.setTransform(m[0], m[1], m[2], m[3], m[4], m[5]);
offsetX = canvas.getBoundingClientRect().left;
offsetY = canvas.getBoundingClientRect().top;
if (bgImage != undefined)
drawBGImage();
currentVP = 0;
for (let i = 0; i < vanishingPoints.length; i++) {
drawVP(vanishingPoints[i]);
currentVP++;
}
if (vanishingPoints.length > 1)
drawLine(horizonLine, [], "OrangeRed", 3);
switch (state) {
case STATE.FirstDottedLine:
drawLine(dottedLine, [5, 15]);
break;
case STATE.FirstLine:
drawLine(firstLine);
break;
case STATE.SecondDottedLine:
drawLine(firstLine);
drawLine(dottedLine, [5, 15]);
break;
case STATE.SecondLine:
drawLine(firstLine);
drawLine(secondLine);
break;
}
requestAnimationFrame(draw);
}
/**
* Draw the background image on the canvas (inside draw loop).
*/
function drawBGImage() {
const hRatio = bgCanvas.width / bgImage.width;
const vRatio = bgCanvas.height / bgImage.height;
const ratio = Math.min(hRatio, vRatio);
const centerX = (bgCanvas.width - bgImage.width * ratio) / 2;
const centerY = (bgCanvas.height - bgImage.height * ratio) / 2;
bgContext.drawImage(bgImage, -canvas.width / 2 + centerX, -canvas.height / 2 + centerY, bgImage.width * ratio, bgImage.height * ratio);
}
/**
* Draw a line on the canvas (inside draw loop).
*/
function drawLine(line, lineDash = [], color = colors[currentVP], width = 1) {
context.beginPath();
context.strokeStyle = color;
context.lineWidth = width / cameraZoom;
context.lineJoin = "round";
context.setLineDash(lineDash);
context.moveTo(line.x1, line.y1);
context.lineTo(line.x2, line.y2);
context.closePath();
context.stroke();
}
/**
* Draw a vanishing point with radial lines (inside draw loop)
*/
function drawVP(vp) {
let degs = 0;
for (let i = 0; i < radialLines[currentVP]; i++) {
let line = { x1: vp.x, y1: vp.y, x2: vp.x + 500000 * Math.cos(degs * Math.PI / 180), y2: vp.x + 500000 * Math.sin(degs * Math.PI / 180) }
drawLine(line);
degs += 360 / radialLines[currentVP];
}
}
/********************************
*** VANISHING POINT ***
********************************/
/**
* Create the line
*/
function createLine(line) {
//Make the line bigger
const dx = line.x2 - line.x1;
const dy = line.y2 - line.y1;
const x1 = line.x1 + dx * -10000;
const y1 = line.y1 + dy * -10000;
const x2 = line.x1 + dx * 10000;
const y2 = line.y1 + dy * 10000;
switch (state) {
case STATE.FirstDottedLine:
firstLine = { x1: x1, y1: y1, x2: x2, y2: y2 };
saveAction({ n: "FirstLine", s: STATE.Init, p1: { x1: x1, y1: y1, x2: x2, y2: y2 } });
state = STATE.FirstLine;
break;
case STATE.SecondDottedLine:
secondLine = { x1: x1, y1: y1, x2: x2, y2: y2 };
saveAction({ n: "SecondLine", s: STATE.FirstLine, p1: { x1: x1, y1: y1, x2: x2, y2: y2 } });
state = STATE.SecondLine;
break;
}
x = 0;
y = 0;
isDrawing = false;
dottedLine = undefined;
updateVPButtons();
}
/**
* Create a vanishing point at the intersection of two lines.
*/
function createVP() {
const vp = intersect();
if (vp != false) { //create the vp and the radiating lines
vanishingPoints.push(vp);
saveAction({ n: "VP", s: STATE.SecondLine, p1: { x: vp.x, y: vp.y }, p2: vanishingPoints.length - 1 });
state = STATE.VP;
if (vanishingPoints.length > 1) createHorizonLine();
}
else {
alert(translation('noIntersectionAlert'));
}
updateVPButtons();
manageVPButtons(-1);
}
/**
* Create the horizon line between two vanishing points.
*/
function createHorizonLine() {
const dx = vanishingPoints[1].x - vanishingPoints[0].x;
const dy = vanishingPoints[1].y - vanishingPoints[0].y;
const x1 = vanishingPoints[0].x + dx * -100;
const y1 = vanishingPoints[0].y + dy * -100;
const x2 = vanishingPoints[0].x + dx * 100;
const y2 = vanishingPoints[0].y + dy * 100;
horizonLine = { x1, y1, x2, y2 };
}
/**
* Determine the intersection point of two line segments.
* Return FALSE if the lines don't intersect.
*/
function intersect() {
const x1 = firstLine.x1
const y1 = firstLine.y1
const x2 = firstLine.x2
const y2 = firstLine.y2
const x3 = secondLine.x1
const y3 = secondLine.y1
const x4 = secondLine.x2
const y4 = secondLine.y2
// Check if none of the lines are of length 0
if ((x1 === x2 && y1 === y2) || (x3 === x4 && y3 === y4)) {
return false;
}
let denominator = (y4 - y3) * (x2 - x1) - (x4 - x3) * (y2 - y1);
// Lines are parallel
if (denominator === 0) {
return false;
}
let ua = ((x4 - x3) * (y1 - y3) - (y4 - y3) * (x1 - x3)) / denominator;
let ub = ((x2 - x1) * (y1 - y3) - (y2 - y1) * (x1 - x3)) / denominator;
// is the intersection along the segments
if (ua < 0 || ua > 1 || ub < 0 || ub > 1) {
return false;
}
// Return a object with the x and y coordinates of the intersection
let x = x1 + ua * (x2 - x1);
let y = y1 + ua * (y2 - y1);
return { x, y };
}
/**
* Manage the visibility of vanishing point buttons.
*/
function manageVPButtons(button) {
vp1Image.src = "./images/1.png";
vp2Image.src = "./images/2.png";
vp3Image.src = "./images/3.png";
hImage.src = "./images/H.png";
switch (button) {
case 0:
if (selectedVP == 0) //deselect
{
selectedVP = -1;
}
else {
if (settings.colors === "pyb")
vp1Image.src = "./images/1_selected.png";
else
vp1Image.src = "./images/1_selected2.png";
selectedVP = 0;
}
break;
case 1:
if (selectedVP == 1) //deselect
{
selectedVP = -1;
}
else {
if (settings.colors === "pyb")
vp2Image.src = "./images/2_selected.png";
else
vp2Image.src = "./images/2_selected2.png";
selectedVP = 1;
}
break;
case 2:
if (selectedVP == 2) //deselect
{
selectedVP = -1;
}
else {
if (settings.colors === "pyb")
vp3Image.src = "./images/3_selected.png";
else
vp3Image.src = "./images/3_selected2.png";
selectedVP = 2;
}
break;
case 3:
if (selectedVP == 3) //deselect
{
selectedVP = -1;
}
else {
selectedVP = 3;
hImage.src = "./images/H_selected.png";
alignHorizon();
}
break;
default:
selectedVP = -1;
break;
}
if (selectedVP == -1 || selectedVP == 3) {
moreButton.hidden = true;
lessButton.hidden = true;
deleteVPButton.hidden = true;
}
else {
moreButton.hidden = false;
lessButton.hidden = false;
deleteVPButton.hidden = false;
}
}
/**
* Update the visibility of vanishing point buttons and the state of the download button.
*/
function updateVPButtons() {
vp1Button.hidden = vanishingPoints.length < 1;
vp2Button.hidden = vanishingPoints.length < 2;
vp3Button.hidden = vanishingPoints.length < 3;
horizonButton.hidden = vanishingPoints.length < 2;
downloadButton.disabled = vanishingPoints.length < 1;
createVPButton.disabled = state !== STATE.SecondLine || vanishingPoints.length >= 3;
}
/**
* Align the horizon line.
*/
function alignHorizon(save = true) {
if (save)
saveAction({ n: "Horizon", p1: vanishingPoints[0].y, p2: vanishingPoints[1].y });
const y = (vanishingPoints[1].y - vanishingPoints[0].y) / 2;
vanishingPoints[1].y -= y;
vanishingPoints[0].y += y;
createHorizonLine();
}
/**
* Increase the number of radial lines for the selected vanishing point.
*/
function more(save = true) {
if (radialLines[selectedVP] >= maxRadialLines)
return;
if (save)
saveAction({ n: "More", p1: selectedVP });
radialLines[selectedVP] += 4;
alertWindow.innerHTML = radialLines[selectedVP] / 4;
alertWindow.style.opacity = 1;
setTimeout(function () {
fadeOut(alertWindow);
}, 200); // <-- time in milliseconds
}
/**
* Decrease the number of radial lines for the selected vanishing point.
*/
function less(save = true) {
if (radialLines[selectedVP] <= minRadialLines)
return;
if (save)
saveAction({ n: "Less", p1: selectedVP });
radialLines[selectedVP] -= 4;
alertWindow.innerHTML = radialLines[selectedVP] / 4;
alertWindow.style.opacity = 1;
setTimeout(function () {
fadeOut(alertWindow);
}, 200); // <-- time in milliseconds
}
/**
* Delete the selected vanishing point.
*/
function deleteVP(save = true) {
if (save)
saveAction({ n: "DeleteVP", s: STATE.VP, p1: vanishingPoints[selectedVP], p2: selectedVP });
vanishingPoints.splice(selectedVP, 1);
updateVPButtons();
manageVPButtons(selectedVP); //and hide
}
/********************************
*** SCENE MANAGEMENT ***
********************************/
/**
* Load the background image.
*/
function loadBGImage(e) {
const tgt = e.target;
const files = tgt.files;
// FileReader support
if (FileReader && files && files.length) {
const fr = new FileReader();
fr.onload = () => showImage(fr);
fr.readAsDataURL(files[0]);
}
};
/**
* Show the loaded background image.
*/
function showImage(fileReader) {
bgImage = new Image();