Skip to content

Instantly share code, notes, and snippets.

@martinklepsch
Last active March 31, 2021 10:37
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save martinklepsch/0cc942365e472671eee2ad5a363c34e7 to your computer and use it in GitHub Desktop.
Save martinklepsch/0cc942365e472671eee2ad5a363c34e7 to your computer and use it in GitHub Desktop.
(ns acme.react-spring
(:require [rum.core :as rum]
["react-spring" :as rs]))
(rum/defc AnimatedDiv < rum/static
[style child]
(js/React.createElement rs/animated.div (clj->js style) child))
(defn use-spring
[config-map]
(rs/useSpring (clj->js config-map)))
(rum/defc AnimatedParticle
[{:keys [from to] :as _positions}]
(let [spring (use-spring {:config {:mass 4, :tension 180, :friction 18}
:delay 150
:from {:xy from, :opacity 0}
:to {:xy to, :opacity 1})
translate-xyr (fn [x y] (str "translate(" x "px," y "px)"))]
(AnimatedDiv
{:style {:position "absolute"
:transform (.. spring -xyrs (interpolate translate-xyr))
:opacity (.. spring -opacity)}}
(Particle))))
(AnimatedParticle {:from [0 0]
:to [123 456]})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment