liabru / matter-js

a 2D rigid body physics engine for the web ▲● ■
MIT License
16.86k stars 1.97k forks source link

Add canvas to engine after its creation #229

Closed iamjoshua closed 8 years ago

iamjoshua commented 8 years ago

I'm integrating MatterJs into a react application that I've developed. I just ran into issue #226 . Since react first modifies its virtual dom before it updates the actual dom, the dom element doesn't exist when the engine is created, so an error is thrown. If I initialize the engine without providing an element, I get a warning and am unable to add the element later.

What is the best way to get around this currently? Is there some way to initialize the engine without an element but then add one after the dom has been updated? I've tried the following:

this.engine.render.canvas = document.getElementById('projectEngine');

It seems to be ignored even though inspecting the engine object shows that it has been set properly. I could initialize the engine after the dom has been updated, but this breaks my component hierarchy since child components add bodies to the world.

liabru commented 8 years ago

Try

engine.render = Matter.Render.create({ element: element })

Where element is where the new canvas will be appended.

iamjoshua commented 8 years ago

That did the trick! Thanks!