This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2020-08-06
Channels
- # announcements (4)
- # beginners (132)
- # calva (37)
- # chlorine-clover (60)
- # cider (1)
- # clara (12)
- # clj-kondo (40)
- # cljs-dev (109)
- # clojure (76)
- # clojure-dev (19)
- # clojure-europe (8)
- # clojure-france (17)
- # clojure-nl (4)
- # clojure-sg (1)
- # clojure-spec (14)
- # clojure-uk (7)
- # clojurescript (98)
- # conjure (96)
- # cursive (15)
- # data-science (2)
- # datalog (11)
- # datomic (24)
- # emacs (17)
- # figwheel-main (3)
- # fulcro (45)
- # jobs-discuss (1)
- # kaocha (3)
- # malli (2)
- # nrepl (1)
- # off-topic (135)
- # portal (2)
- # re-frame (17)
- # reagent (11)
- # reitit (4)
- # sci (60)
- # shadow-cljs (75)
- # spacemacs (3)
- # sql (32)
- # tools-deps (79)
- # vim (88)
- # xtdb (4)
Hey folks 👋. I've been able to embed a Quil canvas sketch in a reagent component. The sketch doesn't have any animation, rather I want to drive the sketch via state changes from reagent (reframe in my case). Currently when the state of the canvas component changes, the whole sketch is torn down and recreated which results in awful flickering. Is there a way to work around this?
Hi Casey, you may want to prevent the canvas element from being re-rendered. You could remove any reagent atom references in the canvas holder, and use a ref to redraw the quil scene (this could be hooked up to a normal atom with a watcher to get the reactive experience when data changes)
> use a ref to redraw the quil scene How does one do this? (i'm ctrl-f-ing in the quil api docs, but not finding anything)
My component looks like:
(defn canvas [state]
(r/create-class
{:component-did-mount
(fn [component]
(let [node (r/dom-node component)
width (/ (.-innerWidth js/window) 2)
height (/ (.-innerHeight js/window) 2)]
(q/sketch
:host node
:draw draw
:setup (init-canvas width height state)
:size [width height]
:middleware [m/fun-mode])))
:render
(fn [] [:div])}))
And the holder looks like:
(defn canvas-holder
[(let [sketch-state @(rf/subscribe [:query-sketch-state])]
[:div
[do-stuff-button]
[(canvas sketch-state)]])])
ah i mean a react ref to reference the element, think it would look like this:
(defonce my-canvas (atom nil))
(defn ui []
[:div
[:canvas {:ref (fn [el] (reset! my-canvas el))}]])
then you could draw whenever another atom changed
(defonce color (atom "red"))
(add-watch color :watcher
(fn [key atom old-state new-state]
(when @my-canvas
(let [ctx (.getContext @my-canvas "2d")]
(.beginPath ctx)
(.rect ctx 10 10 20 20)
(set! ctx -fillStyle @color)
(.fill ctx)))))
(replace with quill drawing stuff)now that I look at https://github.com/yogthos/quil-reagent-demo/blob/master/src/circles_demo/core.cljs though.. maybe you're meant to set up a quil :update
function, then never actually trigger a re-render in the holder?
I don’t know where I can say this but, those of us who uses MaterialUI should be ready for a crazy ride when they switch to 5.0. The maintainers decided to make some random breaking change just for fun...