Advertisement
Serafim

Untitled

Oct 29th, 2013
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. namespace Boo:Core:
  2.   class Animate
  3.     class Algo
  4.       constructor: (algo) ->
  5.         if typeof algo is 'string'
  6.           @algo = @get algo
  7.         else
  8.           @algo = @bezier [algo[0], algo[1]], [algo[2], algo[3]]
  9.  
  10.       delta: (t) -> @algo(t)
  11.  
  12.       get: (name) ->
  13.         switch name
  14.           when 'linear' then @bezier [0, 0], [1, 1]
  15.           when 'ease'   then @bezier [0.25, 0.1], [0.25, 1]
  16.  
  17.           else @bezier [0, 0], [1, 1]
  18.  
  19.       bezier: (from, to) ->
  20.         A = [null, null]
  21.         B = [null, null]
  22.         C = [null, null]
  23.         bezCoOrd = (t, ax) ->
  24.           C[ax] = 3 * from[ax]
  25.           B[ax] = 3 * (to[ax] - from[ax]) - C[ax]
  26.           A[ax] = 1 - C[ax] - B[ax]
  27.  
  28.           t * (C[ax] + t * (B[ax] + t * A[ax]))
  29.  
  30.         xDeriv = (t) ->
  31.           C[0] + t * (2 * B[0] + 3 * A[0] * t)
  32.  
  33.         xForT = (t) ->
  34.           x = t
  35.           i = 0
  36.           z = undefined
  37.           while ++i < 14
  38.             z = bezCoOrd(x, 0) - t
  39.             break  if Math.abs(z) < 1e-3
  40.             x -= z / xDeriv(x)
  41.           x
  42.  
  43.         (t) ->
  44.           bezCoOrd xForT(t), 1
  45.  
  46.     constructor: (@method = 'linear') ->
  47.  
  48.     run: (args, cb) ->
  49.       Utils::apply @, args,
  50.         from: 0
  51.         to:   1
  52.         speed: 30
  53.  
  54.       algo    = new Algo @method
  55.       delta   = if @to > @from then @to - @from else @from - @to
  56.       minimum = if @to > @from then @from else @to
  57.  
  58.       current = if @to > @from then 0 else @speed
  59.       step    = 1 / @speed
  60.       @interval = setInterval =>
  61.         val = algo.delta(step * current) * delta + minimum
  62.         cb val
  63.  
  64.         if @to > @from
  65.           if current++ >= @speed then clearInterval @interval
  66.         else
  67.           if current-- <= 0 then clearInterval @interval
  68.       , 10
  69.  
  70.     stop: ->
  71.       clearInterval @interval
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement