Matter-js: How to change where canvas will be rendered in DOM?

Created on 2 Dec 2016  路  5Comments  路  Source: liabru/matter-js

I am using the default Render.js and cannot figure out how to create my own canvas and have the game take place in there. I just want to center the canvas in the DOM and stylize around it. Thanks.

question

Most helpful comment

Thank you @kroko ! It was a stupid mistake. I had

Its working now!

All 5 comments

Render.create({
  canvas: document.querySelector('.canvas-physics')
});

source: http://brm.io/matter-js/docs/classes/Render.html#properties

What @kroko shows should work (thanks). Also take a look at the Rendering wiki page.

Thank you kroko and liabru. I tried what you suggested, but I am still getting an error:

matter.js:7536 Uncaught TypeError: render.canvas.getContext is not a function
at Object.Render.create

This is what I have:

// create a renderer
var render = Render.create({
element: document.body,
canvas: document.querySelector('#myCanvas'),
engine: engine,
options: {
background:"#2b2b2b",
width: 600,
height: 960,
wireframes: false,
showPositions: false,
showAxes: false,
hasBounds: false,
showDebug: false
}
});

Do not specify element opt, if you specify canvas opt!

Does HTML has

<canvas id="myCanvas"></canvas>

and do you execute JS after DOM has been loaded?

Thank you @kroko ! It was a stupid mistake. I had

Its working now!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

elrodsj picture elrodsj  路  3Comments

djipco picture djipco  路  4Comments

christianmalek picture christianmalek  路  4Comments

ShadewEnder picture ShadewEnder  路  3Comments

253153 picture 253153  路  3Comments