-
Notifications
You must be signed in to change notification settings - Fork 479
/
animation.coffee
344 lines (227 loc) · 8.34 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
337
338
339
340
341
342
343
344
_ = require "underscore"
{config} = require "./config"
utils = require "./utils"
css = require "./css"
{EventEmitter} = require "./eventemitter"
{Matrix} = require "./primitives/matrix"
spring = require "./curves/spring"
bezier = require "./curves/bezier"
AnimationCounter = 0
AnimationList = []
parseCurve = (a, prefix) ->
# "spring(1, 2, 3)" -> 1, 2, 3
a = a.replace prefix, ""
a = a.replace /\s+/g, ""
a = a.replace "(", ""
a = a.replace ")", ""
a = a.split ","
return a.map (i) -> parseFloat i
class Animation extends EventEmitter
AnimationProperties: [
"view", "properties", "curve", "time",
"origin", "tolerance", "precision", "modifiers"
"debug", "profile", "callback"
]
AnimatableCSSProperties: {
opacity: "",
width: "px",
height: "px",
}
AnimatableMatrixProperties: [
"x", "y", "z",
"scaleX", "scaleY", "scaleZ", # "scale",
"rotationX", "rotationY", "rotationZ", # "rotation"
]
constructor: (args) ->
# console.log "Animation.constructor", args
# Set all properties
for p in @AnimationProperties
@[p] = args[p]
# Set all the defaults
@time ?= 1000
@curve ?= "linear"
@count = 0
@precision ?= config.animationPrecision
@debug ?= config.animationDebug
@profile ?= config.animationProfile
AnimationCounter += 1
@animationId = AnimationCounter
start: (callback) =>
AnimationList.push @
########################################################
# Set up some variables to start with
startTime = new Date().getTime()
@count++
@animationName = "framer-animation-#{@animationId}-#{@count}"
console.log "Animation[#{@animationId}].start" if @debug
console.log "Animation[#{@animationId}].view = #{@view.name}" if @debug
console.profile @animationName if @profile
########################################################
# Deal with other animations on this view
@view.animateStop()
@view._currentAnimations.push @
########################################################
# Calculate the curve values
@curveValues = @_parseCurve @curve
@totalTime = (@curveValues.length / @precision) * 1000
########################################################
# Build a property list that we want to animate
# TODO: test if we are trying to animate something that cannot animate
propertiesA = @view.properties
propertiesB = @properties
# Set the derived properties scale and rotation
if propertiesB.scale
propertiesB.scaleX = propertiesB.scale
propertiesB.scaleY = propertiesB.scale
if propertiesB.rotation
propertiesB.rotationZ = propertiesB.rotation
@propertiesA = {}
@propertiesB = {}
# Build up the matrix animation properties
for k in @AnimatableMatrixProperties
@propertiesA[k] = propertiesA[k]
if propertiesB.hasOwnProperty k
@propertiesB[k] = propertiesB[k]
else
@propertiesB[k] = propertiesA[k]
# Build up the css animation properties
for k, v of @AnimatableCSSProperties
if propertiesB.hasOwnProperty k
@propertiesA[k] = propertiesA[k]
@propertiesB[k] = propertiesB[k]
if @debug
for k of @propertiesA
if @propertiesA[k] isnt @propertiesB[k]
console.log " .#{k} #{@propertiesA[k]} -> #{@propertiesB[k]}"
########################################################
# Generate the keyframe css and insert
@keyFrameAnimationCSS = @_css()
@view.once "webkitAnimationEnd", @_finalize
css.addStyle "
#{@keyFrameAnimationCSS}
.#{@animationName} {
-webkit-animation-duration: #{@totalTime / 1000}s;
-webkit-animation-name: #{@animationName};
-webkit-animation-timing-function: linear;
-webkit-animation-fill-mode: both;
}"
@view.addClass @animationName
########################################################
# Finalize
if @debug
endTime = new Date().getTime() - startTime
console.log "Animation[#{@animationId}].setupTime = #{endTime}ms"
console.log "Animation[#{@animationId}].totalTime = #{utils.round @totalTime, 0}ms"
console.profileEnd @animationName if @profile
reverse: =>
# Return the inverse of this animation
options = {}
for p in @AnimationProperties
options[p] = @[p]
options.properties = {}
for k, v of @properties
options.properties[k] = @view[k]
return new Animation options
stop: =>
console.log "Animation[#{@animationId}].stop #{@animationName}" if @debug
@_canceled = true
# @view.style["-webkit-animation-play-state"] = "paused"
@_cleanup false
# @view.style["-webkit-animation-play-state"] = "running"
_finalize: =>
if @_canceled is true
return
console.log "Animation[#{@animationId}].end #{@animationName}" if @debug
@_cleanup true
callback?()
_cleanup: (completed) =>
# Remove this animation from the current ones for this view
@view._currentAnimations = _.without @view._currentAnimations, @
if completed
# A matrix with the defined end state
endMatrix = utils.extend new Matrix(), @propertiesB
endStyles = {}
for k, v of @AnimatableCSSProperties
endStyles[k] = @propertiesB[k] + v
else
# A matrix from the current state
endMatrix = new Matrix(@view._computedMatrix())
endStyles = {}
computedStyles = @view.computedStyle
for k, v of @AnimatableCSSProperties
endStyles[k] = computedStyles[k]
# Remove the animation class
@view.removeClass @animationName
# Set the end states
@view._matrix = endMatrix
@view.style = endStyles
@callback? @
@emit "end"
_css: ->
# Build the css for the keyframe animation. I wish there was a nicer
# way to do this, but this will work for now.
stepIncrement = 0
stepDelta = 100 / (@curveValues.length - 1)
cssString = []
cssString.push "@-webkit-keyframes #{@animationName} {\n"
deltas = @_deltas()
# We define this object outside of the loop to re-use it.
# In theory this should help a bit with perfomance, in practise
# I'm too lazy to prove it.
m = new Matrix()
for springValue in @curveValues
position = stepIncrement * stepDelta
cssString.push "\t#{utils.round position, config.roundingDecimals}%\t{ -webkit-transform: "
# Add the matrix based values
for propertyName in @AnimatableMatrixProperties
# Calculate the clean spring value for this point
value = springValue * deltas[propertyName] + @propertiesA[propertyName]
# Modify the value if we have a modifier set up. This let's us do
# special stuff like drop the friction once we run into the scroll
# bounds for a scrollview.
if @modifiers?[propertyName]?
value = @modifiers[propertyName](value)
m[propertyName] = value
cssString.push m.matrix().cssValues() + "; "
for propertyName, unit of @AnimatableCSSProperties
continue if not @propertiesA.hasOwnProperty propertyName
value = springValue * deltas[propertyName] + @propertiesA[propertyName]
cssString.push "#{propertyName}:#{ utils.round value, config.roundingDecimals}#{unit}; "
cssString.push "}\n"
stepIncrement++
cssString.push "}\n"
cssString.join ""
_deltas: ->
deltas = {}
# Pre-calculate the delta values
for k of @propertiesA
deltas[k] = (@propertiesB[k] - @propertiesA[k]) / 100.0
return deltas
_parseCurve: (curve) ->
curve ?= ""
curve = curve.toLowerCase()
# If the shift key is pressed, we would like to play the animation slowly.
factor = config.timeSpeedFactor
precision = @precision * factor
time = @time * factor
if curve in ["linear"]
return bezier.defaults.Linear @precision, time
else if curve in ["ease"]
return bezier.defaults.Ease @precision, time
else if curve in ["ease-in"]
return bezier.defaults.EaseIn @precision, time
else if curve in ["ease-out"]
return bezier.defaults.EaseOut @precision, time
else if curve in ["ease-in-out"]
return bezier.defaults.EaseInOut @precision, time
else if curve[.."cubic-bezier".length-1] is "cubic-bezier"
v = parseCurve curve, "cubic-bezier"
return bezier.BezierCurve v[0], v[1], v[2], v[3], precision, time
else if curve[.."spring".length-1] is "spring"
v = parseCurve curve, "spring"
return spring.SpringCurve v[0], v[1], v[2], precision
else
# The default curve is linear
console.log "Animation.parseCurve: could not parse curve '#{curve}'"
return bezier.defaults.Linear @precision, @time
exports.Animation = Animation