-
Notifications
You must be signed in to change notification settings - Fork 0
/
rotator.js
69 lines (60 loc) · 1.42 KB
/
rotator.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
// @ts-check
import { rotate, matrixStyle } from './lib/matrix2d.js';
/**
* @param {SVGElement} $element
* @param {number} direction - (1 for clockwise, -1 for widdershins)
*/
export const makeRotatingElementController = ($element, direction) => {
// visibility
let prev = true;
let next = true;
const tick = () => {};
const tock = () => {};
/** @type {import('./progress.js').AnimateFn} */
const animate = progress => {
// TODO unclear why tick and tock aren't detecting these transitions
// properly.
if (progress.linear === 1) {
prev = next;
return;
}
// animate rotation
if (prev !== next) {
if (next) {
// showing
$element.style.transform = matrixStyle(
rotate(direction * (Math.PI / 2) * (1 - progress.sinusoidal)),
);
} else {
// hiding
$element.style.transform = matrixStyle(
rotate(direction * (Math.PI / 2) * progress.sinusoidal),
);
}
} else {
if (next) {
// shown
$element.style.transform = matrixStyle(rotate(0));
} else {
// hidden
$element.style.transform = matrixStyle(
rotate((direction * Math.PI) / 2),
);
}
}
};
const show = () => {
next = true;
};
const hide = () => {
next = false;
};
const controller = {
show,
hide,
tick,
tock,
animate,
};
return controller;
};