简体   繁体   English

html5 canvas - 宽度/高度是否需要内联定义?

[英]html5 canvas - does the width/height need to be defined inline?

I've been messing around with HTML5 / Javascript and found something I don't understand. 我一直在搞乱HTML5 / Javascript,发现了一些我不理解的东西。

I was trying to write some basic routines for working with the HTML5 canvas, and found that the drawImage function wasn't drawing anything. 我正在尝试编写一些用于处理HTML5画布的基本例程,并发现drawImage函数没有绘制任何东西。 Figuring it was my code, I used code from an existing tutorial, namely HTML5 Canvas Image Tutorial . 弄清楚它是我的代码,我使用了现有教程中的代码,即HTML5 Canvas Image Tutorial When I included the Javascript code therein externally (ie as a separate file), I still wasn't seeing my image drawn to the canvas. 当我在外部包含Javascript代码(即作为单独的文件)时,我仍然没有看到我的图像被绘制到画布上。

From there, I copied the complete source, found here , verbatim, and it did work. 从那里,我复制完整的源代码,在这里找到,逐字逐句,它确实有效。 I started messing around to figure out why this worked but mine didn't. 我开始搞乱弄清楚为什么这样有效,但我没有。

I found that the difference was that the verbatim code defined the width and height of the canvas inline. 我发现不同之处在于逐字代码定义了画布内联的宽度和高度。 I prefer to separate styling out of HTML and into CSS. 我更喜欢将样式从HTML和CSS中分离出来。 The code worked if I did: 如果我做了以下代码:

<canvas id = "myCanvas" width = "600" height = "400"></canvas>

but did not work if I defined the canvas width and height in an external CSS stylesheet. 但如果我在外部CSS样式表中定义画布宽度和高度,则无效。

I did determine that one of my problems was that I was including the stylesheet after the Javascript file; 我确定我的一个问题是我在Javascript文件之后包含了样式表; before doing that, my image did not appear at all, even after confirming via an alert() function that the code inside the image onload handler was firing. 在这之前,我的图像根本没有出现,即使在通过alert()函数确认图像onload处理程序中的代码被触发之后也是如此。 After changing the order in which the files were included, my image did appear, but was scaled weirdly, as if the CSS styling was ignored. 在更改了包含文件的顺序后,我的图像确实出现了,但是很奇怪地缩放了,好像忽略了CSS样式。

Why is this? 为什么是这样? Am I missing something regarding the order in which canvas properties are defined via CSS? 我是否遗漏了有关通过CSS定义画布属性的顺序? Why does Javascript ignore the CSS styling in this case? 为什么Javascript在这种情况下会忽略CSS样式? In case it's at all relevant, I've tried this in the latest versions of both Chrome and Firefox. 如果它完全相关,我已经在Chrome和Firefox的最新版本中尝试了这一点。

You don't have to write them there, you can write them later in JavaSript: 您不必那里编写它们,稍后可以在JavaSript中编写它们:

var can = document.getElementById('canvas1');
can.width = 600;
can.height = 400;

The defaults are 300x150 by the way. 顺便提一下,默认值为300x150。

Note that you should never, ever use CSS to define your width and height because you will be scaling the canvas instead of resizing it, causing blurriness and a loss of proportion. 请注意,您永远不应该使用CSS来定义宽度和高度,因为您将缩放画布而不是调整大小,导致模糊和丢失比例。

Yes, they should be written in attributes, otherwise canvas actual size would be equal to 300 x 150 . 是的,它们应该用属性编写,否则画布的实际大小将等于300 x 150 And if you change its size with css, it will be stretched/rescaled as well as any other image. 如果你用css改变它的大小,它将被拉伸/重新缩放以及任何其他图像。

According to HTML5 specification it's not required: 根据HTML5规范,它不是必需的:

The canvas element has two attributes to control the size of the coordinate space: width and height. canvas元素有两个属性来控制坐标空间的大小:宽度和高度。 These attributes, when specified, must have values that are valid non-negative integers. 指定时,这些属性必须具有有效的非负整数值。 The rules for parsing non-negative integers must be used to obtain their numeric values. 必须使用解析非负整数的规则来获取它们的数值。 If an attribute is missing, or if parsing its value returns an error, then the default value must be used instead. 如果缺少某个属性,或者解析其值会返回错误,则必须使用默认值。 The width attribute defaults to 300, and the height attribute defaults to 150. width属性默认为300,height属性默认为150。

http://www.whatwg.org/specs/web-apps/current-work/multipage/the-canvas-element.html http://www.whatwg.org/specs/web-apps/current-work/multipage/the-canvas-element.html

elements have two different width and height attributes. 元素有两个不同的宽度和高度属性。

One is normally defined inline (or later in the javascript itself). 一个通常是内联定义的(或者稍后在javascript本身中定义)。 This is the actual pixel dimension of the canvas as used by the program for drawing purposes. 这是程序用于绘图目的的画布的实际像素尺寸。

One is the style width and height. 一个是样式的宽度和高度。 This can be defined by CSS (while the pixel width and height can not). 这可以通过CSS定义(而像素宽度和高度则不能)。

The simple answer is no. 简单回答是不。 You can set the width and height in your javascript if you prefer, but any height and width you assign via CSS will change the display size of the canvas (not the actual size) which may distort your image. 如果您愿意,可以在javascript中设置宽度和高度,但是通过CSS分配的任何高度和宽度都会改变画布的显示大小(而不是实际大小),这可能会使图像失真。

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

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