-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
475 lines (473 loc) · 17.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
<!doctype html>
<html lang="en">
<head>
<title>Twist 'em up! Turn 'em 'round!</title>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="theme-color" content="#cfcfef" />
<link rel="icon" sizes="400x400" href="thumb.png" />
<link rel="stylesheet" type="text/css" href="icons/webfont.css" />
<link rel="stylesheet" type="text/css" href="umbra.css" />
<style type="text/css">
/* Layout */
html, body {
width: 100%; height: 100%; margin: 0px; padding: 0px;
overflow: hidden;
}
canvas {
position: absolute; margin: auto;
top: 0px; left: 0px; bottom: 0px; right: 0px;
}
#config .box {
min-width: 10em;
}
/* Colors */
body {
background-color: #cfc7df;
color: #000;
}
.button.button_root { color: #537; background: rgba(159, 127, 191, 0.5); }
/* Display of the fraction or 2x2 matrix */
#caption {
position: absolute; margin: auto;
left: 0px; bottom: 0px; right: 0px;
text-align: center;
}
#caption_fraction, #caption_matrix {
margin: 1em; padding: 0.5em; border-radius: 0.5em;
background: rgba(255, 255, 255, 0.5);
}
#caption table {
display: inline-block; vertical-align: middle;
border-spacing: 0px; margin: 0em 0.25em;
}
#caption_fraction tr:first-child > td { border-bottom: 1px solid #000; }
#caption_matrix td { padding: 0em 0.25em; }
#caption_matrix:before { content: '('; }
#caption_matrix:after { content: ')'; }
#caption_matrix:before, #caption_matrix:after {
font-size: 250%; display: inline-block; vertical-align: middle;
}
/* Config box elements */
form ul {
list-style-type: none; text-align: left; padding-left: 0em;
margin-bottom: 0em;
}
/* Navigation links */
ul.inline {
list-style-type: none;
padding: 0em;
text-align: center;
font-weight: bold;
}
ul.inline > li { display: inline-block; }
ul.inline > li > a { padding: 0.25em 0.5em; }
/* Text */
.dedication { font-style: italic; text-align: center; }
.byline { opacity: 0.75; text-align: right; }
/* Custom root buttons */
#button_turn { left: 0.25em; bottom: 0.25em; }
#button_twist { left: 2.0em; bottom: 0.25em; }
</style>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script type="text/javascript" src="hammer.min.js"></script>
<script type="text/javascript" src="umbra.min.js"></script>
<script type="text/javascript" src="anima.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
var CANVAS = '#main';
// dumpling geometry
function createDumpling(resolution) {
resolution = Math.max(resolution || 10, 2);
var num_rows = resolution + 1;
var column_stride = 2 * num_rows
var vertices = [];
var tris = [];
for (var i = 0; i < num_rows; ++i) {
for (var j = 0; j < num_rows; ++j) {
var x = 2 * i/resolution - 1;
var y = 2 * j/resolution - 1;
vertices.push(x, y, 1, 0, x, y, -1, 0);
}
if (i > 0) {
var column_start = i * column_stride;
for (var j = column_start; j < column_start + column_stride - 2; j += 2) {
tris.push(
j - column_stride, j, j + 2 - column_stride,
j, j + 2 - column_stride, j + 2,
j + 1, j + 1 - column_stride, j + 3,
j + 1 - column_stride, j + 3, j + 3 - column_stride,
);
}
}
}
// attach the ends
var end_width = 1 / resolution;
corners = [0, num_rows - 1, num_rows * num_rows - num_rows, num_rows * num_rows - 1];
for (var c of corners) {
vertices[8*c + 3] = end_width;
vertices[8*c + 7] = -end_width;
}
extras_start = num_rows * column_stride;
vertices.push(
// blowing up the corner
-1, -1, 1, -end_width, -1, 1, 1, -end_width,
1, -1, 1, -end_width, 1, 1, 1, -end_width,
// vertex 1 at infinity
-1, -1, 10, end_width, -1, 1, 10, end_width,
1, -1, 10, end_width, 1, 1, 10, end_width,
// vertex 2 at infinity
-1, -1, 10, -end_width, -1, 1, 10, -end_width,
1, -1, 10, -end_width, 1, 1, 10, -end_width,
);
tris.push(
// close corner blowup above and below
2 * corners[0], extras_start, 2 * corners[0] + column_stride,
2 * corners[1], extras_start + 1, 2 * corners[1] + column_stride,
2 * corners[2], extras_start + 2, 2 * corners[2] - column_stride,
2 * corners[3], extras_start + 3, 2 * corners[3] - column_stride,
// close corner blowup to the side
2 * corners[0], extras_start, 2 * corners[0] + 2,
2 * corners[1], extras_start + 1, 2 * corners[1] - 2,
2 * corners[2], extras_start + 2, 2 * corners[2] + 2,
2 * corners[3], extras_start + 3, 2 * corners[3] - 2,
// stab out to infinity
2 * corners[0], extras_start, extras_start + 4,
2 * corners[1], extras_start + 1, extras_start + 5,
2 * corners[2], extras_start + 2, extras_start + 6,
2 * corners[3], extras_start + 3, extras_start + 7,
// close out at infinity
extras_start, extras_start + 4, extras_start + 8,
extras_start + 1, extras_start + 5, extras_start + 9,
extras_start + 2, extras_start + 6, extras_start + 10,
extras_start + 3, extras_start + 7, extras_start + 11,
);
return [vertices, tris];
}
// dumpling tracking
var texture_transform = [1, 0, 0, 1];
var texture_offset = new Float32Array([0, 0]);
var twist = 0;
var turn = 0;
window.TWIST_SPEED = 1 / 500;
window.TURN_SPEED = 1 / 250;
function update_caption() {
$('#caption_matrix').html(
'<table><tr><td>' + texture_transform[3] +
'</td><td>' + -texture_transform[2] +
'</td></tr><tr><td>' + -texture_transform[1] +
'</td><td>' + texture_transform[0] + '</td></tr></table>');
$('#caption_fraction').html(
(texture_transform[1] * texture_transform[3] > 0 ? '-' : '') +
'<table><tr><td>' + Math.abs(texture_transform[1]) +
'</td></tr><tr><td>' + Math.abs(texture_transform[3]) + '</td></tr></table>');
}
function twist_em_up_finish() {
texture_transform[0] -= texture_transform[2];
texture_transform[1] -= texture_transform[3];
texture_offset[1] = (texture_offset[0] + texture_offset[1]) % 2
update_caption();
twist = 0;
}
function turn_em_round_finish() {
var x = texture_transform[2];
var y = texture_transform[3];
texture_transform[2] = texture_transform[0];
texture_transform[3] = texture_transform[1];
texture_transform[0] = -x;
texture_transform[1] = -y;
y = texture_offset[1];
texture_offset[1] = texture_offset[0];
texture_offset[0] = 1 - y;
update_caption();
turn = 0;
}
// motion
var movable = new Anima.Movable3DUpright();
window.movable = movable;
movable.moveReset = function () {
Anima.Movable3DUpright.prototype.moveReset.call(this);
this.scale = 1; // work around a bug in Movable3D
// shift view just slightly so the end ribbons start out visible
this.angle_xz = -0.25;
this.setRotation();
// reset puzzle
texture_transform[0] = texture_transform[3] = 1;
texture_transform[1] = texture_transform[2] = 0;
texture_offset[0] = texture_offset[1] = 0;
update_caption();
// request redraw
this.motionCallback();
};
movable.setRotation = function () {
// exclude a 0.4-radian range centered at pi
this.angle_xz = Math.min(Math.max(0.2, this.angle_xz + Math.PI), 2 * Math.PI - 0.2) - Math.PI;
return Anima.Movable3DUpright.prototype.setRotation.call(this);
};
movable.moveReset();
movable.bindKeyboard(window);
movable.bindTouch($(CANVAS));
// drawing stuff
var glc = new Anima.GL();
glc.setupCallback = function () {
var gl = this.gl;
gl.clearColor(0, 0, 0, 0);
var dumpling = createDumpling(20);
this.vertex_buffer = gl.createBuffer();
this.element_buffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, this.vertex_buffer);
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, this.element_buffer);
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(dumpling[0]), gl.STATIC_DRAW);
gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, new Uint16Array(dumpling[1]), gl.STATIC_DRAW);
this.num_tri_corners = dumpling[1].length;
this.handleResize();
};
var mv = new Float32Array([1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, -4, 1]);
var LIGHT_FROM = new Float32Array([1/3, 2/3, 2/3]);
glc.draw = function () {
var gl = glc.gl;
if (!gl) { return false; }
var draw_again = false;
// Update animations
if (twist > 0) {
draw_again = true;
twist += this.dt * TWIST_SPEED;
if (twist > 1) { twist_em_up_finish(); }
}
if (turn > 0) {
draw_again = true;
turn += this.dt * TURN_SPEED;
if (turn > Math.PI / 2) { turn_em_round_finish(); }
}
movable.update(this.dt);
draw_again = draw_again || movable.isMoving();
for (var i = 0; i < 3; ++i) {
mv[4*i] = movable.rotation[0][i];
mv[4*i+1] = movable.rotation[1][i];
mv[4*i+2] = movable.rotation[2][i];
}
mv[14] = -4 / movable.scale;
var prog = glc.shaders.main;
gl.useProgram(prog);
gl.enable(gl.DEPTH_TEST);
gl.depthFunc(gl.LEQUAL);
gl.bindFramebuffer(gl.FRAMEBUFFER, null);
gl.clear(gl.COLOR_BUFFER_BIT);
var pos = gl.getAttribLocation(prog, 'pos');
gl.enableVertexAttribArray(pos);
gl.vertexAttribPointer(pos, 4, gl.FLOAT, false, 0, 0);
gl.uniformMatrix4fv(gl.getUniformLocation(prog, 'projection'), false, glc.projection_matrix);
gl.uniformMatrix2fv(gl.getUniformLocation(prog, 'textureTransform'), false, new Float32Array(texture_transform));
gl.uniform1f(gl.getUniformLocation(prog, 'twist'), twist);
gl.uniform1f(gl.getUniformLocation(prog, 'turn'), turn);
gl.uniform2fv(gl.getUniformLocation(prog, 'half_offset'), texture_offset);
gl.uniform3fv(gl.getUniformLocation(prog, 'light_from'), LIGHT_FROM);
gl.uniformMatrix4fv(gl.getUniformLocation(prog, 'mv'), false, mv);
gl.drawElements(gl.TRIANGLES, glc.num_tri_corners, gl.UNSIGNED_SHORT, 0);
return draw_again;
};
glc.handleResize = function () {
var w = $(window).innerWidth();
var h = $(window).innerHeight();
var r = Math.min(w, h) * 2.5;
movable.units_per_px = -4 / r;
movable.touch_map.pan_x.amount = 4 / r;
movable.touch_map.pan_y.amount = 4 / r;
movable.touch_map.pinch.amount = 4 / r;
glc.setProjectionMatrix(w / r, h / r, 0.1, 100);
glc.resize(w, h);
};
glc.resizeCallback = function (w, h) {
frame_manager.requestFrame();
};
glc.addProgramByURLs('main', 'vert.c', 'frag.c', setupContinue);
var frame_manager = new Anima.FrameManager(glc.draw);
window.message_box = Umbra.MessageBox('messages');
function setupContinue() {
// attach Anima.GL to the canvas
try {
glc.bindCanvas($(CANVAS)[0]);
} catch (e) {
window.message_box.post('GL: ' + e, 'error');
glc.gl = null;
}
// attach to window resize events
$(window).on('resize', glc.handleResize);
// attach motion tracker to trigger animation management
// otherwise input will seem to have no effect
movable.motionCallback = function () {
frame_manager.requestFrame();
}
// draw first frame
frame_manager.requestFrame();
}
// More motion
$(window).on('mouseup touchcancel touchend mouseleave touchleave',
function (e) {
if (!e.relatedTarget) {
frame_manager.requestFrame();
}
});
// On canvas, suppress selection of hidden text boxes
$(CANVAS).on('mousedown selectstart', function (e) {
e.preventDefault();
});
function twist_em_up(e) {
e.preventDefault();
if (turn == 0 && twist == 0) {
twist = 1e-6;
frame_manager.requestFrame();
}
}
function turn_em_round(e) {
e.preventDefault();
if (turn == 0 && twist == 0) {
turn = 1e-6;
frame_manager.requestFrame();
}
}
$('#button_twist').on('click', twist_em_up);
$('#button_turn').on('click', turn_em_round);
function toggle_caption() {
if ($('#config_fraction')[0].checked) {
$('#caption_fraction').css({'display': 'inline-block'});
} else {
$('#caption_fraction').css({'display': 'none'});
}
if ($('#config_matrix')[0].checked) {
$('#caption_matrix').css({'display': 'inline-block'});
} else {
$('#caption_matrix').css({'display': 'none'});
}
}
toggle_caption();
$('#config_fraction').on('change', toggle_caption);
$('#config_matrix').on('change', toggle_caption);
$('#config_reset').on('click', function (e) {
movable.moveReset();
frame_manager.requestFrame();
});
$('#config_scramble').on('click', function (e) {
movable.moveReset();
twist_em_up_finish();
twist_em_up_finish();
var num_moves = 6 + Math.floor(1 / Math.max(1 / 10, Math.random()));
for (; num_moves > 0; --num_moves) {
if (Math.random() < 0.5) {
twist_em_up_finish();
} else {
turn_em_round_finish();
}
}
frame_manager.requestFrame();
});
});
</script>
</head>
<body>
<canvas id="main">
<img src="thumb.png" alt="Preview" />
This demo requires HTML5 Canvas support.
</canvas>
<div id="caption">
<span id="caption_fraction"></span>
<span id="caption_matrix"></span>
</div>
<div id="messages" class="messagebox"></div>
<div id="config" class="overlay"><div class="box">
<a class="button icon button_close" title="Close" href="#">×</a>
<h2>Options</h2>
<form method="post" action="#" id="form_config">
<ul>
<li><input type="checkbox" id="config_fraction" />
<label for="config_fraction">Show fraction</label></li>
<li><input type="checkbox" id="config_matrix" />
<label for="config_matrix">Show matrix in SL(2, <b>Z</b>)</label></li>
</ul>
<ul class="inline">
<li><button type="button" id="config_reset">Reset</button></li>
<li><button type="button" id="config_scramble">Scramble</button></li>
</ul>
</form>
</div></div>
<div id="help" class="overlay"><div class="box">
<a class="button icon button_close" title="Close" href="#">×</a>
<h2>Help</h2>
<p>You are looking at Conway's <em>rational tangles</em>: the tangles you could get
from two pieces of string by repeatedly picking two neighboring
ends to twist over each other.</p>
<p>But in this game, you have only two kinds of moves:</p>
<ul style="list-style-type: none; padding-left: 1em;">
<li><span class="icon" style="font-size: 100%;">⤮</span> / <span class="keyboard-key">X</span> — Twist 'em up!</li>
<li><span class="icon" style="font-size: 100%;">⟲</span> / <span class="keyboard-key">Z</span> — Turn 'em 'round!</li>
</ul>
<p>Get a friend to scramble it for you—or,
if your friends are social-distancing, check the
<a class="icon" style="font-size: 100%; text-decoration: none;"
href="#config">⚙</a> menu for an auto-scramble button.
Then try untangling it. Good luck!</p>
<nav><ul class="inline">
<li><a href="#help_spoilers">Spoilers</a></li>
<li><a href="#help_further">Further reading</a></li>
</ul></nav>
<p class="dedication">In memory of
<a href="https://en.wikipedia.org/wiki/John_Horton_Conway">
John Horton Conway (1937–2020)</a>,
dwelling in particular on the
<a href="https://www.mathcamp.org/">Canada/USA Mathcamp</a>
performance that this game is an adaptation of—during which
he wrapped the tangle in paper that he later tore open
with his teeth.</p>
<p class="byline">By
<a href="http://pteromys.melonisland.net/">Pteromys</a>,
released under the <a href="http://unlicense.org/">Unlicense</a>.</p>
</div></div>
<div id="help_spoilers" class="overlay"><div class="box">
<a class="button icon button_close" title="Close" href="#">×</a>
<hgroup>
<h2>Spoilers</h2>
<h3>ordered by increasing spoilage</h3>
</hgroup>
<ol>
<li>One of Conway's theorems was that two rational tangles are
<em>ambient isotopic</em> (can be turned into each other
without moving or passing the ends) if and only if they represent
the same fraction via some algorithm he devised.</li>
<li>I'm drawing these tangles constrained to a dumpling
surface, which dodges a lot of magic. In particular, it makes
the string's average slope a fraction.</li>
<li>By "slope", I really mean the number of times a string
flips over the top or bottom, divided by the number of
times it flips around the left and right sides.</li>
<li>What does each move do to the slope?</li>
<li>To see this in action, you can
<a href="#config">make it display fractions</a>.</li>
<li>Mathematicians should think about (a) what has the torus as a
double cover branched at the half-or-whole-integer points, and
(b) what SL(2, <b>Z</b>) does to it.</li>
</ol>
</div></div>
<div id="help_further" class="overlay"><div class="box">
<a class="button icon button_close" title="Close" href="#">×</a>
<h2>Further reading</h2>
<ul>
<li><a href="https://www.youtube.com/watch?v=URg-t3HZE5M">
Video of a class doing it with real string that's under no
obligation to stay on a dumpling surface.</a></li>
<li><a href="http://www.geometer.org/mathcircles/tangle.pdf">
Tom Davis's instructions for doing it in your own class with real string.</a></li>
<li><a href="http://homepages.math.uic.edu/~kauffman/RTang.pdf">
Goldman and Kauffman give a proof of Conway's theorem.</a></li>
<li><a href="https://www.reddit.com/r/math/comments/2gvesa/john_conways_rational_tangles/">
More sources gathered by u/gmsc.</a></li>
</ul>
</div></div>
<a class="button icon button_root"
href="#config" id="button_config" title="Settings">⚙</a>
<a class="button icon button_root" data-key="QUESTION"
href="#help" id="button_help" title="Help">?</a>
<a class="button icon button_root" data-key="X"
id="button_twist" title="Twist 'em up!">⤮</a>
<a class="button icon button_root" data-key="Z"
id="button_turn" title="Turn 'em 'round!">⟲</a>
</body>
</html>