Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Objects fail to render when changing the render bounds #595

Closed
nukadelic opened this issue Apr 1, 2018 · 3 comments
Closed

Objects fail to render when changing the render bounds #595

nukadelic opened this issue Apr 1, 2018 · 3 comments

Comments

@nukadelic
Copy link

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, body 
{
  padding: 0;
  margin: 0;
  min-height: 100%;
  overflow: hidden;
}

HTML

<body>
  <div><canvas></canvas></div>
</body>

Script

Constructor

// ...
    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.

See also: #408

@nukadelic
Copy link
Author

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 )

@andreasjeppsson
Copy link

andreasjeppsson commented Nov 10, 2020

Hi, I have the same issue. Did you manage to fix this? @liabru @nukadelic

@andreasjeppsson
Copy link

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.

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

No branches or pull requests

3 participants