-
Notifications
You must be signed in to change notification settings - Fork 18
/
DemoPinchable.js
69 lines (61 loc) · 2.04 KB
/
DemoPinchable.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
import TinyGesture from './dist/TinyGesture.js';
import { addTransition, removeTransition } from './DemoTransitions.js';
/**
* This function can be used as a Svelte action.
*/
export default function Pinchable(node) {
const gesture = new TinyGesture(node);
let backTimeout;
const preventDefault = (event) => {
event.preventDefault();
};
addTransition(node, 'transform .5s ease');
addTransition(node, 'transform-origin .5s ease');
// Don't allow the page to scroll when the target is first pressed.
node.addEventListener('touchstart', preventDefault, { passive: false });
let scale = 1;
let origin = null;
let myTransform = ` scale(${scale})`;
node.style.transform = '';
function resetTransform() {
node.style.transform = `${node.style.transform}`.replace(/\s*scale\([^)]*\)/, '');
myTransform = ` scale(${scale})`;
}
// When the target is pinched, scale it to the right size.
gesture.on('pinch', () => {
scale = gesture.scale;
if (origin == null) {
const box = node.getBoundingClientRect();
origin = [gesture.touchMove1.clientX - box.x, gesture.touchMove1.clientY - box.y];
node.style.transformOrigin = `${origin[0]}px ${origin[1]}px`;
}
resetTransform();
removeTransition(node, 'transform');
removeTransition(node, 'transform-origin');
node.style.transform = `${node.style.transform}` + myTransform;
});
// When the target is pinched, scale it to the right size.
gesture.on('pinchend', () => {
scale = gesture.scale;
origin = null;
addTransition(node, 'transform .5s ease');
addTransition(node, 'transform-origin .5s ease');
node.style.transformOrigin = 'center';
clearTimeout(backTimeout);
backTimeout = setTimeout(() => {
scale = 0;
resetTransform();
}, 1000);
});
return {
destroy() {
node.removeEventListener('touchstart', preventDefault, {
passive: false,
});
clearTimeout(backTimeout);
node.style.transform = '';
removeTransition(node, 'transform');
gesture.destroy();
},
};
}