2

I need to scale canvas (that have 1920px width and 1080px height) to fit screen width and keep its position on screen, like this: what i need

I tried apply CSS-rule transform: scale(0.6666) to canvas DOM, but I've got this: what i got

How can I fix this and get what I want?

Alex Solari
  • 127
  • 8

1 Answers1

1

OK if you want to use javascript:

You need to know the scale you want to use.

var originalWidth = document.getElementById('container').offsetWidth;
var originalHeight = document.getElementById('container').offsetHeight;

var scale1 = 1080/1920; //if width is always bigger than height
var scale2 = originalWidth/originalHeight; 

//var scale1 = 1080/1920; //if height is always bigger than width
//var scale2 = originalWidth/originalHeight; 

window.onresize = function(event) {
    var getWidth = document.getElementById('container').offsetWidth;

    document.getElementById(('container').setAttribute("style","height" + getWidth*scale1);
};

In your CSS set the width to 100% so it scales automatically and you use the javascript to set the height.

Scale1 is one you preset to keep it consistent, so is scale2 but I would only use scale2 if you resize when the screen is on. For example, on desktop, when resizing the window as you set the scale when it's first launched.

Here is a fiddle showing it working : http://jsfiddle.net/d4kVk/239/

EDIT :

Recently I ran in to how to do this in css with two simple lines :

width: 100vmax;
height:56.25vmax;

'vmax' gets the highest value out of width and height. So putting the value 56.25 for vmax in height (as in 1080/1920, i.e your aspect ration) solves your problem :)

Updated fiddle : http://jsfiddle.net/d4kVk/246/

thatOneGuy
  • 9,977
  • 7
  • 48
  • 90