-
Notifications
You must be signed in to change notification settings - Fork 1
/
Trainer.js
1395 lines (1200 loc) · 56.1 KB
/
Trainer.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
// <!-- hide all this crap if no-scripts
/// // (a little) From: http://www.codeLifter.com (the "IE" stuff)
// (a lot) From: http://stackoverflow.com/questions/12661124/how-to-apply-hovering-on-html-area-tag
var msX=0, msY=0; // mouse coords at last exit from mouseMove()
var imgLeft=0, imgTop=0; // total left/top position offsets for #imgPano
var divPano, imgPano, cvsPano, ctxPano; // imgPano <DIV>, and canvas & its DC handle
var Rose, cvsRose, ctxRose;
var RoseWH=50; // update <IMG> width & height too!!!
var Areas, NAreas; // <MAP>'s kids
// used when creating new shapes/areas
var Editing=false, makingShape="", Made=false;
var shapeNpts=0, shapeXpts=0; // points so far this shape, maX points for shape
var shapePts=[]; // x0,y0, ... xn-1,yn-1 - (100/2) should be enough corners(?)
var editingTip="";
// used when moving them around
var Moving=false, moveArea=null;
var deletedAreas=[];
var delMap;
var ShowAll = true; // default to display all areas/shapes
var ShowHints = true; // and hints (<AREA> tooltips)
var ShowTips = false; // and extra usage tooltips
var ShowAllWas = ShowAll, ShowHintsWas = ShowHints;
var sounding = false; // shhh!
var BrgBtnNdx; // for BrgBtn cycle buttons
// these DOMO's get hit a lot, so remember them to speed things up
var idTextOut, divTeaser, divCards, Cards, divMovie, Movie, Sounder, divTitle, TitleText, BrgOut, Toast;
//==================================================================================================
function imgLoaded() { // setup canvas after panorama img has loaded
let x, y, w, h;
/// bugln("\nimgLoaded: in");
// these DOMO's get hit a lot, so remember them to speed things up
idTextOut = byId("idTextOut");
divTeaser = byId("divTeaser");
divCards = byId("divCards");
Cards = byId("Cards");
divMovie = byId("divMovie");
Movie = byId("Movie");
divTitle = byId("divTitle");
TitleText = byId("TitleText");
Sounder = byId("Sounder");
BrgOut = byId("BrgOut");
Toast = byId("Toast");
delMap = document.createElement("MAP");
delMap.name = "delMap"; // name used in "usemap"
delMap.id = "delMap"; // #id used to find <AREA>a
divPano = byId("divPano");
// set stuff common to all sites. (especially "onload" before "src", dummy. you know who you are.)
divPano.onscroll = divPanoScroll; // update azimuth/bearing display
divPano.onwheel = divPanoWheel; // scroll the pano
// get image's position and width+height
imgPano = byId("imgPano");
x = imgPano.offsetLeft;
y = imgPano.offsetTop;
w = imgPano.width;
h = imgPano.height;
imgPano.onmousemove = imgMouseMove; // update azimuth/bearing display
imgPano.onmouseout = imgMouseOut; // restore azimuth/bearing display
imgPano.onclick = imgClick; // clicked outside of an <AREA>
/// imgPano.oncontextmenu = imgRtClick; // use ctrlKey-click instead. removes last shape point /// TODO bubbling???
/// imgPano.addEventListener("contextmenu", imgRtClick, false); // dumm ol FF sometimes pops menu anyway
imgPano.ondblclick = imgDblClick; // end a 'poly' string
byId("divNewStuff").action = "mailto:ANFFLA@digimeas.com?subject=New%20Landmark%20Data%20for%20"+ siteName +"!"; // OK! We have, as of this moment, officially jumped the shark!
byId("divNewStuff").formAction = "mailto:ANFFLA@digimeas.com?subject=New%20Landmark%20Data%20for%20"+ siteName +"!"; // OK! We have, as of this moment, officially jumped the microshark!
// place canvas in front of the image
cvsPano = byId("cvsPano");
cvsPano.style.zIndex = 1;
// position canvas over the image
cvsPano.style.left = x+"px";
cvsPano.style.top = y+"px";
cvsPano.setAttribute("width", w+"px");
cvsPano.setAttribute("height", h+"px");
// set the "default" values for the color/width of fill/stroke operations
ctxPano = cvsPano.getContext("2d");
ctxPano.strokeStyle = "#008000";
ctxPano.lineWidth = 2; // TODO gush w/ different pens etc
ctxPano.lineJoin = "round"; // 'miter'ed acute joints extend way past the coord and don't clear() well
/// ctxPano.globalCompositeOperation="xor"; ///???
/// ctxPano.globalAlpha = .5;
// find total left/top position offsets for #imgPano. Why isn't there a 'elt.Left' & 'elt.Top'?
let os = getOffsets( imgPano );
imgLeft = os.Left; imgTop = os.Top;
Rose = byId("Rose");
cvsRose = byId("cvsRose");
cvsRose.setAttribute("width", RoseWH+"px");
cvsRose.setAttribute("height", RoseWH+"px");
cvsRose.style.zIndex = 5;
ctxRose = cvsRose.getContext("2d");
ctxRose.drawImage(Rose, 0, 0, RoseWH, RoseWH);
/// ctxRose.globalCompositeOperation="xor"; /// TODO
// ctxRose.globalAlpha = .5;
Areas = byId("idMap").children;
NAreas = Areas.length;
// init az/brg display.
bush(false);
if(false) { // open on 1st <AREA> in the list
let a0 = Areas[0];
BrgBtnNdx = Number(a0.getAttribute("x2k")); // X-coord-sorted index of 1st area
ScrollPanoToX( a0.getAttribute("x") );
/// bug(area2Str(a0));
/// bug(" BrgBtnNdx=", BrgBtnNdx);
}
else if(true) { // open on due North
ScrollPanoToX( PX0 );
BrgBtnNdx = X2KNorthish; // next area to the "right" of North
/// bug("\n X2K=", X2KNorthish, " => "," BBN=", BrgBtnNdx);
/// bug(" => ", area2Str(Areas[areaX2K[BrgBtnNdx]]));
}
/*
else if(false) { NotYet // open on due somewhere else
ScrollPanoToX( Az2X( SomeEnchantedBearing ) ); // N.B. doesn't exist yet!!!
BrgBtnNdx = ???; // next area to the "right" of SomeEnchantedBearing
} // else open on X=0
/* */
divPanoScroll(); // update az/brg display w/new bearing
bush();
doInit();
// call HandleShowHints() before HandleShowAll()
/// HandleEditAreas(Editing);
/// HandleShowText(Editing);
HandleShowHints(ShowHints); // setup the menus per default values
HandleShowAll(ShowAll); // changes ShowHints
HandleShowTips(ShowTips); // ditto
if(idTextOut.checked) // if alerts about the input have been written to #idTextOut ...
idTextOut.scrollIntoView(false);
HandleResize();
/*
let str = "";
str += "Total width*height: " + screen.width + "*" + screen.height;
str += " Available width*height: " + screen.availWidth + "*" + screen.availHeight;
bugln(str);
str = "Window inner-width*height: " + window.innerWidth + "*" + window.innerHeight;
bugln(str);
/* */
// whew! done making page. ready for some (inter-)action
if(false)branch(byId("BODY"));
/// bugln(" imgLoaded: out");
}
//==================================================================================================
// called when an <AREA> is clicked. Displays a pic or the #Teaser or a movie or a ...
function flashCard(elt, evt) { // evt is optional
/// bugln("flashCard() elt = "+ elt, " id=", elt.id);
/// bugln("document.activeElement="+ document.activeElement);
/// bugln("document.activeElement.tagname="+ document.activeElement.tagname);
/// bugln();
if(null === elt) {
LOG("\n! ERROR: flashCard: elt == null. Caller=", flashCard.caller.name);
return -1; // TODO error bugtrack???
}
bush(false);
bug(" FC: elt=", elt);
if(undefined != elt) { // clicked on an area
bug(" FC: BrgBtnNdx=", BrgBtnNdx);
if(elt.getAttribute("x2k") != BrgBtnNdx) // erase 'previous' area
areaClear(Areas[areaX2K[BrgBtnNdx]]);
BrgBtnNdx = Number(elt.getAttribute("x2k")); // X-coord-sorted index
bugln(" => ", BrgBtnNdx);
if(Editing) {
// By 'shift-clicking' an area it can be picked up. Another click drops it.
if(! Moving) {
if((undefined != evt) && evt.shiftKey) { // shift-click to 'pick up' an area
HandleShowAll(true); // light em up
if(evt.ctrlKey) { // but, ctrl-shift-click deletes an area
areaDelete(elt);
areaDrawAll(true);
StartTheToast();
return;
}
HandleShowText(true); // make sure the new position text data is visible
LOG(" Moving '"+ elt.getAttribute("hintTitle") +"'. Click new location.");
moveArea = elt;
Moving = true;
return;
}
}
else { // Moving and got the 2nd click. Drop it.
areaDrawAll(true);
moveArea.title = moveArea.getAttribute("hintTitle"); // oopsie! the 'hints' showed up in the 'title' {chagrin face}
LOG(" Moved to ", area2Str(moveArea) +"\n"); // the new coords
Moving = false;
Made = true;
DisplayClass("Made", "block"); // make sure the 'mailto:' button is visible
/// idTextOut.scrollIntoView(false);
StartTheToast();
return;
} // if(Moving)
} // if(Editing)
} // if(undefined != elt)
bush();
// if we're making shapes don't pop pix ??? Why not???
if("" != makingShape) { // in shape-making mode, and a shape-type has been selected
if(0 == shapeNpts) { // this click is 1st point; don't allow to be in a shape. also allows moves.
/// let it go HandleShapeSelect(""); /// process as area click
}
else { // not 1st point.
shapeCont(); // process as first/next point.
}
}
// not picking (or just-now turned off). Display something in upper-right column
// first, hide everything
divTeaser.style.display = "none"; // hide #Teaser
divCards.style.display = "none"; // hide #Cards
Cards.src = "";
divMovie.style.display = "none"; // hide #Movie
Movie.removeAttribute("autoplay"); // and stop playing it
Movie.pause(); // and stop playing it
divTitle.style.display = "none"; // hide #TitleText
StopTheToast(); // got a click. abort if still popped
if(undefined == elt) { // no <AREA> selected. display #Teaser
divTeaser.style.display = "inline-block"; // show #Teaser
}
else if("" != elt.alt) { // got a #Card image
divCards.style.display = "inline-block"; // show #Cards
Cards.src = cardsPath + elt.alt + cardsExt;
}
else { // it's something, but what?
let type = elt.getAttribute("type");
if(null != type) {
if("video" == type.trim().substr(0,5)) { // got a #Movie
divMovie.style.display = "inline-block"; // Let's see a #Movie
Movie.innerHTML = elt.getAttribute("Sources"); // N.B. "Sources" is NON-STANDARD
Movie.muted = false;
Movie.load(); // (re)-load
Movie.play(); // start playing
/// branch(divMovie);
}
}
else /*if("" != elt.title)*/ { // nothing in particular, I guess
TitleText.innerHTML = elt.getAttribute("hintTitle"); // show the hintTitle
divTitle.style.display = "inline-block";
}
}
}
var Toasted=false;
//==================================================================================================
function StartTheToast() { // sloooowly
if(Toasted)
return;
Toasted = true;
FadeIn(PopTheToast, 700, 12000, 1000, Toast);
}
var dwas = -1;
//==================================================================================================
function PopTheToast(d) {
/* this is slidier
let q2 = 100*(Number(byId("divPix").clientWidth) + Number(Toast.clientWidth)) / Number(byId("divPix").clientWidth) / 2;
if(dwas < d)
Toast.style.left = Number(101 - q2*d*d) + "%";
else
Toast.style.left = Number(100 - q2 - q2*(1-d)*(1-d)) + "%";
dwas = d;
Toast.style.top = "85%";
/* */
/* but this is toastier */
Toast.style.top = Number(101 - 20*d*d) + "%";
Toast.style.opacity = d*d;
}
//==================================================================================================
function StopTheToast() { // rpdly
let f = Toast.getAttribute("FaderIndex");
if(null != f) {
let fo = Faders[f];
if(fo.done) {
return;
}
FadeOut(fo, 500);
Toast.removeAttribute("FaderIndex");
}
}
//==================================================================================================
// called when the "not-visible button" is clicked
function showBug(e) { // un/hide area/shape maker, and other stuff
if(e.ctrlKey) {
DisplayClass("clsDebugs"); // toggle hidedness
ShowAll = true;
}
if(e.shiftKey) {
Toasted = false;
StartTheToast();
HandleShowText(true);
/// idTextOut.scrollIntoView(false);
Toasted = false;
}
}
//==================================================================================================
function area2Str(elt) {
if((undefined == elt) || (null == elt)) {
LOG("! ERROR - area2Str(): elt=='", elt, "'. caller=", area2Str.caller.name);
return;
}
let str = "";
str += "<area shape='"+ elt.shape +"' coords='"+ elt.coords +"'";
if("" != elt.alt) str += " alt='"+ elt.alt +"'";
/// if("" != elt.title) str += " title='"+ elt.title +"'"; // might not be hinting, so ...
/// else
str += " title='"+ elt.getAttribute("hintTitle") +"'"; /// this might not be right. fix in post
if(undefined != elt.type) str += " type='"+ elt.type +"' medaFile='"+ elt.getAttribute("medaFile") +"'";
str += ">";
return str;
}
//==================================================================================================
function PlayMedia(element) { // if elt.type == "audio", play it.
let type = element.getAttribute("type");
if(null == type)
return;
switch(type.trim().substr(0,5).toLowerCase()) {
case "audio":
sounding = true;
Sounder.type = element.getAttribute("type");
Sounder.src = element.getAttribute("Sources"); // N.B. "Sources" is NON-STANDARD
Sounder.play();
break;
case "video":
// plays in #divMovie instead of #Cards
break;
default:
LOG("\n! Error: PlayMedia(): case="+ type);
HandleShowText(true);
break;
}
}
//==================================================================================================
function areaMouseIn(element) { // hovering
if(ShowHints) { // if an <AREA> has a "title" it is displayed when hovered
let str = element.getAttribute("hintTitle"); // display the <AREA>s info
if(ShowTips) {
if(! Moving)
str += "\n (Shift-click to 'pick up')";
else
str += "\n (Click to 'drop')";
if(Editing) {
if("" == makingShape)
str += "\n (still making? pick a shape)";
else
str += "\n (still making)";
}
} // if(ShowTips)
element.setAttribute("title", str);
} // if(ShowHints)
else // not ShowHints
element.setAttribute("title", ""); // turn off the tooltip
areaDraw(element);
PlayMedia(element);
}
//==================================================================================================
function areaMouseMove(event, elt) { // hovering
/// elt.setAttribute("title", elt.getAttribute("title")); /// this didn't work like I hoped
imgMouseMove(event);
}
//==================================================================================================
function areaMouseOut(element) { // no longer hovering
if(element.getAttribute("x2k") != BrgBtnNdx) // erase if not the 'current' area
areaClear(element);
if(ShowHints) { // if an <AREA> has a "title" it is displayed when hovered
element.setAttribute("title", element.getAttribute("hintTitle")); // restore normal title
}
else // not ShowHints
element.setAttribute("title", ""); // turn off the tooltip
if(sounding) {
Sounder.pause();
Sounder.src = "";
}
}
//==================================================================================================
function areaCursor(c) { // re/set visual reminder for all the areas
for(let i=0; i<NAreas; i++)
Areas[i].style.cursor = c;
}
// process mouse clicks in #imgPano
//==================================================================================================
function imgClick(e) { // clicked outside an area. TODO??? use <AREA shape="default" ...> ???
/// bugln(" clk("+ e.clientX +") ");
/// bug(" Clk");
let str="'";
if(e.shiftKey) str += "S";
if(e.ctrlKey) str += "C";
if(e.altKey) str += "A";
if(e.metaKey) str += "M";
bug3(str +"'");
TipOfTheDay();
if("" == makingShape) { // not (or haven't started) making a shape
flashCard(); // show the #Teaser
if(Editing) { // making but haven't selected a shape to Make
byId("ShapeBox").style.backgroundColor = "orange"; // remind user
}
}
else { // making a new shape
if(e.ctrlKey) { // ctl-click; erase last point
if(0 < shapeNpts) {
shapeClear(msX, msY); // draws
shapeNpts--;
if(0 < shapeNpts) { // a poly with some points left
shapeDraw(msX, msY);
}
else { // a circle or rect with no points left
areaDrawAll(true);
}
}
}
else if(e.shiftKey) { // shift-click; this is the last point. same as double-click
shapeCont(); // process click as last point
if("poly" == makingShape)
shapeClose();
}
else // regular click
shapeCont(); // process click as next point
}
TipOfTheDay();
} // imgClick(e)
//==================================================================================================
function imgDblClick(e) { // end of "poly" point input?
/// bug(" dblClk");
if(e.ctrlKey || e.shiftlKey) // imgClick() has already been called,
return; // so ignore 2nd-click if ctrl-key or shift-key
if("poly" == makingShape)
shapeClose();
}
//==================================================================================================
function bodyMouseMove(e) { // brutality and buggery and rum!!!
bug2(e.pageX);
bug3(e.pageY);
}
//==================================================================================================
function imgMouseMove(e) { // mouse is restless. Watch its AZ
/// bug(", "+ e.clientX);
/*
if(0 < scrn) {
bugln(" scrn=", scrn, " scrq=", scrq);
scrn = 0;
}
/* */
let x, y;
// update az/brg display
x = e.pageX;
x -= imgLeft; // account for container position
x += divPano.scrollLeft; // account for image scroll
y = e.pageY;
y -= imgTop; // account for container position
bug0(x);
bug1(y);
if(x < 0) { x = 0; }
if(y < 0) { y = 0; }
UpdateBrgAz(x - imgLeft); // mouse-X to divPix-x
// continuous feedback while moving areas
if(Moving) {
areaClear(moveArea, true); // clear old position
areaMove(moveArea, x-msX, y-msY); // TODO just move a shape??? 'title' stuff???
/// areaDraw(moveArea); // draw new one
areaDrawAll(true);
}
if(Editing) { // making
if("" == makingShape) { // but haven't selected a shape to Make
byId("ShapeBox").style.backgroundColor = "orange"; // notify user
}
}
// continuous feedback while drawing shapes
if(0 < shapeNpts) {
shapeClear(msX, msY); // clear old shape
shapeDraw(x, y); // draw new one
areaDrawAll(true);
}
msX = x; msY = y;
} // imgMouseMove(e)
//==================================================================================================
function imgMouseOut() {
// restore az/brg display to center of visible
UpdateBrgAz(); // mid-screen X to Az
} // imgMouseOut()
//==================================================================================================
function X2Az(x) { // convert #imgPano X coord (pxl) to panorama Azimuth (deg)
// az = 360 * (x + scroll - (px_at_0Az))/(px_in_360)
let az = Number(360.0*(x - PX0)/PX360);
if(az < 0.0) // [0 ..
az += 360.0;
if(360.0 <= Number(az))
az -= 360.0; // .. 359.999]
az = Math.floor(az + 0.5); // integer round(). always positive, so OK
if(360.0 == Number(az))
az = 0.0;
return az;
}
//==================================================================================================
/* --maybe later--???
// convert panorama Azimuth (deg) to #imgPano X coord (pxl)
function Az2X(az) {
// az = 360 * (x + scroll - (px_at_0Az))/(px_in_360)
// x = ((Az * (px_in_360)) / 360) - scroll + (px_at_0Az))/
let x = az*PX360/360;
if(x < 0.0)
x += PX360;
if(PX360 <= Math.floor(x))
x -= 0;
bugln(" A2X: "+ az +" => "+ x);
return Math.floor(x);
}
/* */
//==================================================================================================
function UpdateBrgAz(x) { // update az/brg display when scrolling #imgPano
if(undefined === x)
x = divPano.scrollLeft + divPano.clientWidth/2; // default to center of current
if(x < 0) { x = 0; } // let's keep it positive
let az = X2Az( x ); // view X pxl -> Az deg
if(Number(az) < 10) az = "0"+ az;
if(Number(az) < 100) az = "0"+ az; // 3 digits
BrgOut.innerHTML = az;
return az;
}
var Azwas=0;
//==================================================================================================
function divPanoScroll() { // update az/brg display when scrolling #imgPano
// allow (nae, encourage!) continuous cylindrical scrolling (CCS (C))
// divPano.scrollLeft == 0 when scrolled far right (slider far left)
// divPano.scrollLeft == SR when scrolled far left (slider far right)
let SR = divPano.scrollWidth - divPano.clientWidth; // SL when slider at far right
// This effect is pretty cool when the window width is <= the pano overlap
// that is, when divPano.clientWidth <= (PXALL - PX360)
if(0 == divPano.scrollLeft) { // |<- at the left wall, slider moving left
divPano.scrollLeft = Math.min( SR - 1, PX360 ); // transport right through it (rotate 360 ->)
}
else if(SR <= divPano.scrollLeft) { // at the right wall, slider moving right ->|
divPano.scrollLeft = Math.max( 1, SR - PX360 ); // if the window's too wide (see above) just 1
} // like those guys in 'Ghost', or some other fictional wall transfuser guys
let az = UpdateBrgAz(); // set az for middle of view
let wh2 = Number(RoseWH)/2;
ctxRose.clearRect( 0, 0, RoseWH, RoseWH);
ctxRose.translate( wh2, wh2); // translate (0, 0) to center of Rose (or vice versa?)
ctxRose.rotate((Azwas-az) * Math.PI/180); // incremental rotation about (0, 0)
ctxRose.translate( -wh2, -wh2); // return to (0, 0) so ...
ctxRose.drawImage(Rose, 0, 0, RoseWH, RoseWH); // rotated image shows in right place. Sheesh!
Azwas = az;
}
//==================================================================================================
function divPanoWheel(e) { // scroll #imgPano with wheel
// for every wheel scroll event:
// firefox sends "3 lines" (dY=3, mode=1) and
// chrome sends "100 pxl" (dy=100, mode=0)
// TODO Edge/IE???
// so just use five degrees per pulse. seems to work ok
if(e.altKey) // normally scroll pano horizontally w/ no vertical
return; // +ALT => only scroll wheel window vertically
e.preventDefault(true); // prevents the window from jumping up and down
///??? e.stopPropagation();
let dx = 5*PX360/360 * Math.abs(e.deltaY)/e.deltaY; // IE has no Math.sign()???
ScrollPanoByX( dx ); // divPanoScroll() will handle 'too far' right or left
}
//==================================================================================================
function ScrollPanoToX(x) {
let SL = x - divPano.clientWidth/2; // divPano.scrollLeft when x is centered
let SR = divPano.scrollWidth - divPano.clientWidth; // max divPano.scrollLeft
if(SL <= 0) // |<- at or past the left side
SL = 1;
else if(SR <= SL) // at or past the right wall
SL = SR - 1;
divPano.scrollLeft = SL; // X -> middle of view (hopefully)
}
//==================================================================================================
function ScrollPanoByX(dx) {
divPano.scrollLeft += dx;
}
var wN=-1; // how many buggerys
//==================================================================================================
function wheelNot(e, elt) { // don't scroll parent(s)
// using regular mouse wheel, for each pulse
// FF reports dY = +/-3 lines dX=dZ=0 Mode=1
// Chrome: dY = +/-53px dX=dZ=0 Mode=0 regardless of font-size
// IE:
if(0 < wN) {
wN--;
bug("wN: "+ elt.id);
bugln(" dM=", e.deltaMode, " dX=", e.deltaX, " dY=", e.deltaY, " dZ=", e.deltaZ, " alt=", e.altKey);
}
if(e.altKey) {
return; // ALT => only scroll whoel window vertically
}
// e.preventDefault(true); // prevents the window from jumping up and down
e.stopPropagation();
let dX = e.deltaX; ///??? if(1 == e.deltaMode) { dX *= 53/3; } // dX in pixels. see above
let dY = e.deltaY; // assume pxls (Mode 0)
if(1 == e.deltaMode) { dY *= 53/3; } // dY in pixels. see above
elt.scrollLeft += dX;
elt.scrollTop += dY;
bug3(dY);
}
//==================================================================================================
function HandleResize() { // window resized
byId("Bearings").style.left = Number((byId("divPix").clientWidth - byId("Bearings").clientWidth)/2) + "px";
byId("Toast").style.left = Number((byId("divPix").clientWidth - byId("Toast").clientWidth)/2) + "px";
}
//==================================================================================================
function HandleBrgBtns(i) { // BrgBtns clicked
if(Editing || Moving) // finish that first
return; // too complicated
bush(false);
bug(" HBB: BrgBtnNdx=", BrgBtnNdx, " x2k=", areaX2K[BrgBtnNdx]);
if(ShowAll)
areaDraw(Areas[areaX2K[BrgBtnNdx]]); // old color for old area
else
areaClear(Areas[areaX2K[BrgBtnNdx]]); // no color
BrgBtnNdx += i; // which way?
if(NAreas <= BrgBtnNdx)
BrgBtnNdx = 0; // circulate!
else if(BrgBtnNdx < 0)
BrgBtnNdx = NAreas-1;
bug(" => ", BrgBtnNdx, " x2k=", areaX2K[BrgBtnNdx]);
let a = Areas[areaX2K[BrgBtnNdx]]; // areaX2K is <AREA>s indexes sorted in X-coord order
if(BUG)bugln(" => ", area2Str(a));
bush();
flashCard(a); // show #Card or "Title"
areaDraw(a, "#00FF00"); // light it up
// scroll <AREA> into view
ScrollPanoToX( a.getAttribute("x") ); // put <AREA> in the center
}
// call HandleShowHints() before HandleShowAll()
//==================================================================================================
function HandleShowHints(hintq) {
ShowHints = hintq;
byId("idShowHints").checked = ShowHints; // visual
if(! ShowHints)
HandleShowTips(false); // kinda makes sense
}
//==================================================================================================
function HandleShowTips(tipq) {
ShowTips = tipq;
/// byId("idShowTips").checked = ShowTips; // visual
if(ShowTips)
HandleShowHints(true); // complimentary
}
//==================================================================================================
function HandleShowAll(showq) {
ShowAll = showq;
byId("idShowAll").checked = ShowAll; // change status
if(ShowAll)
HandleShowHints(true); // circle jerk?
areaDrawAll(ShowAll);
}
//==================================================================================================
function HandleShowText(textq) {
if(Made) // there's new data
textq = true; // leave it open
/// if(textq == byId("idShowText").checked) // no change, so ...
/// return; // stop bouncing the screen around
byId("idShowText").checked = textq; // update status
DisplayClass('clsTextOut', textq);
/// if(textq)
/// idTextOut.scrollIntoView(true);
}
//==================================================================================================
function HandleEditAreas(editq) {
Editing = editq;
makingShape = ""; // reset the shape picked
byId("rbUnShape").checked = true; // un-checks the real shape radio buttons :-)
byId("ShapeBox").style.backgroundColor = "#EBE594"; // set a reminder color to pick a shape
imgPano.style.cursor = "auto"; // reset visual reminder
areaCursor("pointer"); // 'pointer' for all the <AREA>s
// update options menu display
if(Editing) {
DisplayClass("clsShapeShifter", "block"); // un-hide all the makey-shapey stuff
DisplayClass("clsTextOut", "block");
DisplayClass("BrgBtns", "none");
byId("makeInstructions").scrollIntoView(false);
ShowAllWas = ShowAll;
ShowHintsWas = ShowHints;
HandleShowAll(true); // also does ShowHints
HandleShowTips(true); // ditto
HandleShowText(true);
imgPano.style.cursor = "not-allowed"; // another visual reminder
byId("idShowAll").disabled = true; // disable hiding when making
byId("idShowHints").disabled = true;
/// byId("idShowTips").disabled = true; // allow to un-check tips
byId("idShowText").disabled = true;
imgPano.title = "(Pick a shape to make)";
editingTip = "\n (Pick a shape to make)";
byId("Bearing").title = "Still making";
LOG(); // stet
} // if(Editing)
else { // stopped Editing
DisplayClass("clsShapeShifter", "none"); // hide all the shapey-makey stuff
DisplayClass("clsTextOut", "block"); // leave this open
if(Made) {
DisplayClass("Made", "block"); // do we need to report in?
}
DisplayClass("BrgBtns", "inline");
/// idTextOut.scrollIntoView(false);
HandleShowHints(ShowHintsWas); // restore checkedness
HandleShowAll(ShowAllWas); // also does ShowHints
HandleShowTips(false); // ditto
HandleShowText(true); // leave text area open
byId("idShowAll").disabled = false; // turn these (back) on
byId("idShowHints").disabled = false;
// byId("idShowTips").disabled = false;
byId("idShowText").disabled = false;
imgPano.title = "";
byId("Bearing").title = "View direction (degrees)";
} // else if(Editing)
} // function HandleOptionsMenu()
//==================================================================================================
function HandleShapeSelect(shapeq) { // response to #ShapeSelect.
if(0 < shapeNpts) // already "Make"-ing a shape
shapeClose(); // finish current
makingShape = shapeq;
byId("ShapeBox").style.backgroundColor = ""; // reset color
if("" == makingShape) {
byId("rbUnShape").checked = true; // un-checks the real shape radio buttons :-)
imgPano.style.cursor = "auto"; // reset visual reminder
areaCursor("pointer"); // for all the areas too
TipOfTheDay();
}
else {
LOG(""); // makes the display prettier
imgPano.style.cursor = "crosshair"; // set visual reminder
areaCursor("crosshair"); // for all the areas too
HandleShowAll(true); // ShowAll the <AREA>s
TipOfTheDay(); // do this before shapeOpen()
shapeOpen(); // wait for 1st click
}
}
//==================================================================================================
function TipOfTheDay() { // re/set visual reminder for all the areas
if(ShowHints) { // if an <AREA> has a "title" it is displayed when hovered
let str = "";
if(ShowTips) {
if(Editing) {
if(0 == shapeNpts) {
if("circle" == makingShape)
str += "(click for center)";
else if("poly" == makingShape)
str += "(click for first point)";
else if("rect" == makingShape)
str += "(click for first corner)";
else
str += "(Pick a shape)";
}
else {
if("circle" == makingShape)
str += "(click to set)";
else if("poly" == makingShape)
str += "(click for next point. Shift-click to end. \n Ctrl-click to back up)";
else if("rect" == makingShape)
str += "(click to set)";
else
str += "(? 0<N shape="+ makingShape +"!?)";
}
} // if(Editing)
} // if(ShowTips)
imgPano.title = str;
} // if(ShowHints)
else
imgPano.title = ""; // turn off the tooltip
}
// process 1st, [intermediate], and last points for new shapes
//==================================================================================================
// called to setup for a new shape for make. No points picked yet.
function shapeOpen() { // setup for a new area/shape
if(0 < shapeNpts) {
LOG("\n! Error: shapeOpen(): shapeNpts = "+ shapeNpts +"\n");
HandleShowText(true);
shapeClose();
}
shapeNpts = 0;
switch(makingShape) {
case "circle": shapeXpts = 2; break; // center, radius
case "poly": shapeXpts = shapePts.length/2; break; // apices
case "rect": shapeXpts = 2; break; // diametrical corners
default:
LOG("! ERROR: shapeOpen: makingShape="+ makingShape);
}
}
//==================================================================================================
// process new shape point
function shapeCont() { // continue/add to shape.
let n2 = 2*shapeNpts;
shapePts[n2] = msX; shapePts[n2+1] = msY;
// bug("("+ n2/2 +": "+ msX +", "+ msY +") ");
shapeNpts++;
if(shapeXpts <= shapeNpts)
shapeClose();
}
//==================================================================================================
// got last shape point. actually create the new <AREA>
//-TODO- should(?) check for zero dimensions, etc. But it's just screen clutter, so let the user clean it???
function shapeClose() { // guess
// <area shape='rect' coords='4043, 249, 4070, 258' title='085 Dawson Pk'>
// <- cstr -> <- tstr ->
let cstr, tstr, xavg; // Coord and Title strings
let str = "<area shape=\'"+ makingShape +"\' coords=\'"; // start of <AREA> text
let A = document.createElement("AREA"); // add the new area to the map
switch(makingShape) {
// standard shape names are 'circle', 'poly', 'rect' & 'default'
case "circle": // center, rad
cstr = cstrCirc(shapePts[0], shapePts[1], shapePts[2], shapePts[3]);
xavg = shapePts[0];
tstr = X2Az(xavg) +" circle"; // standard shape text
A.shape = "circle";
break;
case "poly": // apices
// dblClick registers multiple last points. remove duplicate ADJACENT points
let o=0;
while(o < 2*shapeNpts-2) { // N.B. shapeNpts can change inside the while()!
if((Math.abs(shapePts[o] - shapePts[o+2]) < 2) && (Math.abs(shapePts[o+1] - shapePts[o+1+2]) < 2)) { // ~duplicated adjacent points
for(let i=o+2, n2=2*shapeNpts-2; i<n2; i++) // move higher points down
shapePts[i] = shapePts[i+2];
shapeNpts--;
}
else
o += 2;
}
if(shapeNpts < 3) { // need at least three points for a poly
LOG("! Need at least 3 points for a 'poly'.");
return;
}
let xn=shapePts[0], xx=xn; // get x-range for tstr
for(let i2=2, n2=(2*shapeNpts); i2<n2; i2+=2) {
xn = Math.min(xn, shapePts[i2]); xx = Math.max(xx, shapePts[i2]);
}
cstr = shapePts.slice(0,2*shapeNpts).toString();
xavg = (xn+xx)/2;
tstr = X2Az(xavg) +" poly";
A.shape = "poly";
break;
case "rect": // diametrical corners
cstr = LTRB(shapePts[0], shapePts[1], shapePts[2], shapePts[3]);
xavg = (shapePts[0]+shapePts[2])/2;
tstr = X2Az(xavg) +" rect";
A.shape = "rect";
break;
default:
LOG("! shapeClose default: makingShape=", makingShape);
}
str += cstr +"\' title=\'"+ tstr +"\'>"; // complete <AREA> text
LOG(" New area = "+ str);
shapeNpts = 0;
// create new <AREA> and add it to the <MAP>
A.coords = cstr;
A.title = tstr; // it's a TITLE until we get a pic to pop; then a ALT.
A.setAttribute("hintTitle", A.title); // used for hinting. N.B. "hintTitle" is NON-STANDARD.
areaAdd(A, xavg); // complete the process
areaDrawAll(true);
Made = true; // we got new stuff!
DisplayClass("Made", "block"); // make sure the mailto button is visible
StartTheToast();
TipOfTheDay();
} // function shapeClose()
// make strings of the shape coords =============================================================
// circle: (x0,y0, x1,y1) => (x0,y0, rad)
// rect: (x0,y0, x1,y1) => (L,T, w,h)
//==================================================================================================
function cstrCirc(x0, y0, x1, y1) { // create 'circle' coordstr = "X, Y, Rad"
let rad = Math.sqrt( Math.pow((x1-x0), 2) + Math.pow((y1-y0), 2) );
let cstr = x0 +", "+ y0 +", "+ Math.round(rad);
return cstr;
}
//==================================================================================================
function cstrRect(x0, y0, x1, y1) { // create 'rect' coordstr = "L, T, R, B"