The Graphics Canvas Element - HTML: HyperText Markup Language
Có thể bạn quan tâm
- References
- HTML
- Elements
- <canvas>
- English (US)
- Remember language
- Deutsch
- Español
- Français
- 日本語
- 한국어
- Português (do Brasil)
- Русский
- 中文 (简体)
- 正體中文 (繁體)
- Attributes
- Usage notes
- Accessibility
- Examples
- Technical summary
- Specifications
- Browser compatibility
- See also
- HTML
- Tutorials
- HTML basics
- Introduction to HTML
- Introduction to HTML overview
- Getting started with HTML
- What's in the head? Metadata in HTML
- HTML text fundamentals
- Creating hyperlinks
- Advanced text formatting
- Document and website structure
- Debugging HTML
- Assessment: Marking up a letter
- Assessment: Structuring a page of content
- Multimedia and embedding
- Multimedia and embedding overview
- Images in HTML
- Video and audio content
- From object to iframe — other embedding technologies
- Adding vector graphics to the web
- Responsive images
- Assessment: Mozilla splash page
- HTML tables
- HTML tables overview
- HTML table basics
- HTML table advanced features and accessibility
- Assessment: Structuring planet data
- References
- HTML elements
- <a>
- <abbr>
- <acronym> Deprecated
- <address>
- <area>
- <article>
- <aside>
- <audio>
- <b>
- <base>
- <bdi>
- <bdo>
- <big> Deprecated
- <blockquote>
- <body>
- <br>
- <button>
- <canvas>
- <caption>
- <center> Deprecated
- <cite>
- <code>
- <col>
- <colgroup>
- <data>
- <datalist>
- <dd>
- <del>
- <details>
- <dfn>
- <dialog>
- <dir> Deprecated
- <div>
- <dl>
- <dt>
- <em>
- <embed>
- <fencedframe> Experimental
- <fieldset>
- <figcaption>
- <figure>
- <font> Deprecated
- <footer>
- <form>
- <frame> Deprecated
- <frameset> Deprecated
- <h1>
- <head>
- <header>
- <hgroup>
- <hr>
- <html>
- <i>
- <iframe>
- <img>
- <input>
- <ins>
- <kbd>
- <label>
- <legend>
- <li>
- <link>
- <main>
- <map>
- <mark>
- <marquee> Deprecated
- <menu>
- <meta>
- <meter>
- <nav>
- <nobr> Deprecated
- <noembed> Deprecated
- <noframes> Deprecated
- <noscript>
- <object>
- <ol>
- <optgroup>
- <option>
- <output>
- <p>
- <param> Deprecated
- <picture>
- <plaintext> Deprecated
- <portal> Experimental
- <pre>
- <progress>
- <q>
- <rb> Deprecated
- <rp>
- <rt>
- <rtc> Deprecated
- <ruby>
- <s>
- <samp>
- <script>
- <search>
- <section>
- <select>
- <slot>
- <small>
- <source>
- <span>
- <strike> Deprecated
- <strong>
- <style>
- <sub>
- <summary>
- <sup>
- <table>
- <tbody>
- <td>
- <template>
- <textarea>
- <tfoot>
- <th>
- <thead>
- <time>
- <title>
- <tr>
- <track>
- <tt> Deprecated
- <u>
- <ul>
- <var>
- <video>
- <wbr>
- <xmp> Deprecated
- Global attributes
- accesskey
- anchor Experimental Non-standard
- autocapitalize
- autocorrect Experimental
- autofocus
- class
- contenteditable
- data-*
- dir
- draggable
- enterkeyhint
- exportparts
- hidden
- id
- inert
- inputmode
- is
- itemid
- itemprop
- itemref
- itemscope
- itemtype
- lang
- nonce
- part
- popover
- slot
- spellcheck
- style
- tabindex
- title
- translate
- virtualkeyboardpolicy Experimental
- writingsuggestions
- Attributes
- accept
- autocomplete
- capture
- crossorigin
- dirname
- disabled
- elementtiming
- for
- max
- maxlength
- min
- minlength
- multiple
- pattern
- placeholder
- readonly
- rel
- required
- size
- step
- <input> types
- <input type="button">
- <input type="checkbox">
- <input type="color">
- <input type="date">
- <input type="datetime-local">
- <input type="email">
- <input type="file">
- <input type="hidden">
- <input type="image">
- <input type="month">
- <input type="number">
- <input type="password">
- <input type="radio">
- <input type="range">
- <input type="reset">
- <input type="search">
- <input type="submit">
- <input type="tel">
- <input type="text">
- <input type="time">
- <input type="url">
- <input type="week">
- Guides
-
- Content categories
- Block-level elements
- Inline elements
- Quirks Mode and Standards Mode
- Date and time formats used in HTML
- Constraint validation
- Microdata
- Microformats
- Viewport meta tag
- Allowing cross-origin use of images and canvas
- Attributes
- Usage notes
- Accessibility
- Examples
- Technical summary
- Specifications
- Browser compatibility
- See also
Use the HTML <canvas> element with either the canvas scripting API or the WebGL API to draw graphics and animations.
Attributes
This element's attributes include the global attributes.
heightThe height of the coordinate space in CSS pixels. Defaults to 150.
moz-opaque Non-standard DeprecatedLets the canvas know whether 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.
widthThe width of the coordinate space in CSS pixels. Defaults to 300.
Usage notes
Alternative content
You 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.
Closing </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 exact maximum size of a <canvas> element depends on the browser and environment. While in most cases the maximum dimensions exceed 10,000 x 10,000 pixels, notably iOS devices limit the canvas size to only 4,096 x 4,096 pixels. See canvas size limits in different browsers and devices.
Note: Exceeding the maximum dimensions or area renders the canvas unusable — drawing commands will not work.
Using an offscreen canvas
A canvas can be rendered using the OffscreenCanvas API where the document and canvas are decoupled. The benefit is that a worker thread can handle canvas rendering and the main thread of your web application is not blocked by canvas operations. By parallelizing work, other UI elements of your web application will remain responsive even if you are running complex graphics on an offscreen canvas. For more information, see the OffscreenCanvas API documentation.
Accessibility
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.
- Canvas accessibility use cases
- Canvas element accessibility issues
- HTML Canvas Accessibility in Firefox 13 – by Steve Faulkner
- Best practices for interactive canvas elements
Examples
HTML
This code snippet adds a canvas element to your HTML document. A fallback text is provided if a browser is unable to read or render the canvas.
html<canvas width="120" height="120"> 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:
jsconst canvas = document.querySelector("canvas"); const ctx = canvas.getContext("2d"); ctx.fillStyle = "green"; // Add a rectangle at (10, 10) with size 100x100 pixels ctx.fillRect(10, 10, 100, 100);Result
Technical summary
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 parents | Any element that accepts phrasing content. |
Implicit ARIA role | No corresponding role |
Permitted ARIA roles | Any |
DOM interface | HTMLCanvasElement |
Specifications
Specification |
---|
HTML Standard # the-canvas-element |
Browser compatibility
BCD tables only load in the browser
See also
- Canvas API
- Canvas tutorial
- OffscreenCanvas
- Canvas cheat sheet (2009)
- Canvas cheat sheet (pdf) (2015)
- Safari HTML canvas guide via Apple (2013)
- CanvasRenderingContext2D 2D drawing context for a canvas element via Apple.com
- WebGL API
- <img>
- SVG
- Using HTML audio and video
Help improve MDN
Was this page helpful to you?YesNoLearn how to contribute.This page was last modified on Jul 26, 2024 by MDN contributors.
View this page on GitHub • Report a problem with this contentTừ khóa » Html Canvas Graphics
-
HTML Canvas Graphics - W3Schools
-
HTML Canvas Drawing - W3Schools
-
Canvas Tutorial - Web APIs | MDN
-
How To Draw Graphics On HTML5 Canvas - Tutorial Republic
-
25 Ridiculously Impressive HTML5 Canvas Experiments
-
HTML Canvas Graphics - Dofactory
-
HTML Canvas Tutorial For Beginners - Simplilearn
-
Draw On HTML5 Canvas Using A Mouse - Stack Overflow
-
Drawing On Canvas - Eloquent JavaScript
-
Create Graphics With HTML Canvas
-
Learn HTML5 Canvas By Creating A Drawing App - YouTube
-
HTML5 Canvas Graphics And Animations - Udemy
-
Canvas - Dive Into HTML5