Jump to content

Canvas on resize - keeping the same viewport


Recommended Posts


Because of performance and blurry text/image issues, I turned my fixed canvas resolution to a canvas with width/height dynamically equal to window.innerWidth/Height on resize.

Admitting the same aspect ratio after resizing, how would you deal with the renderer to keep the same viewport ?


Link to comment
Share on other sites

var aspect = 16/9;
var w = window.innerWidth;
var h = window.innerHeight;
if(w > h * aspect)
	//Height is the limiting factor
	w = h*aspect;
	//Width is the limiting factor
	h = w*1/aspect;
renderer.resize(w, h);

The aspect ratio isn't totally correct after resize due to rounding errors.

After that just use css to center the canvas and maybe add some nice ornaments (or something) to the edges with extra space.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...