I want to render custom HTML elements as Bodies in Matter.js. I am using it in React which adds a bit of complexity but it's irrelevant to my issue.
I've searched a lot and the only example I found was this one here, which seems to use querySelector
to select the elements that live in the HTML code then somehow use them inside the rectangle shapes.
The part that seems to be doing the job is the following:
var bodiesDom = document.querySelectorAll('.block');
var bodies = [];
for (var i = 0, l = bodiesDom.length; i < l; i++) {
var body = Bodies.rectangle(
VIEW.centerX,
20,
VIEW.width*bodiesDom[i].offsetWidth/window.innerWidth,
VIEW.height*bodiesDom[i].offsetHeight/window.innerHeight
);
bodiesDom[i].id = body.id;
bodies.push(body);
}
World.add(engine.world, bodies);
(the VIEW
variables there could be just random numbers as they define the shape)
However, I cannot understand how to pass an HTML element inside the Bodies rectangle as in the example above.
Ideally, I want to have complex HTML elements interacting with the physics world (like a small box with buttons, etc).
Any ideas on how this could be achieved? Or, can you explain the method used in the example that seems to have managed it?