Basic usage

  • 版本网址缩略名: Canvas_tutorial/Basic_usage
  • 版本标题: Basic usage
  • 版本 id: 37867
  • 创建于:
  • 创建者: bingdian
  • 是否是当前版本?
  • 评论 HTLM改为HTML; 1 words added, 1 words removed

修订内容

 

 <canvas>元素

Let's start this tutorial by looking at the <canvas> element itself.
让我们从<canvas>元素的定义开始吧。

<canvas id="tutorial" width="150" height="150"></canvas>

This looks a lot like the <img> element, the only difference is that it doesn't have the src and alt attributes. The <canvas> element has only two attributes - width and height. These are both optional and can also be set using DOM properties or CSS rules. When no width and height attributes are specified, the canvas will initially be 300 pixels wide and 150 pixels high.The element can be sized arbitrarily by CSS, but during rendering the image is scaled to fit its layout size.   (If your  renderings seem distorted, try specifying your width and height attributes explicitly in the <canvas> attributes, and not with CSS.)

<canvas>看起来很像<img>,唯一不同就是它不含 src 和 alt 属性。它只有两个属性,width 和 height,两个都是可选的,并且都可以 DOM 或者 CSS 来设置。如果不指定width 和 height,默认的是宽300像素高150像素。虽然可以通过 CSS 来调整canvas的大小,但渲染图像会缩放来适应布局的(如果你发现渲染结果看上去变形了,不必一味依赖CSS,可以尝试显式指定canvas的width 和 height 属性值)。

The id attribute isn't specific to the <canvas> element but is one of default HTML attributes which can be applied to (almost) every HTML element (like class for instance). It's always a good idea to supply an id because this makes it much easier to identify it in our script.

id  属性不是<canvas>专享的,就像标准的HTML标签一样,任何一个HTML元素都可以指定其 id 值。一般,为元素指定 id 是个不错的主意,这样使得在脚本中应用更加方便。

The <canvas> element can be styled just like any normal image (margin, border, background, etc). These rules however don't affect the actual drawing on the canvas. We'll see how this is done later in this tutorial. When no styling rules are applied to the canvas it will initially be fully transparent.

<canvas>元素可以像普通图片一样指定其样式(边距,边框,背景等等)。然而这些样式并不会对canvas实际生成的图像产生什么影响。下面我们会看到如何应用样式。如果不指定样式,canvas默认是全透明的。

替用内容

Because the <canvas> element is still relatively new and isn't implemented in some browsers (such as Firefox 1.0 and Internet Explorer), we need a means of providing fallback content when a browser doesn't support the element.

因为 <canvas> 相对较新,有些浏览器并没实现,如Firefox 1.0 和 Internet Explorer,所以我们需要为那些不支持canvas的浏览器提供替用显示内容。

Luckily this is very straightforward: we just provide alternative content inside the canvas element. Browsers who don't support it will ignore the element completely and render the fallback content, others will just render the canvas normally.
For instance we could provide a text description of the canvas content or provide a static image of the dynamically rendered content. This can look something like this:

我们只需要直接在canvas元素内插入替用内容即可。不支持canvas的浏览器会忽略canvas元素而直接渲染替用内容,而支持的浏览器则会正常地渲染canvas。例如,我们可以把一些文字或图片填入canvas内,作为替用内容:

<canvas id="stockGraph" width="150" height="150">
  current stock price: $3.15 +0.15
</canvas>

<canvas id="clock" width="150" height="150">
  <img src="images/clock.png" width="150" height="150"/>
</canvas>

结束标签 </canvas> 是必须的

In the Apple Safari implementation, <canvas> is an element implemented in much the same way <img> is; it does not have an end tag. However, for <canvas> to have widespread use on the web, some facility for fallback content must be provided. Therefore, Mozilla's implementation requires an end tag (</canvas>).

在Apple Safari里,<canvas>的实现跟<img>很相似,它没有结束标签。然而,为了使 <canvas> 能在web的世界里广泛适用,需要给替用内容提供一个容身之所,因此,在Mozilla的实现里结束标签(</canvas>)是必须的。

If fallback content is not needed, a simple <canvas id="foo" ...></canvas> will be fully compatible with both Safari and Mozilla -- Safari will simply ignore the end tag.

如果没有替用内容,<canvas id="foo" ...></canvas> 对 Safari 和 Mozilla 是完全兼容的—— Safari 会简单地忽略结束标签。

If fallback content is desired, some CSS tricks must be employed to mask the fallback content from Safari (which should render just the canvas), and also to mask the CSS tricks themselves from IE (which should render the fallback content).

如果有替用内容,那么可以用一些 CSS 技巧来为并且仅为 Safari 隐藏替用内容,因为那些替用内容是需要在 IE 里显示但不需要在 Safari 里显示。

渲染上下文(Rendering Context)

<canvas> creates a fixed size drawing surface that exposes one or more rendering contexts, which are used to create and manipulate the content shown. We'll focus on the 2D rendering context, which is the only currently defined rendering context. In the future, other contexts may provide different types of rendering; for example, it is likely that a 3D context based on OpenGL ES will be added.

<canvas> 创建的固定尺寸的绘图画面开放了一个或多个渲染上下文(rendering context),我们可以通过它们来控制要显示的内容。我们专注于2D 渲染上,这也是目前唯一的选择,可能在将来会添加基于OpenGL ES 的 3D 上下文。

The <canvas> is initially blank, and to display something a script first needs to access the rendering context and draw on it. The canvas element has a DOM method called getContext, used to obtain the rendering context and its drawing functions. getContext() takes one parameter, the type of context.

<canvas> 初始化是空白的,要在上面用脚本画图首先需要其渲染上下文(rendering context),它可以通过 canvas 元素对象的 getContext 方法来获取,同时得到的还有一些画图用的函数。getContext() 接受一个用于描述其类型的值作为参数。

var canvas = document.getElementById('tutorial');
var ctx = canvas.getContext('2d');

In the first line we retrieve the canvas DOM node using the getElementById method. We can then access the drawing context using the getContext method.

上面第一行通过 getElementById 方法取得 canvas 对象的 DOM 节点。然后通过其 getContext 方法取得其画图操作上下文。

检查浏览器的支持

The fallback content is displayed in browsers which do not support <canvas>; scripts can also check for support when they execute. This can easily be done by testing for the getContext method. Our code snippet from above becomes something like this:

除了在那些不支持  的浏览器上显示替用内容,还可以通过脚本的方式来检查浏览器是否支持 canvas 。方法很简单,判断 getContext 是否存在即可。

var canvas = document.getElementById('tutorial');
if (canvas.getContext){
  var ctx = canvas.getContext('2d');
  // drawing code here
} else {
  // canvas-unsupported code here
}

代码模板

Here is a minimalistic template, which we'll be using as a starting point for later examples. You can download this file to work with on your system.

我们会用下面这个最简化的代码模板来(后续的示例需要用到)作为开始,你可以 下载文件 到本地备用。

<html>
  <head>
    <title>Canvas tutorial</title>
    <script type="text/javascript">
      function draw(){
        var canvas = document.getElementById('tutorial');
        if (canvas.getContext){
          var ctx = canvas.getContext('2d');
        }
      }
    </script>
    <style type="text/css">
      canvas { border: 1px solid black; }
    </style>
  </head>
  <body onload="draw();">
    <canvas id="tutorial" width="150" height="150"></canvas>
  </body>
</html>

If you look at the script you'll see I've made a function called draw, which will get executed once the page finishes loading (via the onload attribute on the body tag). This function could also have been called from a setTimeout, setInterval, or any other event handler function just as long the page has been loaded first.

细心的你会发现我准备了一个名为 draw 的函数,它会在页面装载完毕之后执行一次(通过设置 body 标签的 onload 属性),它当然也可以在 setTimeoutsetInterval,或者其他事件处理函数中被调用。

一个简单的例子

To start off, here's a simple example that draws two intersecting rectangles, one of which has alpha transparency. We'll explore how this works in more detail in later examples.

作为开始,来一个简单的吧——绘制两个交错的矩形,其中一个是有alpha透明效果。我们会在后面的示例中详细的让你了解它是如何运作的。

Simple Example

观看示例

<html>
 <head>
  <script type="application/x-javascript">
    function draw() {
      var canvas = document.getElementById("canvas");
      if (canvas.getContext) {
        var ctx = canvas.getContext("2d");

        ctx.fillStyle = "rgb(200,0,0)";
        ctx.fillRect (10, 10, 55, 50);

        ctx.fillStyle = "rgba(0, 0, 200, 0.5)";
        ctx.fillRect (30, 30, 55, 50);
      }
    }
  </script>
 </head>
 <body onload="draw();">
   <canvas id="canvas" width="150" height="150"></canvas>
 </body>
</html>

 

{{ PreviousNext("Canvas tutorial", "Canvas tutorial:Drawing shapes") }}

{{ languages( { "fr": "fr/Tutoriel_canvas/Utilisation_de_base", "ja": "ja/Canvas_tutorial/Basic_usage", "pl": "pl/Przewodnik_po_canvas/Proste_zastosowanie", "zh-cn": "cn/Canvas_tutorial/Basic_usage", "ru": "ru/\u041e\u0431\u0443\u0447\u0435\u043d\u0438\u0435_canvas/\u041e\u0441\u043d\u043e\u0432\u044b", "it": "it/Canvas_tutorial/Utilizzo_di_base" } ) }}

修订版来源

<p> </p>
<h2 id=".E5.85.83.E7.B4.A0_.3Ccanvas.3E" name=".E5.85.83.E7.B4.A0_.3Ccanvas.3E"> <code>&lt;canvas&gt;元素</code></h2>
<p>Let's start this tutorial by looking at the <code>&lt;canvas&gt;</code> element itself.<br> 让我们从<code>&lt;canvas&gt;</code>元素的定义开始吧。</p>
<pre class="eval">&lt;canvas id="tutorial" width="150" height="150"&gt;&lt;/canvas&gt;
</pre>
<p>This looks a lot like the <code>&lt;img&gt;</code> element, the only difference is that it doesn't have the <code>src</code> and <code>alt</code> attributes. The <code>&lt;canvas&gt;</code> element has only two attributes - <strong>width</strong> and <strong>height</strong>. These are both optional and can also be set using <a href="/cn/DOM" title="cn/DOM">DOM</a> properties or <a href="/cn/CSS" title="cn/CSS">CSS</a> rules. When no width and height attributes are specified, the canvas will initially be <strong>300 pixels</strong> wide and <strong>150 pixels</strong> high.The element can be sized arbitrarily by <a href="../../../../en/CSS" rel="internal">CSS</a>, but during rendering the image is scaled to fit its layout size.   (If your  renderings seem distorted, try specifying your width and height attributes explicitly in the <code>&lt;canvas&gt;</code> attributes, and not with CSS.)</p>
<p>&lt;canvas&gt;看起来很像&lt;img&gt;,唯一不同就是它不含<code style="font: normal normal normal 100%/normal 'Courier New', 'Andale Mono', monospace; color: inherit; font-weight: inherit; "> src</code> 和 <code style="font: normal normal normal 100%/normal 'Courier New', 'Andale Mono', monospace; color: inherit; font-weight: inherit; ">alt </code>属性。它只有两个属性,<strong>width</strong> 和 <strong>height</strong>,两个都是可选的,并且都可以 <a href="/cn/DOM" style="text-decoration: none; color: rgb(51, 102, 153) !important; cursor: default; " title="cn/DOM">DOM</a> 或者 <a href="/cn/CSS" style="text-decoration: none; color: rgb(51, 102, 153) !important; cursor: default; " title="cn/CSS">CSS</a> 来设置。如果不指定width 和 height,默认的是<strong>宽300像素</strong>,<strong>高150像素</strong>。虽然可以通过 <a href="../../../../en/CSS" rel="internal">CSS</a> 来调整canvas的大小,但渲染图像会缩放来适应布局的(如果你发现渲染结果看上去变形了,不必一味依赖CSS,可以尝试显式指定canvas的width 和 height 属性值)。</p>
<p>The <code>id</code> attribute isn't specific to the <code>&lt;canvas&gt;</code> element but is one of default HTML attributes which can be applied to (almost) every HTML element (like <code>class</code> for instance). It's always a good idea to supply an id because this makes it much easier to identify it in our script.</p>
<p><code>id</code>  属性不是<code>&lt;canvas&gt;</code>专享的,就像标准的HTML标签一样,任何一个HTML元素都可以指定其<code> id</code> 值。一般,为元素指定<code> id</code> 是个不错的主意,这样使得在脚本中应用更加方便。</p>
<p>The <code>&lt;canvas&gt;</code> element can be styled just like any normal image (margin, border, background, etc). These rules however don't affect the actual drawing on the canvas. We'll see how this is done later in this tutorial. When no styling rules are applied to the canvas it will initially be fully transparent.<code> </code></p>
<p><code>&lt;canvas&gt;</code>元素可以像普通图片一样指定其样式(边距,边框,背景等等)。然而这些样式并不会对canvas实际生成的图像产生什么影响。下面我们会看到如何应用样式。如果不指定样式,canvas默认是全透明的。</p>
<h4 id="替用内容">替用内容</h4>
<p>Because the <code>&lt;canvas&gt;</code> element is still relatively new and isn't implemented in some browsers (such as Firefox 1.0 and Internet Explorer), we need a means of providing fallback content when a browser doesn't support the element.</p>
<p>因为 <code>&lt;canvas&gt;</code> 相对较新,有些浏览器并没实现,如Firefox 1.0 和 Internet Explorer,所以我们需要为那些不支持canvas的浏览器提供替用显示内容。</p>
<p>Luckily this is very straightforward: we just provide alternative content inside the canvas element. Browsers who don't support it will ignore the element completely and render the fallback content, others will just render the canvas normally.<br> For instance we could provide a text description of the canvas content or provide a static image of the dynamically rendered content. This can look something like this:</p>
<p>我们只需要直接在canvas元素内插入替用内容即可。不支持canvas的浏览器会忽略canvas元素而直接渲染替用内容,而支持的浏览器则会正常地渲染canvas。例如,我们可以把一些文字或图片填入canvas内,作为替用内容:</p>
<pre>&lt;canvas id="stockGraph" width="150" height="150"&gt;
  current stock price: $3.15 +0.15
&lt;/canvas&gt;

&lt;canvas id="clock" width="150" height="150"&gt;
  &lt;img src="images/clock.png" width="150" height="150"/&gt;
&lt;/canvas&gt;
</pre>
<h4 class="editable" id="结束标签_&lt;/canvas>_是必须的"><span>结束标签 <code>&lt;/canvas&gt;</code> 是必须的</span></h4>
<p>In the Apple Safari implementation, <code>&lt;canvas&gt;</code> is an element implemented in much the same way <code>&lt;img&gt;</code> is; it does not have an end tag. However, for <code>&lt;canvas&gt;</code> to have widespread use on the web, some facility for fallback content must be provided. Therefore, Mozilla's implementation <em>requires</em> an end tag (<code>&lt;/canvas&gt;</code>).</p>
<p>在Apple Safari里,<code>&lt;canvas&gt;</code>的实现跟<code>&lt;img&gt;</code>很相似,它没有结束标签。然而,为了使 <code>&lt;canvas&gt;</code> 能在web的世界里广泛适用,需要给替用内容提供一个容身之所,因此,在Mozilla的实现里结束标签(<code>&lt;/canvas&gt;</code>)是必须的。</p>
<p>If fallback content is not needed, a simple <code>&lt;canvas id="foo" ...&gt;&lt;/canvas&gt;</code> will be fully compatible with both Safari and Mozilla -- Safari will simply ignore the end tag.</p>
<p>如果没有替用内容,<code>&lt;canvas id="foo" ...&gt;&lt;/canvas&gt;</code> 对 Safari 和 Mozilla 是完全兼容的—— Safari 会简单地忽略结束标签。</p>
<p>If fallback content is desired, some CSS tricks must be employed to mask the fallback content from Safari (which should render just the canvas), and also to mask the CSS tricks themselves from IE (which should render the fallback content).</p>
<p>如果有替用内容,那么可以用一些 CSS 技巧来为并且仅为 Safari 隐藏替用内容,因为那些替用内容是需要在 IE 里显示但不需要在 Safari 里显示。</p>
<h2 id="渲染上下文(Rendering_Context)">渲染上下文(Rendering Context)</h2>
<p><code>&lt;canvas&gt;</code> creates a fixed size drawing surface that exposes one or more <em>rendering contexts</em>, which are used to create and manipulate the content shown. We'll focus on the 2D rendering context, which is the only currently defined rendering context. In the future, other contexts may provide different types of rendering; for example, it is likely that a 3D context based on <a class="external" href="http://en.wikipedia.org/wiki/OpenGL_ES" rel="external nofollow" target="_blank" title="http://en.wikipedia.org/wiki/OpenGL_ES">OpenGL ES</a> will be added.</p>
<p><code>&lt;canvas&gt;</code> 创建的固定尺寸的绘图画面开放了一个或多个渲染上下文(<em>rendering context</em>),我们可以通过它们来控制要显示的内容。我们专注于2D 渲染上,这也是目前唯一的选择,可能在将来会添加基于<a class="external" href="http://en.wikipedia.org/wiki/OpenGL_ES" rel="external nofollow" target="_blank" title="http://en.wikipedia.org/wiki/OpenGL_ES">OpenGL ES</a> 的 3D 上下文。</p>
<p>The <code>&lt;canvas&gt;</code> is initially blank, and to display something a script first needs to access the rendering context and draw on it. The canvas element has a DOM method called <code>getContext</code>, used to obtain the rendering context and its drawing functions. <code>getContext()</code> takes one parameter, the type of context.</p>
<p><code>&lt;canvas&gt;</code> 初始化是空白的,要在上面用脚本画图首先需要其渲染上下文(rendering context),它可以通过 canvas 元素对象的 <code>getContext</code> 方法来获取,同时得到的还有一些画图用的函数。<code>getContext() </code>接受一个用于描述其类型的值作为参数。</p>
<pre class="eval">var canvas = document.getElementById('tutorial');
var ctx = canvas.getContext('2d');
</pre>
<p>In the first line we retrieve the canvas DOM node using the <a href="../../../../en/DOM/document.getElementById" rel="internal">getElementById</a> method. We can then access the drawing context using the <code>getContext</code> method.</p>
<p>上面第一行通过 <a href="../../../../en/DOM/document.getElementById" rel="internal">getElementById</a> 方法取得 canvas 对象的 DOM 节点。然后通过其 <code>getContext</code> 方法取得其画图操作上下文。</p>
<h3 id="Prevent_execution_in_unsupported_browsers" name="Prevent_execution_in_unsupported_browsers">检查浏览器的支持</h3>
<p>The fallback content is displayed in browsers which do not support <code>&lt;canvas&gt;</code>; scripts can also check for support when they execute. This can easily be done by testing for the <code>getContext</code> method. Our code snippet from above becomes something like this:</p>
<p>除了在那些不支持  的浏览器上显示替用内容,还可以通过脚本的方式来检查浏览器是否支持 canvas 。方法很简单,判断 <code>getContext</code> 是否存在即可。</p>
<pre class="eval">var canvas = document.getElementById('tutorial');
if (canvas.getContext){
  var ctx = canvas.getContext('2d');
  // drawing code here
} else {
  // canvas-unsupported code here
}
</pre>
<h2 id="Putting_it_together" name="Putting_it_together">代码模板</h2>
<p>Here is a minimalistic template, which we'll be using as a starting point for later examples. You can <a class="external" href="/samples/canvas-tutorial/1_1_canvas_template.html" title="samples/canvas-tutorial/1_1_canvas_template.html">download this file</a> to work with on your system.</p>
<p>我们会用下面这个最简化的代码模板来(后续的示例需要用到)作为开始,你可以 <a class="external" href="/samples/canvas-tutorial/1_1_canvas_template.html" title="samples/canvas-tutorial/1_1_canvas_template.html">下载文件</a> 到本地备用。</p>
<pre>&lt;html&gt;
  &lt;head&gt;
    &lt;title&gt;Canvas tutorial&lt;/title&gt;
    &lt;script type="text/javascript"&gt;
      function draw(){
        var canvas = document.getElementById('tutorial');
        if (canvas.getContext){
          var ctx = canvas.getContext('2d');
        }
      }
    &lt;/script&gt;
    &lt;style type="text/css"&gt;
      canvas { border: 1px solid black; }
    &lt;/style&gt;
  &lt;/head&gt;
  &lt;body onload="draw();"&gt;
    &lt;canvas id="tutorial" width="150" height="150"&gt;&lt;/canvas&gt;
  &lt;/body&gt;
&lt;/html&gt;
</pre>
<p>If you look at the script you'll see I've made a function called <code>draw</code>, which will get executed once the page finishes loading (via the <code>onload</code> attribute on the <code>body</code> tag). This function could also have been called from a <a href="/en/DOM/window.setTimeout" title="en/DOM/window.setTimeout">setTimeout</a>, <a href="/en/DOM/window.setInterval" title="en/DOM/window.setInterval">setInterval</a>, or any other event handler function just as long the page has been loaded first.</p>
<p>细心的你会发现我准备了一个名为 <code>draw</code> 的函数,它会在页面装载完毕之后执行一次(通过设置 <code>body</code> 标签的 <code>onload</code> 属性),它当然也可以在 <a href="/en/DOM/window.setTimeout" title="en/DOM/window.setTimeout">setTimeout</a>,<a href="/en/DOM/window.setInterval" title="en/DOM/window.setInterval">setInterval</a>,或者其他事件处理函数中被调用。</p>
<h2 id="A_simple_example" name="A_simple_example">一个简单的例子</h2>
<p>To start off, here's a simple example that draws two intersecting rectangles, one of which has alpha transparency. We'll explore how this works in more detail in later examples.</p>
<p>作为开始,来一个简单的吧——绘制两个交错的矩形,其中一个是有alpha透明效果。我们会在后面的示例中详细的让你了解它是如何运作的。</p>
<p><img align="right" alt="Simple Example" class="internal" src="/@api/deki/files/602/=Canvas_ex1.png"></p>
<p><a class="internal" href="/@api/deki/files/2931/=simple_example_(1).html" title="/@api/deki/files/2931/=simple example (1).html">观看示例</a></p>
<pre class="eval">&lt;html&gt;
 &lt;head&gt;
  &lt;script type="application/x-javascript"&gt;
    function draw() {
      var canvas = document.getElementById("canvas");
      if (canvas.getContext) {
        var ctx = canvas.getContext("2d");

        ctx.fillStyle = "rgb(200,0,0)";
        ctx.fillRect (10, 10, 55, 50);

        ctx.fillStyle = "rgba(0, 0, 200, 0.5)";
        ctx.fillRect (30, 30, 55, 50);
      }
    }
  &lt;/script&gt;
 &lt;/head&gt;
 &lt;body onload="draw();"&gt;
   &lt;canvas id="canvas" width="150" height="150"&gt;&lt;/canvas&gt;
 &lt;/body&gt;
&lt;/html&gt;
</pre>
<p> </p>
<p>{{ PreviousNext("Canvas tutorial", "Canvas tutorial:Drawing shapes") }}</p>
<p>{{ languages( { "fr": "fr/Tutoriel_canvas/Utilisation_de_base", "ja": "ja/Canvas_tutorial/Basic_usage", "pl": "pl/Przewodnik_po_canvas/Proste_zastosowanie", "zh-cn": "cn/Canvas_tutorial/Basic_usage", "ru": "ru/\u041e\u0431\u0443\u0447\u0435\u043d\u0438\u0435_canvas/\u041e\u0441\u043d\u043e\u0432\u044b", "it": "it/Canvas_tutorial/Utilizzo_di_base" } ) }}</p>
恢复到这个版本