You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
My objective is to preserve aspect ratio while user resizes browser window ( this is needed for portrait / landscape orientation support for mobile ). The problem appears when matter-js is being scaled, and some of the bodies stop being rendered and disappear when they approach the edges of the screen ( sometimes this also happens in the middle of the screen )
// ...
this.container = container; // div element reference
this.canvas = canvas; // canvas element reference
this.engine = Engine.create();
this.render = Render.create({
element: container,
canvas: this.canvas,
engine: this.engine,
options:
{
hasBounds: true,
width: 400,
height: 400,
background: '#AAA',
}
});
Render.run(this.render);
Engine.run(this.engine);
// ...
// add a simple physics objects
// ...
// reset min bounds ( ? )
this.render.bounds.min = { x:0,y:0 };
// Save starting render bounds to calc scale ratio later
this.renderBounds =
// Autofit max bounds to current render bounds
this.render.bounds.max =
{
x : this.render.options.width,
y : this.render.options.height
};
// Initial render bounds ratio
this.renderRatio = this.renderBounds.x / this.renderBounds.y;
// attach event listener & force event resize
window.addEventListener("resize", this.eResize.bind( this ) );
this.eResize();
// ...
Resize event
// On browser resize, change canvas size to fit full width
function eResize()
{
// resize canvas
this.canvas.width = window.innerWidth;
this.canvas.height = window.innerWidth * this.renderRatio;
// Current screen scale ratio relative to starting canvas size
var screenRatio = this.renderBounds.x / window.innerWidth;
// calculate new render bounds based on old size times new ratio
this.render.bounds.max =
{
x : this.renderBounds.x * screenRatio,
y : this.renderBounds.y * screenRatio
};
}
I have also tried changing Render.lookAt & world.bounds but the issue doesn't seem to go away.
I did get the aspect ratio working properly in this, see demo: https://matter-genetic-car.stackblitz.io, but there still is an issue with non rendering objects within the visible screen bounds, try changing the window size to see the bug ( Source code )
I solved this by commenting out this row in Render.js (:346) Render.startViewTransform(render);
Now I can set the render bounds without everything being scaled.
My objective is to preserve aspect ratio while user resizes browser window ( this is needed for portrait / landscape orientation support for mobile ). The problem appears when matter-js is being scaled, and some of the bodies stop being rendered and disappear when they approach the edges of the screen ( sometimes this also happens in the middle of the screen )
CSS
HTML
Script
Constructor
Resize event
I have also tried changing
Render.lookAt
&world.bounds
but the issue doesn't seem to go away.See also: #408
The text was updated successfully, but these errors were encountered: