-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
70 lines (60 loc) · 1.55 KB
/
index.js
File metadata and controls
70 lines (60 loc) · 1.55 KB
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
70
var noop = () => { }
var easing = {
quadratic: function (x) {
return Math.sqrt(x);
}
};
function range(start, stop, step) {
var array = [];
for (var i = start; i < stop; i += step) array.push(i);
return array;
}
function interpolation(fps, easing, finalValue, start = 0) {
function scaleIt(value) { return start + (Math.abs(finalValue - start) * value); }
var x = range(0, 1, 1 / fps),
y = x.map(easing).map(scaleIt);
return y;
}
function animateEl(values, duration, onAnimate = noop, onEndAnimate = noop) {
var frameIndex = 0,
fps = values.length,
id = setInterval(anime, duration / fps);
function anime() {
var current = values[frameIndex],
isLastFrame = (frameIndex === fps - 1);
onAnimate(current, frameIndex, values);
if (isLastFrame) {
clearInterval(id);
id = null
onEndAnimate()
} else {
frameIndex++;
}
}
return () => {
if (id) {
clearInterval(id)
id = null
}
}
}
export default class VsBubble {
constructor(config = {}) {
this.animateTime = config.animateTime || 3000
this.fps = config.fps || 30
}
start(value, onAnimate = noop, onEndAnimate = noop) {
this.end()
const [start, end] = Array.isArray(value) ? value : [0, value]
const fps = Math.min(Math.abs(end - start), 30)
var values = interpolation(fps, easing.quadratic, end, start);
this.timerFn = animateEl(values, 3000, onAnimate, onEndAnimate);
return this.timerFn
}
end() {
if (this.timerFn) {
this.timerFn()
this.timerFn = null
}
}
}