Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

Revision 359821 of <canvas>

  • 版本网址缩略名: HTML/Element/canvas
  • 版本标题: <canvas> 标签
  • 版本 id: 359821
  • 创建于:
  • 创建者: xcffl
  • 是否是当前版本?
  • 评论
标签: 

修订内容

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.

  • Content categories Flow content, phrasing content, embedded content, palpable content.
  • Permitted content Transparent but with no interactive content descendants except for {{HTMLElement("a")}} elements, {{HTMLElement("button")}} elements, {{HTMLElement("input")}} elements whose {{htmlattrxref("type", "input")}} attribute is checkbox or radio, and {{HTMLElement("input")}} elements whose {{htmlattrxref("type", "input")}} attribute is button.
  • Tag omission {{no_tag_omission}}
  • Permitted parent elements Any element that accepts phrasing_content.
  • DOM interface {{domxref("HTMLCanvasElement")}}

Attributes

This element includes the global attributes.

{{htmlattrdef("width")}}
The width of the coordinate space in CSS pixels. Defaults to 300.
{{htmlattrdef("height")}}
The height of the coordinate space in CSS pixels. Defaults to 150.

{{Note("The displayed size of the canvas can be changed using a stylesheet. The image is scaled during rendering to fit the styled size.")}}

Examples

<canvas id="canvas" width="300" height="300">
  Sorry, your browser doesn't support the &lt;canvas&gt; element.
</canvas>

Specifications

Specification Status Comment
{{SpecName('HTML WHATWG', 'the-canvas-element.html#the-canvas-element', '<canvas>')}} {{Spec2('HTML WHATWG')}}  
{{SpecName('HTML5 W3C', 'the-canvas-element.html#the-canvas-element', '<canvas>')}} {{Spec2('HTML5 W3C')}}  

Browser compatibility

{{CompatibilityTable}}

Feature Firefox (Gecko) Chrome Internet Explorer Opera Safari
Basic support {{CompatGeckoDesktop("1.8")}} 1.0 9.0 9.0 2.0
Feature Firefox Mobile (Gecko) Android IE Mobile Opera Mobile Safari Mobile
Basic support {{CompatGeckoMobile("1.8")}} {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}} 1.0

Browser-specific notes

Firefox (Gecko)

  • Before Gecko 5.0 {{geckoRelease("5.0")}}, the canvas width and height were signed integers instead of unsigned integers.
  • Prior to Gecko 6.0 {{geckoRelease("6.0")}}, a {{HTMLElement("canvas")}} element with a zero width or height would be rendered as if it had default dimensions.
  • Before Gecko 12.0 {{geckoRelease("12.0")}}, 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.

See also

{{HTML:Element_Navigation}}

修订版来源

<h2 id="Summary">Summary</h2>
<p>The <strong>HTML <code>&lt;canvas&gt;</code> Element</strong> can be used to draw graphics via scripting (usually <a href="/en-US/docs/JavaScript" title="JavaScript">JavaScript</a>). 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 <code>&lt;canvas&gt;</code> block. That content will be rendered both on older browsers that don't support canvas <strong>and</strong> in browsers with JavaScript disabled.</p>
<p>For more articles on canvas, see the <a href="/en-US/docs/HTML/Canvas" title="HTML/Canvas">canvas topic page</a>.</p>
<ul class="htmlelt">
  <li><dfn><a href="/en-US/docs/HTML/Content_categories" title="HTML/Content_categories">Content categories</a></dfn> <a href="/en-US/docs/HTML/Content_categories#Flow_content" title="HTML/Content categories#Flow content">Flow content</a>, <a href="/en-US/docs/HTML/Content_categories#Phrasing_content" title="HTML/Content categories#Phrasing content">phrasing content</a>, <a href="https://developer.mozilla.org/en-US/docs/HTML/Content_categories#Embedded_content" title="HTML/Content_categories#Embedded content">embedded content</a>, palpable content.</li>
  <li><dfn>Permitted content</dfn> Transparent but with no <a href="/en-US/docs/HTML/Content_categories#Interactive_content">interactive content</a> descendants except for {{HTMLElement("a")}} elements, {{HTMLElement("button")}} elements, {{HTMLElement("input")}} elements whose {{htmlattrxref("type", "input")}} attribute is <code>checkbox</code> or <code>radio</code>, and {{HTMLElement("input")}} elements whose {{htmlattrxref("type", "input")}} attribute is <code>button</code>.</li>
  <li><dfn>Tag omission</dfn> {{no_tag_omission}}</li>
  <li><dfn>Permitted parent elements</dfn> Any element that accepts <a href="/en-US/docs/HTML/Content_categories#Phrasing_content" title="HTML/Content_categories#Phrasing_content">phrasing_content</a>.</li>
  <li><dfn>DOM interface</dfn> {{domxref("HTMLCanvasElement")}}</li>
</ul>
<h2 id="Attributes">Attributes</h2>
<p><span style="line-height: 21px;">This element includes the&nbsp;</span><a href="https://developer.mozilla.org/en-US/docs/HTML/Global_attributes" style="line-height: 21px;" title="HTML/Global attributes">global attributes</a><span style="line-height: 21px;">.</span></p>
<dl>
  <dt>
    {{htmlattrdef("width")}}</dt>
  <dd>
    The width of the coordinate space in CSS pixels. Defaults to 300.</dd>
  <dt>
    {{htmlattrdef("height")}}</dt>
  <dd>
    The height of the coordinate space in CSS pixels. Defaults to 150.</dd>
</dl>
<p>{{Note("The displayed size of the canvas can be changed using a stylesheet. The image is scaled during rendering to fit the styled size.")}}</p>
<h2 id="Examples">Examples</h2>
<pre class="brush: html">
&lt;canvas id="canvas" width="300" height="300"&gt;
  Sorry, your browser doesn't support the &amp;lt;canvas&amp;gt; element.
&lt;/canvas&gt;
</pre>
<h2 id="Specifications" name="Specifications">Specifications</h2>
<table class="standard-table">
  <thead>
    <tr>
      <th scope="col">Specification</th>
      <th scope="col">Status</th>
      <th scope="col">Comment</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>{{SpecName('HTML WHATWG', 'the-canvas-element.html#the-canvas-element', '&lt;canvas&gt;')}}</td>
      <td>{{Spec2('HTML WHATWG')}}</td>
      <td>&nbsp;</td>
    </tr>
    <tr>
      <td>{{SpecName('HTML5 W3C', 'the-canvas-element.html#the-canvas-element', '&lt;canvas&gt;')}}</td>
      <td>{{Spec2('HTML5 W3C')}}</td>
      <td>&nbsp;</td>
    </tr>
  </tbody>
</table>
<h2 id="Browser_compatibility">Browser compatibility</h2>
<p>{{CompatibilityTable}}</p>
<div id="compat-desktop">
  <table class="compat-table">
    <tbody>
      <tr>
        <th>Feature</th>
        <th>Firefox (Gecko)</th>
        <th>Chrome</th>
        <th>Internet Explorer</th>
        <th>Opera</th>
        <th>Safari</th>
      </tr>
      <tr>
        <td>Basic support</td>
        <td>{{CompatGeckoDesktop("1.8")}}</td>
        <td>1.0</td>
        <td>9.0</td>
        <td><a href="https://www.opera.com/docs/changelogs/windows/900/">9.0</a></td>
        <td><a href="https://developer.apple.com/library/safari/#documentation/AudioVideo/Conceptual/HTML-canvas-guide/Introduction/Introduction.html">2.0</a></td>
      </tr>
    </tbody>
  </table>
</div>
<div id="compat-mobile">
  <table class="compat-table">
    <tbody>
      <tr>
        <th>Feature</th>
        <th>Firefox Mobile (Gecko)</th>
        <th>Android</th>
        <th>IE Mobile</th>
        <th>Opera Mobile</th>
        <th>Safari Mobile</th>
      </tr>
      <tr>
        <td>Basic support</td>
        <td>{{CompatGeckoMobile("1.8")}}</td>
        <td>{{CompatUnknown}}</td>
        <td>{{CompatUnknown}}</td>
        <td>{{CompatUnknown}}</td>
        <td>1.0</td>
      </tr>
    </tbody>
  </table>
</div>
<h3 id="Browser-specific_notes">Browser-specific notes</h3>
<h4 id="Firefox_(Gecko)">Firefox (Gecko)</h4>
<ul>
  <li>Before Gecko 5.0 {{geckoRelease("5.0")}}, the canvas width and height were signed integers instead of unsigned integers.</li>
  <li>Prior to Gecko 6.0 {{geckoRelease("6.0")}}, a {{HTMLElement("canvas")}} element with a zero width or height would be rendered as if it had default dimensions.</li>
  <li>Before Gecko 12.0 {{geckoRelease("12.0")}}, if JavaScript is disabled, the <code>&lt;canvas&gt;</code> element was being rendered instead of showing the fallback content as per the <a href="https://www.whatwg.org/specs/web-apps/current-work/multipage/the-canvas-element.html">specification</a>. Now the fallback content is rendered instead.</li>
</ul>
<h2 id="See_also">See also</h2>
<ul>
  <li><a href="/en-US/docs/HTML/Canvas" title="https://developer.mozilla.org/en-US/docs/HTML/Canvas">MDN canvas portal</a></li>
  <li>A <a href="/en-US/docs/Canvas_tutorial" title="Canvas tutorial">canvas tutorial</a></li>
  <li><a href="https://blog.nihilogic.dk/2009/02/html5-canvas-cheat-sheet.html">Canvas cheat sheet</a></li>
  <li><a href="/en-US/demos/tag/tech:canvas" title="demos/tag/tech:canvas/">Canvas-related demos</a></li>
</ul>
<div>
  {{HTML:Element_Navigation}}</div>
恢复到这个版本