UNPKG

1.3 kBJavaScriptView Raw
1function easeInOutSin(time) {
2 return (1 + Math.sin(Math.PI * time - Math.PI / 2)) / 2;
3}
4
5export default function animate(property, element, to) {
6 var options = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
7 var cb = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : function () {};
8 var _options$ease = options.ease,
9 ease = _options$ease === void 0 ? easeInOutSin : _options$ease,
10 _options$duration = options.duration,
11 duration = _options$duration === void 0 ? 300 : _options$duration;
12 var start = null;
13 var from = element[property];
14 var cancelled = false;
15
16 var cancel = function cancel() {
17 cancelled = true;
18 };
19
20 var step = function step(timestamp) {
21 if (cancelled) {
22 cb(new Error('Animation cancelled'));
23 return;
24 }
25
26 if (start === null) {
27 start = timestamp;
28 }
29
30 var time = Math.min(1, (timestamp - start) / duration);
31 element[property] = ease(time) * (to - from) + from;
32
33 if (time >= 1) {
34 requestAnimationFrame(function () {
35 cb(null);
36 });
37 return;
38 }
39
40 requestAnimationFrame(step);
41 };
42
43 if (from === to) {
44 cb(new Error('Element already at target position'));
45 return cancel;
46 }
47
48 requestAnimationFrame(step);
49 return cancel;
50}
\No newline at end of file