`redraw` in ClojureScript

46 views
Skip to first unread message

Simon Katz

unread,
Sep 25, 2020, 8:51:57 AM9/25/20
to clj-processing
Hi.

I'm using Quil 3.1.0 (in a shadow-cljs project, in case that might make any difference).

The documentation for `redraw` says it "does not run draw immediately (it only sets a flag that indicates an update is needed)".

It seems that this is true in Clojure but not in ClojureScript — in ClojureScript it seems that `draw` is called immediately. I'm happy to put together a simple demo of this if it would help.

I'm using functional mode, so I'm relying on return values from eg mouse event handlers updating the state, and I'm also using an `update` function. When I call `redraw` from a mouse event handler, the new state returned from `update` gets discarded, and replaced by the new state returned from the handler.

Any thoughts on a workaround for this? I guess I could not use functional mode and handle state myself.

Simon

Simon Katz

unread,
Sep 25, 2020, 9:17:44 AM9/25/20
to clj-processing
(Answering myself…)

I've worked around it by delaying calling `redraw` using core.async, like this:

#?(:clj (q/redraw)
   :cljs (let [applet quil.sketch/*applet*]
           (a/go
             (a/timeout 100)
             (binding [quil.sketch/*applet* applet]
               (q/redraw)))))


Simon Katz

unread,
Sep 25, 2020, 9:22:24 AM9/25/20
to clj-processing
Grrr! That call of `a/timeout` was supposed to be wrapped in `(a/<! ...)`. But it seems it's not necessary.

So:

#?(:clj (q/redraw)
   :cljs (let [applet quil.sketch/*applet*]
           (a/go
             (binding [quil.sketch/*applet* applet]
               (q/redraw)))))

Jason Fabris

unread,
Sep 26, 2020, 10:34:11 PM9/26/20
to clj-processing

I wish I could say something more intelligent. I haven't really tried quil with clojurescript. Can I ask what the "a" namespace is? 

Simon Katz

unread,
Sep 27, 2020, 6:33:30 AM9/27/20
to clj-processing
The `a` namespace is core.async: https://github.com/clojure/core.async

In my namespace declaration I have:

(:require
 #?(:clj [clojure.core.async :as a]
    :cljs [cljs.core.async :as a]))

Mikita Belahlazau

unread,
Sep 28, 2020, 1:30:12 PM9/28/20
to clj-processing
Hi Simon

The docs you are referring to describe Clojure behavior but not ClojureScript as you figured out already. The reason is that ClojureScript support has been added later and most docs stayed from the Clojure version. In your sketch I assume you pause drawing loop and want to redraw only on certain events? 

Mikita

--
You received this message because you are subscribed to the Google Groups "clj-processing" group.
To unsubscribe from this group and stop receiving emails from it, send an email to clj-processin...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/clj-processing/4fded317-47cd-485d-bb66-c0aec9849d18n%40googlegroups.com.

Simon Katz

unread,
Sep 29, 2020, 6:41:26 AM9/29/20
to clj-processing
Hi Mikita,

Thanks for the explanation about the docs.

Yes, you're right — I've called no-loop to stop the drawing loop. Sorry; I should have said so.

Simon

Mikita Belahlazau

unread,
Oct 3, 2020, 11:14:07 PM10/3/20
to clj-processing
Thanks Simon! I used approach inspired by your workaround to fix it in https://github.com/quil/quil/commit/dd0d1d6221129db3ebae2a34bc1dcb7976932ec4 

Also I checked p5js (which quil cljs uses underneath) and they also have the same issue with docs: https://p5js.org/reference/#/p5/redraw while implementation calls draw() implementation.

Reply all
Reply to author
Forward
0 new messages