UNPKG

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