-
Notifications
You must be signed in to change notification settings - Fork 22
/
sgn.js
2034 lines (1757 loc) · 49.2 KB
/
sgn.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
/*
The MIT License (MIT)
Copyright (c) 2016 SeedCode
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
var sgn = (function(settings) {
'use strict'
return {
load: load,
spot: spot,
}
//public functions
function load(sidebarId, canvasDivId) {
var v;
var r;
var t;
var n;
var c;
var a;
var d;
var o;
var i;
//establish sidebar and pad
if (!sidebarId || !canvasDivId) {
alert("Error:Canvas and Sidebar not specified");
return;
}
settings.sidebarDiv = document.getElementById(sidebarId);
settings.divCanvas = document.getElementById(canvasDivId);
//preserve ids specified in ininitial call
settings.idNames.sidebar = sidebarId;
settings.idNames.pad = canvasDivId;
//set classes
settings.sidebarDiv.className = "sidebar";
settings.divCanvas.className = "pad";
//create data object from url
//load preset/favorote from settings if none retrieved from the URL
var parms = location.search.substring(1).split("&");
if (parms[0]) {
d = {};
for (c in parms) {
a = parms[c].split("=");
d[a[0]] = decodeURIComponent(a[1]);
}
if (d["cl"]) {
//append hash to color
d["cl"] = "#" + d["cl"];
}
if (d["pre"]) {
//a preset was passed, so use that
for (c in settings.presets) {
if (settings.presets[c].name === d["pre"]) {
d = settings.presets[c];
}
}
}
if (!d["st"] || !d["r1"] || !d["pen"]) {
//validate object properties or else load pre-set
var preSet = Math.round(Math.random() * (settings.presets.length - 1));
d = settings.presets[preSet];
}
if (!d["sp"]) {
//set speed if not specified
d["sp"] = 1;
}
} else {
//no parms specified
var preSet = Math.round(Math.random() * (settings.presets.length - 1));
d = settings.presets[preSet];
}
//remove rotors to start clean
while (settings.sidebarDiv.firstChild) {
settings.sidebarDiv.removeChild(settings.sidebarDiv.firstChild);
}
//Begin creating sidebar elements
//add static elements and values from preset
//add events to inputs
var e = document.createElement("SPAN");
e.className = "title";
e.innerHTML = "Spirograph";
e.innerHTML += '<span class="n">ⁿ</span>';
settings.sidebarDiv.appendChild(e);
e = document.createElement("DIV");
e.className = "divSeparator";
settings.sidebarDiv.appendChild(e);
e = document.createElement("SPAN");
e.className = "label";
e.innerHTML = "presets";
settings.sidebarDiv.appendChild(e);
var sel = document.createElement("SELECT");
sel.className = "preset";
sel.setAttribute("name", "presets");
sel.setAttribute("id", settings.idNames.presets);
for (c in settings.presets) {
o = document.createElement("OPTION");
o.setAttribute("value", settings.presets[c].name);
o.innerHTML = settings.presets[c].name;
sel.appendChild(o);
}
settings.sidebarDiv.appendChild(sel)
presetEvent(settings.idNames.presets, "change");
e = document.createElement("DIV");
e.className = "divSeparator";
settings.sidebarDiv.appendChild(e);
e = document.createElement("SPAN");
e.className = "label";
e.innerHTML = "stator";
settings.sidebarDiv.appendChild(e);
e = document.createElement("BR");
settings.sidebarDiv.appendChild(e);
e = document.createElement("SPAN");
e.className = "letterLabel";
e.innerHTML = "sr";
settings.sidebarDiv.appendChild(e);
e = document.createElement("INPUT");
e.className = "number";
e.setAttribute("type", "text");
e.setAttribute("value", d.st);
e.setAttribute("id", settings.idNames.stator);
settings.sidebarDiv.appendChild(e);
inputEvent(settings.idNames.stator, "input");
e = document.createElement("DIV");
e.className = "divSeparator";
settings.sidebarDiv.appendChild(e);
e = document.createElement("SPAN");
e.className = "label";
e.innerHTML = "rotors";
settings.sidebarDiv.appendChild(e);
addButton();
e = document.createElement("BR");
settings.sidebarDiv.appendChild(e);
e = document.createElement("DIV");
e.className = "rotors";
e.setAttribute("id", settings.idNames.rotors);
settings.sidebarDiv.appendChild(e);
//add delete block
e = document.createElement("DIV");
e.setAttribute("id", settings.idNames.deleteBlock);
settings.sidebarDiv.appendChild(e);
e = document.createElement("DIV");
e.className = "divSeparator";
settings.sidebarDiv.appendChild(e);
//add pen block first as rotor routine appends values;
e = document.createElement("SPAN");
e.className = "label";
e.innerHTML = "pen";
settings.sidebarDiv.appendChild(e);
e = document.createElement("BR");
settings.sidebarDiv.appendChild(e);
e = document.createElement("SPAN");
e.className = "letterLabel";
e.innerHTML = "r";
settings.sidebarDiv.appendChild(e);
e = document.createElement("INPUT");
e.className = "number";
e.setAttribute("type", "text");
e.setAttribute("id", settings.idNames.pen);
settings.sidebarDiv.appendChild(e);
inputEvent(settings.idNames.pen, "input");
e = document.createElement("BR");
settings.sidebarDiv.appendChild(e);
e = document.createElement("SPAN");
e.className = "letterLabel";
e.innerHTML = "w";
settings.sidebarDiv.appendChild(e);
e = document.createElement("INPUT");
e.className = "number";
e.setAttribute("type", "text");
e.setAttribute("id", settings.idNames.width);
e.setAttribute("step", ".01");
settings.sidebarDiv.appendChild(e);
inputEvent(settings.idNames.width, "input");
e = document.createElement("BR");
settings.sidebarDiv.appendChild(e);
e = document.createElement("SPAN");
e.className = "letterLabel";
e.innerHTML = "c";
settings.sidebarDiv.appendChild(e);
if (colorInputOK()) {
//load color input
e = document.createElement("INPUT");
e.className = "color";
e.setAttribute("type", "color");
e.setAttribute("id", settings.idNames.color);
settings.sidebarDiv.appendChild(e);
inputEvent(settings.idNames.color, "input");
} else {
//load select color list if color input unavailable
var e = document.createElement("SELECT");
e.className = "text";
e.setAttribute("name", "colors");
e.setAttribute("id", settings.idNames.color);
for (c in settings.penColors) {
o = document.createElement("OPTION");
o.setAttribute("value", settings.penColors[c].hex);
o.innerHTML = settings.penColors[c].name;
e.appendChild(o);
}
settings.sidebarDiv.appendChild(e)
inputEvent(settings.idNames.color, "change");
}
settings.sidebarDiv.appendChild(e);
e = document.createElement("DIV");
e.className = "divSeparator";
settings.sidebarDiv.appendChild(e);
e = document.createElement("SPAN");
e.className = "label";
e.innerHTML = "speed";
settings.sidebarDiv.appendChild(e);
e = document.createElement("BR");
settings.sidebarDiv.appendChild(e);
var e = document.createElement("SELECT");
e.className = "preset";
e.setAttribute("name", "speeds");
e.setAttribute("id", settings.idNames.speed);
for (c in settings.speedSettings) {
o = document.createElement("OPTION");
o.setAttribute("value", settings.speedSettings[c].speed);
o.innerHTML = settings.speedSettings[c].name;
e.appendChild(o);
}
settings.sidebarDiv.appendChild(e)
inputEvent(settings.idNames.speed, "change");
e = document.createElement("DIV");
e.className = "divSeparator";
settings.sidebarDiv.appendChild(e);
drawButton();
e = document.createElement("DIV");
e.className = "divSeparator";
settings.sidebarDiv.appendChild(e);
//utility buttons
clearButton();
restartButton();
hideButton();
openImageButton();
settingsURLButton();
e = document.createElement("BR");
settings.sidebarDiv.appendChild(e);
zoomInButton();
zoomOutButton();
e = document.createElement("INPUT");
e.className = "angle";
e.setAttribute("type", "text");
e.setAttribute("id", settings.idNames.angle);
e.setAttribute("value", settings.iOffset);
e.setAttribute("title", "rotation increment");
settings.sidebarDiv.appendChild(e);
inputEvent(settings.idNames.angle, "input");
rotateButton();
resetButton();
e = document.createElement("DIV");
e.className = "divSeparator";
settings.sidebarDiv.appendChild(e);
gitButton();
e = document.createElement("DIV");
e.className = "divSeparator";
settings.sidebarDiv.appendChild(e);
//load canvas
setValues();
//add canvas
drawCanvas();
//load values into inputs
var lr = loadValues(d);
window.onresize = function(e) {
resizeCanvas(e);
};
var rotorsDiv = document.getElementById('rotors');
rotorsDiv.style.height = (settings.numRotors * 27) + (settings.numRotors * 4.3 ) + 'px';
}
function spot(d) {
var d = {
"a": "600",
"b": "600",
"sz": "600",
"canvasClass": "pad",
"closeFunction": "close",
"sr": "250",
"r1": "125h",
"pen": "125",
}
}
// buttons
function addButton() {
var b = document.createElement("BUTTON");
b.setAttribute("class", "button add");
b.setAttribute("id", settings.idNames.add);
var i = document.createElement("IMG");
i.className = "add";
i.setAttribute("src", "img/add.png");
b.appendChild(i);
var s = document.createElement("SPAN");
s.className = "buttonTextSmall"
s.innerHTML += "add rotor";
b.appendChild(s);
settings.sidebarDiv.appendChild(b);
b.addEventListener("click", function() {
//get current last rotor radius
var newRad = document.getElementById(settings.idNames.rotor + settings.numRotors).value / 2;
//toggle types
if (document.getElementById(settings.idNames.e + settings.numRotors).checked) {
var type = "h";
} else {
var type = "e";
}
settings.numRotors++;
addRotor(settings.numRotors, newRad, type);
//update pen radius
document.getElementById(settings.idNames.pen).value = newRad;
var rotorsDiv = document.getElementById('rotors');
rotorsDiv.style.height = (settings.numRotors * 27) + (settings.numRotors * 4.3 ) + 'px';
//if this is the second rotor we need our delete button.
if (settings.numRotors === 2) {
deleteButton();
}
//scroll to the bottom
document.getElementById(settings.idNames.rotors).scrollTop = document.getElementById(settings.idNames.rotors).scrollHeight;
setValues();
if (!settings.draw) {
drawCircles();
}
});
}
function deleteButton() {
var div = document.getElementById(settings.idNames.deleteBlock);
var e = document.createElement("DIV");
e.className = "divSeparator";
e.setAttribute("id", settings.idNames.deleteSep);
div.appendChild(e);
var span = document.createElement("SPAN");
span.setAttribute("id", settings.idNames.deleteLine);
div.appendChild(span);
var b = document.createElement("BUTTON");
b.className = "delete";
b.setAttribute("id", settings.idNames.delete);
span.appendChild(b);
e = document.createElement("IMG");
e.className = "delete";
e.setAttribute("src", "img/delete.png");
b.appendChild(e);
e = document.createElement("SPAN");
e.className = "buttonTextSmall"
e.innerHTML = "delete last rotor"
e.setAttribute("id", settings.idNames.deleteLabel)
b.appendChild(e);
b.addEventListener("click", function() {
var element = document.getElementById(settings.idNames.item + settings.numRotors);
element.parentNode.removeChild(element);
if (settings.numRotors === 2) {
var element = document.getElementById(settings.idNames.deleteLine);
element.parentNode.removeChild(element);
var element = document.getElementById(settings.idNames.deleteSep);
element.parentNode.removeChild(element);
};
settings.numRotors--;
document.getElementById(settings.idNames.pen).value = document.getElementById(settings.idNames.rotor + settings.numRotors).value;
setValues();
if (!settings.draw) {
drawCircles();
}
//scroll to the bottom
var div = document.getElementById(settings.idNames.rotors);
div.scrollTop = div.scrollHeight;
var rotorsDiv = document.getElementById('rotors');
rotorsDiv.style.height = (settings.numRotors * 27) + (settings.numRotors * 4.3 ) + 'px';
});
}
function makeButton(id, className, title, text) {
var b = document.createElement("BUTTON");
if (className) {
b.setAttribute("class", className);
}
if (id) {
b.setAttribute("id", id);
}
if (title) {
b.setAttribute("title", title);
}
if (text) {
b.innerHTML = text;
}
return b;
}
function makeImage(id, className, src) {
var i = document.createElement("IMG");
if (className) {
i.setAttribute("class", className);
}
if (id) {
i.setAttribute("id", id);
}
if (src) {
i.setAttribute("src", src);
}
return i;
}
function makeSpan(id, className, content) {
var s = document.createElement("SPAN");
if (className) {
s.setAttribute("class", className);
}
if (content) {
s.innerHTML = content;
}
if (id) {
s.setAttribute("id", id);
}
return s;
}
function drawButton() {
var button = makeButton(settings.idNames.draw, "draw", false, "draw");
settings.sidebarDiv.appendChild(button);
button.addEventListener("click", function() {
if (!settings.draw) {
if (settings.i === 0) {
settings.timer = new Date().getTime() / 1000;
}
settings.draw = true;
button.innerHTML = "pause";
setValues();
requestAnimationFrame(draw);
} else {
settings.draw = false;
button.innerHTML = "draw";
}
});
}
function clearButton() {
var button = makeButton(settings.idNames.clear, false, "clear drawing", false);
var image = makeImage(settings.idNames.clearIcon, "edit", "img/clear.png");
button.appendChild(image);
settings.sidebarDiv.appendChild(button);
button.addEventListener("click", clearCanvas);
}
function restartButton() {
var button = makeButton(settings.idNames.restart, false, "reset pen to beginning position", false);
var image = makeImage(settings.idNames.clearIcon, "edit", "img/restart.png");
button.appendChild(image);
settings.sidebarDiv.appendChild(button);
button.addEventListener("click", restart);
}
function hideButton() {
var button = makeButton(settings.idNames.hide, false, "hide circles", false);
var image = makeImage(settings.idNames.hideIcon, "edit", "img/hide.png");
button.appendChild(image);
settings.sidebarDiv.appendChild(button);
button.addEventListener("click", function() {
if (settings.circles === "show") {
settings.circles = "hide";
button.setAttribute("title", "show circles");
setValues();
drawCircles();
} else {
settings.circles = "show";
button.setAttribute("title", "hide circles");
setValues();
drawCircles();
}
});
}
function openImageButton() {
var button = makeButton(settings.idNames.open, false, "open drawing in new tab", false);
var image = makeImage(settings.idNames.openIcon, "edit", "img/open.png");
button.appendChild(image);
settings.sidebarDiv.appendChild(button);
button.addEventListener("click", function() {
var d = settings.canvasPen.toDataURL();
var newTab = window.open();
var img = newTab.document.createElement('img');
img.src = d;
var title = newTab.document.createElement('title');
title.innerHTML = 'Spirographⁿ Image';
newTab.document.head.appendChild(title);
newTab.document.body.appendChild(img);
});
}
function settingsURLButton() {
var button = makeButton(settings.idNames.link, false, "URL for drawing settings", false);
var image = makeImage(settings.idNames.linkIcon, "edit", "img/link.png");
button.appendChild(image);
settings.sidebarDiv.appendChild(button);
button.addEventListener("click", function() {
window.open(settings.url);
});
}
function zoomInButton() {
var button = makeButton(settings.idNames.zoomIn, false, "zoom drawing tools in", false);
var image = makeImage(settings.idNames.zoomInIcon, "edit", "img/zoomIn.png");
button.appendChild(image);
settings.sidebarDiv.appendChild(button);
button.addEventListener("click", function() {
zoom("in");
});
}
function zoomOutButton() {
var button = makeButton(settings.idNames.zoomOut, false, "zoom drawing tools out", false);
var image = makeImage(settings.idNames.zoomOutIcon, "edit", "img/zoomOut.png");
button.appendChild(image);
settings.sidebarDiv.appendChild(button);
button.addEventListener("click", function() {
zoom("out");
});
}
function rotateButton() {
var button = makeButton(settings.idNames.rotate, false, "rotate drawing tools", false);
var image = makeImage(settings.idNames.rotateIcon, "edit", "img/rotate.png");
button.appendChild(image);
settings.sidebarDiv.appendChild(button);
button.addEventListener("click", rotateDrawing);
}
function resetButton() {
var button = makeButton(settings.idNames.reset, false, "clear zoom and rotation", false);
var image = makeImage(settings.idNames.resetIcon, "edit", "img/reset.png");
button.appendChild(image);
settings.sidebarDiv.appendChild(button);
button.addEventListener("click", reset);
}
function gitButton() {
var button = makeButton(settings.idNames.git, false, "clear zoom and rotation", false);
button.setAttribute("style", "width:100%");
var image = makeImage(settings.idNames.gitIcon, "git", "img/gh.png");
var span = makeSpan(settings.idNames.gitLabel, "buttonText", "download at github");
button.appendChild(image);
button.appendChild(span);
settings.sidebarDiv.appendChild(button);
button.addEventListener("click", function() {
window.open("https://github.com/seedcode/SpirographN");
});
}
// private functions
function loadValues(d) {
//set values from d. d is either a loaded preset or created from the url
document.getElementById(settings.idNames.stator).value = d.st;
document.getElementById(settings.idNames.pen).value = d.pen;
document.getElementById(settings.idNames.width).value = d.wd;
document.getElementById(settings.idNames.color).value = d.cl;
document.getElementById(settings.idNames.speed).value = d.sp;
document.getElementById(settings.idNames.presets).value = d.name;
//remove rotors to start clean
var rotors = document.getElementById(settings.idNames.rotors);
while (rotors.firstChild) {
rotors.removeChild(rotors.firstChild);
}
//loop to add rotors
var c = 1;
var v;
var r;
var t;
while (d["r" + c]) {
v = d["r" + c];
r = v.substring(0, v.length - 1);
t = v.substring(v.length - 1);
addRotor(c, r, t);
c++;
}
//track (and maintain) number of rotors
settings.numRotors = c - 1;
//if we only have one rotor, then we don't want the delete button.
if (settings.numRotors === 1 && document.getElementById(settings.idNames.deleteLine)) {
var element = document.getElementById(settings.idNames.deleteLine);
element.parentNode.removeChild(element);
var element = document.getElementById(settings.idNames.deleteSep);
element.parentNode.removeChild(element);
};
//create delete button if we need and don't have
if (settings.numRotors > 1 && document.getElementById(settings.idNames.delete) === null) {
deleteButton()
}
//write values from inputs to settings
setValues();
//if we're not drawing, then we'll show the circles on change
if (!settings.draw) {
settings.circles = "show";
document.getElementById(settings.idNames.hide).setAttribute("title", "hide circles");
}
//reset if we're not drawing
if (!settings.draw) {
reset();
drawCircles();
}
//return number of rotors created
return c - 1;
}
function addRotor(num, r, type) {
var div = document.getElementById(settings.idNames.rotors);
var item = document.createElement("DIV");
item.setAttribute("id", settings.idNames.item + num);
div.appendChild(item);
if (num > 1) {
var e = document.createElement("DIV");
e.className = "divSeparator";
item.appendChild(e);
}
var e = document.createElement("SPAN");
e.className = "letterLabel";
e.setAttribute("style", "margin:0px");
e.innerHTML = "r" + num;
item.appendChild(e);
e = document.createElement("INPUT");
e.className = "number";
e.setAttribute("type", "text");
e.setAttribute("id", settings.idNames.rotor + num);
e.setAttribute("value", r);
item.appendChild(e);
inputEvent(settings.idNames.rotor + num, "input");
//type
e = document.createElement("INPUT");
e.setAttribute("type", "radio");
e.setAttribute("class", "radio");
e.setAttribute("id", settings.idNames.h + num);
e.setAttribute("name", "type" + num);
e.setAttribute("value", "hypotrochoid");
if (type === "h") {
e.setAttribute("checked", true);
}
item.appendChild(e);
inputEvent(settings.idNames.h + num, "click");
e = document.createElement("SPAN");
e.className = "letterLabel";
e.innerHTML = "h";
item.appendChild(e);
e = document.createElement("INPUT");
e.setAttribute("type", "radio");
e.setAttribute("class", "radio");
e.setAttribute("id", settings.idNames.e + num);
e.setAttribute("name", "type" + num);
e.setAttribute("value", "epitrochoid");
if (type === "e") {
e.setAttribute("checked", true);
}
item.appendChild(e);
inputEvent(settings.idNames.e + num, "click");
e = document.createElement("SPAN");
e.className = "letterLabel";
e.innerHTML = "e";
item.appendChild(e);
}
function setValues() {
settings.radii = [document.getElementById(settings.idNames.stator).value];
settings.curveColor = document.getElementById(settings.idNames.color).value;
settings.curveWidth = document.getElementById(settings.idNames.width).value;
settings.penRad = [document.getElementById(settings.idNames.pen).value];
settings.speed = document.getElementById(settings.idNames.speed).value;
settings.iOffset = document.getElementById(settings.idNames.angle).value;
settings.types = [""];
settings.pitches = [1];
settings.drawPitches = [];
settings.spinPitches = []
var c = 1;
var thisId = settings.idNames.rotor + c;
var thisHId = settings.idNames.h + c;
var thisEId = settings.idNames.e + c;
var thisRotor;
var thisType;
var thisHId;
var thisEId;
//build arrays
while (document.getElementById(thisId)) {
thisRotor = document.getElementById(thisId).value;
settings.radii.push(thisRotor);
if (document.getElementById(thisHId).checked) {
settings.types.push("h");
if (c > 1) {
settings.drawPitches.push(settings.spinPitches[c - 2]);
settings.spinPitches.push((settings.radii[c - 1] / thisRotor) - 1);
if (settings.types[c - 1] === "h") {
settings.directions.push(settings.directions[c - 1]);
} else {
settings.directions.push(settings.directions[c - 1] * -1);
}
} else {
settings.directions = [1, 1];
settings.drawPitches.push(1);
settings.spinPitches.push((settings.radii[c - 1] / thisRotor) - 1);
}
} else {
settings.types.push("e");
if (c > 1) {
settings.drawPitches.push(settings.spinPitches[c - 2]);
settings.spinPitches.push((settings.radii[c - 1] / thisRotor) + 1);
if (settings.types[c - 1] === "h") {
settings.directions.push(settings.directions[c - 1]);
} else(
settings.directions.push(settings.directions[c - 1] * -1)
)
} else {
settings.directions = [1, 1];
settings.drawPitches.push(1);
settings.spinPitches.push((settings.radii[c - 1] / thisRotor) + 1);
}
}
c++;
thisId = settings.idNames.rotor + c;
thisHId = settings.idNames.h + c;
thisEId = settings.idNames.e + c;
}
settings.numRotors = c - 1;
//create url string for this config
c = 0;
var u = window.location.origin + window.location.pathname;
for (c in settings.radii) {
if (c == 0) {
u += "?st=" + settings.radii[c];
} else {
u += "&r" + c + "=" + settings.radii[c] + settings.types[c];
}
}
u += "&pen=" + settings.penRad;
u += "&wd=" + settings.curveWidth;
u += "&cl=" + settings.curveColor.substring(1);
settings.url = u;
}
function drawCanvas() {
//create canvas elements then call resize routine
settings.canvasCircles = document.createElement("canvas");
settings.canvasCircles.id = settings.idNames.canvasCircles;
settings.canvasPen = document.createElement("canvas");
settings.canvasPen.id = settings.idNames.canvasPen;
settings.divCanvas.appendChild(settings.canvasCircles);
settings.divCanvas.appendChild(settings.canvasPen);
resizeCanvas();
}
function resizeCanvas() {
var offscreen = 100;
//capture current draw state and pause drawing.
var drawing = settings.draw;
settings.draw = false;
//we need to capture the current drawing and redraw when set
var ctx = settings.canvasPen.getContext("2d");
var ctxCircles = settings.canvasCircles.getContext("2d");
var cd = ctx.getImageData(0, 0, settings.canvasCircles.width, settings.canvasCircles.height);
ctx.save();
ctxCircles.save();
//fill window
settings.windowWidth = window.innerWidth;
settings.windowHeight = window.innerHeight;
settings.divCanvasWidth = 98.5;
settings.divCanvasHeight = settings.windowHeight - 28;
settings.sidebarWidth = settings.sidebarDiv.clientWidth;
//set sidebar and pad sizes and store in settings
settings.divCanvas.setAttribute("style", "width:" + settings.divCanvasWidth + "%;height:" + settings.divCanvasHeight + "px;background:white");
settings.sidebarDiv.setAttribute("style", "min-height:" + settings.divCanvasHeight + "px;");
settings.left = settings.divCanvas.offsetLeft + settings.sidebarWidth - offscreen;
settings.top = settings.divCanvas.offsetTop - offscreen;
//set rotor list height
var rotors = document.getElementById(settings.idNames.rotors);
rotors.setAttribute("style", "max-height:" + settings.divCanvasHeight * .23 + "px;");
//if we're resizing and we have a previous poisition, then track the offset, so we can redraw our canvases in position
//round the coordinates for the center, otherwise the redraws are off.
if (settings.a) {
settings.offsetL = (Math.round((settings.divCanvas.clientWidth - settings.sidebarWidth) / 2)) + offscreen - settings.a;
settings.offsetT = Math.round((settings.divCanvasHeight / 2)) + offscreen - settings.b;
}
//now recenter
settings.a = Math.round(((settings.divCanvas.clientWidth - settings.sidebarWidth) / 2)) + offscreen;
settings.b = Math.round((settings.divCanvasHeight / 2)) + offscreen;
//resize canvases
settings.canvasCircles.height = settings.divCanvasHeight + offscreen * 2;
settings.canvasCircles.width = settings.divCanvas.clientWidth - settings.sidebarWidth + offscreen * 2;
settings.canvasCircles.setAttribute("Style", "left:" + settings.left + "px;top:" + settings.top + "px;position:absolute;z-index:10");
settings.canvasPen.height = settings.divCanvasHeight + offscreen * 2;
settings.canvasPen.width = settings.divCanvas.clientWidth - settings.sidebarWidth + offscreen * 2;
settings.canvasPen.setAttribute("Style", "left:" + settings.left + "px;top:" + settings.top + "px;position:absolute;z-index:20");
//update coordinates based on new position
if (settings.penStart.x != 0) {
settings.penStart.x = settings.penStart.x + settings.offsetL;
settings.penStart.y = settings.penStart.y + settings.offsetT;
settings.curvePoints[0].x = settings.curvePoints[0].x + settings.offsetL;
settings.curvePoints[0].y = settings.curvePoints[0].y + settings.offsetT;
}
//restore rotation
if (settings.iPosition != 0) {
var posHolder = settings.iPosition;
var offSetHolder = settings.iOffset;
settings.iOffset = settings.iPosition * -1;
rotateDrawing();
settings.iOffset = offSetHolder;
settings.iPosition = posHolder;
}
//redraw circles
if (!settings.draw) {
drawCircles();
}
//redraw canvas
ctx.putImageData(cd, settings.offsetL, settings.offsetT);
//restore drawing state
settings.draw = drawing;
}
function circlePoint(a, b, r, ng) {
var rad = ng * (Math.PI / 180);
var y = r * Math.sin(rad);
var x = r * Math.cos(rad);
x = a + x;
y = b - y;
return {
"x": x,
"y": y
}
}
function drawCircles() {
var c = 1;
var i = settings.i;
var thisRad = 0;
var prevRad = 0;
var centerRad = 0;
var thisPitch = 0;
var prevPitch = 0;
var prevSpinPitch = 0;
var prevDrawPitch = 0;
var pen;
var zoom = settings.currentZoom;
//clear circles canvas
var ctx = settings.canvasCircles.getContext("2d");
ctx.clearRect(0, 0, settings.canvasCircles.width, settings.canvasCircles.height);
//draw Stator
if (settings.circles === "show") {
drawOneCircle(settings.canvasCircles, settings.a, settings.b, settings.radii[0] * zoom );
}