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.
(var _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)))))
(set _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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment