mozilla
Your Search Results

    <canvas>

    Summary

    The HTML <canvas> Element can be used to draw graphics via scripting (usually JavaScript). For example, it can be used to draw graphs, make photo compositions or even perform animations. 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.

    For more articles on canvas, see the canvas topic page.

    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 &lt;canvas&gt; element.
    </canvas>
    

    If you use canvas and doesn’t need to be transparent, 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 does only work 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  

    Browser compatibility

    Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
    Basic support 1.0 1.5 (1.8) [1][2][3] 9.0 9.0 2.0 [4]
    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.0 (1.8) ? ? ? 1.0
    moz-opaque 1.0 (1.9.1) Not supported Not supported Not supported Not supported

    Browser-specific notes

    Firefox (Gecko)

    [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] 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