Skip to content

Instantly share code, notes, and snippets.

@nrfm
Last active September 15, 2020 19:36
Show Gist options
  • Save nrfm/e3c7012674614206717299506d877950 to your computer and use it in GitHub Desktop.
Save nrfm/e3c7012674614206717299506d877950 to your computer and use it in GitHub Desktop.
(defn sketch
[]
(let [engine' (.-Engine js/Matter)
render' (.-Render js/Matter)
world (.-World js/Matter)
bodies (.-Bodies js/Matter)
body (.-Body js/Matter)
engine (.create engine')
add (fn []
(let [box (.rectangle bodies 140, 0, (+ 10 (rand-int 80)), (+ 10 (rand-int 80)))]
(.add world (.-world engine) #js[box])
)
)
add-b (fn []
(let [box (.circle bodies 140, 0, (+ 10 (rand-int 40)), (+ 10 (rand-int 40)))]
(.add world (.-world engine) #js[box])
)
)
]
(r/create-class
{:component-did-mount
(fn [this]
(let [render (.create render'
#js{
:element (r/dom-node this)
:engine engine,
:options #js{
:width 800,
:height 400,
:wireframes false
}
})
box-a (.rectangle bodies 140, 0, 8, 80)
circles (keep-indexed
(fn [idx i] (.circle bodies 130, 0, (* idx 2), 1))
(take 3 (range)))
ground (.rectangle bodies 400, 380, 810, 60 #js{:isStatic true})
ramp (.rectangle bodies 140, 180, 210, 10 #js{:isStatic true})
]
(.rotate body ramp (/ (.-PI js/Math) ))
(.add world (.-world engine) (clj->js
(concat [box-a
ground
ramp] circles)))
(.run engine' engine)
(.run render' render)
)
)
:reagent-render (fn []
[:div
[:div
[ant/button {:on-click add} "sq"]
[ant/button {:on-click add-b} "ball"]]])})))
[:div
[:h1 "Simple Matter-js example"]
[h/js-loader
{:scripts {#(exists? js/Matter) "https://cdnjs.cloudflare.com/ajax/libs/matter-js/0.14.2/matter.min.js"}
:loading [:div "loading"]
:loaded [:div [sketch]]}]
]
@nrfm
Copy link
Author

nrfm commented Sep 15, 2020

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment