/
Motion.js
263 lines (220 loc) · 7.28 KB
/
Motion.js
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
import stepper from './stepper'
import presets from './presets'
import { raf, now, isArray, isObject } from './utils'
const msPerFrame = 1000 / 60
export default {
data () {
return {
currentValues: null,
currentVelocities: null,
}
},
props: {
value: Number,
values: [Object, Array],
tag: {
type: String,
default: 'span',
},
spring: {
type: [Object, String],
default: 'noWobble',
},
},
computed: {
springConfig () {
return typeof this.spring === 'string' ? presets[this.spring] : this.spring
},
realValues () {
return this.value != null ? { value: this.value } : this.values
},
},
render (h) {
return h(this.tag, [this.$scopedSlots.default(this.currentValues)])
},
watch: {
realValues (current, old) {
if (old !== current && !this.wasAnimating) {
this.prevTime = now()
this.accumulatedTime = 0
this.animate()
}
},
},
created () {
const current = this.defineInitialValues(this.realValues, null)
this.currentValues = current.values
this.currentVelocities = current.velocities
},
mounted () {
this.prevTime = now()
this.accumulatedTime = 0
const ideal = this.defineInitialValues(this.currentValues, this.currentVelocities)
this.idealValues = ideal.values
this.idealVelocities = ideal.velocities
this.animate()
},
methods: {
defineInitialValues (values, velocities) {
const newValues = {}
const newVelocities = {}
this.defineValues(values, velocities, newValues, newVelocities)
return { values: newValues, velocities: newVelocities }
},
defineValues (values, velocities, newValues, newVelocities) {
for (const key in values) {
// istanbul ignore if
if (!Object.prototype.hasOwnProperty.call(values, key)) continue
if (isArray(values[key]) || isObject(values[key])) {
newValues[key] = {}
newVelocities[key] = {}
this.defineValues(
values[key],
velocities && velocities[key],
newValues[key],
newVelocities[key]
)
continue
}
newValues[key] = values[key]
newVelocities[key] = velocities ? velocities[key] : 0
}
},
animate () {
this.animationId = raf(() => {
if (shouldStopAnimation(this.currentValues, this.realValues, this.currentVelocities)) {
if (this.wasAnimating) this.$emit('motion-end')
// reset everything for next animation
this.animationId = null
this.wasAnimating = false
return
}
if (!this.wasAnimating) this.$emit('motion-start')
this.wasAnimating = true
// get time from last frame
const currentTime = now()
const timeDelta = currentTime - this.prevTime
this.prevTime = currentTime
this.accumulatedTime += timeDelta
// more than 10 frames? prolly switched browser tab. Restart
if (this.accumulatedTime > msPerFrame * 10) {
this.accumulatedTime = 0
}
if (this.accumulatedTime === 0) {
// no need to cancel animationID here; shouldn't have any in flight
this.animationID = null
this.$emit('motion-restart')
this.animate()
return
}
const currentFrameCompletion =
(this.accumulatedTime - Math.floor(this.accumulatedTime / msPerFrame) * msPerFrame) /
msPerFrame
const framesToCatchUp = Math.floor(this.accumulatedTime / msPerFrame)
const springConfig = this.springConfig
this.animateValues({
framesToCatchUp,
currentFrameCompletion,
springConfig,
realValues: this.realValues,
currentValues: this.currentValues,
currentVelocities: this.currentVelocities,
idealValues: this.idealValues,
idealVelocities: this.idealVelocities,
})
// out of the update loop
this.animationID = null
// the amount we're looped over above
this.accumulatedTime -= framesToCatchUp * msPerFrame
// keep going!
this.animate()
})
},
animateValues ({
framesToCatchUp,
currentFrameCompletion,
springConfig,
realValues,
currentValues,
currentVelocities,
idealValues,
idealVelocities,
}) {
for (const key in realValues) {
// istanbul ignore if
if (!Object.prototype.hasOwnProperty.call(realValues, key)) continue
if (isArray(realValues[key]) || isObject(realValues[key])) {
// the value may have been added
if (!idealValues[key]) {
const ideal = this.defineInitialValues(this.realValues[key], null)
const current = this.defineInitialValues(this.realValues[key], null)
this.$set(idealValues, key, ideal.values)
this.$set(idealVelocities, key, ideal.velocities)
this.$set(currentValues, key, current.values)
this.$set(currentVelocities, key, current.velocities)
}
this.animateValues({
framesToCatchUp,
currentFrameCompletion,
springConfig,
realValues: realValues[key],
currentValues: currentValues[key],
currentVelocities: currentVelocities[key],
idealValues: idealValues[key],
idealVelocities: idealVelocities[key],
})
// nothing to animate
continue
}
let newIdealValue = idealValues[key]
let newIdealVelocity = idealVelocities[key]
const value = realValues[key]
// iterate as if the animation took place
for (let i = 0; i < framesToCatchUp; i++) {
[newIdealValue, newIdealVelocity] = stepper(
msPerFrame / 1000,
newIdealValue,
newIdealVelocity,
value,
springConfig.stiffness,
springConfig.damping,
springConfig.precision
)
}
const [nextIdealValue, nextIdealVelocity] = stepper(
msPerFrame / 1000,
newIdealValue,
newIdealVelocity,
value,
springConfig.stiffness,
springConfig.damping,
springConfig.precision
)
currentValues[key] =
newIdealValue + (nextIdealValue - newIdealValue) * currentFrameCompletion
currentVelocities[key] =
newIdealVelocity + (nextIdealVelocity - newIdealVelocity) * currentFrameCompletion
idealValues[key] = newIdealValue
idealVelocities[key] = newIdealVelocity
}
},
},
}
function shouldStopAnimation (currentValues, values, currentVelocities) {
for (const key in values) {
// istanbul ignore if
if (!Object.prototype.hasOwnProperty.call(values, key)) continue
if (isArray(values[key]) || isObject(values[key])) {
if (!shouldStopAnimation(currentValues[key], values[key], currentVelocities[key])) {
return false
}
// skip the other checks
continue
}
if (currentVelocities[key] !== 0) return false
// stepper will have already taken care of rounding precision errors, so
// won't have such thing as 0.9999 !=== 1
if (currentValues[key] !== values[key]) return false
}
return true
}