Jump to content

Scale game to div


Recommended Posts


I've seen a few examples of a canvas scaling to a div, for example:

(You'll need to click play)

When the browser window is reduced in size (< width of 700px ish) the game scales rather nicely to the containing div.

Can anyone advise how the following is achieved? Css or Js?


Link to comment
Share on other sites

Both. To give you an idea, you can set the canvas's style with Javascript like this:

var canvas = document.getElementById("canvas");
var div = document.getElementById("gamediv");

canvas.style.width = gamediv.getBoundingClientRect().width + "px";
canvas.style.height = gamediv.getBoundingClientRect().height + "px";
canvas.width = parseFloat(canvas.style.width);
canvas.height = parseFloat(canvas.style.height);


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