-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtouch-transform.js
More file actions
580 lines (501 loc) · 17.1 KB
/
touch-transform.js
File metadata and controls
580 lines (501 loc) · 17.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
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
/**
* @license MIT (See LICENSE file)
* @author Elias Toft Hansen
* @see https://github.com/EJTH/touch-translate
*/
(function(){
var TouchTransform = function(){
// List of ongoing touches
var ongoingTouches = [];
// List of touches last animation tick.
var lastTouches = [];
// Previous mouse position.
var lastMouse = null;
// Current mouse position.
var mousePos = null;
// Current mouse rotation (scroll).
var mouseRot = 0;
// Current mouse scale (shift+scroll).
var mouseScale = 1;
var mouseScaleFactor = 0;
// Attached elements acting as controls.
var attachedElements = [];
// Element being manipulated currently.
var currentElement;
// Input state from previous animation tick.
var lastState = {};
// Is current input touch based?
var isTouch = false;
// Key defaults
defaultRotateKey = 'Shift';
defaultScaleKey = false;
// scroll wheel mode
var scrollMode = '';
/**
* Handle start of touch drag.
* @param {*} evt
*/
function handleTouchStart(evt) {
endTransform();
ongoingTouches.length = 0;
var touches = evt.touches;
for (var i = 0; i < touches.length; i++) {
ongoingTouches.push(copyTouch(touches[i]));
}
if(ongoingTouches.length > 0 && !currentElement){
var point = getMedianPoint();
var element = document.elementFromPoint(point.clientX, point.clientY);
if(startMultiTouchTransform(element)){
evt.preventDefault();
};
}
}
/**
* Handle start of mouse drag.
* @param {*} evt
*/
function handleMouseDown(evt){
var point = evt;
lastMouse = evt;
mousePos = {clientX: evt.clientX, clientY: evt.clientY};
if(!currentElement){
var element = document.elementFromPoint(point.clientX, point.clientY);
if(startMouseTransform(element)){
evt.preventDefault();
}
}
}
/**
* Handle mouse up event
* @param {*} evt
*/
function handleMouseUp(evt){
endTransform();
}
/**
* Handle mouse movement. Update lastMouse & mousePos
* @param {*} evt
*/
function handleMouseMove(evt){
lastMouse = mousePos;
mousePos = {clientX: evt.clientX, clientY: evt.clientY};
}
/**
* Handle keyboard interaction (shift key state)
* @param {*} evt
*/
function handleKeyDown(evt){
if(!currentElement) return;
if(evt.key == currentElement.__touchTransformOptions.rotateKey){
scrollMode = 'rotate';
} else
if(evt.key == currentElement.__touchTransformOptions.scaleKey){
scrollMode = 'scale';
} else {
scrollMode = getDefaultScrollMode();
}
}
/**
* Handle keyboard interaction (shift key state)
* @param {*} evt
*/
function handleKeyUp(evt){
if(!currentElement) return;
if(evt.key == currentElement.__touchTransformOptions.rotateKey){
scrollMode = '';
}
if(evt.key == currentElement.__touchTransformOptions.scaleKey){
scrollMode = '';
}
if(!scrollMode){
scrollMode = getDefaultScrollMode();
}
}
function getDefaultScrollMode(){
if(currentElement){
if(currentElement.__touchTransformOptions.scaleKey === false) return 'scale';
if(currentElement.__touchTransformOptions.rotateKey === false) return 'rotate';
}
return '';
}
/**
* Handle mouse wheel events (zoom or rotate)
* @param {*} evt
*/
function handleWheel(evt){
if(!currentElement){
return;
}
evt.preventDefault();
evt.stopPropagation();
if(scrollMode == 'rotate'){
mouseRot += Math.sign(evt.deltaY);
}
if(scrollMode == 'scale'){
mouseScaleFactor += (Math.sign(evt.deltaY) * ((mouseScaleFactor * mouseScaleFactor) + 0.005));
}
}
/**
* Handles touch move events. Keeps track of active touch list.
* @param {*} evt
*/
function handleTouchMove(evt) {
var touches = evt.changedTouches;
for (var i = 0; i < touches.length; i++) {
var idx = ongoingTouchIndexById(touches[i].identifier);
if (idx >= 0) {
ongoingTouches.splice(idx, 1, copyTouch(touches[i])); // swap in the new touch record
}
}
if(currentElement) evt.preventDefault();
}
/**
* Handle end of touch events. Updates touch lists.
* @param {*} evt
*/
function handleTouchEnd(evt) {
var touches = evt.changedTouches;
for (var i = 0; i < touches.length; i++) {
var idx = ongoingTouchIndexById(touches[i].identifier);
if (idx >= 0) {
ongoingTouches.splice(idx, 1); // remove it; we're done
}
}
if(ongoingTouches.length < 2) endTransform();
}
/**
* Handle cancel events. Basically same as end.
* @param {*} evt
*/
function handleTouchCancel(evt) {
handleTouchEnd(evt);
}
/**
* Helper function for making a copy of the neccesary parts of touch events.
* @param {*} param0
*/
function copyTouch({ identifier, clientX, clientY }) {
return { identifier, clientX, clientY };
}
/**
* Find ongoing touch index by its ID. Returns -1 if no touch point is found.
* @param {*} idToFind
*/
function ongoingTouchIndexById(idToFind) {
for (var i = 0; i < ongoingTouches.length; i++) {
var id = ongoingTouches[i].identifier;
if (id == idToFind) {
return i;
}
}
return -1;
}
/**
* Begin mouse based transform interaction
* @param {*} element
*/
function startMouseTransform(element){
isTouch = false;
while(element){
if(attachedElements.indexOf(element) > -1){
break;
}
element = element.parentElement;
}
if(!element) return;
var target = element.__touchTransformOptions.target;
if(!target.__touchTransformInfo){
resetTransformInfo(target);
}
lastState = getMouseTransformState();
currentElement = element;
if(!scrollMode) scrollMode = getDefaultScrollMode();
return true;
}
/**
* Begin touch based transform interaction
* @param {*} element
*/
function startMultiTouchTransform(element){
while(element){
if(attachedElements.indexOf(element) > -1){
break;
}
element = element.parentElement;
}
if(!element) return;
lastTouches = JSON.parse(JSON.stringify(ongoingTouches));
var options = element.__touchTransformOptions;
var target = options.target;
// Cancel unless single touch mode is on
if(!options.singleTouch && ongoingTouches.length < 2) return;
if(!target.__touchTransformInfo){
resetTransformInfo(target);
}
lastState = getTouchTransformState();
currentElement = element;
isTouch = true;
return true;
}
/**
* Animation frame function for updating active transform
*/
function updateTransforms(){
requestAnimationFrame(updateTransforms);
if(!currentElement) return;
try {
var opt = currentElement.__touchTransformOptions;
var state = isTouch ? getTouchTransformState() : getMouseTransformState();
applyCSSTransform(opt.target, state);
if(opt.onUpdate){
var css = buildCSSTransform(opt.target.__touchTransformInfo);
opt.onUpdate(css, opt.target.__touchTransformInfo, ongoingTouches);
}
lastState = state;
lastTouches = JSON.parse(JSON.stringify(ongoingTouches));
} catch(e){
console.error(e);
}
}
/**
* Applies CSS3 transform to the intended target element based on the current and past input states.
* @param {*} target
* @param {*} state
*/
function applyCSSTransform(target, state){
var targetInfo = target.__touchTransformInfo;
var pos = {
x: targetInfo.translation.x + state.pos.clientX - lastState.pos.clientX ,
y: targetInfo.translation.y + state.pos.clientY - lastState.pos.clientY
};
var rotation = targetInfo.rotation + state.rot - lastState.rot;
var scale = state.dist && lastState.dist ? targetInfo.scale * (state.dist / lastState.dist) : targetInfo.scale;
// clamp scale
var min = currentElement.__touchTransformOptions.minScale || 0.1;
var max = currentElement.__touchTransformOptions.maxScale || 100;
scale = Math.max(min, Math.min(max, scale));
target.__touchTransformInfo = {
translation: pos,
rotation: rotation,
scale: scale
};
target.style.transform = buildCSSTransform(target.__touchTransformInfo);
}
function buildCSSTransform(transform){
return `translate(${transform.translation.x}px,${transform.translation.y}px) rotate(${transform.rotation}deg) scale(${transform.scale})`;
}
/**
* Get input state for mouse interaction
*/
function getMouseTransformState(){
if(mouseScaleFactor != 0){
mouseScale += mouseScale * mouseScaleFactor;
mouseScaleFactor *= 0.5;
}
return {
rot: mouseRot,
pos: mousePos,
dist: mouseScale
}
}
/**
* Get input state for touch interaction
*/
function getTouchTransformState(){
return {
rot: getTouchRotation(),
dist: getTouchMedianDistance(),
pos: getMedianPoint()
};
}
/**
* Ends transform.
*/
function endTransform(){
isTouch = false;
currentElement = null;
lastTouches = [];
}
/**
* Gets distance between points x1,y1 & x2,y2
* @param {*} x1
* @param {*} y1
* @param {*} x2
* @param {*} y2
*/
function getDistance(x1,y1,x2,y2){
var a = x1 - x2;
var b = y1 - y2;
return Math.hypot(a, b);
}
/**
* Gets median distance between ongoingTouches
*/
function getTouchMedianDistance(){
var middle = getMedianPoint();
var dist = 0;
if(ongoingTouches.length == 1) return 0;
ongoingTouches.forEach(touch => {
dist += getDistance(middle.clientX, middle.clientY, touch.clientX, touch.clientY);
});
return dist / ongoingTouches.length;
}
/**
* Retrieves current rotation state from touch interaction
*/
function getTouchRotation(){
if(ongoingTouches.length == 1) return 0;
var lastTouch = lastTouches[0];
var touch = ongoingTouches[ongoingTouchIndexById(lastTouch.identifier)];
if(!touch) endTransform();
var middle = getMedianPoint(ongoingTouches);
return findAngle(lastTouch, middle, touch);
}
/**
* Finds angle between points a, b and c (c=[b.x+1,b.y])
* @param {*} a
* @param {*} b
*/
function findAngle(a,b) {
var c = {
clientX: b.clientX+1,
clientY: b.clientY
}
var atanA = Math.atan2(a.clientX - b.clientX, a.clientY - b.clientY);
var atanC = Math.atan2(c.clientX - b.clientX, c.clientY - b.clientY);
var diff = atanC - atanA;
diff *= 180 / Math.PI;
return diff;
}
/**
* Find Median point between points in touchList.
* @param {*} touchList Uses ongoing touches if not set.
*/
function getMedianPoint(touchList){
touchList = touchList || ongoingTouches;
var x = 0;
var y = 0;
touchList.forEach(touch => {
x += touch.clientX;
y += touch.clientY;
});
x = x / touchList.length;
y = y / touchList.length;
return {clientX: x, clientY: y};
}
/**
* Reset target elements internal state.
* This function is used to initialize the state in start drag functions
* and used to reset transform entirely through the TouchTransform.reset() function.
* @param {*} target Target element
*/
function resetTransformInfo(target){
if(!target.__touchTransformOrigin){
target.__touchTransformOrigin = parseTransform(target.style.transform, {
translation: {x:0,y:0},
rotation: 0,
scale: 1
});
}
target.__touchTransformInfo = target.__touchTransformOrigin;
target.style = buildCSSTransform(target.__touchTransformOrigin);
}
/**
* Parse CSS transform string into a useful object.
* @param {*} transformStr
* @param {*} defaults
*/
function parseTransform(transformStr, defaults){
var parsed = JSON.parse(JSON.stringify(defaults));
var reg = /(translate|rotate|scale)\(([^)]+)\)/gi
var arr = [...transformStr.matchAll(reg)];
arr.forEach(transform => {
var op = transform[1];
var val = transform[2];
if(op == 'translate'){
var xy = val.split(',');
val = {
x : parseFloat(xy[0]),
y : parseFloat(xy[1])
}
} else {
val = parseFloat(val);
}
parsed[op] = val
});
return parsed;
}
/**
* Returns a skeleton object to be used as a dummy element if target is explicitly set to false.
*/
function getDummyElement(){
return {
style: {
transform: ""
}
}
}
// Attach touch event handlers.
window.addEventListener('touchstart', handleTouchStart, { passive:false });
window.addEventListener('touchmove', handleTouchMove, { passive:false });
window.addEventListener('touchend', handleTouchEnd, false);
window.addEventListener('touchcancel', handleTouchCancel, false);
// Attach mouse event Handlers.
window.addEventListener('mousedown', handleMouseDown, { passive:false });
window.addEventListener('mouseup', handleMouseUp, false);
window.addEventListener('mousemove', handleMouseMove, false);
window.addEventListener('keydown', handleKeyDown, false);
window.addEventListener('keyup', handleKeyUp, false);
window.addEventListener('wheel', handleWheel, { passive:false });
// Start animation loop.
requestAnimationFrame(updateTransforms);
return {
/**
* Attached touch transform functionality to element.
* If optional target is specified, the target will be manipulated instead of the element.
* This is useful for when you want touches on say the entire page to transform a specific element.
* If you use attach on an element which already has a CSS3 transform set, TouchTransform will remember it and reset to it if .reset is called.
* Though there is a caveat: You can only use px values for translation and deg for rotate. If you need to apply a transform with other measurements.
* it is recommended to do so in a wrapping element instead.
* @param {DOMElement} element Element that should respond to transform interactions.
* @param {Object} options Object containing settings, such as:
* target : DOMElement which should be transformed. If undefined defaults to `element`. If false it doesn't affect a target.
* onUpdate : Optional callback called when transform updates. Useful if target is false.
* rotateKey : Key to be pressed for rotation. (Default: "Shift")
* scaleKey : Key to be pressed for scaling (Default: false)
*/
attach: function attach(element, options){
attachedElements.push(element);
options = options || {};
options.target = options.target === undefined ? element : options.target;
options.target = options.target === false ? getDummyElement() : options.target;
if(options.rotateKey === undefined){
options.rotateKey = defaultRotateKey;
}
if(options.scaleKey === undefined){
options.scaleKey = defaultScaleKey;
}
element.__touchTransformOptions = options;
},
/**
* Reset the applied transform to the elements original.
* @param {*} element
*/
reset: function reset(element){
var target = element.__touchTransformOptions.target;
resetTransformInfo(target);
}
};
}();
// Add module node support
if ( typeof module === "object" && module && typeof module.exports === "object" ) {
module.exports = TouchTransform;
} else {
// If not a node like environment check if its AMD
if ( typeof define === "function" && define.amd ) {
define( "touch-transform", [], function () { return TouchTransform; } );
}
// Add to window object
window.TouchTransform = TouchTransform;
}
})();