forked from bjpop/js-turtle
-
Notifications
You must be signed in to change notification settings - Fork 7
/
turtleConsole.js
913 lines (798 loc) · 30.4 KB
/
turtleConsole.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
/************************************************************************
* turtleConsole -- javascript for the turtle graphic language console
*
* Copyright (c) 2015-2019 Kirk Carlson
* MIT license
************************************************************************/
//console.log("Starting up")
//**GLOBALS***
var helpTextActive = true;
var errorFound = false;
//SUPPORT FUNCTIONS
/************************************************************************
* cmd -- put text into the command box
*
* arguments:
* text: (string) string to put into the command box
*
* returns:
* None
************************************************************************/
function cmd (text) {
document.getElementById("command").value=text;
}
if (window.addEventListener) {
window.addEventListener("resize", fixDragButton);
} else if (window.attachEvent) {
window.attachEvent("onresize", fixDragButton);
}
/*************************************************************************
* onWindowLoad -- handler for when window loads
*
* arguments:
* None
*
* returns:
* None
*************************************************************************/
function onWindowLoad() {
fixDragButton()
// check if an example was requested in the URL
var queryString = window.location.search; // was "?..." specified
if (queryString != undefined && queryString != "") {
var exampleValue = ""
var command = ""
var pos = 0
//queryString = queryString.substr(1) // get rid of leading '?'... simple case
// want to (queryStrint + "&").search (/[?&]example=[^=]&/)
// no want to split string up into separate queries... divide on &
queries = queryString.split('&')
console.log("queries was: " + queries + ", " + typeof(queries))
// check specific queries like
if (queries != undefined && queries.length > 0) {
for (var i=0; i<queries.length; i = i+1) {
pos = queries[i].search(/^\??example=/)
//want to change 'code' to 'exampleValue'
// exampleOption ...name that is displayed
// exampleValue ... example string name ,,, its value is the string itself
if (pos >=0) {
console.log( "ind: " + queries[i] + ", " + typeof(queries[i]))
pos = queries[i].indexOf('=')
if (pos > 0 && pos < queries[i].length) {
exampleValue = queries[i].substr(pos + 1)
console.log("example query was: " + exampleValue + ".")
} else {
console.log("example query was null")
}
}
pos = queries[i].search(/^\??command=/)
if (pos >=0) {
pos = queries[i].indexOf('=')
if (pos > 0 && pos < queries[i].length) {
command = queries[i].substr(pos + 1)
console.log("command query was: " + command + ".")
} else {
console.log("command query was null")
}
}
pos = queries[i].search(/^\??codeblock=/)
if (pos >=0) {
pos = queries[i].indexOf('=')
if (pos > 0 && pos < queries[i].length) {
codeBlock = queries[i].substr(pos + 1)
codeBlock = decodeURIComponent(codeBlock)
codeBlock = he.decode(codeBlock)
document.getElementById('codeArea').value = codeBlock
} else {
console.log("command query was null")
}
}
}
if (exampleValue != undefined && exampleValue != "") {
sel = document.getElementById('examples') // post to examples selector
sel.value = exampleValue; // set selector to requested string
//... onchange hander should take over
console.log("sel.value: " + sel.value + ".")
if (sel.value !== undefined && sel.value !== "") {
console.log("almost in it now")
try {
document.getElementById('codeArea').value = eval(examples.value);
} catch (e) {
showError(e)
}
if (command !== undefined || command !== "") { // good enough validation??
console.log("in it now")
cmd ("demo()");
}
console.log("passed it")
commandChanged()
}
}
}
}
}
var draggingleft = false;
var draggingright = false;
/*************************************************************************
* fixDragButton -- handler to fix the drag buttons
*
* arguments:
* None
*
* returns:
* None
*************************************************************************/
function fixDragButton() {
//console.log("fixDragButton")
var w = window.innerWidth
|| document.documentElement.clientWidth
|| document.body.clientWidth; // variations for cross browser support
var h = window.innerHeight
|| document.documentElement.clientHeight
|| document.body.clientHeight; // variations for cross browser support
if (w < 12000) {
var overallWidth = w;
} else {
var overallWidth = 1200;
}
// work area height
var overallHeight = h /* guessed margin */;
var workAreaHeight = h -4 ; /* - 50 /*top displacement* / - 17 /* guessed margin? */;
if (workAreaHeight < 400) {
var canvasHeight = 300;
} else {
var canvasHeight = workAreaHeight - 140 /* APPROXIMATION space for controls */;
}
var wrapWidth = overallWidth - 2; //leftcolWidth + midcolWidth + rightcolWidth;
var referencewidth, refLeftPadding , dragleft, containertop, dropbarwidthleft, dropbarwidthright
var containertop = Number(getStyleValue(document.getElementById("container"), "top").replace("px", ""));
var wrapElement = document.getElementById("wrap");
wrapElement.style.width = wrapWidth + "px";
wrapElement.style.height = overallHeight + "px";
/* dragbar setup*/
/* left setup */
var refElement = document.getElementById("reference");
var leftcolElement = document.getElementById("leftcolumn")
referenceWidth = Number(getStyleValue(document.getElementById("referencewrapper"), "width").replace("px", ""));
var refTitleHeight = Number(getStyleValue(document.getElementById("referenceTitle"), "height").replace("px", ""));
var refLeftPadding = Number(getStyleValue(document.getElementById("reference"), "padding-left").replace("px", ""));
/* center setup */
midWidth = getStyleValue(document.getElementById("canvaswrapper"), "width").replace("px","");
midContainerHeight = getStyleValue(document.getElementById("midcolumncontainer"), "height").replace("px","");
midLeftPadding = getStyleValue(document.getElementById("canvaswrapper"), "padding-left").replace("px","");
midRightPadding = getStyleValue(document.getElementById("canvaswrapper"), "padding-right").replace("px","");
canvasTitleHeight = getStyleValue(document.getElementById("canvastitle"), "height").replace("px","");
commandWrapperHeight = getStyleValue(document.getElementById("commandwrapper"), "height").replace("px","");
var canvasHeight = midContainerHeight - canvasTitleHeight - commandWrapperHeight -25;
var canvasWidth = midWidth - midLeftPadding - midRightPadding;
/* right setup */
exampleWidth = Number(getStyleValue(document.getElementById("examplewrapper"), "width").replace("px", ""));
examplesHeight = Number(getStyleValue(document.getElementById("examples"), "height").replace("px", "")); // basically the select height
examplesMarginTop = Number(getStyleValue(document.getElementById("examples"), "margin-top").replace("px", "")); // around select height
examplesMarginBottom = Number(getStyleValue(document.getElementById("examples"), "margin-bottom").replace("px", "")); // around select height
var rightcolElement = document.getElementById("rightcolumn");
var codeAreaElement = document.getElementById("codeArea");
var codeAreaRightPadding = Number(getStyleValue(document.getElementById("codeArea"), "padding-right").replace("px", ""));
/* dragbar attribute setting */
document.getElementById("dragbarleft").style.width = "5px";
document.getElementById("dragbarright").style.width = "5px";
dropbarwidthleft = Number(getStyleValue(document.getElementById("dragbarleft"), "width").replace("px", ""));
dropbarwidthright = Number(getStyleValue(document.getElementById("dragbarright"), "width").replace("px", ""));
dragleft = referenceWidth + refLeftPadding + (refLeftPadding / 2) - (dropbarwidthleft / 2);
dragright = exampleWidth + codeAreaRightPadding + (codeAreaRightPadding / 2) + (dropbarwidthright / 2);
document.getElementById("dragbarleft").style.top = containertop + "px";
document.getElementById("dragbarleft").style.left = dragleft + "px";
document.getElementById("dragbarleft").style.height = workAreaHeight + "px";/*referenceheight;*/
document.getElementById("dragbarleft").style.cursor = "col-resize";
document.getElementById("dragbarright").style.top = containertop + "px";
document.getElementById("dragbarright").style.right = dragright + "px";
document.getElementById("dragbarright").style.height = workAreaHeight + "px";/*referenceheight;*/
document.getElementById("dragbarright").style.cursor = "col-resize";
/* left attribute setting */
refElement.style.height = workAreaHeight - refTitleHeight -10 + "px";
leftcolElement.style.height = workAreaHeight + "px";
//console.log ("overallheight",overallHeight, "workAreaHeight", workAreaHeight)
/* center attribute setting */
imagecanvas.width = canvasWidth;
imagecanvas.height = canvasHeight;
turtlecanvas.width = canvasWidth;
turtlecanvas.height = canvasHeight;
document.getElementById("canvaswrapper").style.height = canvasHeight +8+ "px";
//console.log("midWidth:", midWidth, midLeftPadding, midRightPadding);
var midcolElement = document.getElementById("midcolumn")
midcolElement.style.height = workAreaHeight + "px";
/* right attribute setting */
rightcolElement.style.height = workAreaHeight + "px";
codeAreaElement.style.height = (workAreaHeight - examplesHeight - examplesMarginTop - examplesMarginBottom - 45) + "px";
}
/*************************************************************************
* dragStartLeft -- handler for start of drag with left button
*
* arguments:
* None
*
* returns:
* None
*************************************************************************/
function dragstartleft(e) {
e.preventDefault();
draggingleft = true;
}
/*************************************************************************
* dragStartRight -- handler for start of drag with right button
*
* arguments:
* None
*
* returns:
* None
*************************************************************************/
function dragstartright(e) {
e.preventDefault();
draggingright = true;
}
/*************************************************************************
* dragMove -- handler for moving a drag button
*
* arguments:
* None
*
* returns:
* None
*************************************************************************/
function dragmove(e) {
if (draggingleft)
{
var rect = document.getElementById("dragbarright").getBoundingClientRect();
//console.log("dragBarRight:", rect.top, rect.right, rect.bottom, rect.left);
//console.log("window width:", window.innerWidth);
var rightPercentage = 100 - (rect.left / window.innerWidth) * 100;
leftPercentage = (e.pageX / window.innerWidth) * 100;
if (leftPercentage > 1 && leftPercentage < 98) {
var centerPercentage = 100-leftPercentage-rightPercentage;
//console.log("left:", leftPercentage, centerPercentage, rightPercentage);
document.getElementById("leftcolumncontainer").style.width = leftPercentage + "%";
document.getElementById("midcolumncontainer").style.width = centerPercentage + "%";
fixDragButton();
}
}
if (draggingright)
{
var rect = document.getElementById("dragbarleft").getBoundingClientRect();
//console.log("dragBarLeft:", rect.top, rect.right, rect.bottom, rect.left);
//console.log("width:", window.innerWidth);
var leftPercentage = (rect.right / window.innerWidth) * 100;
//console.log("leftPercentage:", leftPercentage);
var rightPercentage = 100 - (e.pageX / window.innerWidth) * 100;
//console.log("rightPercentage:", rightPercentage);
if (rightPercentage > 1 && rightPercentage < 98 - leftPercentage) {
var centerPercentage = 100-rightPercentage-leftPercentage;
//console.log("right:", leftPercentage, centerPercentage, rightPercentage);
document.getElementById("rightcolumncontainer").style.width = rightPercentage + "%";
document.getElementById("midcolumncontainer").style.width = centerPercentage + "%";
fixDragButton();
}
}
}
/*************************************************************************
* dragEnd -- handler for ending a drag move
*
* arguments:
* None
*
* returns:
* None
*************************************************************************/
function dragend() {
draggingleft = false;
draggingright = false;
if (window.editor) {
window.editor.refresh();
}
}
if (window.addEventListener) {
document.getElementById("dragbarleft").addEventListener("mousedown", function(e) {dragstartleft(e);});
document.getElementById("dragbarleft").addEventListener("touchstart", function(e) {dragstartleft(e);});
document.getElementById("dragbarright").addEventListener("mousedown", function(e) {dragstartright(e);});
document.getElementById("dragbarright").addEventListener("touchstart", function(e) {dragstartright(e);});
window.addEventListener("mousemove", function(e) {dragmove(e);});
window.addEventListener("touchmove", function(e) {dragmove(e);});
window.addEventListener("mouseup", dragend);
window.addEventListener("touchend", dragend);
window.addEventListener("load", onWindowLoad);
}
/*************************************************************************
* getStyleValue -- function
*
* arguments:
* elmnt: (object) pointer to object
* style: (string) name of the requested style
*
* returns:
* element style (string)
*************************************************************************/
function getStyleValue(elmnt,style) {
if (window.getComputedStyle) {
return window.getComputedStyle(elmnt,null).getPropertyValue(style);
} else {
return elmnt.currentStyle[style];
}
}
//EVENT PROCESSING FUNCTIONS
/*************************************************************************
* stopClicked -- handler for when stop button is clicked
*
* arguments:
* None
*
* returns:
* None
*************************************************************************/
function stopClicked() {
//console.log("stop clicked")
stopAnimation()
}
// set up command field to accept an ENTER without field modification
var command = document.getElementById("command");
if (command.addEventListener) {
command.addEventListener("keypress", function(e) {
if (e.keyCode === 13) {
commandChanged();
e.preventDefault();
}
}, false);
} else if (command.attachEvent) {
command.attachEvent("onkeypress", function(e) {
if (e.keyCode === 13) {
commandChanged();
return e.returnValue = false;
}
});
}
/*************************************************************************
* resetClicked -- handler for when the reset button is clicked
*
* arguments:
* None
*
* returns:
* None
*************************************************************************/
function resetClicked() {
reset()
}
/*************************************************************************
* runClicked -- handler for when the run button is clicked
*
* arguments:
* None
*
* returns:
* None
*************************************************************************/
function runClicked() {
cmd ("demo()");
commandChanged();
}
/*************************************************************************
* uploadChanged(e) -- handler for when the upload file name changes
*
* arguments:
* e: (element object) input file element that has changed
*
* returns:
* None
*************************************************************************/
function uploadChanged(e) {
var file = e.target.files[0];
if (!file) {
return;
}
var reader = new FileReader();
reader.onload = function(e) {
document.getElementById('codeArea').value = e.target.result;
};
reader.readAsText(file);
}
var get_blob = function() {
return Blob;
}
/*************************************************************************
* downloadClicked -- handler for when the download button is clicked
*
* arguments:
* None
*
* returns:
* false to prevent further processing
*************************************************************************/
function downloadClicked(e) {
e.preventDefault();
var BB = get_blob();
saveAs(
new BB(
[codeArea.value || codeArea.placeholder]
, {type: "text/plain;charset=" + document.characterSet}
)
, (downloadFilename.value || downloadFilename.placeholder) + ".js"
);
return false;
}
/*************************************************************************
* svgDownloadClicked -- handler for when the SVG download button is clicked
*
* arguments:
* None
*
* returns:
* false to prevent further processing
*************************************************************************/
function svgDownloadClicked(e) {
/*
e.preventDefault();
svgClose();
var BB = get_blob();
saveAs(
new BB(
svgBlob, {type: "text/plain;charset=" + document.characterSet}
)
, (downloadFilename.value || downloadFilename.placeholder) + ".svg"
);
*/
return false;
}
/*************************************************************************
* saveCanvasClicked -- handler for when the save canvas button is clicked
*
* arguments:
* None
*
* returns:
* false to prevent further processing
*************************************************************************/
function saveCanvasClicked(e) {
e.preventDefault();
var BB = get_blob();
saveAs(
new BB(
[codeArea.value || codeArea.placeholder]
, {type: "text/plain;charset=" + document.characterSet}
)
, "turtleGraphic.png"
);
return false;
}
/*************************************************************************
* clearClicked -- handler for when the clear button is clicked
*
* arguments:
* None
*
* returns:
* None
*************************************************************************/
function clearClicked() {
//console.log("clear clicked")
document.getElementById("codeArea").value = "";
}
/*************************************************************************
* infoClicked -- handler for when the info button is clicked
*
* arguments:
* None
*
* returns:
* None
*************************************************************************/
function infoClicked() {
helpTextActive = !helpTextActive;
if (helpTextActive) {
document.getElementById("infoButton").style.color = "blue";
document.getElementById("infoButton").style.borderColor = "blue";
} else {
document.getElementById("infoButton").style.color = "lightgray";
document.getElementById("infoButton").style.borderColor = "lightgray";
}
}
// Set up all reference code elements to be linked and have onclick functionality
var codeElements = document.querySelectorAll ("#reference code");
for (var i=0; i< codeElements.length; i++) {
codeElements[i].className = "linked";
codeElements[i].onclick = function () {
cmd (this.innerHTML + ";");
commandChanged();
}
}
// Set up all color button elements to be linked and have onclick functionality
var codeElements = document.querySelectorAll ("#reference button");
for (var i=0; i< codeElements.length; i++) {
//console.log(codeElements[i].id)
codeElements[i].onclick = function () {
cmd ("color(\"" + this.id +"\");");
commandChanged();
}
}
//INITITALIZATION FUNCTIONS
// load the example code when the corresponding demo menu item is clicked
document.getElementById("examples").onchange = examplesChanged;
document.getElementById("command").onchange=commandChanged;
/*
--
on window load:
should the code auto run or not...
yes for the examples
yes for the samples
no for codeblock... override with command=demo()
on window load?
not loaded with examples
reloaded with with samples
reloaded with codeblock
document.getElementById('codeArea').value = eval(sel.value);
eval (document.getElementById("codeArea").value);
console.log("eval \"demo()\"")
eval ("demo();");
//eval (document.getElementById("codeArea").value);
// eval (command + "();");
//document.getElementById('codeArea').value = eval(sel.value);
examples
kirk
*/
//**************************************
//***** ******
//***** BEWARE THE EVIL EVAL!!! ******
//***** ******
//**************************************
//*** Boys and girls please don't use eval() functions at home. In general
//*** the evals are evil because 'anything' can be entered by the user and
//*** executed. This includes changing variables and functions. Things
//*** will break. Most problems can be overcome by reloading the page.
//*** eval is useful for this type of web page because we need the student
//*** to enter, try, and experiment with code. That is the point of all this.
/*************************************************************************
* examplesChanged -- handler for when the example select changed
*
* arguments:
* None
*
* returns:
* None
*************************************************************************/
function examplesChanged () {
stopAnimation()
var codeArea = document.getElementById('codeArea')
var examples = document.getElementById('examples')
try {
codeArea.value = eval(examples.value);
} catch (e) {
showError(e)
}
cmd ("demo()");
commandChanged()
}
/*************************************************************************
* commandChanged -- handler for when the command box is changed
*
* arguments:
* None
*
* returns:
* None
*************************************************************************/
function commandChanged () {
var commandText = document.getElementById("command").value;
var codeAreaText = document.getElementById('codeArea').value;
errorFound = false
stopAnimation()
try {
// execute any code in the codeArea box
console.log("cC codeArea")
eval(codeAreaText);
} catch(e) {
errorFound = true
showError(e)
}
// execute the code in the command box
if (!errorFound && ( commandText !== "demo()" ||
commandText !== "demo();" ||
demo !== undefined)) {
//same as !==demo() || ==demo(); && !==undefined
try {
console.log("cC cmd: " + commandText + ".")
eval(commandText);
} catch(e) {
errorFound = true
showError(e)
stopClicked()
} finally {
// clear the command box
this.value = "";
}
}
}
/*************************************************************************
* showErrors -- show the trapped errors on the canvas
*
* arguments:
* e: (error object) error object
*
* returns:
* None
*************************************************************************/
function showError(e) {
//logTurtle("sEtop")
saveTurtleState(turtleState)
imageContext.save();
//turtleState = turtle;
//logTurtle("sEtop")
height=10 // points
goto (minX(), minY()+24+height/2 +2);
angle(90);
wrap(false);
// clear the line for the error message
penDown()
color ("yellow")
width (height+4)
forward (maxY() * 2)
goto (minX(), minY()+24)
// write the error message
color("red");
setfont (height + "pt bold Helvetica, sans-serif")
write(e.name + ": " + e.message);
console.log(e.name + ": " + e.message);
if (e.filename !== undefined) {
// clear the line for the file message
height=10 // points
color ("yellow")
width (height+4)
goto (minX(), minY()+5+height/2 +2)
forward (maxY() * 2)
// write the file message
color("blue");
setfont (height + "pt bold Helvetica, sans-serif")
goto (minX(), minY()+5)
write(e.fileName.substr(-40) + " line: " + e.lineNumber);
console.log("Error: " + e.fileName.substr(-40) + " line: " + e.lineNumber);
}
draw()
restoreTurtleState(turtleState)
imageContext.restore();
draw()
//logTurtle("sEbot")
}
/*************************************************************************
* twoDigits -- convert a number to a two digit string
*
* arguments:
* None
*
* returns:
* None
*************************************************************************/
function twoDigits(n) {
n = n % 100; //in case over 100
if (n <10) {
n = "0" + n
}
return n
}
// set up the control buttons
document.getElementById("resetButton").onclick=resetClicked;
document.getElementById("runButton").onclick=runClicked
document.getElementById("infoButton").onclick=infoClicked;
document.getElementById("body").onresize=fixDragButton;
document.getElementById("stopButton").onclick=stopClicked;
document.getElementById("stopButton").hidden=true;
document.getElementById("downloadButton").onclick=downloadClicked;
document.getElementById("saveSVG").onclick=svgDownloadClicked;
document.getElementById("uploadButton").onclick= function () {
document.getElementById("uploadFile").click();
};
document.getElementById("clearButton").onclick=clearClicked;
//document.getElementById("saveCanvasButton").onclick=saveCanvasClicked;
saveCanvasLink = document.getElementById("saveCanvasButton");
saveCanvasLink.addEventListener('click', function(ev) {
saveCanvasLink.href = imagecanvas.toDataURL();
var d = new Date();
var timestamp =
"" +
d.getFullYear() +
twoDigits(d.getMonth()+1) +
twoDigits(d.getDate()) +
"_" +
twoDigits(d.getHours()) +
twoDigits(d.getMinutes()) +
twoDigits(d.getSeconds())
saveCanvasLink.download = "TurtleGraphics_" + timestamp + ".png";
}, false);
document.getElementById("uploadFile")
.addEventListener('change', uploadChanged, false);
mouseOverElementIds = [ // list of elements with help text
"clearButton",
"codeArea",
"command",
"downloadButton",
"downloadFilename",
"dragbarright",
"dragbarleft",
"examples",
"infoButton",
"reference",
"resetButton",
"runButton",
"stopButton",
"turtlecanvas",
"uploadButton"
];
var helpTextTimer; // global for delaying all help text
var helpDelay = 1000; // global delay in milliseconds for all help text
var bottomY = (window.innerHeight // global for lowest Y on page for tool tip
|| document.documentElement.clientHeight
|| document.body.clientHeight) // variations for cross browser support
- 50; // bottom margin in pixels
for (var i=0; i < mouseOverElementIds.length; i++) {
element = document.getElementById(mouseOverElementIds[i]);
element.onmouseenter = function (event) {
var tooltip = document.getElementById(this.id + "_help_text");
//console.log ( "hamburger: " + mouseOverElementIds[i]) + tooltip.innerHTML;
onHelpEnter(tooltip);
if (event.clientY < bottomY) {
tooltip.style.top = event.clientY + "px";
} else {
tooltip.style.top = bottomY + "px";
}
if (this.id === "examples" || this.id === "codeArea") { // do on left
tooltip.style.right = window.innerWidth - event.clientX + "px";
} else { // do on the right side
tooltip.style.left = event.clientX + "px";
}
}
element.onmouseleave = function () {
var tooltip = document.getElementById(this.id + "_help_text");
onHelpExit(tooltip);
}
}
/*************************************************************************
* onHelpEnter -- handler for when mouse enters an element with help text
*
* arguments:
* None
*
* returns:
* None
*************************************************************************/
function onHelpEnter (helpTextElement) {
if ((helpTextActive ||
(helpTextElement == document.getElementById("infoButton_help_text"))) &&
helpTextTimer === undefined) {
helpTextTimer = setTimeout(onHelpTimeout,helpDelay, helpTextElement);
}
}
/*************************************************************************
* onHelpExit -- handler for when mouse leaves an element with help text
*
* arguments:
* None
*
* returns:
* None
*************************************************************************/
function onHelpExit (helpTextElement) {
if (helpTextTimer != undefined) {
window.clearTimeout (helpTextTimer);
}
helpTextElement.style.display="none";
helpTextTimer = undefined;
}
/*************************************************************************
* onHelpTimeout -- handler for when mouse remains in element with help text
*
* arguments:
* None
*
* returns:
* None
*************************************************************************/
function onHelpTimeout (helpTextElement) {
helpTextElement.style.display="block";
helpTextTimer = undefined;
}