Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace Boo:Core:
- class Animate
- class Algo
- constructor: (algo) ->
- if typeof algo is 'string'
- @algo = @get algo
- else
- @algo = @bezier [algo[0], algo[1]], [algo[2], algo[3]]
- delta: (t) -> @algo(t)
- get: (name) ->
- switch name
- when 'linear' then @bezier [0, 0], [1, 1]
- when 'ease' then @bezier [0.25, 0.1], [0.25, 1]
- else @bezier [0, 0], [1, 1]
- bezier: (from, to) ->
- A = [null, null]
- B = [null, null]
- C = [null, null]
- bezCoOrd = (t, ax) ->
- C[ax] = 3 * from[ax]
- B[ax] = 3 * (to[ax] - from[ax]) - C[ax]
- A[ax] = 1 - C[ax] - B[ax]
- t * (C[ax] + t * (B[ax] + t * A[ax]))
- xDeriv = (t) ->
- C[0] + t * (2 * B[0] + 3 * A[0] * t)
- xForT = (t) ->
- x = t
- i = 0
- z = undefined
- while ++i < 14
- z = bezCoOrd(x, 0) - t
- break if Math.abs(z) < 1e-3
- x -= z / xDeriv(x)
- x
- (t) ->
- bezCoOrd xForT(t), 1
- constructor: (@method = 'linear') ->
- run: (args, cb) ->
- Utils::apply @, args,
- from: 0
- to: 1
- speed: 30
- algo = new Algo @method
- delta = if @to > @from then @to - @from else @from - @to
- minimum = if @to > @from then @from else @to
- current = if @to > @from then 0 else @speed
- step = 1 / @speed
- @interval = setInterval =>
- val = algo.delta(step * current) * delta + minimum
- cb val
- if @to > @from
- if current++ >= @speed then clearInterval @interval
- else
- if current-- <= 0 then clearInterval @interval
- , 10
- stop: ->
- clearInterval @interval
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement