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 974639 of <canvas>

  • 版本網址代稱: Web/HTML/Element/canvas
  • 版本標題: <canvas>
  • 版本 ID: 974639
  • 建立日期:
  • 建立者: GG9
  • 是目前線上的版本?
  • 回應

版本內容

這個 HTML <canvas> 元素 可以被使用於對腳本程式(JaveScript) 繪圖 .舉例來說, 它能用來畫圖,組合照片,甚至作動畫. 你也許(應該) 在<canvas>區段內提供替代方案的內容 .這內容將能被一些不支援<canvas>以及無法使用Javescript功能的瀏覽器所渲染 .

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, radio, or 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("height")}}
The height of the coordinate space in CSS pixels. Defaults to 150.
{{htmlattrdef("moz-opaque")}} {{non-standard_inline}}
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.
{{htmlattrdef("width")}}
The width of the coordinate space in CSS pixels. Defaults to 300.

Description

Required </canvas> tag

Unlike the {{HTMLElement("img")}} element, the {{HTMLElement("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 your canvas does not use transparency 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 only works in Mozilla-based rendering engines.

<canvas id="mycanvas" moz-opaque></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')}} Initial definition

Browser compatibility

{{CompatibilityTable}}

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support 1.0 {{CompatGeckoDesktop("1.8")}}[1]
{{CompatGeckoDesktop("6.0")}}[2]
{{CompatGeckoDesktop("12.0")}}[3]
9.0 9.0[4] 2.0[5]
moz-opaque {{CompatNo}} {{CompatGeckoDesktop("1.9.1")}} {{CompatNo}} {{CompatNo}} {{CompatNo}}
Feature Firefox Mobile (Gecko) Android IE Mobile Opera Mobile Safari Mobile
Basic support {{CompatGeckoDesktop("1.8")}}[1]
{{CompatGeckoDesktop("6.0")}}[2]
{{CompatGeckoDesktop("12.0")}}[3]
{{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}} 1.0
moz-opaque {{CompatGeckoMobile("1.9.1")}} {{CompatNo}} {{CompatNo}} {{CompatNo}} {{CompatNo}}

[1] Before Gecko 5.0 {{geckoRelease("5.0")}}, the canvas width and height were signed integers instead of unsigned integers.

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

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

[4] See the changelog for Opera 9.0.

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

{{HTMLRef}}

版本來源

<p>這個 <strong>HTML <code>&lt;canvas&gt;</code> 元素</strong> 可以被使用於對腳本程式(JaveScript) 繪圖 .舉例來說, 它能用來畫圖,組合照片,甚至作動畫. 你也許(應該) 在&lt;canvas&gt;區段內提供替代方案的內容 .這內容將能被一些不支援&lt;canvas&gt;以及無法使用Javescript功能的瀏覽器所渲染 .</p>

<p>For more articles on canvas, see the <a href="/en-US/docs/Web/API/Canvas_API">canvas topic page</a>.</p>

<table class="properties">
 <tbody>
  <tr>
   <th scope="row"><a href="/en-US/docs/HTML/Content_categories">Content categories</a></th>
   <td><a href="/en-US/docs/HTML/Content_categories#Flow_content">Flow content</a>, <a href="/en-US/docs/HTML/Content_categories#Phrasing_content">phrasing content</a>, <a href="https://developer.mozilla.org/en-US/docs/HTML/Content_categories#Embedded_content">embedded content</a>, palpable content.</td>
  </tr>
  <tr>
   <th scope="row">Permitted content</th>
   <td>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>, <code>radio</code>, or <code>button</code>.</td>
  </tr>
  <tr>
   <th scope="row">Tag omission</th>
   <td>{{no_tag_omission}}</td>
  </tr>
  <tr>
   <th scope="row">Permitted parent elements</th>
   <td>Any element that accepts <a href="/en-US/docs/HTML/Content_categories#Phrasing_content">phrasing_content</a>.</td>
  </tr>
  <tr>
   <th scope="row">DOM interface</th>
   <td>{{domxref("HTMLCanvasElement")}}</td>
  </tr>
 </tbody>
</table>

<h2 id="Attributes">Attributes</h2>

<p>This element includes the <a href="https://developer.mozilla.org/en-US/docs/HTML/Global_attributes">global attributes</a>.</p>

<dl>
 <dt>{{htmlattrdef("height")}}</dt>
 <dd>The height of the coordinate space in CSS pixels. Defaults to 150.</dd>
 <dt>{{htmlattrdef("moz-opaque")}} {{non-standard_inline}}</dt>
 <dd>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.</dd>
 <dt>{{htmlattrdef("width")}}</dt>
 <dd>The width of the coordinate space in CSS pixels. Defaults to 300.</dd>
</dl>

<h2 id="Description">Description</h2>

<h3 id="Required_&lt;canvas&gt;_tag">Required <code>&lt;/canvas&gt;</code> tag</h3>

<p>Unlike the {{HTMLElement("img")}} element, the {{HTMLElement("canvas")}} element <strong>requires</strong> the closing tag (<code>&lt;/canvas&gt;</code>).</p>

<h3 id="Sizing_the_canvas">Sizing the canvas</h3>

<p>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 <code>width</code> and <code>height</code> attributes explicitly in the <code>&lt;canvas&gt;</code> attributes, and not using CSS.</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>

<p>If your canvas does not use transparency&nbsp;set the <code>moz-opaque</code> attribute on the canvas tag. This information can be used internally to optimize rendering. However, this attribute has not been standardized and only works in Mozilla-based rendering engines.</p>

<pre class="brush: html">
&lt;canvas id="mycanvas" moz-opaque&gt;&lt;/canvas&gt;</pre>

<h2 id="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>Initial definition</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>Chrome</th>
   <th>Firefox (Gecko)</th>
   <th>Internet Explorer</th>
   <th>Opera</th>
   <th>Safari</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>1.0</td>
   <td>{{CompatGeckoDesktop("1.8")}}<sup>[1]</sup><br />
    {{CompatGeckoDesktop("6.0")}}<sup>[2]</sup><br />
    {{CompatGeckoDesktop("12.0")}}<sup>[3]</sup></td>
   <td>9.0</td>
   <td>9.0<sup>[4]</sup></td>
   <td>2.0<sup>[5]</sup></td>
  </tr>
  <tr>
   <td><code>moz-opaque</code></td>
   <td>{{CompatNo}}</td>
   <td>{{CompatGeckoDesktop("1.9.1")}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</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>{{CompatGeckoDesktop("1.8")}}<sup>[1]</sup><br />
    {{CompatGeckoDesktop("6.0")}}<sup>[2]</sup><br />
    {{CompatGeckoDesktop("12.0")}}<sup>[3]</sup></td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>1.0</td>
  </tr>
  <tr>
   <td><code>moz-opaque</code></td>
   <td>{{CompatGeckoMobile("1.9.1")}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
  </tr>
 </tbody>
</table>
</div>

<p>[1] Before Gecko 5.0 {{geckoRelease("5.0")}}, the canvas width and height were signed integers instead of unsigned integers.</p>

<p>[2] 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.</p>

<p>[3] 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.</p>

<p>[4] See the <a href="https://www.opera.com/docs/changelogs/windows/900/">changelog for Opera 9.0</a>.</p>

<p>[5] 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.</p>

<h2 id="See_also">See also</h2>

<ul>
 <li><a href="/en-US/docs/Web/API/Canvas_API">MDN canvas portal</a></li>
 <li><a href="/en-US/docs/Web/API/Canvas_API/Tutorial">Canvas tutorial</a></li>
 <li><a href="https://simon.html5.org/dump/html5-canvas-cheat-sheet.html">Canvas cheat sheet</a></li>
 <li><a href="/en-US/demos/tag/tech:canvas">Canvas-related demos</a></li>
 <li><a href="https://developer.apple.com/library/safari/documentation/AudioVideo/Conceptual/HTML-canvas-guide/Introduction/Introduction.html">Canvas introduction by Apple</a></li>
</ul>

<div>{{HTMLRef}}</div>
還原至此版本