Skip to content

Commit

Permalink
Update use of animation arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
nvh committed Sep 15, 2016
1 parent c8c0361 commit 4936620
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 84 deletions.
35 changes: 20 additions & 15 deletions framer/Animation.coffee
Expand Up @@ -56,17 +56,17 @@ class exports.Animation extends BaseClass
@_repeatCounter = @options.repeat

@define "isAnimating",
get: -> @ in @options.layer.context.animations
get: -> @ in @layer.context.animations

@define "looping",
get: -> @options.looping
set: (value) ->
@options?.looping = value
if @options?.looping and @options?.layer? and !@isAnimating
if @options?.looping and @layer? and !@isAnimating
@restart()

start: =>
if @options.layer is null
if @layer is null
console.error "Animation: missing layer"

AnimatorClass = @_animatorClass()
Expand All @@ -76,15 +76,15 @@ class exports.Animation extends BaseClass

@_animator = new AnimatorClass @options.curveOptions

@_target = @options.layer
@_target = @layer
@_stateA = @_currentState()
@_stateB = {}

for k, v of @options.properties
for k, v of @properties

# Evaluate function properties
if _.isFunction(v)
v = v(@options.layer, k)
v = v(@layer, k)

# Evaluate relative properties
else if isRelativeProperty(v)
Expand Down Expand Up @@ -148,16 +148,17 @@ class exports.Animation extends BaseClass
if @_delayTimer?
Framer.CurrentContext.removeTimer(@_delayTimer)
@_delayTimer = null
@options.layer.context.removeAnimation(@)
@layer.context.removeAnimation(@)

@emit("stop") if emit
Framer.Loop.off("update", @_update)

reverse: ->
# TODO: Add some tests
options = _.clone(@options)
options.properties = @_originalState
animation = new Animation options
properties = _.clone(@_originalState)
properties.options = _.clone(@options)
properties.layer = @layer
animation = new Animation properties
animation

reset: ->
Expand All @@ -168,7 +169,11 @@ class exports.Animation extends BaseClass
@reset()
@start()

copy: -> new Animation(_.clone(@options))
copy: ->
properties = _.clone(@properties)
properties.options = _.clone(@options)
properties.layer = @layer
new Animation(properties)

# A bunch of common aliases to minimize frustration
revert: -> @reverse()
Expand All @@ -178,7 +183,7 @@ class exports.Animation extends BaseClass
emit: (event) ->
super
# Also emit this to the layer with self as argument
@options.layer.emit(event, @)
@layer.emit(event, @)

animatingProperties: ->
_.keys(@_stateA)
Expand All @@ -191,7 +196,7 @@ class exports.Animation extends BaseClass
@emit("stop")

_start: =>
@options.layer.context.addAnimation(@)
@layer.context.addAnimation(@)
@emit("start")
Framer.Loop.on("update", @_update)

Expand Down Expand Up @@ -229,7 +234,7 @@ class exports.Animation extends BaseClass
@_target[key] = Color.mix(@_stateA[key], @_stateB[key], value, false, @options.colorModel)

_currentState: ->
return _.pick(@options.layer, _.keys(@options.properties))
return _.pick(@layer, _.keys(@properties))

_animatorClass: ->

Expand Down Expand Up @@ -303,7 +308,7 @@ class exports.Animation extends BaseClass
return animatableProperties

toInspect: ->
return "<#{@constructor.name} id:#{@id} isAnimating:#{@isAnimating} [#{_.keys(@options.properties)}]>"
return "<#{@constructor.name} id:#{@id} isAnimating:#{@isAnimating} [#{_.keys(@properties)}]>"


##############################################################
Expand Down
10 changes: 5 additions & 5 deletions framer/Layer.coffee
Expand Up @@ -902,17 +902,15 @@ class exports.Layer extends BaseClass
@animateTo properties, options

animateTo: (properties,options={}) ->

if typeof properties == 'string'
stateName = properties
return @animateToState stateName, options

_.defaults(options,properties.options,@options)
delete properties.options

animatableProperties = Animation.filterAnimatableProperties(properties)
nonAnimatableProperties = _.omit(_.clone(properties),_.keys(animatableProperties))
options.properties = animatableProperties
options.layer = @

start = options.start
start ?= true
Expand All @@ -921,8 +919,10 @@ class exports.Layer extends BaseClass
if instant
options.animate = false
delete options.instant
parameters = animatableProperties
parameters.layer = @

animation = new Animation options
animation = new Animation parameters, options
animationFinished = =>
for k, v of nonAnimatableProperties
@[k] = v
Expand Down Expand Up @@ -953,7 +953,7 @@ class exports.Layer extends BaseClass
animations: ->
# Current running animations on this layer
_.filter @_context.animations, (animation) =>
animation.options.layer is @
animation.layer is @

animatingProperties: ->

Expand Down
2 changes: 1 addition & 1 deletion framer/LayerDraggable.coffee
Expand Up @@ -145,7 +145,7 @@ class exports.LayerDraggable extends BaseClass

# Stop any animations influencing the position, but no others.
for animation in @layer.animations()
properties = animation.options.properties
properties = animation.properties
if properties.hasOwnProperty("x") or properties.hasOwnProperty("y")
animation.stop()

Expand Down

0 comments on commit 4936620

Please sign in to comment.