-
Notifications
You must be signed in to change notification settings - Fork 2
/
jigsaw.js
358 lines (344 loc) · 10.9 KB
/
jigsaw.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
window.Jigsaw = (function () {
var SVG = 'http://www.w3.org/2000/svg';
var XLINK = 'http://www.w3.org/1999/xlink';
var doc = null;
var rootElt = null;
// Find an element by ID.
var getElt = function (idOrElt) {
if (idOrElt.getAttribute) {
return idOrElt;
}
return doc.getElementById(idOrElt);
}
// Create an element with the given name and attributes.
var mk = function (n, atts) {
var e = document.createElementNS(SVG, n);
if (atts) {
for (var i in atts) {
if (i == 'href') {
e.setAttributeNS(XLINK, 'href', atts[i]);
} else {
e.setAttribute(i, atts[i]);
}
}
}
return e;
}
// Create an element and attach it to an existing one.
var subelt = function (p, n, atts) {
e = mk(n, atts);
p.appendChild(e);
return e;
}
/*
var logElt;
var logTspans;
var logLineHt = 16;
var logMax = 600 / 16;
var log = function (text) {
if (logTspans == null) {
logElt = subelt(getElt('bg'), 'text', {
'font-height': logLineHt + 'px'
});
logTspans = [];
}
if (logTspans.length >= logMax) {
var e = logTspans.shift();
logElt.removeChild(e);
for (var i in logTspans) {
logTspans[i].setAttribute('y', (1 + i) * logLineHt);
}
}
var newElt = subelt(logElt, 'tspan', {
x: 0,
y: (1 + logTspans.length) * logLineHt
});
newElt.appendChild(doc.createTextNode(text));
logTspans.push(newElt);
}
*/
//
// Application specific
//
// Create the path for one bump on a jigsaw piece.
// sideHt -- total length of the side (height of the piece). May be -ve.
// isIn -- whether the bump turns intwards or points outwards
var hBump = function (sideWd, isIn) {
var bwd = .4 * sideWd;
var bht = .1 * sideWd * (!!isIn ^ (sideWd < 0) ? -1 : 1);
var cht = .5 * bht;
var cwd = cht * ((sideWd < 0) != (cht < 0) ? -1 : 1);
return [
'h' + (.5 * (sideWd - bwd)),
'q' + cwd + ',0 ' + cwd + ',' + cht,
't' + -cwd + ',' + cht,
-cwd + ',' + cht,
(cwd + .5 * bwd) + ',' + cht,
(cwd + .5 * bwd) + ',' + -cht,
-cwd + ',' + -cht,
-cwd + ',' + -cht,
cwd + ',' + -cht,
'h' + (.5 * (sideWd - bwd))
];
}
// Create the path for one bump on a jigsaw piece.
// sideHt -- total length of the side (height of the piece). May be -ve.
// isIn -- whether the bump turns intwards or points outwards
var vBump = function (sideHt, isIn) {
var bht = .4 * sideHt;
var bwd = .1 * sideHt * (!!isIn ^ (sideHt < 0) ? -1 : 1);
var cwd = .5 * bwd;
var cht = cwd * ((sideHt < 0) != (cwd < 0) ? -1 : 1);
return [
'v' + (.5 * (sideHt - bht)),
'q0,' + cht + ' ' + cwd + ',' + cht,
't' + cwd + ',' + -cht,
cwd + ',' + -cht,
cwd + ',' + (cht + .5 * bht),
-cwd + ',' + (cht + .5 * bht),
-cwd + ',' + -cht,
-cwd + ',' + -cht,
-cwd + ',' + cht,
'v' + (.5 * (sideHt - bht))
];
}
var mkPieceElts = function (u, imWd, imHt, nh, nv) {
var wd = imWd / nh;
var ht = imHt / nv;
var hunks = [];
var defsElt = getElt('defs');
var hunksElt = getElt('p');
var svgElt = hunksElt.ownerSVGElement;
var dwd, dht;
if (svgElt.getBoundingClientRect) {
// Firefox
var rc = svgElt.getBoundingClientRect();
dwd = rc.width;
dht = rc.height;
} else {
dwd = hunksElt.ownerSVGElement.width.animVal.value;
dht = hunksElt.ownerSVGElement.height.animVal.value;
}
var hash = function (i, j) {
return ((i * 37) ^ (j * 1009)) % 17;
};
var symbolElt = subelt(defsElt, 'symbol', {
id: 'im'
});
var imageElt = subelt(symbolElt, 'image', {
href: u,
width: imWd,
height: imHt
});
var pieceEltss = []; // 2-d array of hunks
var pieceElts = []; // simple list of hunks
for (var i = 0; i < nh; ++i) {
pieceEltss[i] = []
for (var j = 0; j < nv; ++j) {
var id = String.fromCharCode(97 + i) + String.fromCharCode(97 + j);
// Create path.
var ds = ['M0,0'];
if (j == 0) {
ds.push('h' + wd);
} else {
ds = ds.concat(hBump(wd, hash(i, j) & 1));
}
if (i == nh - 1) {
ds.push('v' + ht);
} else {
ds = ds.concat(vBump(ht, hash(i + 1, j) & 2));
}
if (j == nv - 1) {
ds.push('h' + -wd);
} else {
ds = ds.concat(0, hBump(-wd, hash(i, j + 1) & 1));
}
if (i == 0) {
ds.push('v' + -ht);
} else {
ds = ds.concat(0, vBump(-ht, hash(i, j) & 2));
}
ds.push('z');
d = ds.join(' ');
// Create pattern.
// The image is added using a pattern (a) so that it does
// not expand the bbox of the piece, and (b) because
// images are independently draggable in Safari
var patternElt = subelt(defsElt, 'pattern', {
id: 'p' + id,
patternUnits: 'userSpaceOnUse',
patternContentUnits: 'userSpaceOnUse',
patternTransform: 'translate(' + (-i * wd) + ',' + (-j * ht) + ')',
x: 0,
y: 0,
width: imWd,
height: imHt
});
var useElt = subelt(patternElt, 'use', {
href: '#im'
})
// Random starting position.
var x = Math.random() * (dwd - wd);
var y = Math.random() * (dht - ht);
/*
if (false && (i ^ j) & 1) {
// Non-random starting points to see that the hunks fit.
x = i * wd * 1.2 + 50;
y = j * ht * 1.2 + 40;
}
*/
// Create image with clip path.
var pieceElt = subelt(hunksElt, 'g', {
id: 'g' + id,
transform: 'translate(' + x + ',' + y + ')'
})
var pathElt = subelt(pieceElt, 'path', {
d: d,
fill: 'url(#p' + id + ')'
})
pieceEltss[i][j] = pieceElt;
pieceElts.push(pieceElt)
}
}
for (i = 0; i < nh; ++i) {
for (j = 0; j < nv; ++j) {
var adjs = pieceEltss[i][j].adjs = [];
if (i > 0) {
adjs.push({elt: pieceEltss[i - 1][j], dx: -wd});
}
if (i + 1 < nh) {
adjs.push({elt: pieceEltss[i + 1][j], dx: wd});
}
if (j > 0) {
adjs.push({elt: pieceEltss[i][j - 1], dy: -ht});
}
if (j + 1 < nv) {
adjs.push({elt: pieceEltss[i][j + 1], dy: ht});
}
}
}
return pieceElts;
}
var getPt = function (elt) {
var m = elt.getCTM();
return {x: m.e, y: m.f};
}
var mkJigsaw = function (bgElt, pieceElts) {
bgElt = getElt(bgElt);
var hunks = [];
for (var i in pieceElts) {
hunks.push(pieceElts[i].hunk = {i: i, elts: [pieceElts[i]]});
}
var isDrag = false;
var dragHunk = null;
var dragStartX = null;
var dragStartY = null;
var startX = null;
var startY = null;
bgElt.addEventListener('mousemove', function (evt) {
if (isDrag) {
var shiftX = evt.clientX - dragStartX;
var shiftY = evt.clientY - dragStartY;
for (var j in dragHunk.elts) {
var e = dragHunk.elts[j];
var t = 'translate(' + (e.startX + shiftX) + ',' + (e.startY + shiftY) + ')';
e.setAttribute('transform', t);
}
}
}, false);
bgElt.addEventListener('mouseup', function (evt) {
if (isDrag) {
// Is thus chunk close enough to other chunks to snap together?
for (var i = 0; i < dragHunk.elts.length; ++i) {
var dragElt = dragHunk.elts[i];
var dragPt = getPt(dragElt);
// Check each adjacent piece to this one.
for (var j in dragElt.adjs) {
var adj = dragElt.adjs[j];
if (adj.elt.hunk != dragHunk) { // Must be in a different hunk.
var adjPt = getPt(adj.elt);
if (adj.dx) {
adjPt.x -= adj.dx;
}
if (adj.dy) {
adjPt.y -= adj.dy;
}
var dSquared = Math.pow(dragPt.x - adjPt.x, 2) + Math.pow(dragPt.y - adjPt.y, 2);
if (dSquared < 25) { // Must be close to the dragged piece.
/*
log('snap');
*/
// Adjust the position of the dragged piece to close the gap.
var dx = adjPt.x - dragPt.x;
var dy = adjPt.y - dragPt.y;
for (var k in dragHunk.elts) {
var ee = dragHunk.elts[k];
var pt = getPt(ee);
var t = 'translate(' + (pt.x + dx) + ',' + (pt.y + dy) + ')';
ee.setAttribute('transform', t);
}
dragPt = getPt(dragElt);
// Attach the elements of the absorbed chunk to this one.
var absorbedElts = adj.elt.hunk.elts;
dragHunk.elts = dragHunk.elts.concat(absorbedElts);
for (var k in absorbedElts) {
absorbedElts[k].hunk = dragHunk;
}
}
}
}
}
/*
log('Hunk size: ' + dragHunk.elts.length);
*/
dragHunk = null;
isDrag = false;
}
}, false);
for (var k in hunks) {
var hunk = hunks[k];
for (var eltID in hunk.elts) {
var pieceElt = hunk.elts[eltID];
pieceElt.addEventListener('mousedown', function (evt) {
dragHunk = evt.currentTarget.hunk;
isDrag = true;
dragStartX = evt.clientX;
dragStartY = evt.clientY;
for (var j in dragHunk.elts) {
var e = dragHunk.elts[j];
var m = e.getCTM();
e.startX = m.e;
e.startY = m.f;
e.parentNode.appendChild(e);
}
}, false);
}
}
}
return {
// Called from main SVG element when document loaded.
init: function (evt) {
doc = evt.target.ownerDocument;
rootElt = doc.documentElement;
var s = location.search;
var args = {
u: 'http://farm5.static.flickr.com/4077/4871527376_35120786b3_z.jpg',
wd: 640,
ht: 512,
nh: 8,
nv: 7
};
if (s) {
var ps = s.slice(1).split('&');
for (var i in ps) {
var pos = ps[i].indexOf('=');
var k = ps[i].slice(0, pos);
var v = unescape(ps[i].slice(pos + 1));
args[k] = v;
}
}
var pieceElts = mkPieceElts(args.u, args.wd, args.ht, args.nh, args.nv);
mkJigsaw('p', pieceElts);
}
};
})();