forked from quasarframework/quasar
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanimate.js
More file actions
38 lines (34 loc) · 831 Bytes
/
animate.js
File metadata and controls
38 lines (34 loc) · 831 Bytes
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
import uid from './uid'
let ids = {}
function animate ({id, finalPos, pos, threshold, factor, done, apply}) {
ids[id] = requestAnimationFrame(() => {
let diff = (finalPos - pos)
if (Math.abs(diff) < threshold) {
delete ids[id]
apply(finalPos)
done && done(finalPos)
return
}
let newPos = pos + (finalPos - pos) / factor
apply(newPos)
animate({id, finalPos, pos: newPos, threshold, done, factor, apply})
})
}
export default function start ({name, finalPos, pos, threshold = 1, factor = 5, done, apply}) {
let id = name
if (id) {
start.stop(id)
}
else {
id = uid()
}
animate({id, finalPos, pos, threshold, factor, done, apply})
return id
}
start.stop = id => {
let timer = ids[id]
if (timer) {
cancelAnimationFrame(timer)
delete ids[id]
}
}