Fork me on GitHub
#rum
<
2018-10-02
>
pez08:10:36

Thanks, @mattly. (I missed that someone answered 😃 )

pez08:10:28

Now another question. With reagent I liked to use reanimated for smooth tweening of values. Is there something similar for rum? Reanimated lets me have animated and reactive atoms that I deref and then can trust reagent to react on.

mattly14:10:27

I know in the react world the typical solution would be to just use something like: https://github.com/chenglou/react-motion

mattly14:10:57

but I don't think this translates to cljs well

mattly14:10:23

really if you had easing functions that you could trigger over a local state variable

mattly14:10:39

I thought I knew of a good JS library for just easing functions but can't find it now

mattly14:10:48

basically, take these functions: https://gist.github.com/gre/1650294

mattly14:10:25

do a requestAnimationFrame that increments t as a function of how far along it is from startTime to startTime + animationTime

mattly14:10:51

and then make the result of that plus your easing function the value of a local state var

pez15:10:10

So rum rerenders on change of local state vars?

pez15:10:06

I actually have some easing functions and a tween function that I've adapted from some code I found somewhere:

(defn easeOutQuad
  [elapsed-t duration]
  (let [dt (/ elapsed-t duration)]
    (* dt (- 2 dt))))

(defn easeLinear
  [elapsed-t duration]
  (let [dt (/ elapsed-t duration)]
    dt))

#?(:cljs ;; TODO: Add signature for choosing ease function
   (defn tween-to! [state key new-value duration]
     (let [anim-key (keyword (str (name key) "-anim"))
           initial-value (key @state)]
       (letfn [(tick [_]
                 (let [a-map (anim-key @state)
                       t (- (.now js/Date) (::t0 a-map))]
                   (if (< t duration)
                     (let [progress (easeLinear t duration)
                           new-val (+ (::initial-value a-map)
                                      (* progress (::delta a-map)))]
                       (swap! state assoc key new-val
                              ::ticker (js/requestAnimationFrame tick)))
                     (do
                       (js/cancelAnimationFrame (::ticker a-map))
                       (swap! state dissoc anim-key)
                       (swap! state assoc key new-value)))))]

         (swap! state assoc anim-key
                {::t0            (.now js/Date.)
                 ::ticker        (js/requestAnimationFrame tick)
                 ::delta         (- new-value initial-value)
                 ::initial-value initial-value}))))
   :clj
   (defn tween-to! [state key new-value duration]
     (swap! state assoc key new-value)))

pez15:10:46

But that looks a bit more complicated than what you describe, @mattly. And, for reasons that are yet unclear to me, I don't seem to be able to use it to animate more than one value in a map at a time.

pez15:10:52

So, before I continued to dig myself deeper down this hole, I started to wonder if there is a library or better pattern to use.