-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.html
4768 lines (4173 loc) · 151 KB
/
index.html
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
<!DOCTYPE html>
<html>
<head>
<title>Solid Knitting UI</title>
<style>
body.darkmode {
--far-background-color: #333230;
--background-color: #232220;
--background-color-medium: #232220bb;
--background-color-light: #23222066;
--background-color-highlight: #dde1;
--separator-color: #434240;
--main-font-color: #C4C0AD;
--far-font-color: #E4E0CD;
--font-color-highlight: #D4D0CD;
--link-color: #D1C6B5;
/* syntax highlighting */
--comment: #74705D;
--location: #A6E42C;
--direction: #AC7DFF;
--keyword: #FF2275;
--carrier: #EEEEEE;
--string: #EDE277;
}
body.lightmode {
--far-background-color: #d4ccd2;
--background-color: #fbf8f6;
--background-color-medium: #f6f0eebb;
--background-color-light: #f6f0ee66;
--background-color-highlight: #2221;
--separator-color: #c4bcc2;
--main-font-color: #222;
--far-font-color: #111;
--font-color-highlight: #222;
--link-color: #181818;
/* syntax highlighting, from solarized https://en.wikipedia.org/wiki/Solarized */
--comment: #839496;
--location: #2aa198;
--direction: #268bd2;
--keyword: #cb4b16;
--carrier: #073642;
--string: #b58900;
}
body {
padding:0;
margin:0;
background-color: var(--far-background-color);
color: var(--far-font-color);
}
body a {
color: var(--link-color);
}
#dropTarget {
position:fixed;
left:0;
bottom:0;
width:100%;
height:100%;
background:#ccc;
outline:4px dashed #eee;
outline-offset:-20px;
z-index:100;
visibility:hidden;
}
#dropTarget.active {
visibility:visible;
background:#eee;
outline-color:#ccc;
}
#file {
display:none;
}
#views {
display:grid;
/* width:99vw;*/
height:99vh;
grid-template-columns: 1fr auto ;
grid-template-rows: auto 1.0fr 2em;
overflow: none;
grid-gap: 5px;
/*background: #0008 ;*/
}
#controls {
grid-column: 1 / 3 ;
grid-row: 1 ;
gap: 8px;
font-size: 16px;
line-height:16px;
padding: 5px 5px;
display:flex;
flex-flow: row wrap;
align-items: center;
/*background: #f008 ;*/
}
.controlsButton {
box-sizing:border-box;
cursor:pointer;
display:inline-block;
background:#cfbb96;
padding:4px 8px;
margin:0;
box-shadow: 0 1px 2px 0 #0008;
text-decoration:none;
line-height:1;
border: none;
border-radius:2px;
/* height:100%;*/
font-size:inherit;
font-family: sans-serif;
color: black;
}
.controlsButton:hover {
background:#f6d69b;
}
.controlsButton:active {
box-shadow: 0 0 1px 0 #0008;
}
.controlsInfo {
display:block;
text-align:right;
flex-grow: 1;
}
.controlsInfo + .controlsInfo {
flex-grow: 0;
}
.controls-section {
margin-left: 1em;
}
#view-3d {
grid-column: 1 ;
grid-row: 2 ;
/*background: #0f08 ;*/
display: grid;
grid-template-rows: auto 1fr;
grid-template-columns: 1fr;
color: var(--main-font-color);
}
#canvas-wrapper {
position:relative;
overflow: hidden;
background-color: var(--background-color);
border-top: solid 2pt var(--separator-color);
border-radius: 0 5px 5px 0;
}
#canvas {
position:absolute;
top:0;
left:0;
width:100%;
height:100%;
background-color: var(--background-color);
}
#status-line {
position:absolute;
left:0;
bottom:0;
background:#fff8;
color: black;
padding: 0.15em 0.3em;
}
#darkmode-checkbox {
width: 0;
height: 0;
visibility: hidden;
}
body.darkmode #darkmode-checkbox-label::after {
content: "\263C";
}
body.lightmode #darkmode-checkbox-label::after {
content: "\263D";
}
#render-options {
padding: 0 0.5em;
position: absolute;
top: 0;
left: 0;
background: var(--background-color-medium);
}
#render-options > summary {
cursor:pointer;
font-size: 2em;
list-style: none;
}
#render-options > summary:hover {
color: var(--font-color-highlight);
}
#render-options > summary::-webkit-details-marker { /* hide details triangle https://stackoverflow.com/a/56649741 */
display: none;
}
#sidebar {
grid-column: 2 ;
grid-row: 2 ;
display:grid;
grid-template-rows: min-content 1fr min-content;
grid-gap: 5px;
width: 24em;
min-height: 0; /* don't expand past parent height https://stackoverflow.com/a/43312314 */
/* background: #00f8 ;*/
color: var(--main-font-color);
background-color: var(--far-background-color);
}
.tab-menu-container {
margin: 0;
margin-left: 0.25em;
padding: 0;
display: grid;
box-sizing: border-box;
grid-template-columns: auto 1fr auto;
grid-template-rows: auto;
align-items: center; /* center-align text in menu items */
background-color: var(--far-background-color);
color: var(--far-font-color);
}
.tab-menu span {
padding: 0.25em 0.5em;
}
.tab-menu {
box-sizing: border-box;
margin: 0;
padding: 0;
display: grid;
grid-template-rows: 1fr;
grid-template-columns: repeat(8, max-content);
grid-gap: 1pt;
}
.tab-menu input[type=radio] { /* invisible radio button to save state https://stackoverflow.com/a/8037883 */
visibility: hidden;
width: 0;
height: 0;
margin: 0;
padding: 0;
}
.tab-menu label {
border: 0;
font-family: sans-serif;
border-radius: 0.25em 0.25em 0 0;
padding: 0.5em 1em 0.25em 1em;
border: solid 2pt var(--separator-color);
border-bottom-style: none;
cursor: pointer;
background-color: var(--background-color-light);
}
.tab-menu label:hover {
background-color: var(--background-color-medium);
}
.tab-menu label.active {
background-color: var(--background-color);
border-bottom: solid 2pt var(--background-color);
margin-bottom: -2pt;
z-index: 1;
}
#sidebar h2, #sidebar summary {
font-size:20px;
font-weight: bold;
line-height:20px;
padding: 0;
margin: 0;
margin-bottom: 0.5em;
font-family: sans-serif;
cursor: pointer;
}
#status {
/* grid-row: 2 ;*/
margin: 0;
min-height: 0;
display: grid; /* set to grid from javascript later too */
grid-template-rows: auto 1fr;
padding: 0.5em 0 0.25em 0.25em; /* top right bottom left */
border-radius: 5px;
background-color: var(--background-color);
}
#status-text {
margin:0;
padding: 0 0 0.5em 0; /* top right bottom left */
}
#library {
/* grid-row: 2 ;*/
margin:0;
padding: 0.5em 0 0 0.5em; /* top right bottom left */
min-height: 0;
display:grid; /* set to grid from javascript later too */
grid-template-rows: auto 1fr;
padding-bottom: 0.5em;
/* border-left: 2pt solid var(--separator-color);*/
border-radius: 5px 0 0 5px;
background-color: var(--background-color);
margin-top: -5px;
border-top: solid 2pt var(--separator-color);
}
#library-list {
/* grid-row: 2 ;*/
margin:0;
padding:0;
min-width: 0;
min-height: 0;
overflow-y: scroll;
}
#library ul {
list-style:none;
}
#library li {
display:block;
margin:0em 0.15em;
padding:0.15em 0.5em;
border-radius:0.2em;
cursor:pointer;
border: 1pt solid var(--background-color);
}
#library li:hover {
background-color: var(--background-color-highlight);
color: var(--font-color-highlight);
}
#library .library-active-template {
background-color: var(--background-color-highlight);
color: var(--font-color-highlight);
border: 1pt solid var(--separator-color);
}
#view-shortcuts {
/*border-top: 2px solid var(--separator-color);
border-left: 2pt solid var(--separator-color);*/
padding: 0.5em 0 0.25em 0.25em; /* top right bottom left */
border-radius: 5px;
background-color: var(--background-color);
}
#view-shortcuts summary {
padding-left: 0.5em;
margin: 0;
margin-bottom: 0.5em;
}
#shortcuts {
grid-row: 3 ;
margin:0;
padding:0;
}
#shortcuts li {
display:block;
margin:0.1em 0.15em;
padding:0.0em 0.5em;
}
#shortcuts .key {
font-family: monospace;
}
#view-knitout {
margin: 0;
padding: 0;
overflow: auto;
display:grid; /* set to grid from javascript later too */
grid-template-rows: 1fr auto;
/* border-left: 2pt solid var(--separator-color);*/
border-radius: 5px 0 0 5px;
background-color: var(--background-color);
margin-top: -5px;
border-top: solid 2pt var(--separator-color);
}
#view-knitout #view-knitout-options {
padding: 0.5em 1em;
background-color: var(--background-color);
}
#knitout-source {
font-family: monospace;
padding-top: 0.5em;
min-height: 0;
overflow-y: scroll;
margin-bottom: 0.5em;
/* background-color: #272822;*/
}
/* code styling */
#knitout-source span {
/* position: relative; /* for tooltips */
white-space: pre-wrap; /* preserve whitespace but allow line wrapping */
}
/* https://www.w3schools.com/css/css_tooltip.asp */
/*#knitout-source span .tooltiptext {
visibility: hidden;
position: absolute;
z-index: 1;
background-color: #202010;
padding: 0.5em 1em;
color: white;
top: 0;
left: 4em;
border: 1pt solid #555;
border-radius: 0.25em;
}
#knitout-source span:hover .tooltiptext {
visibility: visible;
}*/
#knitout-source .line {
padding: 0 0;
display: flex;
}
#knitout-source .line-number {
color: #94908D;
width: 2em;
padding: 0 0.5em;
display: inline-block;
/* disable selection: https://stackoverflow.com/a/4407335 */
-webkit-touch-callout: none; /* iOS Safari */
-webkit-user-select: none; /* Safari */
-khtml-user-select: none; /* Konqueror HTML */
-moz-user-select: none; /* Old versions of Firefox */
-ms-user-select: none; /* Internet Explorer/Edge */
user-select: none; /* Non-prefixed version, currently
supported by Chrome, Edge, Opera and Firefox */
}
#knitout-source .line:hover {
color: #C4C0BD;
background-color: var(--background-color-highlight);
}
#knitout-source .code {
border-left: 2px solid var(--separator-color);
/* box-shadow: inset 8px 0 8px -10px black;*/
padding-left: 0.5em;
width: 100%;
}
#knitout-source .comment {
color: var(--comment);
}
#knitout-source .location {
color: var(--location);
}
#knitout-source .direction {
color: var(--direction);
}
#knitout-source .keyword {
color: var(--keyword);
}
#knitout-source .carrier {
color: var(--carrier);
}
#knitout-source .string {
color: var(--string);
}
#view-block-data {
display: grid; /* set to grid from javascript later too */
grid-template-rows: auto 1fr;
gap: 0;
padding: 0.5em 0 0 0.25em; /* top right bottom left */
/*border-top: 2pt solid var(--separator-color);
border-left: 2pt solid var(--separator-color);*/
border-radius: 5px 0 0 5px;
background-color: var(--background-color);
}
#view-block-data summary {
padding-left: 0.5em;
margin: 0;
margin-bottom: 0.5em;
}
#hovered-block-info {
display: grid;
grid-template-columns: auto 1fr;
grid-template-rows: max-content;
align-content: start;
gap: 0;
align-items: stretch;
}
/* shade every other row https://stackoverflow.com/a/44937583 */
#hovered-block-info > :nth-child(4n+1), #hovered-block-info > :nth-child(4n+2) {
background-color: var(--background-color-highlight);
}
#hovered-block-info .field-name {
padding: 0.15em 0.25em 0.15em 0.5em; /* top right bottom left */
min-height: 0;
display: inline-flex;
align-items: center; /* vertically-center text */
justify-content: flex-end; /* align right */
}
#hovered-block-info .field-name:after {
content: ":";
}
#hovered-block-info .field-value {
padding: 0.25em 1em 0.25em 0.5em; /* top right bottom left */
font-family: monospace;
}
#hovered-block-info input[type=text] {
font-family: monospace;
width: 100%;
border: none;
background-color: #fff1;
color: var(--main-font-color);
border-radius: 2pt;
border: 1pt var(--separator-color) solid;
}
#message-box {
grid-column: 1 / 3 ;
grid-row: 3 ;
margin: 0;
min-height: 0;
padding: 0.5em 0 0.25em 0.25em; /* top right bottom left */
border-radius: 5px;
background-color: var(--background-color);
}
</style>
</head>
<body class="lightmode">
<div id="views">
<!-- controls bar at the top of the window -->
<div id="controls">
<input id="file" type="file" />
<label id="fileLabel" for="file" class="controlsButton">Load</label>
<!-- <button id="benchmark">benchmark</button>
<span id="benchmark-result"></span> -->
<button id="save" class="controlsButton">Save</button>
<button id="save-yarn-path" class="controlsButton">Save Yarn Path</button>
<button id="save-knitout" class="controlsButton">Save Knitout</button>
<!-- <button id="reallocate-spots" class="controlsButton">Reallocate Needle Locations</button> -->
<span class="controlsInfo"><a href="https://github.com/textiles-lab/solid-knitting-ui/">github page</a>;</span>
<span class="controlsInfo"><a href="https://github.com/textiles-lab/solid-knitting-ui/issues">report a bug</a></span>
</div>
<!-- 3d view -->
<div id="view-3d">
<div id="viewmode-options" class="tab-menu-container">
<span id="fileName">(no file loaded)</span>
<form id="select-3dview-mode" class="tab-menu">
<input type="radio" name="3dview-mode" id="3dview-pattern-mode" value="Pattern" checked><label id="3dview-set-pattern-mode" for="3dview-pattern-mode" class="tab">Pattern</label><input type="radio" name="3dview-mode" id="3dview-template-mode" value="Template" class="tab"><label id="3dview-set-template-mode" for="3dview-template-mode">Templates</label>
</form>
<form id="darkmode-toggle">
<input type="checkbox" id="darkmode-checkbox" name="darkmode" value="darkmode" checked><label for="darkmode-checkbox" id="darkmode-checkbox-label"></label>
</form>
</div>
<div id="canvas-wrapper">
<canvas id="canvas" width="120" height="120"></canvas>
<div id="status-line">(Status Line)</div>
<details id="render-options">
<summary>⚙</summary>
<div id="yarn-vis-options" class="controls-section">
Yarn Visualization:
<select id="select-yarn-vis-option">
<option value="Tube" selected>Tube</option>
<option value="Wire">Wire</option>
<option value="Body">Body</option>
</select>
</div>
<div id="layer-visibility-options" class="controls-section">
Layer Visibility:
<form id="layer-visibility-form" style="display:inline-block;">
<input type="checkbox" name="layer-visible" value="0" checked>0
<input type="checkbox" name="layer-visible" value="1" checked>1
<input type="checkbox" name="layer-visible" value="2" checked>2
<input type="checkbox" name="layer-visible" value="3" checked>3
<input type="checkbox" name="layer-visible" value="4" checked>4
</form>
</div>
<div id="yarn-direction-vis-options" class="controls-section">
<form id="show-yarn-direction-form" style="display:inline-block;">
<input type="checkbox" name="show-yarn-direction" value="0"> Show Yarn Direction
</form>
</div>
</details>
</div>
</div>
<!-- library panel -->
<div id="sidebar">
<div id="sidebar-menu" class="tab-menu-container">
<form id="select-sidebar-mode" class="tab-menu">
<input type="radio" name="sidebar-mode" id="sidebar-design-mode" value="PatternDesign" checked><label id="sidebar-set-design-mode" for="sidebar-design-mode" class="tab">Pattern Design</label><input type="radio" name="sidebar-mode" id="sidebar-codegen-mode" value="CodeGen"><label id="sidebar-set-codegen-mode" for="sidebar-codegen-mode" class="tab">Code Generation</label>
</form>
</div>
<div id="library">
<h2>Block Library:</h2>
<ul id="library-list">
</ul>
</div>
<!-- preview generated knitout -->
<div id="view-knitout">
<div id="view-knitout-options">
<form id="group-knitout-passes-form" style="display:inline-block;">
<label for="group-knitout-passes">Group knitout passes:</label>
<input type="checkbox" name="group-knitout-passes">
</form>
<button id="regenerate-code" class="controlsButton">Regenerate Code</button>
</div>
<div id="knitout-source"></div>
</div>
<!-- see information about highlighted block -->
<details id="view-block-data" open>
<summary> Block Info:</summary>
<form id="hovered-block-info"></form>
</details>
<!-- also list shortcuts -->
<details id="view-shortcuts" open>
<summary>Commands:</summary>
<ul id="shortcuts">
<li> <button class="controlsButton" id="relax-shape">Relax shape</button> (<span class="key">space</span> or <span class="key">R</span>)</li>
<li> <button class="controlsButton" id="select-hovered-template">Select hovered template</button> (<span class="key">enter</span>)</li>
<li> <button class="controlsButton" id="delete">Delete cell</button> (<span class="key">X</span>)</li>
<li> <button class="controlsButton" id="grab">Grab cell</button> (<span class="key">G</span>)</li>
<li> <button class="controlsButton" id="add">Add cell</button> (<span class="key">E</span>)</li>
<li> <button class="controlsButton" id="extrude">Extrude cell</button> (<span class="key">G</span>)</li>
<li> <button class="controlsButton" id="extrude-contiguous">Extrude contiguous faces</button> (<span class="key">W</span>)</li>
<li> <button class="controlsButton" id="start-connection">Start/make connection</button> (<span class="key">C</span>)</li>
<li> <button class="controlsButton" id="prev-template">Prev template</button> (<span class="key">up</span>)</li>
<li> <button class="controlsButton" id="next-template">Next template</button> (<span class="key">down</span>)</li>
<li> <button class="controlsButton" id="prev-compatible-template">Prev compatible template</button> (<span class="key">K</span>)</li>
<li> <button class="controlsButton" id="next-compatible-template">Next compatible template</button> (<span class="key">J</span>)</li>
<li> <button class="controlsButton" id="select-hovered-block">Select hovered block</button> (<span class="key">S</span>)</li>
<li> <button class="controlsButton" id="undo">Undo</button> (<span class="key">ctrl-Z</span>)</li>
<li> <button class="controlsButton" id="escape-from-action">Escape from grab/connect/selection</button> (<span class="key">esc</span>)</li>
</ul>
</details>
</div>
<div id="message-box">
<div id="cycle-text"></div>
<div id="yarn-count-text"></div>
</div>
<!-- full-window drop target -->
<div id="dropTarget"></div>
<!-- File loading code -->
<script>
// TODO: Consider moving each script into its own module for the sake of code
// clarity.
//-----------------------------------------------------------------------------
// File loading code
window.currentFile = null;
function setFilename(name) {
document.getElementById("fileName").textContent = name;
}
function readFile(file) {
console.log("Attempting to read file: '" + file.name + "'");
setFilename(file.name);
if (readFile.reader) {
readFile.reader.abort();
delete readFile.reader;
}
let reader = readFile.reader = new FileReader();
reader.onload = function(){
console.log("File was " + reader.result.byteLength + " bytes long.");
window.body = sv.Body.fromArrayBuffer(reader.result, library);
bodyDirty();
cursorDirty();
resetCamera();
requestRedraw();
window.currentFile = file;
};
reader.readAsArrayBuffer(file);
}
var dropTarget = document.getElementById("dropTarget");
//dragging into the window also loads files:
dropTarget.addEventListener('dragover', function(evt){
dropTarget.classList.add("active");
evt.preventDefault();
return false;
});
dropTarget.addEventListener('dragleave', function(evt){
dropTarget.classList.remove("active");
evt.preventDefault();
return false;
});
dropTarget.addEventListener('drop', function(evt){
dropTarget.classList.remove("active");
try {
file.value = "";
window.currentFile = null;
readFile(evt.dataTransfer.files[0]);
} catch (e) {
console.log(e);
}
evt.preventDefault();
return false;
});
//dragging into the window shows the target:
document.addEventListener('dragover', function(evt){
dropTarget.classList.add("active");
evt.preventDefault();
return false;
});
var file = document.getElementById("file");
file.addEventListener('change', function(evt){
try {
window.currentFile = null;
readFile(file.files[0]);
} catch (e) {
console.log(e);
}
evt.preventDefault();
return false;
});
file.addEventListener('click', function(evt){
file.value = ""; //reset so 'change' event fires
});
document.getElementById("save").addEventListener('click', function(evt){
console.log("Saving");
const text = JSON.stringify(body.toData());
fileSave(text, "solid-knitting.body");
});
document.getElementById("save-yarn-path").addEventListener('click', function(evt){
console.log("Saving yarn path");
let text = "";
let iV = 1;
for (const cell of body.cells) {
const xf = cell.xform;
for (const yarn of cell.template.yarns) {
for (let iP=0; iP<yarn.pts.length; ++iP) {
const pt = yarn.pts[iP];
let at = gm.mul_mat4x3_vec4(xf, [...pt, 1]);
text += "v " + at[0] + " " + at[1] + " " + at[2] + "\n";
}
text += "l ";
for (let iP=0; iP<yarn.pts.length; ++iP) {
text += iV + " ";
++iV;
}
text += "\n";
}
}
fileSave(text, "solid-knitting-yarn-path.obj");
});
//from knitout-live-visualizer:
function fileSave(sourceText, fileIdentity) {
var workElement = document.createElement("a");
if ('download' in workElement) {
workElement.href = "data:" + 'text/plain' + "charset=utf-8," + escape(sourceText);
workElement.setAttribute("download", fileIdentity);
document.body.appendChild(workElement);
var eventMouse = document.createEvent("MouseEvents");
eventMouse.initMouseEvent("click", true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
workElement.dispatchEvent(eventMouse);
document.body.removeChild(workElement);
console.log(workElement);
// window.currentFile = file;
} else throw 'File saving not supported for this browser';
}
</script>
<!-- Solid Knitting UI code -->
<script type="module">
'use strict';
import * as gm from './code/gm.mjs';
window.gm = gm; //DEBUG
import * as sv from './code/sv.mjs';
window.sv = sv; //DEBUG
window.library = new sv.Library();
window.template = null;
window.body = new sv.Body();
import {Geometry, Program, loadTexture} from './code/gl.mjs';
import {writeHighlightedCode, groupBlocks, groupPasses, noPassGrouping} from './code/sk.mjs';
document.getElementById("save-knitout").addEventListener('click', function(evt){
if (groupKnitoutPassesCheckbox.checked) {
fileSave(groupPasses(knitoutCode.fragments), "solid-knitting-pattern.sk");
} else {
fileSave(noPassGrouping(knitoutCode.fragments), "solid-knitting-pattern.sk");
}
});
// document.getElementById("reallocate-spots").addEventListener('click', function(evt){
// allocateSpots();
// });
//-----------------------------------------------------------------------------
// Adapted from yarn visualizer code (from smobj)
// NOTE: based largely on the MDN WebGL tutorials at:
// https://developer.mozilla.org/en-US/docs/Web/API/WebGL_API/Tutorial/Getting_started_with_WebGL
const canvas = document.getElementById("canvas");
const gl = canvas.getContext('webgl', {alpha:false, antialias:false});
window.gl = gl; //DEBUG
//enable extensions:
const ext_WEBGL_depth_texture = gl.getExtension("WEBGL_depth_texture"); //used for postprocessing
const STATUS_LINE = document.getElementById("status-line");
window.STATUS_LINE = STATUS_LINE;
// "enum" representing possible displays
const ViewMode = {
Pattern: 'Pattern',
Template: 'Template'
};
let viewMode = ViewMode.Pattern;
let viewPerspective = true;
//------- camera -------
const camera = {
target:gm.vec3(0),
radius:2.5,
azimuth:0.0,
elevation:0.0,
fovy:45.0,
aspect:1.0
};
window.camera = camera; //DEBUG
camera.computeUp = function camera_computeUp() {
return gm.vec3(
Math.cos(this.azimuth)*-Math.sin(this.elevation),
Math.sin(this.azimuth)*-Math.sin(this.elevation),
Math.cos(this.elevation)
);
};
camera.computeRight = function camera_computeRight() {
return gm.vec3(
-Math.sin(this.azimuth),
Math.cos(this.azimuth),
0.0
);
};
camera.computeOut = function camera_computeOut() {
return gm.vec3(
Math.cos(this.azimuth)*Math.cos(this.elevation),
Math.sin(this.azimuth)*Math.cos(this.elevation),
Math.sin(this.elevation)
);
};
camera.computeAt = function camera_computeAt() {
return gm.add(this.target, gm.scale(this.radius, this.computeOut()));
};
function resetCamera() {
let min = gm.vec3(Infinity);
let max = gm.vec3(-Infinity);
//TOOD: compute bounding box
if (min[0] > max[0]) {
//for now, just some default:
min[0] = -10.0; min[1] = -10.0; min[2] = -10.0;
max[0] = 10.0; max[1] = 10.0; max[2] = 10.0;
}
camera.target = gm.scale(0.5, gm.add(min,max));
//console.log("Range: ",min,max);
camera.radius = 0.5 * (max[1] - min[1]) / Math.tan(0.5 * camera.fovy * Math.PI / 180.0) + 0.5 * (max[2] - min[2]);
camera.azimuth = Math.PI / 6.0;
camera.elevation = Math.PI / 4.0;
requestRedraw();
}
window.resetCamera = resetCamera;
//--- controls ---
const MOUSE = {x:NaN, y:NaN, over:null, grid:null};
let CURSOR = MOUSE; // <-- if anything is selected, set to selection. Otherwise, set to MOUSE
let ACTION = null; //<-- current action; gets all events if not null
let camFlipX = false; //used for camera rotation control
function mouseRay() {
const origin = camera.computeAt();
//compute ray direction through mouse:
const o = camera.computeOut();
const r = camera.computeRight();
const u = camera.computeUp();
const rh = Math.tan(0.5 * camera.fovy / 180.0 * Math.PI); //half of the image plane height
const rw = rh * camera.aspect; //half of the image plane width
const direction = gm.sub( gm.add( gm.scale(rw * MOUSE.x, r), gm.scale(rh * MOUSE.y, u)), o);
return {origin, direction};
}
function mouseDirty() {
MOUSE.over = null;
MOUSE.grid = null;
MOUSE.dirty = true;
requestRedraw(); //mouse will be updated as part of redraw
}
window.mouseDirty = mouseDirty;
function cursorDirty() {
mouseDirty(); // always update MOUSE
// if CURSOR is frozen, we shouldn't update
if (CURSOR.frozen) return;
CURSOR.over = null;
CURSOR.grid = null;
CURSOR.dirty = true;
requestRedraw();
}
window.cursorDirty = cursorDirty;
function bodyDirty() {
visBody.dirty = true;
visWire.dirty = true;
visWireCells.dirty = true;
visTubes.setDirty();
visTransparentTubes.setDirty();
for (let cell of body.cells) {
cell.dirty = true;
}
requestRedraw();
}
window.bodyDirty = bodyDirty;
function bodyClean() {
for (let cell of body.cells) {
delete cell.dirty;
}
}
function cellDirty(cell) {
visBody.dirty = true;
visWire.dirty = true;
visWireCells.dirty = true;
visTubes.dirty = true;
visTransparentTubes.dirty = true;
cell.dirty = true;
requestRedraw();
}
function templateDirty() {
visTemplateBody.dirty = true;
visTemplateWire.dirty = true;
visTemplateTubes.dirty = true;
visTemplateWireCells.dirty = true;
requestRedraw();
}
window.templateDirty = templateDirty;
function knitoutDirty() {
knitoutCode.dirty = true;
requestRedraw();
}
window.knitoutDirty = knitoutDirty; // for debugging
function setMouseOver() {
delete MOUSE.dirty;
MOUSE.over = null;
MOUSE.grid = null;
if (MOUSE.x !== MOUSE.x) return;
const ray = mouseRay();
let close = Infinity;
//faces of cells: