<canvas>: The Graphics Canvas element - HTML: HyperText Markup Language 编辑

Use the HTML <canvas> element with either the canvas scripting API or the WebGL API to draw graphics and animations.

Content categoriesFlow content, phrasing content, embedded content, palpable content.
Permitted contentTransparent but with no interactive content descendants except for <a> elements, <button> elements, <input> elements whose type attribute is checkbox, radio, or button.
Tag omissionNone, both the starting and ending tag are mandatory.
Permitted parentsAny element that accepts phrasing content.
Implicit ARIA roleNo corresponding role
Permitted ARIA rolesAny
DOM interfaceHTMLCanvasElement

Attributes

This element's attributes include the global attributes.

height
The height of the coordinate space in CSS pixels. Defaults to 150.
moz-opaque This API has not been standardized. This deprecated API should no longer be used, but will probably still work.
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. This is only supported by Mozilla-based browsers; use the standardized canvas.getContext('2d', { alpha: false }) instead.
width
The width of the coordinate space in CSS pixels. Defaults to 300.

Usage notes

Alternative content

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 canvas more accessible.

Required </canvas> tag

Unlike the <img> element, the <canvas> element requires the closing tag (</canvas>).

Sizing the canvas using CSS versus HTML

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.

Maximum canvas size

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. Stack Overflow):

BrowserMaximum heightMaximum widthMaximum area
Chrome32,767 pixels32,767 pixels268,435,456 pixels (i.e., 16,384 x 16,384)
Firefox32,767 pixels32,767 pixels472,907,776 pixels (i.e., 22,528 x 20,992)
Safari32,767 pixels32,767 pixels268,435,456 pixels (i.e., 16,384 x 16,384)
IE8,192 pixels8,192 pixels?

Note: Exceeding the maximum dimensions or area renders the canvas unusable — drawing commands will not work.

Examples

HTML

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>

JavaScript

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);

Result

Accessibility concerns

Alternative content

The <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.

Specifications

SpecificationStatusComment
HTML Living Standard
The definition of '<canvas>' in that specification.
Living Standard
HTML5
The definition of '<canvas>' in that specification.
RecommendationInitial definition

Browser compatibility

BCD tables only load in the browser

See also

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

词条统计

浏览:88 次

字数:11803

最后编辑:8年前

编辑次数:0 次

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文