-
Notifications
You must be signed in to change notification settings - Fork 1
/
SkeletonPlot.js
1166 lines (981 loc) · 32.4 KB
/
SkeletonPlot.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
/*Functions for app settings and logic*/
function appInit(){
var userGraph = document.getElementById("userGraph");
var masterGraph = document.getElementById("masterGraph");
var coreStrip = document.getElementById("coreStrip");
/*Define global variables*/
//data for the marks to be rendered on canvases
markData = {
userGraph: { normal: [], //Members: x, y
falses: [], //Members: x
wide: [], //Members: x
absent: [] //Members: x
},
masterGraph: { normal: [], //Members: x, y
wide: [], //Members: x
index: [] //Members:
},
coreStrip: { ring: [] //Members: x, width, color
}
};
userAction = {
done: [],
undone: []
}; //Members: x, y(if applicable), type, mode
//Corresponds to settings in control panel
appSettings = {
absents: "off",
falses: "off",
graphMag: "Large",
coreMag: "1x",
mouseMode: "Draw",
mouseMark: "Normal",
hintVis: "Hidden",
answerVis: "Hidden",
masterVis: "Hidden",
sensitivity: 1,
sensitivityNext: 1,
rings: 61,
ringsNext: 61
};
//Values to be displayed
answer = {
yearStart: 0,
yearEnd: 0,
falses: [], //Array of two ring numbers
absents: [] //Array of years
}
//misc global variables
data = {
masterLengthFactor: 6,
masterYearStart: 0,
targetRingWidth: 50,
coreLength: 0
}
//Position Elements
userGraph.style.top = "190px";
userGraph.style.left = "10px";
masterGraph.style.top = masterGraph.style.top = parseInt(userGraph.style.top) + userGraph.height + "px";
masterGraph.style.left = "10px";
coreStrip.style.top = "134px";
coreStrip.style.left = "10px";
inputInit();
applySettings();
populateRings();
writeAnswer();
writeHint();
renderGraphics();
}
//Initializes check status of input elements
function inputInit() {
var allInputs = document.getElementsByTagName("input");
for(var x=0;x<allInputs.length;x++){
if(allInputs[x].value == "Absents" || allInputs[x].value == "Falses"){
allInputs[x].checked = false;
}
}
}
function restart(){
//Update settings
appSettings.rings = appSettings.ringsNext;
appSettings.sensitivity = appSettings.sensitivityNext;
//Reset mark values and answers
markData = {
userGraph: { normal: [],
falses: [],
wide: [],
absent: []
},
masterGraph: { normal: [],
wide: [],
index: []
},
coreStrip: { ring: []
}
};
answer = {
yearStart: 0,
yearEnd: 0,
falses: [],
absents: []
}
data = {
masterLengthFactor: 6,
masterYearStart: 0,
targetRingWidth: 50,
coreLength: 0
}
//Reposition elements
userGraph.style.top = "190px";
userGraph.style.left = "10px";
masterGraph.style.top = masterGraph.style.top = parseInt(userGraph.style.top) + userGraph.height + "px";
masterGraph.style.left = "10px";
coreStrip.style.top = "134px";
coreStrip.style.left = "10px";
applySettings();
populateRings();
writeAnswer();
writeHint();
}
//Reads appSettings, makes necessary changes
function applySettings() {
var i;
if(typeof graphUnit !== 'undefined'){graphUnitPrev = graphUnit;} //Set previous graph unit if not the first time
//scales graphUnit; used for rendering graphs
switch(appSettings.graphMag){
case "Large":
graphUnit = 8;
break;
case "Medium":
graphUnit = 6;
break;
case "Small":
graphUnit = 5;
break;
}
//Toggles hint visibility
switch(appSettings.hintVis) {
case "Hidden":
document.getElementById("hintText").style.visibility = "hidden";
break;
case "Visible":
document.getElementById("hintText").style.visibility = "visible";
break;
}
//Toggles answer visibility
switch(appSettings.answerVis) {
case "Hidden":
document.getElementById("answerBox").style.visibility = "hidden";
break;
case "Visible":
document.getElementById("answerBox").style.visibility = "visible";
break;
}
//Toggles master visibility
switch(appSettings.masterVis) {
case "Hidden":
document.getElementById("masterGraph").style.visibility = "hidden";
break;
case "Visible":
document.getElementById("masterGraph").style.visibility = "visible";
break;
}
//Reads displayed values for ringNumber and sensitivity, sends them to appSettings
document.getElementById("sensitivityNumber").innerHTML = appSettings.sensitivityNext;
document.getElementById("ringNumber").innerHTML = appSettings.ringsNext;
renderGraphics();
}
/*Detects and responds to interaction with control panel*/
function handleInputPress(e){
var targ = e.target ? e.target : e.srcElement;
//Detect which input is being changed
switch(targ.type){
case "checkbox":
switch(targ.value) { //Toggle absents or falses on or off
case "Absents":
if(targ.checked){
appSettings.absents = "off";
}
else{
appSettings.absents = "on";
}
break;
case "Falses":
if(targ.checked){
appSettings.falses = "off";
}
else{
appSettings.falses = "on";
}
break;
}
break;
case "radio":
switch(targ.name) { //update settings to value of input
case "coreMag":
appSettings.coreMag = targ.value;
break;
case "graphMag":
appSettings.graphMag = targ.value;
break;
case "mouseMode":
appSettings.mouseMode = targ.value;
break;
case "mouseMark":
appSettings.mouseMark = targ.value;
break;
}
break;
case "button":
switch(targ.value) { //Toggles visibility of elements
case "Undo":
undo();
break;
case "Redo":
redo();
break;
case "Hint":
if(appSettings.hintVis == "Hidden"){
appSettings.hintVis = "Visible";
}
else{
appSettings.hintVis = "Hidden";
}
break;
case "Answer":
if(appSettings.answerVis == "Hidden"){
appSettings.answerVis = "Visible";
}
else{
appSettings.answerVis = "Hidden";
}
break;
case "Master":
if(appSettings.masterVis == "Hidden"){
appSettings.masterVis = "Visible";
}
else{
appSettings.masterVis = "Hidden";
}
break;
}
}
//Increase/Decrease sensitivity and ringNumbers
if(targ == document.getElementById("decreaseSensitivity")){
if(appSettings.sensitivityNext > 1) {
--appSettings.sensitivityNext;
}
}
else if(targ == document.getElementById("decreaseRings")) {
if(appSettings.ringsNext > 11) {
appSettings.ringsNext -= 10;
}
}
if(targ == document.getElementById("increaseSensitivity")){
if(appSettings.sensitivityNext < 9) {
++appSettings.sensitivityNext;
}
}
else if(targ == document.getElementById("increaseRings")){
if(appSettings.ringsNext < 401) {
appSettings.ringsNext += 10;
}
}
if(targ.value == "Restart a New Core"){restart();}
applySettings();
}
//Graphics Functions
function renderGraphics(){
scaleGraphs();
drawCoreStrip();
}
//Render graphPaper background for userGraph and masterGraph
function renderGraphPaper(canvas) {
var height = canvas.height;
var width = canvas.width;
var mag = appSettings.graphMag;
var ctx = canvas.getContext("2d");
var i, txt, txtWidth, temp;
//Draw graph squares
ctx.lineWidth = 1;
//Draw vertical lines
//Leave 5 units for margin
for (i = graphUnit*5; i < width; i+=graphUnit) {
ctx.beginPath();
ctx.moveTo(i+.5, 0);
ctx.lineTo(i+.5, height);
ctx.strokeStyle = "rgb(102,255,102)";
ctx.stroke();
ctx.closePath();
}
//Draw horizontal lines
for (i = 0; i < height; i+=graphUnit) {
ctx.beginPath();
ctx.moveTo(graphUnit*5, i+.5); //Leave room for margin
ctx.lineTo(width, i+.5);
ctx.strokeStyle = "rgb(102,255,102)";
ctx.stroke();
ctx.closePath();
}
//Dark Green Lines every 5 squares
for (i = graphUnit*5; i < width; i+=graphUnit*5) {
ctx.beginPath();
ctx.moveTo(i+.5, 0);
ctx.lineTo(i+.5, height);
ctx.strokeStyle = "rgb(000,204,051)";
ctx.stroke();
ctx.closePath();
}
for (i = 0; i < height; i+=graphUnit*5) {
ctx.beginPath();
ctx.moveTo(graphUnit*5, i+.5);
ctx.lineTo(width, i+.5);
ctx.strokeStyle = "rgb(000,204,051)"
ctx.stroke();
ctx.closePath();
}
//adjust font size according to scale
switch(graphUnit) {
case 5:
ctx.font = "italic bold 10px Times";
break;
case 6:
ctx.font = "italic bold 12px Times";
break;
case 8:
ctx.font = "italic bold 15px Times";
break;
}
//Draw special margins; depend on which graph it is
switch(canvas){
case document.getElementById("userGraph"):
//Draw Margin Markers
ctx.beginPath();
ctx.strokeStyle = "red"
ctx.moveTo(graphUnit*5+.5, graphUnit*5 + 1);
ctx.lineTo(graphUnit*5+.5, height);
ctx.stroke();
ctx.moveTo(graphUnit*5+.5, graphUnit*13+.5);
ctx.lineTo(graphUnit*3, graphUnit*13+.5);
ctx.stroke();
ctx.lineTo(graphUnit*5, height-.5);
ctx.stroke();
ctx.moveTo(width-graphUnit*15+.5, graphUnit*5+1);
ctx.lineTo(width-graphUnit*15+.5, height);
ctx.stroke();
ctx.moveTo(width-graphUnit*15+.5, graphUnit*13+.5);
ctx.lineTo(width-graphUnit*13+.5, graphUnit*13+.5);
ctx.stroke();
ctx.lineTo(width-graphUnit*15+.5, height-.5);
ctx.stroke();
ctx.closePath();
//Write Ring Numbers
for (i=graphUnit*5; i<width; i+=10*graphUnit) {
txt = i/graphUnit - 5;
txtWidth = ctx.measureText(txt).width;
ctx.fillText(txt, i-txtWidth/2, 4*graphUnit + 5);
}
break;
case document.getElementById("masterGraph"):
//Draw black horizontal lines
ctx.lineWidth = 3;
ctx.beginPath();
ctx.moveTo(graphUnit*5, graphUnit*10 + .5);
ctx.lineTo(width, graphUnit*10 + .5);
ctx.moveTo(graphUnit*5, graphUnit*20 + .5);
ctx.lineTo(width, graphUnit*20 + .5);
ctx.strokeStyle = "rgb(0,0,0)";
ctx.stroke();
ctx.closePath();
//Write Year Numbers
for (i=graphUnit*10; i<width; i+=10*graphUnit) {
temp = data.masterYearStart + i/graphUnit - 5;
if(temp%100 != 0){temp = temp%100;}
txt = temp;
txtWidth = ctx.measureText(txt).width;
ctx.fillText(txt, i-txtWidth/2, 12*graphUnit);
}
//Note halfway mark on index plot
ctx.font = "bold 18px Times";
txt = "1.0";
txtWidth = ctx.measureText(txt).width;
ctx.fillText(txt, 2.5*graphUnit - txtWidth/2, 21*graphUnit);
break;
}
}
function scaleGraphs(){
var userGraph = document.getElementById("userGraph");
var masterGraph = document.getElementById("masterGraph");
var i;
if(typeof graphUnitPrev === 'undefined') { //If first time, define previous graph unit as current graphunit
graphUnitPrev = graphUnit; //Note: one graph square == 5 * graphUnit
}
//Modify userGraph dimensions
userGraph.height = graphUnit*15 + 1; //15 squares tall
//userGraph must be long enough for all rings, the margin on left, and 3 large blocks to right
userGraph.width = graphUnit*(appSettings.rings-1) + graphUnit*20; //one square for each ring, plut 5 left margin and 15 right margin
//Modify graph positions
userGraph.style.left = 760/2 - (760/2 - parseInt(userGraph.style.left)) * graphUnit/graphUnitPrev +"px";
masterGraph.style.left = 760/2 - (760/2 - parseInt(masterGraph.style.left)) * graphUnit/graphUnitPrev +"px";
//Scale dimensions of all graph marks
for(i = 0; i < markData.userGraph.normal.length; ++i){
markData.userGraph.normal[i].x *= graphUnit/graphUnitPrev;
markData.userGraph.normal[i].y *= graphUnit/graphUnitPrev;
}
for(i = 0; i < markData.userGraph.wide.length; ++i){
markData.userGraph.wide[i].x *= graphUnit/graphUnitPrev;
markData.userGraph.wide[i].y *= graphUnit/graphUnitPrev;
}
for(i = 0; i < markData.userGraph.absent.length; ++i){
markData.userGraph.absent[i].x *= graphUnit/graphUnitPrev;
markData.userGraph.absent[i].y *= graphUnit/graphUnitPrev;
}
for(i = 0; i < markData.userGraph.falses.length; ++i){
markData.userGraph.falses[i].x *= graphUnit/graphUnitPrev;
markData.userGraph.falses[i].y *= graphUnit/graphUnitPrev;
}
for(i = 0; i < markData.masterGraph.normal.length; ++i){
markData.masterGraph.normal[i].x *= graphUnit/graphUnitPrev;
markData.masterGraph.normal[i].y *= graphUnit/graphUnitPrev;
}
for(i = 0; i < markData.masterGraph.wide.length; ++i){
markData.masterGraph.wide[i].x *= graphUnit/graphUnitPrev;
markData.masterGraph.wide[i].y *= graphUnit/graphUnitPrev;
}
//Modify masterGraph dimensions
masterGraph.height = graphUnit*30 + 1;
masterGraph.width = (markData.masterGraph.index.length*graphUnit) + graphUnit*20 - graphUnit - 1;
//Reposition Graphs
masterGraph.style.top = parseInt(userGraph.style.top) + userGraph.height + "px";
drawGraphMarks();
}
function drawGraphMarks(){
var userGraph = document.getElementById("userGraph");
var masterGraph = document.getElementById("masterGraph");
var ctxU = userGraph.getContext("2d");
var ctxM = masterGraph.getContext("2d");
var i, txt, txtWidth, x, y;
ctxU.clearRect(0, 0, userGraph.width, userGraph.height);
renderGraphPaper(userGraph);
ctxM.clearRect(0, 0, masterGraph.width, masterGraph.height);
renderGraphPaper(masterGraph);
//Make normal marks
ctxU.lineWidth = 3;
for(i = 0; i < markData.userGraph.normal.length; ++i) {
ctxU.beginPath();
ctxU.moveTo(markData.userGraph.normal[i].x-.5, markData.userGraph.normal[i].y-.5);
ctxU.lineTo(markData.userGraph.normal[i].x-.5, userGraph.height);
ctxU.strokeStyle = "rgb(0,0,0)";
ctxU.stroke();
ctxU.closePath();
}
for(i = 0; i < markData.masterGraph.normal.length; ++i) {
ctxM.beginPath();
ctxM.moveTo(markData.masterGraph.normal[i].x+.5, markData.masterGraph.normal[i].y-.5);
ctxM.lineTo(markData.masterGraph.normal[i].x+.5, 0);
ctxM.strokeStyle = "rgb(0,0,0)";
ctxM.stroke();
ctxM.closePath();
}
//Wide Marks
switch(graphUnit) {
case 5:
ctxU.font = "bold 10px Times";
ctxM.font = "bold 10px Times";
break;
case 6:
ctxU.font = "bold 12px Times";
ctxM.font = "bold 12px Times";
break;
case 8:
ctxU.font = "bold 16px Times";
ctxM.font = "bold 16px Times";
break;
}
for(i = 0; i < markData.userGraph.wide.length; ++i) {
txt = "b";
txtWidth = ctxU.measureText(txt).width;
ctxU.fillText(txt, markData.userGraph.wide[i].x - txtWidth/2, 6.5*graphUnit);
}
for(i = 0; i < markData.masterGraph.wide.length; ++i) {
txt = "b";
txtWidth = ctxM.measureText(txt).width;
ctxM.fillText(txt, markData.masterGraph.wide[i].x - txtWidth/2, 9.5*graphUnit);
}
//Absent Marks
switch(graphUnit) {
case 5:
ctxU.font = "normal 10px Times";
ctxM.font = "normal 10px Times";
break;
case 6:
ctxU.font = "normal 12px Times";
ctxM.font = "normal 12px Times";
break;
case 8:
ctxU.font = "normal 16px Times";
ctxM.font = "normal 16px Times";
break;
}
ctxU.lineWidth=2;
ctxU.strokeStyle= "rgb(0,0,0)";
for(i = 0; i < markData.userGraph.absent.length; ++i) {
ctxU.beginPath();
ctxU.moveTo(markData.userGraph.absent[i].x, userGraph.height -1);
ctxU.lineTo(markData.userGraph.absent[i].x, userGraph.height - graphUnit*2 -1);
ctxU.stroke();
ctxU.moveTo(markData.userGraph.absent[i].x, userGraph.height - graphUnit*4-1);
ctxU.lineTo(markData.userGraph.absent[i].x, userGraph.height - graphUnit*6-1);
ctxU.stroke();
ctxU.moveTo(markData.userGraph.absent[i].x, userGraph.height - graphUnit*8-1);
ctxU.lineTo(markData.userGraph.absent[i].x, userGraph.height - graphUnit*10-1);
ctxU.stroke();
ctxU.closePath();
txt = ">"
txtWidth = ctxU.measureText(txt).width;
ctxU.fillText(txt, markData.userGraph.absent[i].x-txtWidth/2, graphUnit + graphUnit*.75);
}
//False Marks
ctxU.lineWidth = 1;
for(i = 0; i < markData.userGraph.falses.length; ++i) {
ctxU.beginPath();
ctxU.moveTo(markData.userGraph.falses[i].x + graphUnit*.5, userGraph.height -1);
ctxU.lineTo(markData.userGraph.falses[i].x - graphUnit*.5, userGraph.height - graphUnit*2-1);
ctxU.stroke();
ctxU.moveTo(markData.userGraph.falses[i].x + graphUnit*.5, userGraph.height - graphUnit*4-1);
ctxU.lineTo(markData.userGraph.falses[i].x - graphUnit*.5, userGraph.height - graphUnit*6-1);
ctxU.stroke();
ctxU.moveTo(markData.userGraph.falses[i].x + graphUnit*.5, userGraph.height - graphUnit*8-1);
ctxU.lineTo(markData.userGraph.falses[i].x - graphUnit*.5, userGraph.height - graphUnit*10-1);
ctxU.stroke();
ctxU.closePath();
txt = "<"
txtWidth = ctxU.measureText(txt).width;
ctxU.fillText(txt, markData.userGraph.falses[i].x-txtWidth/2, graphUnit + graphUnit*.75);
}
//Master Index Plot
ctxM.lineWidth = 1;
for(i = 1; i < markData.masterGraph.index.length; ++i){
ctxM.beginPath();
ctxM.strokeStyle = "rgb(0,0,0)";
if(i == 1){
x = 5*graphUnit;
y = (2-markData.masterGraph.index[i-1])*graphUnit*10 + 10*graphUnit;
}
ctxM.moveTo(x+.5, y+.5);
x += graphUnit;
y = (2-markData.masterGraph.index[i])*graphUnit*10 + 10*graphUnit;
ctxM.lineTo(x+.5, y+.5);
ctxM.stroke();
ctxM.closePath();
}
}
function drawCoreStrip() {
var coreStrip =document.getElementById("coreStrip");
var ctx = coreStrip.getContext("2d");
var coreEdge1, coreEdge2;
var i, txt, txtWidth;
var adjRingWidth, adjRingX, prevMag;
prevMag = (coreStrip.width - 20) / data.coreLength;
coreStrip.height = 40;
coreStrip.width = 20 + data.coreLength * parseInt(appSettings.coreMag);
//Reposition strip so that it does not leave center anchor
if(prevMag > 0 && prevMag < 4) {
coreStrip.style.left = 760/2 - (760/2 - parseInt(coreStrip.style.left)) * parseInt(appSettings.coreMag)/prevMag +"px";
}
coreEdge1 = 10; coreEdge2 = coreStrip.width-20;
ctx.clearRect(0, 0, coreStrip.width, coreStrip.height);
ctx.lineWidth = 1;
ctx.rect(10.5,10.5,coreStrip.width - 20,20);
ctx.strokeStyle="rgb(0,0,0)";
ctx.stroke();
ctx.fillStyle="rgb(255,255,41)";
ctx.fillRect(11.5,11.5,coreStrip.width-22,18);
//Draw rings
for(i = 0; i < markData.coreStrip.ring.length; ++i) {
//Adjust ring position and width according to magnification setting
adjRingX = markData.coreStrip.ring[i].x * parseInt(appSettings.coreMag) + 10;;
adjRingWidth = markData.coreStrip.ring[i].width * parseInt(appSettings.coreMag);
switch(markData.coreStrip.ring[i].color) {
case "lightLatewood":
ctx.fillStyle="rgb(255,204,051)"
break;
case "mediumLatewood":
ctx.fillStyle="rgb(204,153,000)"
break;
case "darkLatewood":
ctx.fillStyle="rgb(153,102,000)"
break;
}
ctx.beginPath();
//Draw latewood on left 0.25 of ring
ctx.fillRect(Math.floor(adjRingX-adjRingWidth*0.25), 11, Math.floor(adjRingWidth*0.25)+1, 19);
ctx.moveTo(adjRingX+.5, 10);
ctx.lineTo(adjRingX+.5, 30);
ctx.stroke();
ctx.closePath();
//Draw circle and ring number on every 10th ring
ctx.fillStyle="rgb(0,0,0)";
if(i % 10 == 0) {
ctx.beginPath();
ctx.arc(adjRingX-adjRingWidth/2, 20, 2, 0, 2*Math.PI);
ctx.fill();
ctx.closePath();
ctx.font = "bold 12px Times";
txt = i;
txtWidth = ctx.measureText(txt).width;
ctx.fillText(txt, adjRingX-adjRingWidth/2-txtWidth/2, 40);
}
}
}
function writeAnswer() {
var answerText = document.getElementById("answerText");
var i;
answerText.style.fontWeight = "900";
answerText.style.fontFamily = "Times";
answerText.style.fontSize = "16px";
answerText.style.whiteSpace = "pre-line";
answerText.innerHTML = "All of these are part of the answer:\n";
answerText.innerHTML += "Start year: " + answer.yearStart + "\n";
answerText.innerHTML += "Absent rings: ";
if(appSettings.absents == "on") {
for(i = 0; i < answer.absents.length; ++i) {
answerText.innerHTML += answer.absents[i] + " ";
}
}
answerText.innerHTML += "\n";
answerText.innerHTML += "False rings: "
if(appSettings.falses == "on") {
if(answer.falses.length != 0 ) {
answerText.innerHTML += "Rings " + answer.falses[0] + " & " + answer.falses[1] +" are 1 ring"
}
}
answerText.innerHTML += "\n"
answerText.innerHTML += "End year: " + answer.yearEnd;
}
function writeHint() {
var hintText = document.getElementById("hintText");
hintText.style.fontFamily = "Times";
hintText.style.fontWeight = "900";
hintText.style.fontSize="12px";
hintText.innerHTML = "Hint: Core is missing " + answer.absents.length + " rings and has ";
hintText.innerHTML += answer.falses.length / 2 + " false ring(s).";
}
/*Mouse Action Functions*/
//If element has multiple mouse actions, determine which one to use
//Here, only used on userGraph
function handleClick(e) {
var targ = e.target ? e.target : e.srcElement;
//If top 1/3 of element, drag
if(e.offsetY < targ.height / 3) {
startDrag(e);
}
//If lower 1/3 of graph and outside of white margin, draw/erase
else if (e.offsetY >= targ.height / 3 && e.offsetX >= 5*graphUnit) {
if(appSettings.mouseMode == "Draw"){
addMark(e);
}
else if (appSettings.mouseMode == "Erase"){
removeMark(e);
}
drawGraphMarks();
}
}
function startDrag(e) {
// IE uses srcElement, others use target
var targ = e.target ? e.target : e.srcElement;
document.dragTarg = targ;
// calculate event X, Y coordinates
offsetX = e.clientX;
offsetY = e.clientY;
// assign default values for top and left properties
if(targ.style.left == "") { targ.style.left='0px'};
if(targ.style.top == "") { targ.style.top='0px'};
// calculate integer values for top and left
// properties
coordX = parseInt(targ.style.left);
coordY = parseInt(targ.style.top);
// move element
document.onmouseup = stopDrag;
document.onmousemove=dragHoriz;
return false;
}
function dragHoriz(e) {
var targ = document.dragTarg;
// move element
targ.style.left=coordX+e.clientX-offsetX+'px';
//Restrain element
dragLimit(e);
return false;
}
//Conforms dragged element to specific restraints
function dragLimit(e) {
var targ = document.dragTarg;
var frame = document.getElementById("appFrame");
switch(targ) {
//Keep graphs inside appFrame by 50px
case document.getElementById("userGraph"):
case document.getElementById("masterGraph"):
if(parseInt(targ.style.left) > parseInt(frame.style.width) - 50) {
targ.style.left=parseInt(frame.style.width) - 50 + "px";
}
if(parseInt(targ.style.left) < 50 - parseInt(targ.width)) {
targ.style.left = 50 - parseInt(targ.width) + "px";
}
break;
//Keep coreStrip anchored to middle of screen
case document.getElementById("coreStrip"):
if(parseInt(targ.style.left) > parseInt(frame.style.width) / 2) {
targ.style.left=parseInt(frame.style.width) / 2 +"px";
}
if(parseInt(targ.style.left) < parseInt(frame.style.width) / 2 - parseInt(targ.width)) {
targ.style.left = parseInt(frame.style.width) / 2 - parseInt(targ.width) + "px";
}
break;
}
}
function stopDrag() {
document.onmousemove = null;
document.onmouseup = null;
document.dragTarg = null;
}
/*Functions for mark Data*/
//Add a mark to array when userGraph is drawn on by user
function addMark(e) {
var targ = e.target ? e.target : e.srcElement;
switch(appSettings.mouseMark) {
case "Normal":
var newMark = {x:e.offsetX, y:e.offsetY};
newMark.x = Math.round((newMark.x-1)/graphUnit)*graphUnit+1;
markData.userGraph.normal.push(newMark);
userAction.done.push({x: newMark.x, y: newMark.y, array: markData.userGraph.normal, mode:"draw"});
break;
case "Wide":
var newMark = {x:e.offsetX};
markData.userGraph.wide.push(newMark);
userAction.done.push({x: newMark.x, array: markData.userGraph.wide, mode:"draw"});
break;
case "Absent":
var newMark = {x:e.offsetX};
tempVal = Math.round((newMark.x-1)/graphUnit)*graphUnit+1;
if(newMark.x < tempVal) {
newMark.x = Math.round((newMark.x-1)/graphUnit)*graphUnit-Math.round(graphUnit/2);
}
else if(newMark.x >= tempVal){
newMark.x = Math.round((newMark.x-1)/graphUnit)*graphUnit+Math.round(graphUnit/2);
}
markData.userGraph.absent.push(newMark);
userAction.done.push({x: newMark.x, array: markData.userGraph.absent, mode:"draw"});
break;
case "False":
var newMark = {x:e.offsetX};
tempVal = Math.round((newMark.x-1)/graphUnit)*graphUnit+1;
if(newMark.x < tempVal) {
newMark.x = Math.round((newMark.x-1)/graphUnit)*graphUnit-Math.round(graphUnit/2);
}
else if(newMark.x >= tempVal){
newMark.x = Math.round((newMark.x-1)/graphUnit)*graphUnit+Math.round(graphUnit/2);
}
markData.userGraph.falses.push(newMark);
userAction.done.push({x: newMark.x, array: markData.userGraph.falses, mode:"draw"});
break;
}
}
//Remove mark from array when user erases userGraph mark
function removeMark(e){
var targ = e.target ? e.target : e.srcElement;
var bestArray = null;
var bestIdx = 0;
var bestNum = graphUnit; //user must click within one graphUnit away from desired mark
var i;
//For each type of mark, sift through array and find closest mark. Save its index and array
for(i = 0; i < markData.userGraph.normal.length; ++i) {
if(Math.abs(e.offsetX - markData.userGraph.normal[i].x) < bestNum) {
bestNum = Math.abs(e.offsetX - markData.userGraph.normal[i].x);
bestArray = markData.userGraph.normal;
bestIdx = i;
}
}
for(i = 0; i < markData.userGraph.falses.length; ++i) {
if(Math.abs(e.offsetX - markData.userGraph.falses[i].x) < bestNum) {
bestNum = Math.abs(e.offsetX - markData.userGraph.falses[i].x);
bestArray = markData.userGraph.falses;
bestIdx = i;
}
}
for(i = 0; i < markData.userGraph.wide.length; ++i) {
if(Math.abs(e.offsetX - markData.userGraph.wide[i].x) < bestNum) {
bestNum = Math.abs(e.offsetX - markData.userGraph.wide[i].x);
bestArray = markData.userGraph.wide;
bestIdx = i;
}
}
for(i = 0; i < markData.userGraph.absent.length; ++i) {
if(Math.abs(e.offsetX - markData.userGraph.absent[i].x) < bestNum) {
bestNum = Math.abs(e.offsetX - markData.userGraph.absent[i].x);
bestArray = markData.userGraph.absent;
bestIdx = i;
}
}
//If no mark is found, do nothing
if(bestArray != null) {
if(bestArray == markData.userGraph.normal) {
userAction.done.push({x: bestArray[bestIdx].x, y:bestArray[bestIdx].y, array:bestArray, mode:"erase"});
}
else {
userAction.done.push({x: bestArray[bestIdx].x, array:bestArray, mode:"erase"});
}
bestArray.splice(bestIdx, 1);
}
}
function undo() {
if(!(userAction.done.length > 0) ){
return;
}
var mark = userAction.done[userAction.done.length - 1];
switch(mark.mode) {
case "draw":
mark.array.splice(mark.array.length - 1,1);
break;
case "erase":
mark.array.push(mark);
break;
}
userAction.undone.push(mark);
userAction.done.splice(userAction.done.length-1,1);
drawGraphMarks();
}
function redo() {
if(!(userAction.undone.length > 0) ){
return;
}
var mark = userAction.undone[userAction.undone.length-1];
switch(mark.mode) {
case "draw":
mark.array.push(mark);
break;
case "erase":
mark.array.splice(mark.array.length - 1,1);
break;
}
userAction.done.push(mark);
userAction.undone.splice(userAction.undone.length-1,1);
drawGraphMarks();
}