Skip to content

Instantly share code, notes, and snippets.

@selfsame
Last active June 9, 2021 06:57
Show Gist options
  • Save selfsame/d18a591a726fa7f888b384d22069f070 to your computer and use it in GitHub Desktop.
Save selfsame/d18a591a726fa7f888b384d22069f070 to your computer and use it in GitHub Desktop.

Revisions

  1. selfsame revised this gist Apr 21, 2019. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions tween.fnl
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    (global _ts {})
    (var _ts {})
    (fn lerp [a b r] (+ a (* (- b a) r)))
    (fn powf [n] (fn [v] (^ v n)))

    @@ -32,7 +32,7 @@
    (set t.o z))
    (if (> t.d 0) (table.insert ts t)
    (if t.f (t.f t.o)))))
    (global _ts ts))
    (set _ts ts))

    ;(tween object prop_name target_value duration options_table)
    ; options are
  2. selfsame created this gist Apr 21, 2019.
    42 changes: 42 additions & 0 deletions tween.fnl
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,42 @@
    (global _ts {})
    (fn lerp [a b r] (+ a (* (- b a) r)))
    (fn powf [n] (fn [v] (^ v n)))

    (fn tween [o p v d _]
    (let [_ (or _ {})
    t {:o o :p p :v v :d d :s d
    :f _.f
    :l (or _.l lerp)
    :ei (or _.ei _.e)
    :eo (or _.eo _.e)
    :iv (or (and p (. o p)) o)}]
    (table.insert _ts t) t))

    (fn wait [d f] (tween 0 nil 0 d {:f f}))

    (fn update-tweens []
    (local ts {})
    (each [_ t (ipairs _ts)]
    (when t.o
    (set t.d (- t.d 1))
    (var r (- 1 (/ t.d t.s)))
    (if (and t.ei t.eo)
    (set r (or (and (< r 0.5) (/ (t.ei (* r 2)) 2))
    (- 1 (/ (t.eo (* (- 1 r) 2)) 2))))
    t.ei
    (set r (t.ei r))
    t.eo
    (set r (- 1 (t.eo (- 1 r)))))
    (var z (t.l t.iv t.v r))
    (if t.p (tset t.o t.p z)
    (set t.o z))
    (if (> t.d 0) (table.insert ts t)
    (if t.f (t.f t.o)))))
    (global _ts ts))

    ;(tween object prop_name target_value duration options_table)
    ; options are
    ; :e dual in+out easing fn
    ; :ei,:eo in,out easing fn
    ; :f tween callback (takes 1 argument - the object)
    ; :l lerping function