-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
367 lines (321 loc) · 8.2 KB
/
script.js
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
var canvas = null;
var ctx = null;
var transform = {
x: 0,
y: 0,
sx: 1,
sy: 1,
};
var shiftOn = false;
var mousePos = {x: 0, y: 0}
var state = {
oldRope: [{x: 0, y: 600}, {x: 0, y: 600}, {x: 0, y: 600}],
rope: [{x: 0, y: 600}, {x: 0, y: 600}, {x: 0, y: 600}],
pegs: [{x: 200, y: 200}, {x: 600, y: 200}],
mode: INIT,
};
var original = state;
var reset = original;
var INIT = 1;
var PULL = 2;
var DEPEG = 3;
var FALL = 4;
function pull(x, y) {
state = {...original, mode: PULL, rope: [{x, y}, {x, y}]};
}
function twoPegs() {
let pegs = [{x: 200, y: 200}, {x: 600, y: 200}];
state = reset = original = {...original, pegs};
}
function threePegs() {
let pegs = [{x: 150, y: 200}, {x: 400, y: 250}, {x: 650, y: 200}];
state = reset = original = {...original, pegs};
}
function stopPull() {
let {rope} = state;
let x = rope[0].x;
let y = rope[0].y;
let last = rope[rope.length-1];
let dx = x - last.x;
let dy = y - last.y;
let n = ~~(Math.sqrt(dx*dx + dy*dy)/ropesize);
if (n < 1) {
return;
}
let newRope = rope.slice();
let oldRope = state.oldRope.slice();
for (let i=0; i<n; i++) {
newRope.push({x: last.x+i*dx/n, y: last.y+i*dy/n});
oldRope.push({x: last.x+i*dx/n, y: last.y+i*dy/n});
state = {...state, oldRope, rope: newRope};
}
state = {...state, mode: FALL};
reset = state;
}
function space() {
if (state.mode === PULL) {
stopPull();
} else if (state.mode > PULL) {
state = reset;
} else {
stopPull();
}
}
function clickPeg(i) {
if (state.mode === PULL) {
return
} else {
reset = state;
let {pegs} = state;
let newPegs = pegs.slice();
newPegs.splice(i, 1);
state = {...state, pegs: newPegs};
}
}
function clickMouse(x, y) {
let pegs = state.pegs;
for (let i =0; i < pegs.length; i++) {
let dx = pegs[i].x - x;
let dy = pegs[i].y - y;
if (dx*dx + dy*dy < pegsize2) {
clickPeg(i);
return;
}
}
if (state.mode === PULL) {
stopPull();
} else {
pull(x, y);
}
}
function moveMouse(x, y) {
let {mode, rope} = state;
if (mode !== PULL) {
return;
}
let last = rope[rope.length-1];
let dx = x - last.x;
let dy = y - last.y;
let n = ~~(Math.sqrt(dx*dx + dy*dy)/ropesize);
if (n < 1) {
return;
}
let newRope = rope.slice();
let oldRope = state.oldRope.slice();
for (let i=0; i <n; i++) {
newRope.push({x: last.x+i*dx/n, y: last.y+i*dy/n});
oldRope.push({x: last.x+i*dx/n, y: last.y+i*dy/n});
state = {...state, oldRope, rope: newRope};
}
}
function consume() {
for (let j=0; j<30; j++) {
let {rope} = state;
if (rope.length <= 1000) {
return;
}
let lowest = 0;
for (let i=0; i<rope.length; i++) {
if (rope[i].y > rope[lowest].y) {
lowest = i;
}
}
let newRope = rope.slice();
newRope.splice(lowest, 1);
let oldRope = state.oldRope.slice();
oldRope.splice(lowest, 1);
state = {...state, rope: newRope, oldRope};
}
}
window.addEventListener('load', main);
function onmousemove(e, cont) {
let rect = canvas.getBoundingClientRect();
let sx = canvas.width / rect.width;
let sy = canvas.height / rect.height;
let mx = ((e.clientX - rect.left) * sx - transform.x) / transform.sx;
let my = ((e.clientY - rect.top) * sy - transform.y) / transform.sy;
cont(mx, my);
}
function onkeyup(e) {
if (e.keyCode === 32) {
space();
} else if (e.keyCode === 50) {
twoPegs();
} else if (e.keyCode === 51) {
threePegs();
} else if (e.keyCode === 16) {
shiftOn = false;
}
}
function onkeydown(e) {
if (e.keyCode === 16) {
shiftOn = true;
}
}
function main() {
canvas = document.getElementById('root');
ctx = canvas.getContext('2d');
ctx.save();
window.addEventListener('mousemove', e => onmousemove(e, moveMouse));
window.addEventListener('click', e => onmousemove(e, clickMouse));
window.addEventListener('keyup', onkeyup);
window.addEventListener('keydown', onkeydown);
window.addEventListener('resize', resize);
resize();
requestAnimationFrame(loop);
}
function loop() {
requestAnimationFrame(loop);
if (state.mode > PULL) {
simtick();
}
draw();
}
function resize() {
ctx.restore();
ctx.save();
ctx.fillStyle = 'black';
ctx.fillRect(0, 0, canvas.width, canvas.height);
let sc = (canvas.offsetWidth * canvas.height) / (canvas.offsetHeight * canvas.width);
if (sc < 1) {
transform = {
x: 0,
y: (1 - sc)*canvas.height*0.5,
sx: 1,
sy: sc,
};
ctx.translate(0, transform.y);
ctx.scale(1, sc);
} else if (sc > 1) {
sc = 1/sc;
transform = {
x: (1 - sc)*canvas.width*0.5,
y: 0,
sx: sc,
sy: 1,
};
ctx.translate(transform.x, 0);
ctx.scale(sc, 1);
}
ctx.rect(0, 0, canvas.width, canvas.height);
ctx.clip();
draw();
}
function draw() {
let {pegs, rope} = state;
ctx.fillStyle = 'white';
ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.font = "20px Arial";
ctx.fillStyle = "black";
ctx.fillText("Goal: Wrap string so that removing either peg allows string to fall.", 0, 560);
ctx.fillText("Click: Draw string, remove peg. Space: Reset. Shift: Tighten. 2/3: Choose level.", 0, 590);
ctx.fillStyle = 'blue';
for (let i=0; i<pegs.length; i++){
ctx.beginPath();
ctx.arc(pegs[i].x, pegs[i].y, pegsize, 0, 2*Math.PI);
ctx.fill();
}
if (state.mode !== INIT) {
ctx.strokeStyle = 'red';
ctx.lineWidth = 4;
ctx.beginPath();
for (let i=0; i < rope.length; i++) {
ctx.lineTo(rope[i].x, rope[i].y);
}
if (state.mode > FALL) {
ctx.closePath();
}
ctx.stroke();
}
}
function simtick() {
if (shiftOn) {
consume();
}
integrate();
for (let i=0; i<iters; i++) {
constrain();
}
}
function update(c, o) {
if (state.mode > PULL) {
return {
x: c.x + keep*(c.x - o.x),
y: c.y + keep*(c.y - o.y) + grav,
};
} else {
return {
x: c.x + keep*(c.x - o.x),
y: c.y + keep*(c.y - o.y) + lowgrav,
};
}
}
function integrate() {
let {rope, oldRope} = state;
let newRope = [];
let i = 0;
if (state.mode <= FALL) {
newRope.push({...rope[0]});
i = 1;
}
for (; i < rope.length; i++) {
newRope.push(update(rope[i], oldRope[i]));
}
state = {
...state,
oldRope: rope,
rope: newRope,
};
}
function ropenodes(a, b) {
let d2 = dist2(a, b);
if (d2 <= ropesize2) {
return;
}
let d = Math.sqrt(d2);
let diff = d - ropesize;
let dx = a.x - b.x;
let dy = a.y - b.y;
let ddx = 0.5 * dx * diff / d;
let ddy = 0.5 * dy * diff / d;
a.x -= ddx;
a.y -= ddy;
b.x += ddx;
b.y += ddy;
}
function constrain() {
let {rope, pegs} = state;
let end = state.mode > PULL ? 0 : 1;
for (let i=rope.length-1; i > end; i--) {
ropenodes(rope[i], rope[i-1]);
}
if (state.mode > PULL) {
ropenodes(rope[0], rope[rope.length-1]);
}
for (let i=0; i < rope.length; i++) {
for (let j=0; j < pegs.length; j++) {
let d2 = dist2(rope[i], pegs[j]);
if (d2 >= pegsize2) {
continue;
}
dx = rope[i].x - pegs[j].x;
dy = rope[i].y - pegs[j].y;
d = Math.sqrt(d2);
rope[i].x = pegs[j].x + pegsize * dx / d;
rope[i].y = pegs[j].y + pegsize * dy / d;
}
}
}
function dist2(a, b) {
let x = a.x - b.x;
let y = a.y - b.y;
return x*x + y*y;
}
var iters = 100;
var grav = 0.3;
var lowgrav = 0;
var keep = 0.99;
var pegsize = 70;
var pegsize2 = pegsize * pegsize;
var ropesize = 1;
var ropesize2 = ropesize * ropesize;