-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathsimplified-visualizer.html
More file actions
297 lines (261 loc) · 6.33 KB
/
simplified-visualizer.html
File metadata and controls
297 lines (261 loc) · 6.33 KB
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
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Test of knitout visualizer (2)</title>
<style>
code.knitout {
display:block;
height:10em;
overflow:auto;
background:#eee;
border-radius:5px;
}
#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;
}
#fileLabel span {
cursor:pointer;
text-decoration:underline;
}
.highlight {
background-color: #ffeeb7;
}
#show1 {
margin: 1em;
width:100vh;
height:80vh;
float: left;
box-sizing: border-box;
}
#code1 {
box-sizing: border-box;
margin: 1em;
padding: 1em 0;
background-color: #f3f3f3;
float: left;
height: 80vh;
overflow: scroll;
position: relative;
}
.line {
display: inline-block;
padding: 0.2em 2em 0.2em 0.5em;
width: 100%;
box-sizing: border-box;
}
.lineNumber {
color: #888;
width: 2.5em;
display: inline-block;
text-align: right;
margin-right: 1em;
}
.spinning {
border: 4px solid #f3f3f3; /* Light grey */
border-top: 4px solid #3498db; /* Blue */
border-radius: 50%;
width: 1em;
height: 1em;
animation: spin 2s linear infinite;
display: inline-block;
}
@keyframes spin {
0% { transform: rotate(0deg); }
100% { transform: rotate(360deg); }
}
</style>
</head>
<body>
<div>
<input id="file" type="file">
</div><label id="fileLabel" for="file"><span>Choose a knitout
file</span> or drag one into the window to visualize it. <span id=
"spinner" class="spinning" style=
"visibility: hidden;"></span></label>
<p>Here's some example knitout code:</p>
<canvas class="ShowKnitout" id="show1" data-code="code1"></canvas>
<pre><code class="Knitout" id="code1">;!knitout-2
;;Carriers: 1 3 7
;;Machine: SWGXYZ
;;Gauge: 15
inhook 7
tuck + f0 7
tuck + f1 7
tuck + f2 7
tuck + f3 7
knit - f3 7
knit - f2 7
knit - f1 7
knit - f0 7
xfer f0 b0
xfer f1 bs1
xfer f2 b2
xfer f3 bs3
xfer b0 fs0
xfer bs1 f1
xfer b2 fs2
xfer bs3 f3
xfer fs0 b0
xfer f1 b1
xfer fs2 b2
xfer f3 b3
knit + b0 7
knit + b1 7
knit + b2 7
knit + b3 7
outhook 7</code></pre>
<div id="dropTarget"></div>
<script src="parseKnitout.js"></script>
<script src="CellMachine.js"></script>
<script src="VectorTiles.js"></script>
<script src="show-knitout.js"></script>
<script>
function spin() {
document.getElementById("spinner").style.visibility = 'visible';
}
function noSpin() {
document.getElementById("spinner").style.visibility = 'hidden';
}
function removeDecoration(lineNumber) {
let line = document.getElementById("LineNo" + lineNumber);
if (line)
line.classList.remove("highlight");
}
function addDecoration(lineNumber) {
let line = document.getElementById("LineNo" + lineNumber);
if (line) {
line.classList.add("highlight");
}
}
function jumpToLine(lineNumber) {
let line = document.getElementById("LineNo" + lineNumber);
if (line) {
code.scrollTop = line.offsetTop - 60;
}
}
var code = document.getElementById('code1');
var show = document.getElementById('show1');
let oldHoveredRow = NaN;
function selectRow(source) {
if (oldHoveredRow === oldHoveredRow) {
removeDecoration(oldHoveredRow);
}
let line = parseInt(source);
oldHoveredRow = line;
if (line === line) {
addDecoration(oldHoveredRow);
} else {
//console.log("Unhighlight");
}
}
show.showKnitout.onHoverSource = function(source) {
selectRow(source);
};
show.showKnitout.onClickSource = function(source) {
selectRow(source);
jumpToLine(source);
};
function readFile(file) {
var oldSize = 0;
var oldCheck = 0;
var oldText = null;
function spinPoll() {
spin();
setTimeout(function() {
pollFile();
setTimeout(noSpin, 50);
}, 50);
}
function pollFile() {
spin();
//generate new data:
let reader = new FileReader();
reader.onload = function(){
if (reader.result !== oldText) {
oldText = reader.result;
//line ending conversion:
let convertedText = oldText.replace(/\r\n/g,"\n");
if (oldText != convertedText) {
console.warn("Converted dos-style line endings to unix-style.")
}
console.log("parsing...");
show.showKnitout.parse(convertedText, true);
console.log("done parsing.");
}
//poll again:
readFile.pollTimeout = window.setTimeout(spinPoll, 5000);
};
reader.onerror = function() {
console.log("ERROR")
console.log(reader.error)
};
console.log("polling " + file.name);
reader.readAsText(file);
console.log("Attempting to read file: '" + file.name + "'");
}
if ('pollTimeout' in readFile) {
window.clearTimeout(readFile.pollTimeout);
delete readFile.pollTimeout;
}
spinPoll();
}
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 {
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 {
readFile(file.files[0]);
} catch (e) {
console.log(e);
}
evt.preventDefault();
return false;
});
show.showKnitout.parse(code.innerHTML, true);
</script>
</body>
</html>