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
58 lines (48 loc) · 1.01 KB
/
animate.js
File metadata and controls
58 lines (48 loc) · 1.01 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
import uid from './uid'
import { linear } from './easing'
let ids = {}
export function start ({name, duration = 300, to, from, apply, done, cancel, easing}) {
let id = name
const start = new Date()
if (id) {
stop(id)
}
else {
id = uid()
}
const delta = easing || linear
const handler = () => {
let progress = ((new Date()) - start) / duration
if (progress > 1) {
progress = 1
}
const newPos = from + (to - from) * delta(progress)
apply(newPos, progress)
if (progress === 1) {
delete ids[id]
done && done(newPos)
return
}
anim.last = {
pos: newPos,
progress
}
anim.timer = window.requestAnimationFrame(handler)
}
const anim = ids[id] = {
cancel,
timer: window.requestAnimationFrame(handler)
}
return id
}
export function stop (id) {
if (!id) {
return
}
let anim = ids[id]
if (anim && anim.timer) {
cancelAnimationFrame(anim.timer)
anim.cancel && anim.cancel(anim.last)
delete ids[id]
}
}