-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
animation.ts
128 lines (107 loc) 路 3.28 KB
/
animation.ts
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import { KeyValue } from '../types'
import { ObjectExt, Dom } from '../util'
import { Timing, Interp } from '../common'
import { Cell } from './cell'
export class Animation {
protected readonly ids: { [path: string]: number } = {}
constructor(protected readonly cell: Cell) {}
get() {
return Object.keys(this.ids)
}
start<T extends string | number | KeyValue<number>>(
path: string | string[],
target: T,
options: Animation.Options = {},
delim: string = '/',
) {
const localOptions: Animation.Options = {
delay: 10,
duration: 100,
timing: 'linear',
...options,
}
let timing = Timing.linear
if (localOptions.timing != null) {
if (typeof localOptions.timing === 'string') {
timing = Timing[localOptions.timing]
} else {
timing = localOptions.timing
}
}
const current = this.cell.getPropByPath<T>(path)
const interp = localOptions.interp
let interpolate: any
if (interp) {
interpolate = interp(current, target)
} else if (typeof target === 'object') {
interpolate = Interp.object(
current as KeyValue<number>,
target as KeyValue<number>,
)
} else if (typeof target === 'number') {
interpolate = Interp.number(current as number, target)
} else if (typeof target === 'string') {
if (target[0] === '#') {
interpolate = Interp.color(current as string, target)
} else {
interpolate = Interp.unit(current as string, target)
}
}
let startTime = 0
const pathStr = Array.isArray(path) ? path.join(delim) : path
const setter = () => {
let id
let val
const now = new Date().getTime()
if (startTime === 0) {
startTime = now
}
const elaspe = now - startTime
let progress = elaspe / localOptions.duration!
if (progress < 1) {
this.ids[pathStr] = id = Dom.requestAnimationFrame(setter)
} else {
progress = 1
delete this.ids[pathStr]
}
val = interpolate(timing(progress))
options.transitionId = id
this.cell.setPropByPath(
Array.isArray(path) ? path : path.split(delim),
val,
)
if (id == null) {
this.cell.notify('transition:end', { cell: this.cell, path: pathStr })
}
}
const initiator = (transition: FrameRequestCallback) => {
this.stop(path, delim)
this.ids[pathStr] = Dom.requestAnimationFrame(transition)
this.cell.notify('transition:begin', { cell: this.cell, path: pathStr })
}
return setTimeout(() => {
initiator(setter)
}, options.delay)
}
stop(path: string | string[], delim: string = '/') {
const paths = Array.isArray(path) ? path : path.split(delim)
Object.keys(this.ids)
.filter((key) =>
ObjectExt.isEqual(paths, key.split(delim).slice(0, paths.length)),
)
.forEach((key) => {
Dom.cancelAnimationFrame(this.ids[key])
delete this.ids[key]
this.cell.notify('transition:end', { cell: this.cell, path: key })
})
return this
}
}
export namespace Animation {
export interface Options extends KeyValue {
delay?: number
duration?: number
timing?: Timing.Names | Timing.Definition
interp?: <T>(from: T, to: T) => (time: number) => T
}
}