-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsvgtest.html
More file actions
474 lines (441 loc) · 15.1 KB
/
svgtest.html
File metadata and controls
474 lines (441 loc) · 15.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
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
<!DOCTYPE HTML>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Okami Map - Test</title>
<meta name="description" content="Map of treasure, clovers, etc. in Okami">
<meta name="author" content="Daniel Shepsis">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!--Favicon.io-->
<link rel="apple-touch-icon" sizes="180x180" href="./apple-touch-icon.png">
<link rel="icon" type="image/png" sizes="32x32" href="./favicon-32x32.png">
<link rel="icon" type="image/png" sizes="16x16" href="./favicon-16x16.png">
<link rel="manifest" href="./site.webmanifest">
<style>
* {
font: 20px/1.5 sans-serif;
color: #222;
box-sizing: border-box;
}
html {
background: #f8f8f8;
height:100%;
margin: 0;
}
body {
margin: 0;
height: 100%;
}
svg {
height: 500px;
width: 500px;
margin: 0;
display: block;
border: 5px dotted black;
}
svg.dragging {
cursor: all-scroll;
}
</style>
</head>
<body>
<svg tabindex="0" viewBox="0 0 10 10" xmlns="http://www.w3.org/2000/svg">
<g id="matrix-group">
<rect fill="#f0f3" x="0" y="0" width="10" height="10" />
<rect fill="#ff03" stroke="green" stroke-width="0.2" x="-5" y="-5" width="16" height="16" />
<circle cx="5" cy="5" r="5"/>
<circle fill="red" cx="5" cy="3" r="3"/>
<rect fill="orange" x="2.5" y = "2.5" width="2.5" height="2.5" />
<rect fill="purple" x="5" y = "5" width="2.5" height="2.5" />
</g>
</svg>
<script>
//Credit http://www.petercollingridge.co.uk/tutorials/svg/interactive/dragging/
function getMousePosition(ev, svg) {
const CTM = svg.getScreenCTM();
return {
x: (ev.clientX - CTM.e) / CTM.a,
y: (ev.clientY - CTM.f) / CTM.d
};
}
/* Normalize bounds so that they are defined by a top-left and bottom-right
* corner: (lx, ly) and (rx, ry) respectively. */
function normalizeAndValidateBounds(bounds) {
if (bounds === null) return {
lx: -Infinity,
rx: Infinity,
width: Infinity,
ly: -Infinity,
ry: Infinity,
height: Infinity
};
if (bounds.lx === undefined) bounds.lx = bounds.x;
if (bounds.ly === undefined) bounds.ly = bounds.y;
/* Checks the bounds in a single dimension: */
function validateInterval(bounds, lName, rName, wName) {
const lb = bounds[lName];
if (lb === Infinity) throw new Error(
`${lName} bound may be -Infinity, but must not be +Infinity.`
);
const rb = bounds[rName];
if (rb === -Infinity) throw new Error(
`${rName} bound may be +Infinity, but must not be -Infinity.`
);
const width = bounds[wName];
if (width === -Infinity) throw new Error(
`${wName} bound may be +Infinity, but must not be -Infinity.`
);
if (isFinite(width)) {
if (lb === -Infinity || rb === Infinity) {
throw new Error(
`${wName} cannot by finite if ${lName} or ${rName} are infinite.`
)
}
if (isFinite(lb)) {
if (isFinite(rb)) {
/* Defining all 3 bounds is okay if they agree exactly: */
if (rb !== width + lb) throw new Error(
`${lName}, ${wName}, and ${rName} are all finite, but do not agree.`
);
} else {
bounds[rName] = lb + width;
}
} else {
if (!isFinite(rb)) throw new Error(
`If ${wName} is finite, ${lName} or ${rName} must be defined.`
);
bounds[lName] = rb - width;
}
} else {
const lFinite = isFinite(lb);
const rFinite = isFinite(rb);
if (lFinite && rFinite) {
if (width === Infinity) throw new Error(
`${wName} cannot be Infinity if ${lName} and ${rName} are finite.`
);
bounds[wName] = rb - lb;
} else {
/* If width is undefined, and lb or rb (or both) are undefined,
* width is effectively infinite */
bounds[wName] = Infinity;
/* Set undefined bounds as infinite: */
bounds[lName] = lFinite ? lb : -Infinity;
bounds[rName] = rFinite ? rb : Infinity;
}
}
}
validateInterval(bounds, 'lx', 'rx', 'width');
validateInterval(bounds, 'ly', 'ry', 'height');
return bounds;
}
/**
* Takes a given <g> element within a parent SVG element, and makes the parent
* interactive, such that clicking/tapping and dragging within the parent
* applies transforms to the <g> to simulate panning around within the given
* bounds (or with no bounds, if they're not given), and scrolling or pinching/ * stretching zooms in and out about the pointer(s).
*
* The expected use is to wrap all the contents of an <svg> within a <g>, so
* that the
*/
var movTemp;
function makeSVGExplorable({
g, //Required. The <g> element to apply transforms to.
svg = g.ownerSVGElement, //Optional. Captures pan/zoomevents
bounds = svg.getBBox(), //Optional. Use null for no bounds.
minScale = 0.01, //Optional. Use 0 for no minimum.
maxScale = 100, //Optional. Use Infinity for no maximum.
} = {}) {
normalizeAndValidateBounds(bounds);
const transform = svg.createSVGTransform();
g.transform.baseVal.insertItemBefore(transform, 0);
const tMat = transform.matrix;
const initViewBox = svg.viewBox.baseVal;
/**
* The effective position and dimensions of the viewbox, relative to the
* initial svg frame.
*/
function effectiveViewBox() {
return {
x: initViewBox.x - tMat.e / tMat.a,
y: initViewBox.y - tMat.f / tMat.d,
width: initViewBox.width / tMat.a,
height: initViewBox.height / tMat.d
};
// right bounds: x + width <= bounds.lx + bounds.width
// initViewBox.x - tMat.e / tMat.a + initViewBox.width / tMat.a <= bounds.lx + bounds.width
// tMat.e / tMat.a >= initViewBox.x + initViewBox.width / tMat.a - bounds.lx - bounds.width
// tMat.e >= (initViewBox.x - bounds.lx - bounds.width) * tMat.a + initViewBox.width
// left bounds: x >= bounds.lx
// initViewBox.x - tMat.e / tMat.a >= bounds.lx
// tMat.e / tMat.a <= initViewBox.x - bounds.lx
// tMat.e <= (initViewBox.x - bounds.lx) * tMat.a
}
gls = svg;
function clamp(n, min, max) {
if (min > max) {
let temp = min;
min = max;
max = temp;
// throw new Error(`Minimum ${min} greater than maximum ${max}.`);
}
if (n < min) return min;
if (n > max) return max;
return n;
}
function translationBounds(dimName, scaleName, wName) {
const leftBound = bounds['l' + dimName];
const rightBound = bounds['r' + dimName];
if (leftBound === -Infinity && rightBound === Infinity) {
return [-Infinity, Infinity];
}
let minTranslate, maxTranslate;
const viewXY = initViewBox[dimName];
const curScale = tMat[scaleName];
const viewWH = initViewBox[wName];
if (rightBound === Infinity) minTranslate = -Infinity;
else minTranslate = (viewXY - rightBound) * curScale + viewWH;
if (leftBound === -Infinity) maxTranslate = Infinity;
else maxTranslate = (viewXY - leftBound) * curScale
return [minTranslate, maxTranslate];
}
/**
* Translates <g> such that the effectiveViewBox has the given x and y (top
* left corner). Obeys the given bounds.
*/
function panAbsolute({x = -tMat.e, y = -tMat.f} = {}) {
if (bounds === null) {
tMat.e = -x;
tMat.f = -y;
return;
}
tMat.e = clamp(-x, ...translationBounds('x', 'a', 'width'));
tMat.f = clamp(-y, ...translationBounds('y', 'd', 'height'));
}
/**
* Translates <g> such that the effectiveViewBox is moved dx to the right and * dy downward. Obeys the given bounds.
*/
function panRelative({dx = 0, dy = 0} = {}) {
if (bounds === null) {
tMat.e = -x;
tMat.f = -y;
return;
}
tMat.e = clamp(tMat.e - dx, ...translationBounds('x', 'a', 'width'));
tMat.f = clamp(tMat.f - dy, ...translationBounds('y', 'd', 'height'));
}
function zoomRelative({scaleMul, cx, cy}) {
if (isNaN(scaleMul)) {
throw new Error(`scaleMul must be a number. Instead, found ${scaleMul}.`);
}
let minAD, maxAD;
if (tMat.a < tMat.d) {
minAD = tMat.a;
maxAD = tMat.d;
} else {
minAD = tMat.d;
maxAD = tMat.a;
}
scaleMul = clamp(scaleMul, minScale / maxAD, maxScale / minAD);
tMat.a *= scaleMul;
tMat.d *= scaleMul;
//I don't actually know if this works with different a and d
tMat.e = (1 - scaleMul) * cx + scaleMul * tMat.e;
tMat.f = (1 - scaleMul) * cy + scaleMul * tMat.f;
}
function zoomAbsolute({scale, cx, cy}) {
scale = clamp(scale, minScale, maxScale);
//I don't actually know if this works with different a and d
const scaleMulX = scale / tMat.a;
const scaleMulY = scale / tMat.d;
tMat.a = tMat.d = scale;
tMat.e = (1 - scaleMulX) * cx + scaleMulX * tMat.e;
tMat.f = (1 - scaleMulY) * cy + scaleMulY * tMat.f;
}
let dragging = false;
window.dragStart = null;
// window.movTemp = dragStart;
const pointers = new Map();
window.pss = pointers;
function pointPos(index) {
const values = Array.from(pointers.values());
const pointer = values[index];
return getMousePosition(pointer, svg);
}
function downHandler(ev) {
dragging = true;
pointers.set(ev.pointerId, ev);
if (pointers.size === 1) {
dragStart = pointPos(0);
dragStart.x -= tMat.e;
dragStart.y -= tMat.f;
} else if (pointers.size === 2) {
const [pointA, pointB] = [pointPos(0), pointPos(1)];
dragStart = Object.create(null);
/* The transform origin becomes the middle of the two pointers: */
dragStart.x = (pointA.x + pointB.x) / 2 - tMat.e;
dragStart.y = (pointA.y + pointB.y) / 2 - tMat.f;
dragStart.dist = Math.hypot(pointA.x - pointB.x, pointA.y - pointB.y);
dragStart.initZoom = tMat.a; //TODO: Figure out whether we're going to handle different zoom in x and y (a and d)
} else {
//TODO FIXME HELP: IDK what do for 3 or more fingers!!!
console.warn("Sorry buddy idk what to do for 3+ fingers.")
}
}
function upHandler(ev) {
pointers.delete(ev.pointerId);
if (pointers.size === 0) {
dragging = false;
svg.classList.remove('dragging');
} else if (pointers.size === 1) { //TODO Fix this being duplicated from above
const point = pointPos(0);
dragStart = Object.create(null);
dragStart.x = point.x - tMat.e;
dragStart.y = point.y - tMat.f;
} else if (pointers.size === 2) {
// console.warn("UP");
// console.log(uptemp = pointers);
const [pointA, pointB] = [pointPos(0), pointPos(1)];
dragStart = Object.create(null);
/* The transform origin becomes the middle of the two pointers: */
dragStart.x = (pointA.x + pointB.x) / 2 - tMat.e;
dragStart.y = (pointA.y + pointB.y) / 2 - tMat.f;
// console.log(dragStart);
dragStart.dist = Math.hypot(pointA.x - pointB.x, pointA.y - pointB.y);
dragStart.initZoom = tMat.a; //TODO: Figure out whether we're going to handle different zoom in x and y (a and d)
} else {
//TODO FIXME HELP: IDK what do for 3 or more fingers!!!
console.warn("Sorry buddy idk what to do for 3+ fingers.")
}
}
/* For touch screens, disable the browser's handling of touching so we can
* implement our own behavior: */
svg.style.touchAction = 'none';
svg.addEventListener('pointerdown', downHandler);
//Attach to window so that the pointer is followed even outside the <svg>
window.addEventListener('pointerup', upHandler);
window.addEventListener('pointermove', ev=>{
if (dragging === false) return;
svg.classList.add('dragging');
pointers.set(ev.pointerId, ev);
if (pointers.size === 1) {
const dragCurrent = pointPos(0);
panAbsolute({
x: dragStart.x - dragCurrent.x,
y: dragStart.y - dragCurrent.y
});
} else if (pointers.size === 2) {
// console.warn("MOVING");
const [pointA, pointB] = [pointPos(0), pointPos(1)];
const dragCurrent = Object.create(null);
/* The transform origin becomes the middle of the two pointers: */
dragCurrent.x = (pointA.x + pointB.x) / 2; //- tMat.e;
dragCurrent.y = (pointA.y + pointB.y) / 2; //- tMat.f;
// console.warn("changes");
dragCurrent.dist = Math.hypot(pointA.x - pointB.x, pointA.y - pointB.y);
// movTemp = ev.x;
// console.log(dragCurrent);
zoomAbsolute({
scale: dragCurrent.dist / dragStart.dist * dragStart.initZoom,
cx: dragCurrent.x,
cy: dragCurrent.y
});
// panAbsolute({
// x: dragStart.x - dragCurrent.x,
// y: dragStart.y - dragCurrent.y
// });
} else {
//TODO FIXME HELP: IDK what do for 3 or more fingers!!!
console.warn("Sorry buddy idk what to do for 3+ fingers.")
}
});
const USE_FIXED_SCALE = false;
svg.addEventListener('wheel', ev=>{
ev.preventDefault();
const factor = 0.1 * ev.deltaY;
/* Calculate the scale such that scrolling up and down by the same number
* of notches gives you the same zoom every time. */
const scale = (factor > 0) ? (1 - factor) : (1 / (1 + factor));
const scaleCenter = getMousePosition(ev, svg);
zoomRelative({scaleMul: scale, cx: scaleCenter.x, cy: scaleCenter.y});
});
(()=>{
const DBL_ZOOM = 2;
let dblClickToggle = false;
svg.addEventListener('dblclick', ev=>{
const scaleCenter = getMousePosition(ev, svg);
const scaleMul = dblClickToggle ? 1 / DBL_ZOOM : DBL_ZOOM;
dblClickToggle = !dblClickToggle;
zoomRelative({scaleMul, cx: scaleCenter.x, cy: scaleCenter.y});
});
})();
svg.addEventListener('keydown', ev=>{
if (ev.ctrlKey || ev.metaKey || ev.altKey) return;
// ev.preventDefault();
const KEY_ZOOM = 1.5;
const KEY_PAN_FACTOR = 10;
const vbox = initViewBox;
let preventDefault = true;
switch (ev.key) {
case "w": case "W":
case "ArrowUp":
panRelative({dy: -vbox.height / KEY_PAN_FACTOR});
break;
case "s": case "S":
case "ArrowDown":
panRelative({dy: vbox.height / KEY_PAN_FACTOR});
break;
case "a": case "A":
case "ArrowLeft":
panRelative({dx: -vbox.width / KEY_PAN_FACTOR});
break;
case "d": case "D":
case "ArrowRight":
panRelative({dx: vbox.width / KEY_PAN_FACTOR});
break;
case "e": case "E":
case "=": case "+":
zoomRelative({
scaleMul: KEY_ZOOM,
cx: vbox.x + vbox.width / 2,
cy: vbox.y + vbox.height / 2,
});
break;
case "q": case "Q":
case "-": case "_":
zoomRelative({
scaleMul: 1 / KEY_ZOOM,
cx: vbox.x + vbox.width / 2,
cy: vbox.y + vbox.height / 2,
});
break;
case " ":
case "Home":
zoomAbsolute({
scale: 1,
cx: 0,
cy: 0,
});
panAbsolute({x: 0, y: 0});
break
default:
preventDefault = false;
}
if (preventDefault) ev.preventDefault();
})
return({
g, svg, bounds,
effectiveViewBox,
panRelative, panAbsolute,
zoomRelative, zoomAbsolute
});
}
const g = document.getElementById('matrix-group');
const ext = makeSVGExplorable({
g,
// bounds:{lx: -5, ly:-5, rx: 11, height: 16},
minScale: 0.1,
maxScale: 10
});
</script>
</body>
</html>