-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.html
491 lines (444 loc) · 18.1 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
<!DOCTYPE html>
<html lang="">
<head>
<meta charset="utf-8">
<title>Tabletweaving Pattern Generator</title>
<script>
var debug_pattern;
var global_pattern = {
nr_tablets: NaN,
nr_holes: NaN,
nr_rows: NaN,
warping_direction: [],
color_values: [],
colors: [],
turns: [],
bg_colors: ["", ""],
}
// add another color selector
function addColor(){
let new_col = document.querySelector("#cols > input:last-of-type").cloneNode();
document.querySelector("#cols").innerHTML += "\n";
document.querySelector("#cols").appendChild(new_col);
readColors();
}
// remove the last color
function removeColor(){
if(confirm("The last color will be replaced by empty holes.")){
document.querySelector("#cols > input:last-of-type").remove();
readColors();
} else {
}
}
// read background colors
function readBGColors(){
global_pattern.bg_colors[0] = document.querySelector("#col_forward").value;
global_pattern.bg_colors[1] = document.querySelector("#col_backward").value;
buildPatternChart();
}
// read values from gui and generate the svg of the warping chart
function readSetup(){
// read values from inputs
let nr_tablets = parseInt(document.querySelector("#nr_tablets").value);
let nr_holes = parseInt(document.querySelector("#nr_holes").value);
let nr_rows = parseInt(document.querySelector("#nr_rows").value);
global_pattern.nr_tablets = nr_tablets;
global_pattern.nr_holes = nr_holes;
global_pattern.nr_rows = nr_rows;
global_pattern.colors = new Array(nr_tablets);
// set colors to initial values
for(i=0; i<nr_tablets; i++){
global_pattern.colors[i] = new Array(nr_holes);
for(j=0; j<nr_holes; j++){
global_pattern.colors[i][j] = 1;
}
}
// initialise warping warping_direction to ZSZ...ZS...SSZS
// start with ZS, repeat Z to the middle of the pattern, repeat S from the middle, end with ZS
let warping_direction = Array(nr_tablets);
warping_direction[0] = "Z";
warping_direction[1] = "S";
warping_direction[nr_tablets - 1] = "S";
warping_direction[nr_tablets - 2] = "Z";
for(i=2; i < Math.ceil(nr_tablets/2); i++){
warping_direction[i] = "Z";
}
for(i=Math.ceil(nr_tablets/2); i < nr_tablets - 2; i++){
warping_direction[i] = "S";
}
global_pattern.warping_direction = warping_direction;
// initialise turns in pattern
for(i=0; i<nr_tablets; i++){
global_pattern.turns[i] = [];
}
addRows_helper(nr_rows);
}
// update data and redraw when colors are changed
function readColors(){
// iterate over color inputs and get the colors
let all_colors = Array.from(document.querySelectorAll("#cols > input")).map(ele => {
let val=ele.value;
return(val);
});
all_colors.unshift("none");
global_pattern.color_values = all_colors;
for(i=0; i<global_pattern.nr_tablets; i++){
for(j=0; j<global_pattern.nr_holes; j++){
global_pattern.colors[i][j] = (global_pattern.colors[i][j] >= global_pattern.color_values.length)
? 0
: global_pattern.colors[i][j];
}
}
buildWarpingChart();
}
// create the svg graphic for the lower (warping) diagram
function buildWarpingChart(){
// set the view-box of the svg element so that i can use a 1x1 grid for the chart
document.querySelector("#warping_chart").setAttribute("viewBox", `-1 0 ${global_pattern.nr_tablets+1} ${global_pattern.nr_holes + 1}`);
// build svg contents
let border_width = 0.05;
let stroke_width = 0.2;
color_definitions = global_pattern.color_values.map((col, i) => {
return(`.thread_color_${i} {stroke:${col}}`);
}).reduce((x,y) => {
return(x + "\n" + y);
}, "");
let svg_contents = `
<style>
.text_dir {font: bold 0.8px sans-serif;}
.text_hole {font: bold 0.8px sans-serif;}
.box {fill:white; stroke:black; stroke-width:${border_width};}
.thread {stroke-width:${stroke_width}; stroke-linecap:round;}
${color_definitions}
</style>
<symbol id="thread_z" width="1" height="1" viewBox="-0.5 -0.5 1 1">
<g>
<rect x="-0.5" y="-0.5" width="1" height="1" class="box" />
<path class="thread" d="M -0.3 -0.3 0.3 0.3" />
</g>
</symbol>
<symbol id="thread_s" width="1" height="1" viewBox="-0.5 -0.5 1 1">
<g>
<rect x="-0.5" y="-0.5" width="1" height="1" class="box" />
<path class="thread" d="M 0.3 -0.3 -0.3 0.3" />
</g>
</symbol>
<symbol id="card_z" width="1" height="1" viewBox="-0.5 -0.5 1 1">
<g>
<rect x="-0.5" y="-0.5" width="1" height="1" class="box" />
<text x="-0.4" y="0.4" class="text_dir">Z</text>
</g>
</symbol>
<symbol id="card_s" width="1" height="1" viewBox="-0.5 -0.5 1 1">
<g>
<rect x="-0.5" y="-0.5" width="1" height="1" class="box" />
<text x="-0.4" y="0.4" class="text_dir">S</text>
</g>
</symbol>
<symbol id="hole_0" width="1" height="1" viewBox="-0.5 -0.5 1 1">
<g>
<rect x="-0.5" y="-0.5" width="1" height="1" class="box" />
<text x="-0.4" y="0.4" class="text_hole">A</text>
</g>
</symbol>
<symbol id="hole_1" width="1" height="1" viewBox="-0.5 -0.5 1 1">
<g>
<rect x="-0.5" y="-0.5" width="1" height="1" class="box" />
<text x="-0.4" y="0.4" class="text_hole">B</text>
</g>
</symbol>
<symbol id="hole_2" width="1" height="1" viewBox="-0.5 -0.5 1 1">
<g>
<rect x="-0.5" y="-0.5" width="1" height="1" class="box" />
<text x="-0.4" y="0.4" class="text_hole">C</text>
</g>
</symbol>
<symbol id="hole_3" width="1" height="1" viewBox="-0.5 -0.5 1 1">
<g>
<rect x="-0.5" y="-0.5" width="1" height="1" class="box" />
<text x="-0.4" y="0.4" class="text_hole">D</text>
</g>
</symbol>
<symbol id="hole_4" width="1" height="1" viewBox="-0.5 -0.5 1 1">
<g>
<rect x="-0.5" y="-0.5" width="1" height="1" class="box" />
<text x="-0.4" y="0.4" class="text_hole">E</text>
</g>
</symbol>
<symbol id="hole_5" width="1" height="1" viewBox="-0.5 -0.5 1 1">
<g>
<rect x="-0.5" y="-0.5" width="1" height="1" class="box" />
<text x="-0.4" y="0.4" class="text_hole">F</text>
</g>
</symbol>
`;
svg_contents = svg_contents + "<g>";
for(i=0; i<global_pattern.nr_tablets; i++){
if(global_pattern.warping_direction[i] == "Z") {
svg_contents = svg_contents + `<use href="#card_z" x="${i}" y="${global_pattern.nr_holes}" onclick="cardClicked(this);" />`;
} else {
svg_contents = svg_contents + `<use href="#card_s" x="${i}" y="${global_pattern.nr_holes}" onclick="cardClicked(this);" />`;
}
}
svg_contents = svg_contents + "</g><g>";
for(i=0; i<global_pattern.nr_holes; i++){
svg_contents = svg_contents + `<use href="#hole_${i}" x="-1" y="${i}" />`;
}
svg_contents = svg_contents + "</g><g>";
for(i=0; i<global_pattern.nr_holes; i++){
svg_contents = svg_contents + "<g>";
for(j=0; j<global_pattern.nr_tablets; j++){
if(global_pattern.warping_direction[j] == "Z"){
svg_contents = svg_contents + `<use href="#thread_z" x="${j}" y="${i}" onclick="threadClicked(this);" class="thread_color_${global_pattern.colors[j][i]}" />`;
} else {
svg_contents = svg_contents + `<use href="#thread_s" x="${j}" y="${i}" onclick="threadClicked(this);" class="thread_color_${global_pattern.colors[j][i]}" />`;
}
}
svg_contents = svg_contents + "</g>";
}
svg_contents = svg_contents + "</g>";
document.querySelector("#warping_chart").innerHTML = svg_contents;
}
// build upper (pattern) chart
function buildPatternChart(){
document.querySelector("#pattern_chart").setAttribute("viewBox", `-1 ${-global_pattern.nr_rows} ${global_pattern.nr_tablets+1} ${global_pattern.nr_rows + 1}`);
// get pattern
let pattern_colors = buildPatternColors();
let pattern_direction = buildPatternDirection();
let box_colors = Array(global_pattern.nr_tablets);
for(i=0; i<global_pattern.nr_tablets; i++){
box_colors[i] = Array(global_pattern.nr_rows);
for(j=0; j<global_pattern.nr_rows; j++){
if(global_pattern.turns[i][j] == -1){
box_colors[i][j] = "fwd";
} else {
box_colors[i][j] = "rev";
}
}
}
// build svg contents
let border_width = 0.05;
let stroke_width = 0.2;
color_definitions = global_pattern.color_values.map((col, i) => {
return(`.thread_color_${i} {stroke:${col}}`);
}).reduce((x,y) => {
return(x + "\n" + y);
}, "");
let svg_contents = `
<style>
.box {fill:white; stroke:black; stroke-width:${border_width};}
.box_fwd {fill:${global_pattern.bg_colors[0]}; stroke:black; stroke-width:${border_width};}
.box_rev {fill:${global_pattern.bg_colors[1]}; stroke:black; stroke-width:${border_width};}
.thread {stroke-width:${stroke_width}; stroke-linecap:round;}
${color_definitions}
</style>
<symbol id="thread_z_fwd" width="1" height="1" viewBox="-0.5 -0.5 1 1">
<g>
<rect x="-0.5" y="-0.5" width="1" height="1" class="box_fwd" />
<path class="thread" d="M -0.3 -0.3 0.3 0.3" />
</g>
</symbol>
<symbol id="thread_s_fwd" width="1" height="1" viewBox="-0.5 -0.5 1 1">
<g>
<rect x="-0.5" y="-0.5" width="1" height="1" class="box_fwd" />
<path class="thread" d="M 0.3 -0.3 -0.3 0.3" />
</g>
</symbol>
<symbol id="thread_z_rev" width="1" height="1" viewBox="-0.5 -0.5 1 1">
<g>
<rect x="-0.5" y="-0.5" width="1" height="1" class="box_rev" />
<path class="thread" d="M -0.3 -0.3 0.3 0.3" />
</g>
</symbol>
<symbol id="thread_s_rev" width="1" height="1" viewBox="-0.5 -0.5 1 1">
<g>
<rect x="-0.5" y="-0.5" width="1" height="1" class="box_rev" />
<path class="thread" d="M 0.3 -0.3 -0.3 0.3" />
</g>
</symbol>
`;
for(i=0; i<global_pattern.nr_tablets; i++){
for(j=0; j<global_pattern.nr_rows; j++){
svg_contents = svg_contents + `<use href="#thread_${pattern_direction[i][j]}_${box_colors[i][j]}" x="${i}" y="${-j}" onclick="patternClicked(this);" class="thread_color_${pattern_colors[i][j]}" />`;
}
}
document.querySelector("#pattern_chart").innerHTML = svg_contents;
}
// which color does each cell in the pattern have?
// TODO: not working for backwards turns: modulo working correctly?
function buildPatternColors(){
let pattern = [];
var i;
var position;
for(i=0; i<global_pattern.nr_tablets; i++){
position=global_pattern.nr_holes-1;
pattern[i] = Array(global_pattern.nr_rows);
pattern[i][0] = global_pattern.colors[i][position]
for(j=1; j<global_pattern.nr_rows; j++){
if(global_pattern.turns[i][j-1] == global_pattern.turns[i][j]){
position = position + global_pattern.turns[i][j];
}
pattern[i][j] = global_pattern.colors[i][my_mod(position, global_pattern.nr_holes)];
}
}
debug_pattern = pattern;
return(pattern);
}
// directions in the pattern (Z or S)
function buildPatternDirection(){
let pattern = [];
var i;
var tmp_direction;
for(i=0; i<global_pattern.nr_tablets; i++){
pattern[i] = Array(global_pattern.nr_rows);
for(j=0; j<global_pattern.nr_rows; j++){
if((global_pattern.warping_direction[i] == "Z") & (global_pattern.turns[i][j] == -1)){
pattern[i][j] = "z";
}
if((global_pattern.warping_direction[i] == "S") & (global_pattern.turns[i][j] == -1)){
pattern[i][j] = "s";
}
if((global_pattern.warping_direction[i] == "Z") & (global_pattern.turns[i][j] == 1)){
pattern[i][j] = "s";
}
if((global_pattern.warping_direction[i] == "S") & (global_pattern.turns[i][j] == 1)){
pattern[i][j] = "z";
}
}
}
return(pattern);
}
// circle through colors when thread is clicked
function threadClicked(event_source){
let card = event_source.getBBox().x;
let hole = event_source.getBBox().y;
let ncolors = global_pattern.color_values.length;
global_pattern.colors[card][hole] = my_mod((global_pattern.colors[card][hole] + 1), ncolors);
buildWarpingChart();
buildPatternChart();
}
// toggle S/Z on click on card
function cardClicked(event_source){
let card = event_source.getBBox().x;
global_pattern.warping_direction[card] = (global_pattern.warping_direction[card] == "Z") ? "S" : "Z";
buildWarpingChart();
buildPatternChart();
};
// toggle turn direction
function patternClicked(event_source){
let x = event_source.getBBox().x;
let y = -event_source.getBBox().y;
global_pattern.turns[x][y] *= -1;
buildPatternChart();
}
// build interface when site first loads
function firstSetup(){
readSetup();
readColors();
readBGColors();
buildWarpingChart();
buildPatternChart();
}
// confirm changes to setup that would overwrite the current pattern
function updateSetup(){
if(confirm("Updating the number of tablets or holes will delete your current pattern.")){
firstSetup();
} else {
document.querySelector("#nr_tablets").value = global_pattern.nr_tablets;
document.querySelector("#nr_holes").value = global_pattern.nr_holes;
document.querySelector("#nr_rows").value = global_pattern.nr_rows;
}
}
// helper function to add rows (alternating forward backward per default)
function addRows_helper(n){
for(i=0; i<n; i++){
for(j=0; j<global_pattern.nr_tablets; j++){
global_pattern.turns[j].push(-1);
}
}
}
// function to just add rows without changing anything else
function addRows(){
let add_rows = parseInt(document.querySelector("#add_rows").value);
global_pattern.nr_rows += add_rows;
// todo: add rows to global_pattern.turns
buildPatternChart();
}
// modulo that actually works on the filed of Z_n and does not give negative results
function my_mod(x,y){
return( ((x % y) + y) % y );
}
// save pattern
function savePattern(){
let dl_content = "data:text/json;charset=utf-8,";
dl_content += encodeURIComponent(JSON.stringify(global_pattern));
document.querySelector("#download_link").setAttribute("href", dl_content);
document.querySelector("#download_link").click();
};
</script>
</head>
<body onload="firstSetup();">
<div id="pattern">
<p id="pattern_p">
<svg id="pattern_chart" width="400" viewBox="-1 0 15 15">
</svg>
</p>
</div>
<div id="warp">
<p id="threading">
<svg id="warping_chart" width="400" viewBox="-1 0 15 5">
</svg>
</p>
</div>
<div>
<p id="tablets">
<label for="nr_tablets">Number of Tablets</label>
<input id="nr_tablets" type="number" min=4 max=20 step=1, value=14>
<br>
<label for="nr_holes">Number of Holes per Tablet</label>
<input id="nr_holes" type="number" min=3 max=6 step=1, value=4>
<br>
<label for="nr_rows">Number of Rows in Pattern</label>
<input id="nr_rows" type="number" min=2 max=100 step=1, value=16>
<br>
<label for="button_setup">Update Setup</label>
<input id="button_setup" type="button" value="Update" onclick="updateSetup();">
<br>
<label for="add_rows">Add Rows</label>
<input id="add_rows" type="number" min=1 max=100 step=1, value=1>
<br>
<label for="button_add_rows">Add Rows</label>
<input id="button_add_rows" type="button" value="Add Rows" onclick="addRows();">
</p>
</div>
<div id="colors">
<p id="cols">
Thread Colors <br>
<input type="color" value="#bb0000" onchange="readColors();">
<input type="color" value="#00bb00" onchange="readColors();">
<input type="color" value="#0000bb" onchange="readColors();">
</p>
<p id="buttons">
<input type="button" value="Add Color" onclick="addColor();">
<input type="button" value="Remove Last Color" onclick="removeColor();">
</p>
</div>
<div id="bg_colors">
Box Background Colors <br>
<label for="col_forward">Forward</label>
<input id="col_forward" type="color" value="#ffffff" onchange="readBGColors();">
<br>
<label for="col_backward">Backward</label>
<input id="col_backward" type="color" value="#deddda" onchange="readBGColors();">
</div>
<div>
<p>
Save <input type="button" value="save" onclick="savePattern();">
<a id="download_link" download="pattern.JSON" style="display:none;" href="">download</a>
</p>
<p>
Load
</p>
</div>
</body>
</html>