UNPKG

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