简体   繁体   English

HTML5:调整画布大小

[英]HTML5: Resizing a canvas

All this time I worked on an app in a 800x480 resolution (popular phone resolution). 一直以来,我一直在以800x480分辨率(手机分辨率)开发应用程序。

My canvas HTML: 我的画布HTML:

<canvas id="main" width="800" height="480">

Now to make it fullscreen for other resolutions, I do the following after a resize event: 现在要使其全屏显示其他分辨率,我在调整大小事件之后执行以下操作:

$('#main').css('height', window.innerHeight);
$('#main').css('width', window.innerWidth);

This works but the problem is I can't maintain the aspect ratio this way. 这可行,但是问题是我不能以这种方式保持宽高比。 800x480 is 4:3, but if I run this app on a 5:3 phone, some things (especially circles) will look stretched. 800x480是4:3,但是如果我在5:3的手机上运行此应用,则某些内容(尤其是圆圈)会显得很拉伸。

Is there any way to make it look good on all resolutions without having to create a unique set of images and code for every aspect ratio? 有什么方法可以使它在所有分辨率下看起来都不错,而不必为每个纵横比都创建一组独特的图像和代码?

CSS properties width and height stretch the canvas, in order to scale the board, you must use .attr instead of .css , this will work, but, something to note: CSS属性widthheight拉伸画布,为了缩放.attr ,必须使用.attr而不是.css ,这将起作用,但是,需要注意以下几点:

If you want the canvas to look perfect on mobile phones, too, you must pay attention to window.devicePixelRatio . 如果您也希望画布在手机上看起来完美,则必须注意window.devicePixelRatio

What's devicePixelRatio 什么是设备像素比

MDN: MDN:

The devicePixelRatio property returns the ratio between physical pixels and device independent pixels in the current display. devicePixelRatio属性返回当前显示中物理像素与设备独立像素之间的比率。

What does it mean? 这是什么意思? it means how many pixels does a device show, in one physical pixel. 它表示设备在一个物理像素中显示多少个像素。

If a device's pixel ratio is 2, it means two graphical pixels are drawn in a single physical pixel. 如果设备的像素比例为2,则意味着在单个物理像素中绘制了两个图形像素。

In order to make your canvas look perfect, you must do this trick: 为了使您的画布看起来完美,您必须执行以下操作:

set width and height of canvas to the amount you want multiplied by devicePixelRatio, ie 将画布的宽度和高度设置为要乘以devicePixelRatio的数量,即

$('canvas').attr({
  'width': window.innerWidth*devicePixelRatio,
  'height': window.innerHeight*devicePixelRatio
});

Then, scale your canvas down with CSS: 然后,使用CSS缩小画布:

$('canvas').css({
  'width': window.innerWidth + 'px',
  'height': window.innerHeight + 'px'
});

It seems innerWidth and innerHeight are not right at first, I tried wrapping the code in a 100 milisecond setTimeout and it worked, I guess you can reduce the delay to something like 10 miliseconds, test it. 似乎innerWidthinnerHeight ,我尝试将代码包装在100毫秒的setTimeout中,并且可以正常工作,我想您可以将延迟减少到10毫秒左右,然后进行测试。

Fiddle: http://jsfiddle.net/mdibaiee/7o4tLr4L/ 小提琴: http : //jsfiddle.net/mdibaiee/7o4tLr4L/

To test on your mobile: http://fiddle.jshell.net/mdibaiee/7o4tLr4L/show/ 要在您的手机上进行测试: http : //fiddle.jshell.net/mdibaiee/7o4tLr4L/show/

You can use @media for different resolutions like tablets and phones for example 您可以将@media用于不同的分辨率,例如平板电脑和手机

@media only screen and (max-device-width : 640px) {
/* Styles */
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM