Sign in to follow this  
trinoo

proper canvas resizing method

Recommended Posts

Guys, sorry for bothering with this question, I found a few answers to this problem, but im still not able to get it to work.

I have made a complex app in Pixi, but now Im fighting with a trivial problem, how to properly set up HTML file and resize function to let my canvas resize to actual window size.

Now my HTML file contains one div:

<div id="floorplanner" class="pixi"></div>

here is css file:

body {
    position: absolute;
    height:100%;
    width:100%;
    padding: 0;
    margin: 0;
    overflow: hidden;
}

canvas {
    padding: 0;
    margin: 0;
}

.pixi {
    height: 100%;
    width: 100%;
    overflow: hidden;
}

I append canvas to this div using:

document.getElementById("floorplanner").appendChild(renderer.view);

and finally I have attached function scaleToWindow(renderer.view, "#CCCCCC");

(function source bellow) to window "resize" event:

  //Center and scale the game engine inside the HTML page 
  function scaleToWindow(canvas, backgroundColor) {

    backgroundColor = backgroundColor || "#2C3539";
    var scaleX, scaleY, scale, center;
	
	//1. Scale the canvas to the correct size
    scaleX = window.innerWidth / canvas.width;
    scaleY = window.innerHeight / canvas.height;

    //Scale the canvas based on whichever value is less: `scaleX` or `scaleY`
    scale = Math.min(scaleX, scaleY);
    canvas.style.transformOrigin = "0 0";
    canvas.style.transform = "scale(" + scale + ")";

    //3. Remove any padding from the canvas  and body and set the canvas
    //display style to "block"
    canvas.style.paddingLeft = 0;
    canvas.style.paddingRight = 0;
    canvas.style.paddingTop = 0;
    canvas.style.paddingBottom = 0;
    canvas.style.display = "block";
    
    //4. Set the color of the HTML body background
    document.body.style.backgroundColor = backgroundColor;    

    //Fix some quirkiness in scaling for Safari
    var ua = navigator.userAgent.toLowerCase(); 
    if (ua.indexOf("safari") != -1) { 
      if (ua.indexOf("chrome") > -1) {
        // Chrome
      } else {
        // Safari
        canvas.style.maxHeight = "100%";
        canvas.style.minHeight = "100%";
      }
    }

    return scale;

  };

It works somehow (buggy), but according to this topic 

I should use renderer.resize(window.innerWidth, window.innerHeight); metod, but im not able to iplement it. Can anybody please help or provide a few rows simple sample of proper canvas+HTML setting up?

I also found this article http://www.williammalone.com/articles/html5-game-scaling/, but the William uses plain image in showcase, but I dont know, how to change his image to my canvas to get it to work...or simply how to implement it...

Share this post


Link to post
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.

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

Loading...
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.