简体   繁体   English

设置画布宽度100%和最大宽度

[英]Setting canvas width 100% and max-width

I want to set width 100% and limit it with max-width to my canvas element. 我想将宽度设置为100%,并使用max-width将其限制为我的canvas元素。 Regardless of canvas width I want to have static canvas height. 无论画布宽度如何,我都希望具有静态画布高度。 But when I set it with css, it seems that browser draws canvas with standard siza and rezises canvas to 100% width and proporionaly it resises height. 但是,当我使用CSS进行设置时,似乎浏览器使用标准的siza绘制画布,并将画布调整为100%的宽度,并且按比例调整其高度。 But the worst is that it scales the picture inside the canvas. 但最糟糕的是它会缩放画布内的图片。 I also tried to set css width property of canvas from JS but it led to the same result. 我还尝试从JS设置canvas的css width属性,但结果相同。

<canvas id="myCanvas" width="100%" height="630px"></canvas>

makes canvas with 100px width and 630px height 制作宽度为100px,高度为630px的画布

<canvas id="myCanvas"></canvas>

and css 和CSS

#myCanvas {
    width:100%;
    height:630px;
}

makes canvas 100% width but proportionaly resizes height and scales an image inside canvas. 使画布的宽度为100%,但按比例调整高度,并缩放画布内的图像。 Setting css with JS does the same. 用JS设置css的功能相同。

How am I supposed to do this? 我应该怎么做?

The drawing API dimensions for the canvas element does not need to match the CSS dimensions that it occupies on the screen. canvas元素的图形API尺寸不需要与它在屏幕上占用的CSS尺寸匹配。 This allows one to do sub pixel graphics if they so desired, (to create smooth motion with anti aliasing). 这样一来,便可以根据需要进行子像素图形处理(以创建具有抗锯齿的平滑运动)。 So the canvas width of 100% means nothing it wants the number of pixels it should be using, I believe you get the default width of 400 as if no width was used at all. 因此,画布宽度为100%意味着它不需要它应该使用的像素数,我相信您会获得默认宽度400,就好像根本没有使用宽度一样。 However it can be changed with javascript to any width you would like. 但是,可以使用javascript将其更改为任意宽度。

var canvas = document.getElementsByTagName('canvas')[0];
canvas.width  = $(window).width(); 
canvas.height = 630;

Or to do anti-aliasing. 或做抗锯齿。

var canvas = document.getElementsByTagName('canvas')[0];
canvas.width  = $(window).width()*2; 
canvas.height = 630*2;

Set the canvas width to a number higher that the space it has on the screen. 将画布宽度设置为大于其在屏幕上的空间的数字。 Each canvas pixel only takes up 1/2 an actual pixel and movement looks more video like. 每个画布像素仅占用实际像素的1/2,并且运动看起来更像视频。

Update added jsfiddle 更新添加的jsfiddle

http://jsfiddle.net/juaovd7o/ http://jsfiddle.net/juaovd7o/

<canvas id="myCanvas" width="284px" height="120px">hello</canvas>
<canvas id="myCanvas2" style="width:284px;height:120px;"></canvas>
<img src="http://whitedove-coding.com/static/whitedove-829.png" id="dove">

 var canvas = document.getElementById('myCanvas');
      var context = canvas.getContext('2d');
      context.beginPath();
      context.rect(0, 0, 284, 120);
      context.lineWidth = 4;
      context.strokeStyle = 'black';
      context.stroke();
 var canvas2 = document.getElementById('myCanvas2');
      canvas2.width=568;
      canvas2.height=240;
      var context2 = canvas2.getContext('2d');
      context2.beginPath();
      context2.rect(0, 0, 568, 240);
      context2.lineWidth = 4;
      context2.strokeStyle = 'black';
      context2.stroke();
var img=document.getElementById("dove");
      context2.drawImage(img,0,0);
      context.drawImage(img,0,0);

The width attrib will set both the css and the API width if they are not set by other means, but 100% width is not valid for the API so the API falls back to its default width of 400px. 如果未通过其他方式设置,则宽度属性将同时设置css和API宽度,但是100%的宽度对API无效,因此API会退回到其默认宽度400px。

Per your question, you need to use both the CSS width of 100% and set the canvas API with or to whatever number of pixels that will be? 根据您的问题,您需要同时使用100%的CSS宽度,并将canvas API设置为或设置为多少像素? jquery $(window).width() gives us the pixel count of 100%. jQuery $(window).width()给我们的像素数为100%。

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

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