/
Animation.coffee
336 lines (260 loc) · 9.1 KB
/
Animation.coffee
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
{_} = require "./Underscore"
Utils = require "./Utils"
{Config} = require "./Config"
{Defaults} = require "./Defaults"
{BaseClass} = require "./BaseClass"
{Animator} = require "./Animator"
{LinearAnimator} = require "./Animators/LinearAnimator"
numberRE = /[+-]?(?:\d*\.|)\d+(?:[eE][+-]?\d+|)/
relativePropertyRE = new RegExp("^(?:([+-])=|)(" + numberRE.source + ")([a-z%]*)$", "i")
isRelativeProperty = (v) ->
_.isString(v) and relativePropertyRE.test(v)
evaluateRelativeProperty = (target, k, v) ->
[match, sign, number, unit, rest...] = relativePropertyRE.exec(v)
return target[k] + (sign + 1) * number if sign
return +number
class exports.Animation extends BaseClass
constructor: (args...) ->
# Old API detection
# animationA = new Animation
# layer: layerA
# properties:
# x: 100
layer = null
properties = {}
options = {}
# Actual current api
if arguments.length is 3
layer = args[0]
properties = args[1]
options = args[2]
# Mix of current and old api
if arguments.length is 2
layer = args[0]
if args[1].properties?
properties = args[1].properties
else
properties = args[1]
options = args[1].options if args[1].options?
# Old api
if arguments.length is 1
layer = args[0].layer
properties = args[0].properties
if args[0].options?
options = args[0].options
else
options = args[0]
delete options.layer
delete options.properties
delete options.options
@options = _.cloneDeep(Defaults.getDefaults("Animation", options))
super
@_layer = layer
unless layer instanceof _Layer
throw Error("Animation: missing layer")
@properties = Animation.filterAnimatableProperties(properties)
if properties.origin
console.warn "Animation.origin: please use layer.originX and layer.originY"
@options.curveOptions = Animator.curveOptionsFor(@options)
@_originalState = @_currentState()
@_repeatCounter = @options.repeat
@define "layer",
get: -> @_layer
@define "isPending", get: -> @_delayTimer?
@define "isAnimating",
get: -> @ in @layer.animations()
@define "looping",
get: -> @options.looping
set: (value) ->
@options?.looping = value
if @options?.looping and @layer? and not @isAnimating
@restart()
@define "isNoop", @simpleProperty("isNoop", false)
start: =>
@_animator = Animation._createAnimator(@options) ? new LinearAnimator(@options.curveOptions)
@_target = @layer
@_stateA = @_currentState()
@_stateB = {}
for k, v of @properties
# Evaluate function properties
if _.isFunction(v)
v = v(@layer, k)
# Evaluate relative properties
else if isRelativeProperty(v)
v = evaluateRelativeProperty(@_target, k, v)
# Filter out the properties that are equal
@_stateB[k] = v if @_stateA[k] isnt v
if _.keys(@_stateA).length is 0
console.warn "Animation: nothing to animate, no animatable properties"
return @_noop()
if _.isEqual(@_stateA, @_stateB)
console.warn "Animation: nothing to animate, all properties are equal to what it is now"
return @_noop()
if _.keys(@_stateB).length is 0
return @_noop()
# If this animation wants to animate a property that is already being animated, it stops
# that currently running animation. If not, it allows them both to continue.
for property, animation of @_target.animatingProperties()
if @_stateA.hasOwnProperty(property)
animation.stop()
# We also need to account for derivatives from x, y
if property is "x" and (
@_stateA.hasOwnProperty("minX") or
@_stateA.hasOwnProperty("midX") or
@_stateA.hasOwnProperty("maxX"))
animation.stop()
if property is "y" and (
@_stateA.hasOwnProperty("minY") or
@_stateA.hasOwnProperty("midY") or
@_stateA.hasOwnProperty("maxY"))
animation.stop()
if @options.debug
console.log "Animation.start"
console.log "\t#{k}: #{@_stateA[k]} -> #{@_stateB[k]}" for k, v of @_stateB
# Add the callbacks
@on(Events.AnimationStart, @options.onStart) if _.isFunction(@options.onStart)
@on(Events.AnimationHalt, @options.onHalt) if _.isFunction(@options.onHalt)
@on(Events.AnimationStop, @options.onStop) if _.isFunction(@options.onStop)
@on(Events.AnimationEnd, @options.onEnd) if _.isFunction(@options.onEnd)
# See if we need to repeat this animation
# Todo: more repeat behaviours:
# 1) add (from end position) 2) reverse (loop between a and b)
@once "end", =>
if @_repeatCounter > 0 or @looping
@restart()
if not @looping
@_repeatCounter--
# Figure out what kind of values we have so we don't have to do it in
# the actual update loop. This saves a lot of frame budget.
@_prepareUpdateValues()
# The option keywords animate and instant trigger an instant animation
if @options.animate is false or @options.instant is true
# If animate is false we set everything immediately and skip the actual animation
start = @_instant
else
start = @_start
@layer.context.addAnimation(@)
# If we have a delay, we wait a bit for it to start
if @options.delay
@_delayTimer = Utils.delay(@options.delay, start)
else
start()
return true
stop: (emit=true) ->
if @_delayTimer?
Framer.CurrentContext.removeTimer(@_delayTimer)
@_delayTimer = null
@layer.context.removeAnimation(@)
@emit(Events.AnimationHalt) if emit
@emit(Events.AnimationStop) if emit
Framer.Loop.off("update", @_update)
reverse: ->
# TODO: Add some tests
properties = _.clone(@_originalState)
options = _.clone(@options)
new Animation @layer, properties, options
reset: ->
for k, v of @_stateA
@_target[k] = v
restart: ->
@reset()
@start()
copy: ->
properties = _.clone(@properties)
options = _.clone(@options)
new Animation(@layer, properties, options)
# A bunch of common aliases to minimize frustration
revert: -> @reverse()
inverse: -> @reverse()
invert: -> @reverse()
emit: (event) ->
super
# Also emit this to the layer with self as argument
@layer.emit(event, @)
animatingProperties: ->
_.keys(@_stateA)
_instant: =>
@emit(Events.AnimationStart)
@_updateValues(1)
@emit(Events.AnimationStop)
@emit(Events.AnimationEnd)
_noop: =>
@isNoop = true
# We don't emit these so you can call layer.animate safely
# from the same layers layer.onAnimationEnd handler
# @emit(Events.AnimationStart)
# @emit(Events.AnimationStop)
# @emit(Events.AnimationEnd)
return not @isNoop
_start: =>
@_delayTimer = null
@emit(Events.AnimationStart)
Framer.Loop.on("update", @_update)
finish: =>
@stop()
@_updateValues(1)
_update: (delta) =>
if @_animator.finished()
@_updateValues(1)
@stop(emit=false)
@emit(Events.AnimationStop)
@emit(Events.AnimationEnd)
else
@_updateValues(@_animator.next(delta))
_prepareUpdateValues: =>
@_valueUpdaters = {}
for k, v of @_stateB
if Color.isColorObject(v) or Color.isColorObject(@_stateA[k])
@_valueUpdaters[k] = @_updateColorValue
else
@_valueUpdaters[k] = @_updateNumberValue
_updateValues: (value) =>
@_valueUpdaters[k](k, value) for k, v of @_stateB
return null
_updateNumberValue: (key, value) =>
@_target[key] = Utils.mapRange(value, 0, 1, @_stateA[key], @_stateB[key])
_updateColorValue: (key, value) =>
@_target[key] = Color.mix(@_stateA[key], @_stateB[key], value, false, @options.colorModel)
_currentState: ->
return _.pick(@layer, _.keys(@properties))
@_createAnimator: (options) ->
AnimatorClass = Animator.classForCurve(options.curve)
return null if not AnimatorClass?
curveOptions = options.curveOptions ? Animator.curveOptionsFor(options)
if options.debug
console.log "Animation.start #{AnimatorClass.name}", curveOptions
return new AnimatorClass curveOptions
@isAnimatable = (v) ->
_.isNumber(v) or _.isFunction(v) or isRelativeProperty(v) or Color.isColorObject(v)
@filterAnimatableProperties = (properties) ->
# Function to filter only animatable properties out of a given set
animatableProperties = {}
# Only animate numeric properties for now
for k, v of properties
if k in ["frame", "size", "point"] # Derived properties
switch k
when "frame" then derivedKeys = ["x", "y", "width", "height"]
when "size" then derivedKeys = ["width", "height"]
when "point" then derivedKeys = ["x", "y"]
else derivedKeys = []
if _.isObject(v)
_.defaults(animatableProperties, _.pick(v, derivedKeys))
else if _.isNumber(v)
for derivedKey in derivedKeys
animatableProperties[derivedKey] = v
else if @isAnimatable(v)
animatableProperties[k] = v
else if Color.isValidColorProperty(k, v)
animatableProperties[k] = new Color(v)
return animatableProperties
toInspect: ->
return "<#{@constructor.name} id:#{@id} isAnimating:#{@isAnimating} [#{_.keys(@properties)}]>"
##############################################################
## EVENT HELPERS
onAnimationStart: (cb) -> @on(Events.AnimationStart, cb)
onAnimationHalt: (cb) -> @on(Events.AnimationHalt, cb)
onAnimationStop: (cb) -> @on(Events.AnimationStop, cb)
onAnimationEnd: (cb) -> @on(Events.AnimationEnd, cb)
onAnimationDidStart: (cb) -> @on(Events.AnimationDidStart, cb)
onAnimationDidStop: (cb) -> @on(Events.AnimationDidStop, cb)
onAnimationDidEnd: (cb) -> @on(Events.AnimationDidEnd, cb)