-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
543 lines (485 loc) · 19.7 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
<!DOCTYPE html><meta charset="utf-8">
<!--
Copyright 2018 Billie Rinaldi
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<head>
<title>prototile-js</title>
<link rel="stylesheet" type="text/css" href="prototile.css">
<script src="https://d3js.org/d3.v5.min.js"></script>
<script src="https://d3js.org/d3-voronoi.v1.min.js"></script>
<script src="prototile.js"></script>
</head>
<body>
<h1>prototile-js tiling generator</h1>
<p>
<a class="manual" href="manual.html">Instructions</a>
<a class="manual" href="https://github.com/billierinaldi/prototile">View Code</a>
<a class="manual" onclick="downloadMatrix()" href="javascript:void(0);">Download Tiling Matrix</a>
<a class="manual" href="index.html">Reset</a>
<a class="manual" onclick="clearSubsts()" href="javascript:void(0);">Clear</a>
</p><br>
<form id="paramsForm" action="index.html">
<p>
<label># Colors</label><input class="number" type="number" name="numColors" min="2" max="10" value="2" onChange="updateParams()">
<label>Initial Size</label><input class="number" type="number" name="initialSize" min="1" max="20" value="1" onChange="updateParams()">
<label>Square Size</label><input class="number" type="number" name="squareSize" min="1" max="50" value="10" onChange="updateParams()">
</p>
<p>
<label># Iterations</label><input class="number" type="number" name="iterations" min="0" max="4" value="2" onChange="updateParams()">
<label>Substitution Size</label><input class="number" type="number" name="substSize" min="2" max="20" value="5" onChange="updateParams()">
<label>Transparency</label><input class="number" type="number" name="transparency" min="0.1" max="1" step="0.1" value="0.5" onChange="updateParams()">
</p>
<p>
<label>Min Color</label><input type="color" name="minColor" value="#ffffff" onChange="updateParams()">
<label>Offset Row</label><input class="number" type="number" name="offsetRow" min="0" step="0.5" value="0" onChange="updateParams()">
<label>Rotate</label><input class="number" type="number" name="rotate" min="0" max="360" value="0" onChange="updateParams()">
</p>
<p>
<label>Max Color</label><input type="color" name="maxColor" value="#000000" onChange="updateParams()">
<label>Offset Column</label><input class="number" type="number" name="offsetColumn" min="0" step="0.5" value="0" onChange="updateParams()">
<label>Voronoi</label><input type="checkbox" name="useVoronoi" onChange="updateParams()">
</p>
<p>
<label>Shift Substs</label><span>
<i class="carat l" onclick="shiftSubsLeft()"></i>
<i class="carat r" onclick="shiftSubsRight()"></i>
<i class="carat u" onclick="shiftSubsUp()"></i>
<i class="carat d" onclick="shiftSubsDown()"></i>
</span>
<label>Mod Substs</label><input type="checkbox" name="modSubsts" onChange="updateParams()">
<label>Hide Updates</label><input type="checkbox" name="hideUpdates" onChange="updateParams(false)">
<label></label><input class="button" type="submit" value="Update URL">
<label id="needsUpdate"></label>
</p>
<input type="hidden" name="initial" value="0">
<input type="hidden" name="substitutions" value="0010001110110110111000100,1101110001001001000111011">
</form><br>
<div class="svg-container">
<svg version="1.1" viewBox="0 0 1000 1000" preserveAspectRatio="xMinYMin meet" class="svg-content"></svg>
</div>
<script>
// IDs for the SVG groups
const INITIAL = "initial";
const SUBSTITUTIONS = "substitutions";
const SUBSTITUTION_PREFIX = "subst";
const TILING = "tiling";
const COPY = "copy";
const FORM_ID = "paramsForm";
const NEEDS_UPDATE_ID = "needsUpdate";
// Names for the form inputs
const NUM_COLORS = "numColors";
const INITIAL_SIZE = "initialSize";
const SQUARE_SIZE = "squareSize";
const NUM_ITERATIONS = "iterations";
const SUSBST_SIZE = "substSize";
const TRANSPARENCY = "transparency";
const MIN_COLOR = "minColor";
const MAX_COLOR = "maxColor"
const OFFSET_ROW = "offsetRow";
const OFFSET_COLUMN = "offsetColumn";
const ROTATE = "rotate";
const USE_VORONOI = "useVoronoi";
const MOD_SUBSTS = "modSubsts";
const HIDE_UPDATES = "hideUpdates";
// INITIAL and SUBSTITUTIONS are already initialized
// Layout constants
const MARGIN_WIDTH = 10;
const MARGIN_HEIGHT = 20;
const LEFT_PANEL_MIN_WIDTH = 110;
// Tiling variables
var tilingSquareSize = 10;
var substSquareSize = 10;
var numColors = 2;
var iterations = 2;
var transparency = 0.5;
var tilingColors = [d3.color("white"), d3.color("black")];
tilingColors[0].opacity = transparency;
tilingColors[1].opacity = transparency;
var substColors = [d3.color("white"), d3.color("black")];
var useVoronoi = false;
var modSubsts = true;
var hideUpdates = false;
var tilingSize = 0;
// Substitution tiling creator
var prototile = new Prototile(
// initial tiling
[[0]],
// substitution tiles
[ // 0 substitution:
[[0, 0, 1, 0, 0], [0, 1, 1, 1, 0], [1, 1, 0, 1, 1], [0, 1, 1, 1, 0], [0, 0, 1, 0, 0]],
// 1 substitution:
[[1, 1, 0, 1, 1], [1, 0, 0, 0, 1], [0, 0, 1, 0, 0], [1, 0, 0, 0, 1], [1, 1, 0, 1, 1]]
]
);
// SVG element
var svg = d3.select("svg");
// Read form values from URL.
var populateFormFromURL = function() {
var formContents = document.getElementById(FORM_ID);
var url = decodeURIComponent(window.location.search);
if (url.length == 0) {
// handle default value of mod substitutions specially
// it is less confusing to see it switch from unchecked to checked than vice versa
formContents.elements[MOD_SUBSTS].checked = modSubsts;
return;
}
var regex = /[?&]([a-zA-Z]+)=([0-9,]+(.[1-9])?|#[0-9a-fA-F]{6})/g;
var match = regex.exec(url);
while (match != null) {
if (formContents.elements[match[1]]) {
formContents.elements[match[1]].value = match[2];
}
match = regex.exec(url);
}
[USE_VORONOI, MOD_SUBSTS].forEach(function(name) {
if (url.includes(name + "=on")) {
formContents.elements[name].checked = true;
} else {
formContents.elements[name].checked = false;
}
});
updateParams(false);
}
// Get form element value as an int.
var getFormInputInt = function(formContents, name) {
return parseInt(formContents.elements[name].value);
}
// Get form element value as a string.
var getFormInput = function(formContents, name) {
return formContents.elements[name].value;
}
// Read the tiling parameters from the form and update the display.
var updateParams = function(needsUpdate=true) {
if (needsUpdate) {
// Advise user that current parameters may not be reflected in the URL.
document.getElementById(NEEDS_UPDATE_ID).innerHTML = "Parameters changed<br>since last update!"
}
var formContents = document.getElementById(FORM_ID);
numColors = getFormInputInt(formContents, NUM_COLORS);
prototile.numColors = numColors;
var initialSize = getFormInputInt(formContents, INITIAL_SIZE);
prototile.resizeInitialTiling(initialSize, initialSize);
var substSize = getFormInputInt(formContents, SUSBST_SIZE);
// resize the substitution tiles matrix so that there is
// a substSize x substSize substitution tile for each color
prototile.resizeSubstitutionTiles(numColors, substSize, substSize);
console.log("init", prototile.initialTiling);
console.log("subst", prototile.substitutionTiles);
iterations = getFormInputInt(formContents, NUM_ITERATIONS);
tilingSquareSize = getFormInputInt(formContents, SQUARE_SIZE);
useVoronoi = formContents.elements[USE_VORONOI].checked;
modSubsts = formContents.elements[MOD_SUBSTS].checked;
transparency = getFormInput(formContents, TRANSPARENCY);
var hexColor0 = d3.color(formContents.elements[MIN_COLOR].value)
var hexColor1 = d3.color(formContents.elements[MAX_COLOR].value);
// give the substitution tiles no transparency so they are easier to see
var interpolator = d3.interpolate(hexColor0, hexColor1);
substColors = [];
for (var i = 0; i < numColors; i++) {
substColors.push(interpolator(i / (numColors - 1)));
}
// give the main tiling transparency in the same colors
hexColor0.opacity = transparency;
hexColor1.opacity = transparency;
interpolator = d3.interpolate(hexColor0, hexColor1);
tilingColors = [];
for (var i = 0; i < numColors; i++) {
tilingColors.push(interpolator(i / (numColors - 1)));
}
prototile.setInitialFromString(getFormInput(formContents, INITIAL));
prototile.setSubstitutionsFromString(getFormInput(formContents, SUBSTITUTIONS));
if (modSubsts) {
prototile.setModSubstitutions();
}
reposition();
redraw();
// position the copy tiling
var row = getFormInput(formContents, OFFSET_ROW);
var col = getFormInput(formContents, OFFSET_COLUMN);
positionCopyByOffsets(row, col);
}
// Set all the squares of substitution 0 to min color, 0.
var clearSubsts = function() {
prototile.setSubstitutionsFromString("0");
prototile.setModSubstitutions();
document.getElementById(FORM_ID).elements[SUBSTITUTIONS].value = prototile.getSubstitutionsAsString();
updateParams();
}
// Download the tiling matrix.
var downloadMatrix = function() {
// format the matrix as a string with space-separated elements and newline-separated rows
var matrix = prototile.getTilingAsString(iterations);
// create and click an anchor element referencing the base64 encoded matrix
var a = document.createElement('a');
a.setAttribute('download', 'tiling.txt');
a.setAttribute('href', "data:text/attachment;base64," + window.btoa(unescape(encodeURIComponent(matrix))));
a.setAttribute('target', '_blank');
a.dispatchEvent(new MouseEvent('click', {
view: window,
bubbles: false,
cancelable: true
}));
}
// Create top level SVG groups.
var initialize = function() {
// group for the initial tiling
var initGroup = svg.append("g")
.attr("id", INITIAL)
.attr("transform", "translate(" + MARGIN_WIDTH + ", " + MARGIN_HEIGHT + ")");
addBorder(initGroup, INITIAL);
addTitle(initGroup, "Initial");
// group for all the substitution tiles
var substitutionsGroup = svg.append("g")
.attr("id", SUBSTITUTIONS);
// groups for individual substitution tiles
positionSubstitutionTiles();
addTitle(svg.select("#" + SUBSTITUTION_PREFIX + "0"), "Substitutions");
// copy of main tiling
svg.append("use")
.attr("id", COPY)
.attr("xlink:href", "#" + TILING);
// group for main tiling
var tilingGroup = svg.append("g")
.attr("id", TILING)
.attr("style", "pointer-events: bounding-box;")
.on("click", function(d) {
var position = [0, 0];
position = d3.mouse(this);
positionCopyByMouse(position[0], position[1]);
})
.on("dblclick", function(d) {
document.getElementById(FORM_ID).submit();
});
addBorder(tilingGroup, TILING);
positionTiling();
}
// Add a border to a group.
var addBorder = function(group, name) {
group.append("rect")
.attr("id", name + "Border")
.attr("class", "tilingBorder");
}
// Add a title to a group.
var addTitle = function(group, title) {
group.append("text")
.text(title)
.attr("class", "title")
.attr("y", -5);
}
// Reposition groups in case elements have been resized.
var reposition = function() {
positionSubstitutionTiles();
positionTiling();
}
// Position the substitution tiles.
var positionSubstitutionTiles = function() {
var substRange = d3.range(0, prototile.substitutionTiles.length);
var substGroups = svg.select("#" + SUBSTITUTIONS).selectAll("g")
.data(substRange, function(d) { return d; });
substGroups.exit().remove();
substGroups.enter().append("g")
.attr("id", function(d) { return SUBSTITUTION_PREFIX + d; })
.append("rect")
.attr("id", function(d) { return SUBSTITUTION_PREFIX + d + "Border"; })
.attr("class", "tilingBorder");
var initPanelHeight = prototile.initialTiling.length * substSquareSize + 2.5 * MARGIN_HEIGHT;
var substPanelHeight = prototile.substitutionTiles[0].length * substSquareSize + substSquareSize;
substRange.forEach(function(d, i) {
svg.select("#" + SUBSTITUTION_PREFIX + d)
.attr("transform", "translate(" + MARGIN_WIDTH + ", " + (initPanelHeight + d * substPanelHeight) + ")");
});
}
// Calculate width of left panel.
var getLeftPanelWidth = function() {
return Math.max(LEFT_PANEL_MIN_WIDTH,
Math.max(prototile.initialTiling[0].length, prototile.substitutionTiles[0][0].length)
* substSquareSize + MARGIN_WIDTH + substSquareSize);
}
// Position the main tiling.
var positionTiling = function() {
svg.select("#" + TILING)
.attr("transform", "translate(" + getLeftPanelWidth() + ", " + 0 + ")");
}
// Position the copy tiling from the offset values.
var positionCopyByOffsets = function(row, col) {
var angle = document.getElementById(FORM_ID).elements[ROTATE].value;
var cx = getLeftPanelWidth() + tilingSize / 2;
var cy = tilingSize / 2;
svg.select("#" + COPY)
.transition()
.attr("transform", function(d) {
return "translate(" + col * tilingSquareSize + " " + row * tilingSquareSize + ") rotate(" + angle + " " + cx + " " + cy + ")";
});
}
// Position the copy tiling from a mouse position.
var positionCopyByMouse = function(x, y) {
x = roundToHalfSquare(x);
y = roundToHalfSquare(y);
var angle = document.getElementById(FORM_ID).elements[ROTATE].value;
var cx = getLeftPanelWidth() + tilingSize / 2;
var cy = tilingSize / 2;
svg.select("#" + COPY)
.transition()
.attr("transform", function(d) {
return "translate(" + x + " " + y + ") rotate(" + angle + " " + cx + " " + cy + ")";
});
document.getElementById(FORM_ID).elements[OFFSET_ROW].value = y / tilingSquareSize;
document.getElementById(FORM_ID).elements[OFFSET_COLUMN].value = x / tilingSquareSize;
}
// Shift columns of the substitutions left.
var shiftSubsLeft = function() {
prototile.substitutionTiles.forEach(function(matrix) {
matrix.forEach(function(row) {
row.push(row.shift());
});
});
document.getElementById(FORM_ID).elements[SUBSTITUTIONS].value = prototile.getSubstitutionsAsString();
updateParams();
}
// Shift columns of the substitutions right.
var shiftSubsRight = function() {
prototile.substitutionTiles.forEach(function(matrix) {
matrix.forEach(function(row) {
row.unshift(row.pop());
});
});
document.getElementById(FORM_ID).elements[SUBSTITUTIONS].value = prototile.getSubstitutionsAsString();
updateParams();
}
// Shift rows of the substitutions up.
var shiftSubsUp = function() {
prototile.substitutionTiles.forEach(function(matrix) {
matrix.push(matrix.shift());
});
document.getElementById(FORM_ID).elements[SUBSTITUTIONS].value = prototile.getSubstitutionsAsString();
updateParams();
}
// Shift rows of the substitutions down.
var shiftSubsDown = function() {
prototile.substitutionTiles.forEach(function(matrix) {
matrix.unshift(matrix.pop());
});
document.getElementById(FORM_ID).elements[SUBSTITUTIONS].value = prototile.getSubstitutionsAsString();
updateParams();
}
// Round position to nearest half square size.
var roundToHalfSquare = function(x) {
return Math.round(x / (tilingSquareSize / 2)) * (tilingSquareSize / 2);
}
// Redraw initial, substitution, and main tiling.
var redraw = function() {
displayTiling(INITIAL, prototile.initialTiling);
prototile.substitutionTiles.forEach(function(d, i) {
displayTiling(SUBSTITUTION_PREFIX + i, d);
})
if (!document.getElementById(FORM_ID).elements[HIDE_UPDATES].checked) {
displayTiling(TILING, prototile.iterate(iterations));
}
}
// Display a tiling based on matrix data.
var displayTiling = function(name, data) {
var tilingGroup = svg.select("#" + name);
var squareSize = substSquareSize;
if (name == TILING) {
squareSize = tilingSquareSize;
tilingSize = data.length * squareSize;
// set max offsets
document.getElementById(FORM_ID).elements[OFFSET_ROW].max = data.length;
document.getElementById(FORM_ID).elements[OFFSET_COLUMN].max = data[0].length;
if (useVoronoi) {
var voronoi = d3.voronoi();
var points = prototile.getTilingAsPoints(data, squareSize);
voronoi.extent([[0, 0], [tilingSize, tilingSize]]);
var diagram = voronoi(points);
var polygons = diagram.polygons();
tilingGroup.selectAll("g").remove();
var pointGroups = tilingGroup.selectAll("g")
.data(points)
.enter().append("g");
pointGroups.append("circle")
.attr("cx", function(d) { return d[0]; })
.attr("cy", function(d) { return d[1]; })
.attr("r", 1)
.attr("style", "fill:" + substColors[substColors.length - 1]);
pointGroups.append("svg:path")
.attr("d", function(d, i) { return "M" + polygons[i].join("L") + "Z"; })
.attr("style", "fill:none; stroke:" + substColors[substColors.length - 1]);
return;
}
}
// add row group for each row of data
var rows = tilingGroup.selectAll("g")
.data(data, function(d) { return d; });
rows.exit().remove();
rows = rows.enter().append("g")
.attr("class", function(d, i) { return "row"+i; })
.merge(rows)
.attr("transform", function(d, i) { return "translate(0," + i * squareSize + ")"; });
// add rectangle for each element of data
var rects = rows.selectAll("rect")
.data(function(d) { return d; });
rects.exit().remove();
rects.enter().append("rect")
.merge(rects)
.data(function(d) { return d; })
.attr("x", function(d, i, j) { return i * squareSize; })
.attr("width", squareSize)
.attr("height", squareSize)
.style("fill", function(d, i, j) {
if (name == TILING) {
// main tiling gets transparent colors
return tilingColors[d];
} else {
// substitution tiles get solid colors
return substColors[d];
}
})
.on("click", function(d, i, j) {
// get row and column index
var row = parseInt(this.parentNode.className.baseVal.substring(3));
var col = i;
// identify which tiling this is based on group name
var name = this.parentNode.parentNode.id;
if (name == INITIAL) {
// clicking on a square in the initial tiling changes its color
prototile.initialTiling[row][col] = (prototile.initialTiling[row][col] + 1) % numColors;
document.getElementById(FORM_ID).elements[INITIAL].value = prototile.getInitialTilingAsString();
updateParams();
} else if (name == TILING) {
// clicking on the main tiling translates the copy
// handled by generic on click handler
} else if (name.startsWith(SUBSTITUTION_PREFIX)) {
// clicking on a square in the substitution tiles changes its color
if (modSubsts) {
for (var k = 0; k < numColors; k++) {
prototile.substitutionTiles[k][row][col] = (prototile.substitutionTiles[k][row][col] + 1) % numColors;
}
} else {
var substIndex = parseInt(name.substring(SUBSTITUTION_PREFIX.length));
prototile.substitutionTiles[substIndex][row][col] = (prototile.substitutionTiles[substIndex][row][col] + 1) % numColors;
}
document.getElementById(FORM_ID).elements[SUBSTITUTIONS].value = prototile.getSubstitutionsAsString();
updateParams();
}
});
// resize border rectangle to fit new dimensions
svg.select("#" + name + "Border")
.attr("width", data.length * squareSize)
.attr("height", data.length * squareSize);
}
// create initial display
initialize();
populateFormFromURL();
redraw();
</script>
</body>
</html>