-
Notifications
You must be signed in to change notification settings - Fork 18
/
DemoRotatable.js
69 lines (61 loc) · 2.06 KB
/
DemoRotatable.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 Rotatable(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 angle = 0;
let origin = null;
let myTransform = ` rotate(${angle}deg)`;
node.style.transform = '';
function resetTransform() {
node.style.transform = `${node.style.transform}`.replace(/\s*rotate\([^)]*\)/, '');
myTransform = ` rotate(${angle}deg)`;
}
// When the target is rotated, rotate it to the right angle.
gesture.on('rotate', () => {
angle = gesture.rotation;
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 rotated, rotate it to the right angle.
gesture.on('rotateend', () => {
angle = gesture.angle;
origin = null;
addTransition(node, 'transform .5s ease');
addTransition(node, 'transform-origin .5s ease');
node.style.transformOrigin = 'center';
clearTimeout(backTimeout);
backTimeout = setTimeout(() => {
angle = 0;
resetTransform();
}, 1000);
});
return {
destroy() {
node.removeEventListener('touchstart', preventDefault, {
passive: false,
});
clearTimeout(backTimeout);
node.style.transform = '';
removeTransition(node, 'transform');
gesture.destroy();
},
};
}