Skip to content
Permalink
Browse files

Merge pull request #425 from nvh/feature/animator-refactor

Feature/animator refactor
  • Loading branch information
nvh committed Oct 4, 2016
2 parents ea73dcd + 20b6591 commit 2a7ad6cfa1903ced6982cea68443ab0051f2a781
@@ -5,22 +5,7 @@ Utils = require "./Utils"
{Config} = require "./Config"
{Defaults} = require "./Defaults"
{BaseClass} = require "./BaseClass"

{LinearAnimator} = require "./Animators/LinearAnimator"
{BezierCurveAnimator} = require "./Animators/BezierCurveAnimator"
{SpringRK4Animator} = require "./Animators/SpringRK4Animator"
{SpringDHOAnimator} = require "./Animators/SpringDHOAnimator"

AnimatorClasses =
"linear": LinearAnimator
"bezier-curve": BezierCurveAnimator
"spring-rk4": SpringRK4Animator
"spring-dho": SpringDHOAnimator

AnimatorClasses["spring"] = AnimatorClasses["spring-rk4"]
AnimatorClasses["cubic-bezier"] = AnimatorClasses["bezier-curve"]

AnimatorClassBezierPresets = ["ease", "ease-in", "ease-out", "ease-in-out"]
{Animator} = require "./Animator"

numberRE = /[+-]?(?:\d*\.|)\d+(?:[eE][+-]?\d+|)/
relativePropertyRE = new RegExp("^(?:([+-])=|)(" + numberRE.source + ")([a-z%]*)$", "i")
@@ -90,7 +75,7 @@ class exports.Animation extends BaseClass
if properties.origin
console.warn "Animation.origin: please use layer.originX and layer.originY"

@_parseAnimatorOptions()
@options.curveOptions = Animator.curveOptionsFor(@options)
@_originalState = @_currentState()
@_repeatCounter = @options.repeat

@@ -111,7 +96,7 @@ class exports.Animation extends BaseClass

start: =>

@_animator = @_createAnimator()
@_animator = Animation._createAnimator(@options) ? new LinearAnimator(@options.curveOptions)
@_target = @layer
@_stateA = @_currentState()
@_stateB = {}
@@ -293,68 +278,14 @@ class exports.Animation extends BaseClass
_currentState: ->
return _.pick(@layer, _.keys(@properties))

_createAnimator: ->
AnimatorClass = @_animatorClass()

if @options.debug
console.log "Animation.start #{AnimatorClass.name}", @options.curveOptions

return new AnimatorClass @options.curveOptions

_animatorClass: ->

parsedCurve = Utils.parseFunction(@options.curve)
animatorClassName = parsedCurve.name.toLowerCase()

if AnimatorClasses.hasOwnProperty(animatorClassName)
return AnimatorClasses[animatorClassName]

if animatorClassName in AnimatorClassBezierPresets
return BezierCurveAnimator

return LinearAnimator

_parseAnimatorOptions: ->

animatorClass = @_animatorClass()
parsedCurve = Utils.parseFunction @options.curve
animatorClassName = parsedCurve.name.toLowerCase()

# This is for compatibility with the direct Animation.time argument. This should
# ideally also be passed as a curveOption

@options.curveOptions ?= {}
if animatorClass in [LinearAnimator, BezierCurveAnimator]
if _.isString(@options.curveOptions) or _.isArray(@options.curveOptions)
@options.curveOptions =
values: @options.curveOptions

@options.curveOptions.time ?= @options.time

# Support ease-in etc
if animatorClass in [BezierCurveAnimator] and animatorClassName in AnimatorClassBezierPresets
@options.curveOptions.values = animatorClassName
@options.curveOptions.time ?= @options.time

# All this is to support curve: "spring(100, 20, 10)". In the future we'd like people
# to start using curveOptions: {tension:100, friction:10} etc

if parsedCurve.args.length

# console.warn "Animation.curve arguments are deprecated. Please use Animation.curveOptions"

if animatorClass is BezierCurveAnimator
@options.curveOptions.values = parsedCurve.args.map (v) -> parseFloat(v) or 0

if animatorClass is SpringRK4Animator
for k, i in ["tension", "friction", "velocity", "tolerance"]
value = parseFloat parsedCurve.args[i]
@options.curveOptions[k] = value if value
@_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

if animatorClass is SpringDHOAnimator
for k, i in ["stiffness", "damping", "mass", "tolerance"]
value = parseFloat parsedCurve.args[i]
@options.curveOptions[k] = value if value
return new AnimatorClass curveOptions

@isAnimatable = (v) ->
_.isNumber(v) or _.isFunction(v) or isRelativeProperty(v) or Color.isColorObject(v)
@@ -1,6 +1,6 @@
Utils = require "./Utils"
AnimatorClasses = {}

{Config} = require "./Config"
AnimatorClassBezierPresets = ["ease", "ease-in", "ease-out", "ease-in-out"]

class exports.Animator

@@ -30,6 +30,71 @@ class exports.Animator
break
return values

@classForCurve: (curve) ->
parsedCurve = Utils.parseFunction(curve)
animatorClassName = parsedCurve.name.toLowerCase()

if AnimatorClasses.hasOwnProperty(animatorClassName)
return AnimatorClasses[animatorClassName]

if animatorClassName in AnimatorClassBezierPresets
return BezierCurveAnimator

return null

@curveOptionsFor: (options={}) ->
curveOptions = options.curveOptions ? {}
animatorClass = @classForCurve(options.curve)
parsedCurve = Utils.parseFunction options.curve
animatorClassName = parsedCurve.name.toLowerCase()

# This is for compatibility with the direct Animation.time argument. This should
# ideally also be passed as a curveOption

if animatorClass in [LinearAnimator, BezierCurveAnimator]
if _.isString(curveOptions) or _.isArray(curveOptions)
curveOptions =
values: curveOptions

curveOptions.time ?= options.time

# Support ease-in etc
if animatorClass in [BezierCurveAnimator] and animatorClassName in AnimatorClassBezierPresets
curveOptions.values = animatorClassName
curveOptions.time ?= options.time

# All this is to support curve: "spring(100, 20, 10)". In the future we'd like people
# to start using curveOptions: {tension:100, friction:10} etc

if parsedCurve.args.length

# console.warn "Animation.curve arguments are deprecated. Please use Animation.curveOptions"

if animatorClass is BezierCurveAnimator
curveOptions.values = parsedCurve.args.map (v) -> parseFloat(v) or 0

if animatorClass is SpringRK4Animator
for k, i in ["tension", "friction", "velocity", "tolerance"]
value = parseFloat parsedCurve.args[i]
curveOptions[k] = value if value

if animatorClass is SpringDHOAnimator
for k, i in ["stiffness", "damping", "mass", "tolerance"]
value = parseFloat parsedCurve.args[i]
curveOptions[k] = value if value
return curveOptions

# start: -> Framer.Loop.on("update", )
# stop: -> AnimationLoop.remove @

{LinearAnimator} = require "./Animators/LinearAnimator"
{BezierCurveAnimator} = require "./Animators/BezierCurveAnimator"
{SpringRK4Animator} = require "./Animators/SpringRK4Animator"
{SpringDHOAnimator} = require "./Animators/SpringDHOAnimator"
AnimatorClasses["linear"] = LinearAnimator
AnimatorClasses["bezier-curve"] = BezierCurveAnimator
AnimatorClasses["spring-rk4"] = SpringRK4Animator
AnimatorClasses["spring-dho"] = SpringDHOAnimator

AnimatorClasses["spring"] = AnimatorClasses["spring-rk4"]
AnimatorClasses["cubic-bezier"] = AnimatorClasses["bezier-curve"]
@@ -1,6 +1,3 @@
{_} = require "../Underscore"
Utils = require "../Utils"

{Animator} = require "../Animator"

BezierCurveDefaults =
@@ -1,5 +1,3 @@
Utils = require "../Utils"

{Animator} = require "../Animator"

class exports.LinearAnimator extends Animator
@@ -1,5 +1,3 @@
Utils = require "../Utils"

{Animator} = require "../Animator"

class exports.SpringDHOAnimator extends Animator
@@ -1,5 +1,3 @@
Utils = require "../Utils"

{Animator} = require "../Animator"
{Integrator} = require "../Integrator"

@@ -1,8 +1,3 @@

Utils = require "./Utils"

{Config} = require "./Config"

class exports.Integrator

"""
@@ -497,7 +497,6 @@ Utils.modulate = (value, rangeA, rangeB, limit=false) ->
# STRING FUNCTIONS

Utils.parseFunction = (str) ->

result = {name: "", args: []}

if _.endsWith str, ")"

0 comments on commit 2a7ad6c

Please sign in to comment.
You can’t perform that action at this time.