hoplon

danielneal 2024-06-12T09:40:49.331719Z

I want to use matter in my hoplon + reveal presentation to do some rigid body demos. I’m thinking of something like the following - but I’m guessing init-matter! will fail here because the dom element doesn’t exist yet (it needs the dom element that it plans to render into) How do I structure this?

(defn init-matter!
  "Initializes matter in the provided id, returns state"
  [id]
  (let [Engine (.-Engine Matter)
        Render (.-Render Matter)
        Runner (.-Runner Matter)
        Bodies (.-Bodies Matter)
        Composite (.-Composite Matter)
        engine (.create Engine)
        runner (.create Runner)
        render (.create Render
                         #js {:element (.getElementById js/document id)
                              :engine engine
                              :wireframes false})]
    (.run Render render)
    (.run Runner runner engine)
    {:render render
     :engine engine
     :runner runner}))

(defn visualization
  []
  (let [state (init-matter! "visualization")]
    (slide
      (h/p :class (css :text-6xl) "Visualization")
      (h/div
        :style {:width "300px"
                :height "300px"}
        :id "visualization"))))

danielneal 2024-06-12T10:48:10.042149Z

^ ended up using with-init! for this - is that wrong?

2024-06-12T13:20:34.253659Z

If you will add the element when first constructing the page it will be fine. If you are adding the element in a dynamic way it will be better to use when-dom or with-dom. I have an example using when-dom https://github.com/nomotosolucoes/ritmo-de-aprender/blob/main/src/aprender/view.cljs#L137

danielneal 2024-06-12T13:36:49.224699Z

Ah interesting, thanks! What’s the difference between when-dom and with-dom?

2024-06-12T13:47:05.790319Z

when-dom is a function and it receives an anonymous function as argument, with-dom is a macro and you pass it forms that will be run, the macro wraps it in an function and calls when-dom. https://github.com/hoplon/hoplon/blob/master/src/hoplon/core.clj#L195-L198

danielneal 2024-06-12T13:58:39.755679Z

ah great, thank you 🙂