翻譯不完整。請協助 翻譯此英文文件。
這個 HTML <canvas>
元素 可以被使用於對腳本程式(JaveScript) 繪圖 .舉例來說, 它能用來畫圖,組合照片,甚至作動畫. 你也許(應該) 在<canvas>區段內提供替代方案的內容 .這內容將能被一些不支援<canvas>以及無法使用Javescript功能的瀏覽器所渲染 .
For more articles on canvas, see the canvas topic page.
Content categories | Flow content, phrasing content, embedded content, palpable content. |
---|---|
Permitted content | Transparent but with no interactive content descendants except for <a> elements, <button> elements, <input> elements whose type attribute is checkbox , radio , or button . |
Tag omission | None, both the starting and ending tag are mandatory. |
Permitted parent elements | Any element that accepts phrasing_content. |
DOM interface | HTMLCanvasElement |
Attributes
This element includes the global attributes.
height
- The height of the coordinate space in CSS pixels. Defaults to 150.
moz-opaque
- Lets the canvas know whether or not translucency will be a factor. If the canvas knows there's no translucency, painting performance can be optimized.
width
- The width of the coordinate space in CSS pixels. Defaults to 300.
Description
Required </canvas>
tag
Unlike the <img>
element, the <canvas>
element requires the closing tag (</canvas>
).
Sizing the canvas
The displayed size of the canvas can be changed using a stylesheet. The image is scaled during rendering to fit the styled size. If your renderings seem distorted, try specifying your width
and height
attributes explicitly in the <canvas>
attributes, and not using CSS.
Examples
<canvas id="canvas" width="300" height="300"> Sorry, your browser doesn't support the <canvas> element. </canvas>
If your canvas does not use transparency set the moz-opaque
attribute on the canvas tag. This information can be used internally to optimize rendering. However, this attribute has not been standardized and only works in Mozilla-based rendering engines.
<canvas id="mycanvas" moz-opaque></canvas>
Specifications
Specification | Status | Comment |
---|---|---|
WHATWG HTML Living Standard The definition of '<canvas>' in that specification. |
Living Standard | |
HTML5 The definition of '<canvas>' in that specification. |
Recommendation | Initial definition |
Browser compatibility
Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|
Basic support | 1.0 | 1.5 (1.8)[1] 6.0 (6.0)[2] 12.0 (12.0)[3] |
9.0 | 9.0[4] | 2.0[5] |
moz-opaque |
Not supported | 3.5 (1.9.1) | Not supported | Not supported | Not supported |
Feature | Firefox Mobile (Gecko) | Android | IE Mobile | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|
Basic support | 1.5 (1.8)[1] 6.0 (6.0)[2] 12.0 (12.0)[3] |
? | ? | ? | 1.0 |
moz-opaque |
1.0 (1.9.1) | Not supported | Not supported | Not supported | Not supported |
[1] Before Gecko 5.0 (Firefox 5.0 / Thunderbird 5.0 / SeaMonkey 2.2), the canvas width and height were signed integers instead of unsigned integers.
[2] Prior to Gecko 6.0 (Firefox 6.0 / Thunderbird 6.0 / SeaMonkey 2.3), a <canvas>
element with a zero width or height would be rendered as if it had default dimensions.
[3] Before Gecko 12.0 (Firefox 12.0 / Thunderbird 12.0 / SeaMonkey 2.9), if JavaScript is disabled, the <canvas>
element was being rendered instead of showing the fallback content as per the specification. Now the fallback content is rendered instead.
[4] See the changelog for Opera 9.0.
[5] Although early versions of Apple's Safari browser don't require the closing tag, the specification indicates that it is required, so you should be sure to include it for broadest compatibility. Those versions of Safari (prior to version 2.0) will render the content of the fallback in addition to the canvas itself unless you use CSS tricks to mask it. Fortunately, users of these versions of Safari are rare nowadays.
See also
- MDN canvas portal
- Canvas tutorial
- Canvas cheat sheet
- Canvas-related demos
- Canvas introduction by Apple