使用
HTML
<canvas>
element
采用
画布脚本 API
或
WebGL API
绘制图形和动画。
| 内容类别 | 流内容 , 措词内容 , embedded content ,可触及内容。 |
|---|---|
| 准许内容 |
Transparent but with no
interactive content
descendants except for
<a>
elements,
<button>
elements,
<input>
elements whose
type
属性为
checkbox
,
radio
,或
button
.
|
| Tag omission | None, both the starting and ending tag are mandatory. |
| Permitted parents | Any element that accepts 措词内容 . |
| Implicit ARIA role | 无对应角色 |
| Permitted ARIA roles | 任何 |
| DOM 接口 |
HTMLCanvasElement
|
This element's attributes include the 全局属性 .
height
The height of the coordinate space in CSS pixels. Defaults to 150.
moz-opaque
canvas.getContext('2d', { alpha: false })
代替。
width
The width of the coordinate space in CSS pixels. Defaults to 300.
You may (and should) provide alternate content inside the
<canvas>
block. That content will be rendered both on older browsers that don't support canvas and in browsers with JavaScript disabled. Providing a useful fallback text or sub DOM helps to
make the the canvas more accessible
.
不像
<img>
element, the
<canvas>
element
requires
the closing tag (
</canvas>
).
The displayed size of the canvas can be changed using CSS, but if you do this the image is scaled during rendering to fit the styled size, which can make the final graphics rendering end up being distorted.
It is better to specify your canvas dimensions by setting the
width
and
height
attributes directly on the
<canvas>
elements, either directly in the HTML or by using JavaScript.
The maximum size of a
<canvas>
element is very large, but the exact size depends on the browser. The following is some data we've collected from various tests and other sources (e.g.
堆栈溢出
):
| Browser | Maximum height | Maximum width | Maximum area |
|---|---|---|---|
| Chrome | 32,767 pixels | 32,767 pixels | 268,435,456 pixels (i.e., 16,384 x 16,384) |
| Firefox | 32,767 pixels | 32,767 pixels | 472,907,776 pixels (i.e., 22,528 x 20,992) |
| Safari | 32,767 pixels | 32,767 pixels | 268,435,456 pixels (i.e., 16,384 x 16,384) |
| IE | 8,192 pixels | 8,192 pixels | ? |
注意 : Exceeding the maximum dimensions or area renders the canvas unusable — drawing commands will not work.
This code snippet adds a canvas element to your HTML document. A fallback text is provided if a browser is unable to render the canvas, or if can't read a canvas.
<canvas width="300" height="300"> An alternative text describing what your canvas displays. </canvas>
Then in the JavaScript code, call
HTMLCanvasElement.getContext()
to get a drawing context and start drawing onto the canvas:
const canvas = document.querySelector('canvas');
const ctx = canvas.getContext('2d');
ctx.fillStyle = 'green';
ctx.fillRect(10, 10, 100, 100);
<canvas>
element on its own is just a bitmap and does not provide information about any drawn objects. Canvas content is not exposed to accessibility tools as semantic HTML is. In general, you should avoid using canvas in an accessible website or app. The following guides can help to make it more accessible.
| 规范 | 状态 | 注释 |
|---|---|---|
|
HTML 实时标准
The definition of '<canvas>' in that specification. |
实时标准 | |
|
HTML5
The definition of '<canvas>' in that specification. |
推荐 | 初始定义 |
| 桌面 | 移动 | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
canvas
|
Chrome 完整支持 1 | Edge 完整支持 12 |
Firefox
完整支持
1.5
注意事项
|
IE 完整支持 9 | Opera 完整支持 9 |
Safari
完整支持
2
注意事项
|
WebView Android 完整支持 37 | Chrome Android 完整支持 18 |
Firefox Android
完整支持
4
注意事项
|
Opera Android 完整支持 10.1 | Safari iOS 完整支持 1 | Samsung Internet Android 完整支持 1.0 |
height
|
Chrome 完整支持 1 | Edge 完整支持 12 |
Firefox
完整支持
1.5
注意事项
|
IE 完整支持 9 | Opera 完整支持 9 |
Safari
完整支持
2
注意事项
|
WebView Android 完整支持 37 | Chrome Android 完整支持 18 |
Firefox Android
完整支持
4
注意事项
|
Opera Android 完整支持 10.1 | Safari iOS 完整支持 1 | Samsung Internet Android 完整支持 1.0 |
moz-opaque
非标
|
Chrome 不支持 No | Edge 不支持 No | Firefox 完整支持 3.5 | IE 不支持 No | Opera 不支持 No | Safari 不支持 No | WebView Android 不支持 No | Chrome Android 不支持 No | Firefox Android 完整支持 4 | Opera Android 不支持 No | Safari iOS 不支持 No | Samsung Internet Android 不支持 No |
width
|
Chrome 完整支持 1 | Edge 完整支持 12 |
Firefox
完整支持
1.5
注意事项
|
IE 完整支持 9 | Opera 完整支持 9 |
Safari
完整支持
2
注意事项
|
WebView Android 完整支持 37 | Chrome Android 完整支持 18 |
Firefox Android
完整支持
4
注意事项
|
Opera Android 完整支持 10.1 | Safari iOS 完整支持 1 | Samsung Internet Android 完整支持 1.0 |
完整支持
不支持
非标。预期跨浏览器支持较差。
见实现注意事项。
<canvas>
<noscript>
<script>