Repository navigation
Expand file tree
/
Copy pathjigsaw.html
More file actions
229 lines (211 loc) · 11.1 KB
/
Copy pathjigsaw.html
File metadata and controls
229 lines (211 loc) · 11.1 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
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Jigsaw puzzle</title>
<script type="text/javascript">
// save function pieced together from here: https://stackoverflow.com/questions/19327749/javascript-blob-filename-without-link
function save(filename, data)
{
var blob = new Blob([data], {type: "image/svg+xml"});
if (window.navigator.msSaveOrOpenBlob)
{
window.navigator.msSaveBlob(blob, filename);
}
else
{
var elem = window.document.createElement('a');
elem.href = window.URL.createObjectURL(blob);
elem.download = filename;
document.body.appendChild(elem);
elem.click();
document.body.removeChild(elem);
}
}
var seed = 1;
function random() { var x = Math.sin(seed) * 10000; seed += 1; return x - Math.floor(x); }
function uniform(min, max) { var r = random(); return min + r * (max - min); }
function rbool() { return random() > 0.5; }
function $(id) { return document.getElementById(id); }
function updateseed() { $("_seed").value = $("seed").value; update(); }
function updatetabsize() { $("_tabsize").value = $("tabsize").value + "%"; update(); }
function updatejitter() { $("_jitter").value = $("jitter").value + "%"; update(); }
function update_seed() { var val = parseFloat($("_seed").value); if (!isNaN(val)) { $("seed").value = val; } updateseed(); }
function update_tabsize() { var val = parseFloat($("_tabsize").value); if (!isNaN(val)) { $("tabsize").value = val; } updatetabsize(); }
function update_jitter() { var val = parseFloat($("_jitter").value); if (!isNaN(val)) { $("jitter").value = val; } updatejitter(); }
var a, b, c, d, e, t, j, flip, xi, yi, xn, yn, vertical, offset, width, height, radius;
function first() { e = uniform(-j, j); next();}
function next() { var flipold = flip; flip = rbool(); a = (flip == flipold ? -e: e); b = uniform(-j, j); c = uniform(-j, j); d = uniform(-j, j); e = uniform(-j, j);}
function sl() { return vertical ? height / yn : width / xn; }
function sw() { return vertical ? width / xn : height / yn;}
function ol() { return offset + sl() * (vertical ? yi : xi); }
function ow() { return offset + sw() * (vertical ? xi : yi); }
function l(v) { var ret = ol() + sl() * v; return Math.round(ret * 100) / 100; }
function w(v) { var ret = ow() + sw() * v * (flip ? -1.0 : 1.0); return Math.round(ret * 100) / 100; }
function p0l() { return l(0.0); }
function p0w() { return w(0.0); }
function p1l() { return l(0.2); }
function p1w() { return w(a); }
function p2l() { return l(0.5 + b + d); }
function p2w() { return w(-t + c); }
function p3l() { return l(0.5 - t + b); }
function p3w() { return w(t + c); }
function p4l() { return l(0.5 - 2.0 * t + b - d); }
function p4w() { return w(3.0 * t + c); }
function p5l() { return l(0.5 + 2.0 * t + b - d); }
function p5w() { return w(3.0 * t + c); }
function p6l() { return l(0.5 + t + b); }
function p6w() { return w(t + c); }
function p7l() { return l(0.5 + b + d); }
function p7w() { return w(-t + c); }
function p8l() { return l(0.8); }
function p8w() { return w(e); }
function p9l() { return l(1.0); }
function p9w() { return w(0.0); }
function parse_input()
{
seed = parseInt($("seed").value);
t = parseFloat($("tabsize").value) / 200.0;
j = parseFloat($("jitter").value) / 100.0;
xn = parseInt($("xn").value);
yn = parseInt($("yn").value);
}
function gen_dh()
{
var str = "";
vertical = 0;
for (yi = 1; yi < yn; ++yi)
{
xi = 0;
first();
str += "M " + p0l() + "," + p0w() + " ";
for (; xi < xn; ++xi)
{
str += "C " + p1l() + " " + p1w() + " " + p2l() + " " + p2w() + " " + p3l() + " " + p3w() + " ";
str += "C " + p4l() + " " + p4w() + " " + p5l() + " " + p5w() + " " + p6l() + " " + p6w() + " ";
str += "C " + p7l() + " " + p7w() + " " + p8l() + " " + p8w() + " " + p9l() + " " + p9w() + " ";
next();
}
}
return str;
}
function gen_dv()
{
var str = "";
vertical = 1;
for (xi = 1; xi < xn; ++xi)
{
yi = 0;
first();
str += "M " + p0w() + "," + p0l() + " ";
for (; yi < yn; ++yi)
{
str += "C " + p1w() + " " + p1l() + " " + p2w() + " " + p2l() + " " + p3w() + " " + p3l() + " ";
str += "C " + p4w() + " " + p4l() + " " + p5w() + " " + p5l() + " " + p6w() + " " + p6l() + " ";
str += "C " + p7w() + " " + p7l() + " " + p8w() + " " + p8l() + " " + p9w() + " " + p9l() + " ";
next();
}
}
return str;
}
function gen_db()
{
var str = "";
str += "M " + (offset + radius) + " " + (offset) + " ";
str += "L " + (offset + width - radius) + " " + (offset) + " ";
str += "A " + (radius) + " " + (radius) + " 0 0 1 " + (offset + width) + " " + (offset + radius) + " ";
str += "L " + (offset + width) + " " + (offset + height - radius) + " ";
str += "A " + (radius) + " " + (radius) + " 0 0 1 " + (offset + width - radius) + " " + (offset + height) + " ";
str += "L " + (offset + radius) + " " + (offset + height) + " ";
str += "A " + (radius) + " " + (radius) + " 0 0 1 " + (offset) + " " + (offset + height - radius) + " ";
str += "L " + (offset) + " " + (offset + radius) + " ";
str += "A " + (radius) + " " + (radius) + " 0 0 1 " + (offset + radius) + " " + (offset) + " ";
return str;
}
function update()
{
width = parseInt($("width").value);
height = parseInt($("height").value);
radius = parseFloat($("radius").value);
var ratio = 1.0 * width / height;
if (ratio > 1.5)
{
radius = radius * 900 / width;
width = 900;
height = width / ratio;
}
else
{
radius = radius * 600 / height;
height = 600;
width = height * ratio;
}
$("puzzlecontainer").setAttribute("width", width + 30);
$("puzzlecontainer").setAttribute("height", height + 30);
offset = 15;
parse_input();
$("puzzlepath_h").setAttribute("d", gen_dh());
$("puzzlepath_v").setAttribute("d", gen_dv());
$("puzzlepath_b").setAttribute("d", gen_db());
}
function generate()
{
width = parseInt($("width").value);
height = parseInt($("height").value);
radius = parseFloat($("radius").value);
offset = 0.0;
parse_input();
var data = "<svg xmlns=\"http://www.w3.org/2000/svg\" version=\"1.0\" ";
data += "width=\"" + width + "mm\" height=\"" + height + "mm\" viewBox=\"0 0 " + width + " " + height + "\">";
data += "<path fill=\"none\" stroke=\"DarkBlue\" stroke-width=\"0.1\" d=\"";
data += gen_dh();
data += "\"></path>";
data += "<path fill=\"none\" stroke=\"DarkRed\" stroke-width=\"0.1\" d=\"";
data += gen_dv();
data += "\"></path>";
data += "<path fill=\"none\" stroke=\"Black\" stroke-width=\"0.1\" d=\"";
data += gen_db();
data += "\"></path>";
data += "</svg>";
save("jigsaw.svg", data);
}
</script>
</head>
<body onload="$('seed').value = Math.random() * 10000; updateseed();">
<table>
<tr>
<td>Seed:</td>
<td><input id="_seed" type="text" value="0" onchange="update_seed()"/></td>
<td><input id="seed" type="range" value="0" min="0" max="9999" step="1" onchange="updateseed()"/></td>
</tr>
<tr>
<td>Tab Size:</td>
<td><input id="_tabsize" type="text" value="20%" onchange="update_tabsize()"/></td>
<td><input id="tabsize" type="range" value="20" min="10" max="30" step="0.1" onchange="updatetabsize()"/></td>
</tr>
<tr>
<td>Jitter:</td>
<td><input id="_jitter" type="text" value="4%" onchange="update_jitter()"/></td>
<td><input id="jitter" type="range" value="4" min="0" max="13" step="0.1" onchange="updatejitter()"/></td>
</tr>
<tr>
<td>Corner Radius:</td>
<td><input id="radius" type="text" value="2.0" size="4" onchange="update()"/> mm</td>
<td></td>
</tr>
<tr>
<td>Tiles:</td>
<td><input id="xn" type="text" value="15" size="4" onchange="update()"/> x <input id="yn" type="text" value="10" size="4" onchange="update()"/></td>
<td></td>
</tr>
<tr>
<td>Size:</td>
<td><input id="width" type="text" value="300" size="4" onchange="update()"/> x <input id="height" type="text" value="200" size="4" onchange="update()"/> mm</td>
<td><button onclick="generate()">Download SVG</button></td>
</tr>
</table>
<svg id="puzzlecontainer"><path id="puzzlepath_h" fill="none" stroke="DarkBlue"></path><path id="puzzlepath_v" fill="none" stroke="DarkRed"></path><path id="puzzlepath_b" fill="none" stroke="Black"></path></svg>
<p><a href="index.html">Back</a></p>
<a href="https://github.com/Draradech/jigsaw" class="github-corner" aria-label="View source on GitHub"><svg width="80" height="80" viewBox="0 0 250 250" style="fill:#FD6C6C; color:#fff; position: absolute; top: 0; border: 0; right: 0;" aria-hidden="true"><path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path><path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path><path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"></path></svg></a><style>.github-corner:hover .octo-arm{animation:octocat-wave 560ms ease-in-out}@keyframes octocat-wave{0%,100%{transform:rotate(0)}20%,60%{transform:rotate(-25deg)}40%,80%{transform:rotate(10deg)}}@media (max-width:500px){.github-corner:hover .octo-arm{animation:none}.github-corner .octo-arm{animation:octocat-wave 560ms ease-in-out}}</style>
</body>
</html>