学习HTML5 Canvas这一篇文章就够了
<h1 id="一canvas简介" style="margin-top: 0.8em; margin-bottom: 0.8em; box-sizing: border-box; font-family: "microsoft yahei"; font-weight: 300; line-height: 1.1; color: rgb(63, 63, 63); font-size: 2.6em; font-variant-numeric: normal; font-variant-east-asian: normal;">一、canvas简介</h1><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;"><span class="Apple-converted-space"> </span><code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;"><canvas></code><span class="Apple-converted-space"> </span>是<span class="Apple-converted-space"> </span><code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">HTML5</code><span class="Apple-converted-space"> </span>新增的,一个可以使用脚本(通常为<code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">JavaScript</code>)在其中绘制图像的<span class="Apple-converted-space"> </span><code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">HTML</code><span class="Apple-converted-space"> </span>元素。它可以用来制作照片集或者制作简单(也不是那么简单)的动画,甚至可以进行实时视频处理和渲染。</p><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;"> 它最初由苹果内部使用自己<code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">MacOS X WebKit</code>推出,供应用程序使用像仪表盘的构件和<span class="Apple-converted-space"> </span><code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">Safari</code><span class="Apple-converted-space"> </span>浏览器使用。 后来,有人通过<code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">Gecko</code>内核的浏览器 (尤其是<code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">Mozilla</code>和<code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">Firefox</code>),<code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">Opera</code>和<code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">Chrome</code>和超文本网络应用技术工作组建议为下一代的网络技术使用该元素。</p><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;"><span class="Apple-converted-space"> </span><code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">Canvas</code>是由<code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">HTML</code>代码配合高度和宽度属性而定义出的可绘制区域。<code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">JavaScript</code>代码可以访问该区域,类似于其他通用的二维<code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">API</code>,通过一套完整的绘图函数来动态生成图形。</p><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;"> Mozilla 程序从 Gecko 1.8 (Firefox 1.5)开始支持<span class="Apple-converted-space"> </span><code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;"><canvas></code>, Internet Explorer 从IE9开始<code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;"><canvas></code><span class="Apple-converted-space"> </span>。Chrome和Opera 9+ 也支持<code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;"><canvas></code>。</p><h1 id="二canvas基本使用" style="margin-top: 0.8em; margin-bottom: 0.8em; box-sizing: border-box; font-family: "microsoft yahei"; font-weight: 300; line-height: 1.1; color: rgb(63, 63, 63); font-size: 2.6em; font-variant-numeric: normal; font-variant-east-asian: normal;"><a name="t1" target="_blank" style="box-sizing: border-box; color: rgb(12, 137, 207);"></a>二、Canvas基本使用</h1><h2 id="21-canvas元素" style="margin-top: 0.8em; margin-bottom: 0.8em; box-sizing: border-box; font-family: "microsoft yahei"; font-weight: 300; line-height: 1.1; color: rgb(63, 63, 63); font-size: 2.15em; font-variant-numeric: normal; font-variant-east-asian: normal;"><a name="t2" target="_blank" style="box-sizing: border-box; color: rgb(12, 137, 207);"></a>2.1<span class="Apple-converted-space"> </span><code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 31px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;"><canvas></code>元素</h2><pre class="prettyprint" name="code" style="white-space: nowrap; box-sizing: border-box; position: relative; overflow-y: hidden; overflow-x: auto; margin-bottom: 1.7em; line-height: 23px; font-family: "Source Code Pro", monospace; padding: 5px 5px 5px 60px; font-size: 14px; word-break: break-all; color: rgb(51, 51, 51); background-color: rgba(128, 128, 128, 0.047); border: 0px solid rgb(136, 136, 136); border-radius: 0px; font-variant-numeric: normal; font-variant-east-asian: normal;"><code class="language-html hljshas-numbering" style="display: block; background-color: transparent; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace; font-size: 13px; white-space: pre; border-radius: 0px; overflow-x: auto; word-wrap: normal;"><span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box;"><<span class="hljs-title" style="box-sizing: border-box; color: rgb(0, 0, 136);">canvas</span> <span class="hljs-attribute" style="box-sizing: border-box; color: rgb(102, 0, 102);">id</span>=<span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 136, 0);">"tutorial"</span> <span class="hljs-attribute" style="box-sizing: border-box; color: rgb(102, 0, 102);">width</span>=<span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 136, 0);">"300"</span> <span class="hljs-attribute" style="box-sizing: border-box; color: rgb(102, 0, 102);">height</span>=<span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 136, 0);">"300"</span>></span><span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box;"></<span class="hljs-title" style="box-sizing: border-box; color: rgb(0, 0, 136);">canvas</span>></span></code><ul class="pre-numbering" style="box-sizing: border-box; line-height: 23px; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; padding-top: 6px; padding-bottom: 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">1</li></ul></pre><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;"><span class="Apple-converted-space"> </span><code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;"><canvas></code>看起来和<code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;"><img></code>标签一样,只是<span class="Apple-converted-space"> </span><code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;"><canvas></code><span class="Apple-converted-space"> </span>只有两个可选的属性<span class="Apple-converted-space"> </span><code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">width、heigth</code><span class="Apple-converted-space"> </span>属性,而没有<span class="Apple-converted-space"> </span><code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">src、alt</code><span class="Apple-converted-space"> </span>属性。</p><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;"> 如果不给<code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;"><canvas></code>设置<code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">widht、height</code>属性时,则默认<span class="Apple-converted-space"> </span><code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">width</code>为300、<code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">height</code>为150,单位都是<code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">px</code>。也可以使用<code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">css</code>属性来设置宽高,但是如宽高属性和初始比例不一致,他会出现扭曲。所以,建议永远不要使用<code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">css</code>属性来设置<code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;"><canvas></code>的宽高。</p><h3 id="替换内容" style="margin-top: 0.8em; margin-bottom: 0.8em; box-sizing: border-box; font-family: "microsoft yahei"; font-weight: 300; line-height: 1.1; color: rgb(63, 63, 63); font-size: 1.7em; font-variant-numeric: normal; font-variant-east-asian: normal;"><a name="t3" target="_blank" style="box-sizing: border-box; color: rgb(12, 137, 207);"></a>替换内容</h3><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;"> 由于某些较老的浏览器(尤其是IE9之前的IE浏览器)或者浏览器不支持HTML元素<code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;"><canvas></code>,在这些浏览器上你应该总是能展示替代内容。</p><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;"> 支持<code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;"><canvas></code>的浏览器会只渲染<code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;"><canvas></code>标签,而忽略其中的替代内容。不支持<span class="Apple-converted-space"> </span><code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;"><canvas></code><span class="Apple-converted-space"> </span>的浏览器则 会直接渲染替代内容。</p><blockquote style="box-sizing: border-box; margin-bottom: 1.7em; line-height: 27px; padding: 15px 20px; border-left: 10px solid rgba(128, 128, 128, 0.075); background-color: rgba(128, 128, 128, 0.047); border-radius: 0px 5px 5px 0px; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;"><p style="box-sizing: border-box; line-height: 1.5; font-size: 1em; color: rgb(111, 111, 111); word-break: normal;">用文本替换:</p></blockquote><pre class="prettyprint" name="code" style="white-space: nowrap; box-sizing: border-box; position: relative; overflow-y: hidden; overflow-x: auto; margin-bottom: 1.7em; line-height: 23px; font-family: "Source Code Pro", monospace; padding: 5px 5px 5px 60px; font-size: 14px; word-break: break-all; color: rgb(51, 51, 51); background-color: rgba(128, 128, 128, 0.047); border: 0px solid rgb(136, 136, 136); border-radius: 0px; font-variant-numeric: normal; font-variant-east-asian: normal;"><code class="language-html hljshas-numbering" style="display: block; background-color: transparent; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace; font-size: 13px; white-space: pre; border-radius: 0px; overflow-x: auto; word-wrap: normal;"><span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box;"><<span class="hljs-title" style="box-sizing: border-box; color: rgb(0, 0, 136);">canvas</span>></span>你的浏览器不支持canvas,请升级你的浏览器
<span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box;"></<span class="hljs-title" style="box-sizing: border-box; color: rgb(0, 0, 136);">canvas</span>></span></code><ul class="pre-numbering" style="box-sizing: border-box; line-height: 23px; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; padding-top: 6px; padding-bottom: 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">1</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">2</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">3</li></ul></pre><blockquote style="box-sizing: border-box; margin-bottom: 1.7em; line-height: 27px; padding: 15px 20px; border-left: 10px solid rgba(128, 128, 128, 0.075); background-color: rgba(128, 128, 128, 0.047); border-radius: 0px 5px 5px 0px; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;"><p style="box-sizing: border-box; line-height: 1.5; font-size: 1em; color: rgb(111, 111, 111); word-break: normal;">用<span class="Apple-converted-space"> </span><code style="box-sizing: border-box; font-family: 'Source Code Pro', monospace; padding: 2px 4px; font-size: 14px; color: rgb(63, 63, 63); background-color: rgba(128, 128, 128, 0.0745098); white-space: nowrap; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px;"><img></code><span class="Apple-converted-space"> </span>替换:</p></blockquote><pre class="prettyprint" name="code" style="white-space: nowrap; box-sizing: border-box; position: relative; overflow-y: hidden; overflow-x: auto; margin-bottom: 1.7em; line-height: 23px; font-family: "Source Code Pro", monospace; padding: 5px 5px 5px 60px; font-size: 14px; word-break: break-all; color: rgb(51, 51, 51); background-color: rgba(128, 128, 128, 0.047); border: 0px solid rgb(136, 136, 136); border-radius: 0px; font-variant-numeric: normal; font-variant-east-asian: normal;"><code class="language-html hljshas-numbering" style="display: block; background-color: transparent; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace; font-size: 13px; white-space: pre; border-radius: 0px; overflow-x: auto; word-wrap: normal;"><span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box;"><<span class="hljs-title" style="box-sizing: border-box; color: rgb(0, 0, 136);">canvas</span>></span>
<span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box;"><<span class="hljs-title" style="box-sizing: border-box; color: rgb(0, 0, 136);">img</span> <span class="hljs-attribute" style="box-sizing: border-box; color: rgb(102, 0, 102);">src</span>=<span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 136, 0);">"./美女.jpg"</span> <span class="hljs-attribute" style="box-sizing: border-box; color: rgb(102, 0, 102);">alt</span>=<span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 136, 0);">""</span>></span>
<span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box;"></<span class="hljs-title" style="box-sizing: border-box; color: rgb(0, 0, 136);">canvas</span>></span></code><ul class="pre-numbering" style="box-sizing: border-box; line-height: 23px; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; padding-top: 6px; padding-bottom: 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">1</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">2</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">3</li></ul></pre><h3 id="结束标签canvas不可省" style="margin-top: 0.8em; margin-bottom: 0.8em; box-sizing: border-box; font-family: "microsoft yahei"; font-weight: 300; line-height: 1.1; color: rgb(63, 63, 63); font-size: 1.7em; font-variant-numeric: normal; font-variant-east-asian: normal;"><a name="t4" target="_blank" style="box-sizing: border-box; color: rgb(12, 137, 207);"></a>结束标签<code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 24px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;"></canvas></code>不可省</h3><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;">与<span class="Apple-converted-space"> </span><code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;"><img></code>元素不同,<code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;"><canvas></code>元素<strong style="box-sizing: border-box;">需要</strong>结束标签(<code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;"></canvas></code>)。如果结束标签不存在,则文档的其余部分会被认为是替代内容,将不会显示出来。</p><h2 id="22-渲染上下文thre-rending-context" style="margin-top: 0.8em; margin-bottom: 0.8em; box-sizing: border-box; font-family: "microsoft yahei"; font-weight: 300; line-height: 1.1; color: rgb(63, 63, 63); font-size: 2.15em; font-variant-numeric: normal; font-variant-east-asian: normal;"><a name="t5" target="_blank" style="box-sizing: border-box; color: rgb(12, 137, 207);"></a>2.2 渲染上下文(Thre Rending Context)</h2><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;"><span class="Apple-converted-space"> </span><code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;"><canvas></code>会创建一个固定大小的画布,会公开一个或多个<span class="Apple-converted-space"> </span><strong style="box-sizing: border-box;">渲染上下文</strong>(画笔),使用<span class="Apple-converted-space"> </span><strong style="box-sizing: border-box;">渲染上下文</strong>来绘制和处理要展示的内容。</p><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;"> 我们重点研究 2D渲染上下文。 其他的上下文我们暂不研究,比如, WebGL使用了基于OpenGL ES的3D上下文 (“experimental-webgl”) 。</p><pre class="prettyprint" name="code" style="white-space: nowrap; box-sizing: border-box; position: relative; overflow-y: hidden; overflow-x: auto; margin-bottom: 1.7em; line-height: 23px; font-family: "Source Code Pro", monospace; padding: 5px 5px 5px 60px; font-size: 14px; word-break: break-all; color: rgb(51, 51, 51); background-color: rgba(128, 128, 128, 0.047); border: 0px solid rgb(136, 136, 136); border-radius: 0px; font-variant-numeric: normal; font-variant-east-asian: normal;"><code class="language-javascript hljshas-numbering" style="display: block; background-color: transparent; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace; font-size: 13px; white-space: pre; border-radius: 0px; overflow-x: auto; word-wrap: normal;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">var</span> canvas = document.getElementById(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'tutorial'</span>);
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//获得 2d 上下文对象</span>
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">var</span> ctx = canvas.getContext(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'2d'</span>);</code><ul class="pre-numbering" style="box-sizing: border-box; line-height: 23px; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; padding-top: 6px; padding-bottom: 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">1</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">2</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">3</li></ul></pre><h2 id="23-检测支持性" style="margin-top: 0.8em; margin-bottom: 0.8em; box-sizing: border-box; font-family: "microsoft yahei"; font-weight: 300; line-height: 1.1; color: rgb(63, 63, 63); font-size: 2.15em; font-variant-numeric: normal; font-variant-east-asian: normal;"><a name="t6" target="_blank" style="box-sizing: border-box; color: rgb(12, 137, 207);"></a>2.3 检测支持性</h2><pre class="prettyprint" name="code" style="white-space: nowrap; box-sizing: border-box; position: relative; overflow-y: hidden; overflow-x: auto; margin-bottom: 1.7em; line-height: 23px; font-family: "Source Code Pro", monospace; padding: 5px 5px 5px 60px; font-size: 14px; word-break: break-all; color: rgb(51, 51, 51); background-color: rgba(128, 128, 128, 0.047); border: 0px solid rgb(136, 136, 136); border-radius: 0px; font-variant-numeric: normal; font-variant-east-asian: normal;"><code class="language-javascript hljshas-numbering" style="display: block; background-color: transparent; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace; font-size: 13px; white-space: pre; border-radius: 0px; overflow-x: auto; word-wrap: normal;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">var</span> canvas = document.getElementById(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'tutorial'</span>);
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> (canvas.getContext){
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">var</span> ctx = canvas.getContext(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'2d'</span>);
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// drawing code here</span>
} <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">else</span> {
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// canvas-unsupported code here</span>
}</code><ul class="pre-numbering" style="box-sizing: border-box; line-height: 23px; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; padding-top: 6px; padding-bottom: 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">1</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">2</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">3</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">4</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">5</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">6</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">7</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">8</li></ul></pre><h2 id="24-代码模板" style="margin-top: 0.8em; margin-bottom: 0.8em; box-sizing: border-box; font-family: "microsoft yahei"; font-weight: 300; line-height: 1.1; color: rgb(63, 63, 63); font-size: 2.15em; font-variant-numeric: normal; font-variant-east-asian: normal;"><a name="t7" target="_blank" style="box-sizing: border-box; color: rgb(12, 137, 207);"></a>2.4 代码模板</h2><pre class="prettyprint" name="code" style="white-space: nowrap; box-sizing: border-box; position: relative; overflow-y: hidden; overflow-x: auto; margin-bottom: 1.7em; line-height: 23px; font-family: "Source Code Pro", monospace; padding: 5px 5px 5px 60px; font-size: 14px; word-break: break-all; color: rgb(51, 51, 51); background-color: rgba(128, 128, 128, 0.047); border: 0px solid rgb(136, 136, 136); border-radius: 0px; font-variant-numeric: normal; font-variant-east-asian: normal;"><code class="language-html hljshas-numbering" style="display: block; background-color: transparent; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace; font-size: 13px; white-space: pre; border-radius: 0px; overflow-x: auto; word-wrap: normal;"><span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box;"><<span class="hljs-title" style="box-sizing: border-box; color: rgb(0, 0, 136);">html</span>></span>
<span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box;"><<span class="hljs-title" style="box-sizing: border-box; color: rgb(0, 0, 136);">head</span>></span>
<span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box;"><<span class="hljs-title" style="box-sizing: border-box; color: rgb(0, 0, 136);">title</span>></span>Canvas tutorial<span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box;"></<span class="hljs-title" style="box-sizing: border-box; color: rgb(0, 0, 136);">title</span>></span>
<span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box;"><<span class="hljs-title" style="box-sizing: border-box; color: rgb(0, 0, 136);">style</span> <span class="hljs-attribute" style="box-sizing: border-box; color: rgb(102, 0, 102);">type</span>=<span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 136, 0);">"text/css"</span>></span><span class="css" style="box-sizing: border-box;">
<span class="hljs-tag" style="color: rgb(0, 0, 0); box-sizing: border-box;">canvas</span> <span class="hljs-rules" style="box-sizing: border-box;">{
<span class="hljs-rule" style="box-sizing: border-box;"><span class="hljs-attribute" style="box-sizing: border-box;">border</span>:<span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 102, 102);"> <span class="hljs-number" style="box-sizing: border-box;">1</span>px solid black</span></span>;
<span class="hljs-rule" style="box-sizing: border-box;">}</span></span>
</span><span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box;"></<span class="hljs-title" style="box-sizing: border-box; color: rgb(0, 0, 136);">style</span>></span>
<span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box;"></<span class="hljs-title" style="box-sizing: border-box; color: rgb(0, 0, 136);">head</span>></span>
<span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box;"><<span class="hljs-title" style="box-sizing: border-box; color: rgb(0, 0, 136);">canvas</span> <span class="hljs-attribute" style="box-sizing: border-box; color: rgb(102, 0, 102);">id</span>=<span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 136, 0);">"tutorial"</span> <span class="hljs-attribute" style="box-sizing: border-box; color: rgb(102, 0, 102);">width</span>=<span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 136, 0);">"300"</span> <span class="hljs-attribute" style="box-sizing: border-box; color: rgb(102, 0, 102);">height</span>=<span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 136, 0);">"300"</span>></span><span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box;"></<span class="hljs-title" style="box-sizing: border-box; color: rgb(0, 0, 136);">canvas</span>></span>
<span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box;"></<span class="hljs-title" style="box-sizing: border-box; color: rgb(0, 0, 136);">body</span>></span>
<span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box;"><<span class="hljs-title" style="box-sizing: border-box; color: rgb(0, 0, 136);">script</span> <span class="hljs-attribute" style="box-sizing: border-box; color: rgb(102, 0, 102);">type</span>=<span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 136, 0);">"text/javascript"</span>></span><span class="javascript" style="box-sizing: border-box;">
<span class="hljs-function" style="box-sizing: border-box;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">function</span> <span class="hljs-title" style="box-sizing: border-box;">draw</span><span class="hljs-params" style="color: rgb(102, 0, 102); box-sizing: border-box;">()</span>{</span>
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">var</span> canvas = document.getElementById(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'tutorial'</span>);
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span>(!canvas.getContext) <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span>;
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">var</span> ctx = canvas.getContext(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"2d"</span>);
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//开始代码</span>
}
draw();
</span><span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box;"></<span class="hljs-title" style="box-sizing: border-box; color: rgb(0, 0, 136);">script</span>></span>
<span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box;"></<span class="hljs-title" style="box-sizing: border-box; color: rgb(0, 0, 136);">html</span>></span></code><ul class="pre-numbering" style="box-sizing: border-box; line-height: 23px; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; padding-top: 6px; padding-bottom: 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">1</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">2</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">3</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">4</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">5</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">6</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">7</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">8</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">9</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">10</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">11</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">12</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">13</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">14</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">15</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">16</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">17</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">18</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">19</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">20</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">21</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">22</li></ul></pre><h2 id="25-一个简单的例子" style="margin-top: 0.8em; margin-bottom: 0.8em; box-sizing: border-box; font-family: "microsoft yahei"; font-weight: 300; line-height: 1.1; color: rgb(63, 63, 63); font-size: 2.15em; font-variant-numeric: normal; font-variant-east-asian: normal;"><a name="t8" target="_blank" style="box-sizing: border-box; color: rgb(12, 137, 207);"></a>2.5 一个简单的例子</h2><blockquote style="box-sizing: border-box; margin-bottom: 1.7em; line-height: 27px; padding: 15px 20px; border-left: 10px solid rgba(128, 128, 128, 0.075); background-color: rgba(128, 128, 128, 0.047); border-radius: 0px 5px 5px 0px; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;"><p style="box-sizing: border-box; line-height: 1.5; font-size: 1em; color: rgb(111, 111, 111); word-break: normal;">绘制两个长方形。</p></blockquote><pre class="prettyprint" name="code" style="white-space: nowrap; box-sizing: border-box; position: relative; overflow-y: hidden; overflow-x: auto; margin-bottom: 1.7em; line-height: 23px; font-family: "Source Code Pro", monospace; padding: 5px 5px 5px 60px; font-size: 14px; word-break: break-all; color: rgb(51, 51, 51); background-color: rgba(128, 128, 128, 0.047); border: 0px solid rgb(136, 136, 136); border-radius: 0px; font-variant-numeric: normal; font-variant-east-asian: normal;"><code class="language-html hljshas-numbering" style="display: block; background-color: transparent; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace; font-size: 13px; white-space: pre; border-radius: 0px; overflow-x: auto; word-wrap: normal;"><span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box;"><<span class="hljs-title" style="box-sizing: border-box; color: rgb(0, 0, 136);">html</span>></span>
<span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box;"><<span class="hljs-title" style="box-sizing: border-box; color: rgb(0, 0, 136);">head</span>></span>
<span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box;"><<span class="hljs-title" style="box-sizing: border-box; color: rgb(0, 0, 136);">title</span>></span>Canvas tutorial<span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box;"></<span class="hljs-title" style="box-sizing: border-box; color: rgb(0, 0, 136);">title</span>></span>
<span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box;"><<span class="hljs-title" style="box-sizing: border-box; color: rgb(0, 0, 136);">style</span> <span class="hljs-attribute" style="box-sizing: border-box; color: rgb(102, 0, 102);">type</span>=<span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 136, 0);">"text/css"</span>></span><span class="css" style="box-sizing: border-box;">
<span class="hljs-tag" style="color: rgb(0, 0, 0); box-sizing: border-box;">canvas</span> <span class="hljs-rules" style="box-sizing: border-box;">{
<span class="hljs-rule" style="box-sizing: border-box;"><span class="hljs-attribute" style="box-sizing: border-box;">border</span>:<span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 102, 102);"> <span class="hljs-number" style="box-sizing: border-box;">1</span>px solid black</span></span>;
<span class="hljs-rule" style="box-sizing: border-box;">}</span></span>
</span><span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box;"></<span class="hljs-title" style="box-sizing: border-box; color: rgb(0, 0, 136);">style</span>></span>
<span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box;"></<span class="hljs-title" style="box-sizing: border-box; color: rgb(0, 0, 136);">head</span>></span>
<span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box;"><<span class="hljs-title" style="box-sizing: border-box; color: rgb(0, 0, 136);">canvas</span> <span class="hljs-attribute" style="box-sizing: border-box; color: rgb(102, 0, 102);">id</span>=<span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 136, 0);">"tutorial"</span> <span class="hljs-attribute" style="box-sizing: border-box; color: rgb(102, 0, 102);">width</span>=<span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 136, 0);">"300"</span> <span class="hljs-attribute" style="box-sizing: border-box; color: rgb(102, 0, 102);">height</span>=<span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 136, 0);">"300"</span>></span><span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box;"></<span class="hljs-title" style="box-sizing: border-box; color: rgb(0, 0, 136);">canvas</span>></span>
<span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box;"></<span class="hljs-title" style="box-sizing: border-box; color: rgb(0, 0, 136);">body</span>></span>
<span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box;"><<span class="hljs-title" style="box-sizing: border-box; color: rgb(0, 0, 136);">script</span> <span class="hljs-attribute" style="box-sizing: border-box; color: rgb(102, 0, 102);">type</span>=<span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 136, 0);">"text/javascript"</span>></span><span class="javascript" style="box-sizing: border-box;">
<span class="hljs-function" style="box-sizing: border-box;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">function</span> <span class="hljs-title" style="box-sizing: border-box;">draw</span><span class="hljs-params" style="color: rgb(102, 0, 102); box-sizing: border-box;">()</span>{</span>
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">var</span> canvas = document.getElementById(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'tutorial'</span>);
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span>(!canvas.getContext) <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span>;
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">var</span> ctx = canvas.getContext(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"2d"</span>);
ctx.fillStyle = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"rgb(200,0,0)"</span>;
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//绘制矩形</span>
ctx.fillRect (<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">10</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">10</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">55</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">50</span>);
ctx.fillStyle = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"rgba(0, 0, 200, 0.5)"</span>;
ctx.fillRect (<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">30</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">30</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">55</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">50</span>);
}
draw();
</span><span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box;"></<span class="hljs-title" style="box-sizing: border-box; color: rgb(0, 0, 136);">script</span>></span>
<span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box;"></<span class="hljs-title" style="box-sizing: border-box; color: rgb(0, 0, 136);">html</span>></span></code><ul class="pre-numbering" style="box-sizing: border-box; line-height: 23px; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; padding-top: 6px; padding-bottom: 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">1</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">2</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">3</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">4</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">5</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">6</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">7</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">8</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">9</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">10</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">11</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">12</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">13</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">14</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">15</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">16</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">17</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">18</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">19</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">20</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">21</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">22</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">23</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">24</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">25</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">26</li></ul></pre><h1 id="三绘制形状" style="margin-top: 0.8em; margin-bottom: 0.8em; box-sizing: border-box; font-family: "microsoft yahei"; font-weight: 300; line-height: 1.1; color: rgb(63, 63, 63); font-size: 2.6em; font-variant-numeric: normal; font-variant-east-asian: normal;"><a name="t9" target="_blank" style="box-sizing: border-box; color: rgb(12, 137, 207);"></a>三、绘制形状</h1><h2 id="31-栅格grid和坐标空间" style="margin-top: 0.8em; margin-bottom: 0.8em; box-sizing: border-box; font-family: "microsoft yahei"; font-weight: 300; line-height: 1.1; color: rgb(63, 63, 63); font-size: 2.15em; font-variant-numeric: normal; font-variant-east-asian: normal;"><a name="t10" target="_blank" style="box-sizing: border-box; color: rgb(12, 137, 207);"></a>3.1 栅格<code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 31px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">(grid)</code>和坐标空间</h2><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;"> 如下图所示,<code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">canvas</code>元素默认被网格所覆盖。通常来说网格中的一个单元相当于<code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">canvas</code>元素中的一像素。栅格的起点为左上角(坐标为(0,0))。所有元素的位置都相对于原点来定位。所以图中蓝色方形左上角的坐标为距离左边(X轴)x像素,距离上边(Y轴)y像素(坐标为(x,y))。</p><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;"> 后面我们会涉及到坐标原点的平移、网格的旋转以及缩放等。</p><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;"><img src="https://mdn.mozillademos.org/files/224/Canvas_default_grid.png" alt="" title="" style="border: none; box-sizing: border-box; margin-top: 15px; margin-bottom: 15px; max-width: 602px; max-height: 100%; height: auto;"></p><h2 id="32-绘制矩形" style="margin-top: 0.8em; margin-bottom: 0.8em; box-sizing: border-box; font-family: "microsoft yahei"; font-weight: 300; line-height: 1.1; color: rgb(63, 63, 63); font-size: 2.15em; font-variant-numeric: normal; font-variant-east-asian: normal;"><a name="t11" target="_blank" style="box-sizing: border-box; color: rgb(12, 137, 207);"></a>3.2 绘制矩形</h2><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;"><span class="Apple-converted-space"> </span><code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;"><canvas></code><span class="Apple-converted-space"> </span>只支持一种原生的 图形绘制:矩形。所有其他图形都至少需要生成一种路径(<code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">path</code>)。不过,我们拥有众多路径生成的方法让复杂图形的绘制成为了可能。</p><blockquote style="box-sizing: border-box; margin-bottom: 1.7em; line-height: 27px; padding: 15px 20px; border-left: 10px solid rgba(128, 128, 128, 0.075); background-color: rgba(128, 128, 128, 0.047); border-radius: 0px 5px 5px 0px; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;"><p style="box-sizing: border-box; line-height: 1.5; font-size: 1em; color: rgb(111, 111, 111); word-break: normal;"><code style="box-sizing: border-box; font-family: 'Source Code Pro', monospace; padding: 2px 4px; font-size: 14px; color: rgb(63, 63, 63); background-color: rgba(128, 128, 128, 0.0745098); white-space: nowrap; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px;">canvas</code>t 提供了三种方法绘制矩形:</p></blockquote><ol style="box-sizing: border-box; line-height: 27px; margin-bottom: 1.7em; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;"><li style="box-sizing: border-box;"><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal;"><code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">fillRect(x, y, width, height)</code></p><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal;">绘制一个填充的矩形</p></li><li style="box-sizing: border-box;"><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal;"><code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">strockRect(x, y, width, height)</code></p><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal;">绘制一个矩形的边框</p></li><li style="box-sizing: border-box;"><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal;"><code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">clearRect(x, y, widh, height)</code></p><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal;">清除指定的矩形区域,然后这块区域会变的完全透明。</p></li></ol><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;">说明:</p><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;"> 这3个方法具有相同的参数。</p><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;"><span class="Apple-converted-space"> </span><code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">x, y</code>:指的是矩形的左上角的坐标。(相对于<code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">canvas</code>的坐标原点)</p><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;"><span class="Apple-converted-space"> </span><code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">width, height</code>:指的是绘制的矩形的宽和高。</p><pre class="prettyprint" name="code" style="white-space: nowrap; box-sizing: border-box; position: relative; overflow-y: hidden; overflow-x: auto; margin-bottom: 1.7em; line-height: 23px; font-family: "Source Code Pro", monospace; padding: 5px 5px 5px 60px; font-size: 14px; word-break: break-all; color: rgb(51, 51, 51); background-color: rgba(128, 128, 128, 0.047); border: 0px solid rgb(136, 136, 136); border-radius: 0px; font-variant-numeric: normal; font-variant-east-asian: normal;"><code class="language-javascript hljshas-numbering" style="display: block; background-color: transparent; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace; font-size: 13px; white-space: pre; border-radius: 0px; overflow-x: auto; word-wrap: normal;"><span class="hljs-function" style="box-sizing: border-box;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">function</span> <span class="hljs-title" style="box-sizing: border-box;">draw</span><span class="hljs-params" style="color: rgb(102, 0, 102); box-sizing: border-box;">()</span>{</span>
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">var</span> canvas = document.getElementById(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'tutorial'</span>);
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span>(!canvas.getContext) <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span>;
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">var</span> ctx = canvas.getContext(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"2d"</span>);
ctx.fillRect(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">10</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">10</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">100</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">50</span>);<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//绘制矩形,填充的默认颜色为黑色</span>
ctx.strokeRect(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">10</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">70</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">100</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">50</span>);<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//绘制矩形边框</span>
}
draw();</code><ul class="pre-numbering" style="box-sizing: border-box; line-height: 23px; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; padding-top: 6px; padding-bottom: 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">1</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">2</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">3</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">4</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">5</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">6</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">7</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">8</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">9</li></ul></pre><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;"><img src="http://o7cqr8cfk.bkt.clouddn.com/17-6-4/57498980.jpg" alt="" title="" style="border: none; box-sizing: border-box; margin-top: 15px; margin-bottom: 15px; max-width: 602px; max-height: 100%; height: auto;"></p><pre class="prettyprint" name="code" style="white-space: nowrap; box-sizing: border-box; position: relative; overflow-y: hidden; overflow-x: auto; margin-bottom: 1.7em; line-height: 23px; font-family: "Source Code Pro", monospace; padding: 5px 5px 5px 60px; font-size: 14px; word-break: break-all; color: rgb(51, 51, 51); background-color: rgba(128, 128, 128, 0.047); border: 0px solid rgb(136, 136, 136); border-radius: 0px; font-variant-numeric: normal; font-variant-east-asian: normal;"><code class="language-javascript hljshas-numbering" style="display: block; background-color: transparent; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace; font-size: 13px; white-space: pre; border-radius: 0px; overflow-x: auto; word-wrap: normal;">ctx.clearRect(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">15</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">15</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">50</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">25</span>);</code><ul class="pre-numbering" style="box-sizing: border-box; line-height: 23px; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; padding-top: 6px; padding-bottom: 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">1</li></ul></pre><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;"><img src="http://o7cqr8cfk.bkt.clouddn.com/17-6-4/92358331.jpg" alt="" title="" style="border: none; box-sizing: border-box; margin-top: 15px; margin-bottom: 15px; max-width: 602px; max-height: 100%; height: auto;"></p><h1 id="四绘制路径path" style="margin-top: 0.8em; margin-bottom: 0.8em; box-sizing: border-box; font-family: "microsoft yahei"; font-weight: 300; line-height: 1.1; color: rgb(63, 63, 63); font-size: 2.6em; font-variant-numeric: normal; font-variant-east-asian: normal;"><a name="t12" target="_blank" style="box-sizing: border-box; color: rgb(12, 137, 207);"></a>四、绘制路径(<code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 37px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">path</code>)</h1><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;"> 图形的基本元素是路径。</p><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;"> 路径是通过不同颜色和宽度的线段或曲线相连形成的不同形状的点的集合。</p><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;"> 一个路径,甚至一个子路径,都是闭合的。</p><blockquote style="box-sizing: border-box; margin-bottom: 1.7em; line-height: 27px; padding: 15px 20px; border-left: 10px solid rgba(128, 128, 128, 0.075); background-color: rgba(128, 128, 128, 0.047); border-radius: 0px 5px 5px 0px; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;"><p style="box-sizing: border-box; line-height: 1.5; font-size: 1em; color: rgb(111, 111, 111); word-break: normal;">使用路径绘制图形需要一些额外的步骤:</p></blockquote><ol style="box-sizing: border-box; line-height: 27px; margin-bottom: 1.7em; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;"><li style="box-sizing: border-box;">创建路径起始点</li><li style="box-sizing: border-box;">调用绘制方法去绘制出路径</li><li style="box-sizing: border-box;">把路径封闭</li><li style="box-sizing: border-box;">一旦路径生成,通过描边或填充路径区域来渲染图形。</li></ol><blockquote style="box-sizing: border-box; margin-bottom: 1.7em; line-height: 27px; padding: 15px 20px; border-left: 10px solid rgba(128, 128, 128, 0.075); background-color: rgba(128, 128, 128, 0.047); border-radius: 0px 5px 5px 0px; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;"><p style="box-sizing: border-box; line-height: 1.5; font-size: 1em; color: rgb(111, 111, 111); word-break: normal;">下面是需要用到的方法:</p></blockquote><ol style="box-sizing: border-box; line-height: 27px; margin-bottom: 1.7em; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;"><li style="box-sizing: border-box;"><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal;"><code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">beginPath()</code></p><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal;">新建一条路径,路径一旦创建成功,图形绘制命令被指向到路径上生成路径</p></li><li style="box-sizing: border-box;"><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal;"><code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">moveTo(x, y)</code></p><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal;">把画笔移动到指定的坐标<code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">(x, y)</code>。相当于设置路径的起始点坐标。</p></li><li style="box-sizing: border-box;"><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal;"><code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">closePath()</code></p><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal;">闭合路径之后,图形绘制命令又重新指向到上下文中</p></li><li style="box-sizing: border-box;"><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal;"><code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">stroke()</code></p><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal;">通过线条来绘制图形轮廓</p></li><li style="box-sizing: border-box;"><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal;"><code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">fill()</code></p><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal;">通过填充路径的内容区域生成实心的图形</p></li></ol><h2 id="41-绘制线段" style="margin-top: 0.8em; margin-bottom: 0.8em; box-sizing: border-box; font-family: "microsoft yahei"; font-weight: 300; line-height: 1.1; color: rgb(63, 63, 63); font-size: 2.15em; font-variant-numeric: normal; font-variant-east-asian: normal;"><a name="t13" target="_blank" style="box-sizing: border-box; color: rgb(12, 137, 207);"></a>4.1 绘制线段</h2><pre class="prettyprint" name="code" style="white-space: nowrap; box-sizing: border-box; position: relative; overflow-y: hidden; overflow-x: auto; margin-bottom: 1.7em; line-height: 23px; font-family: "Source Code Pro", monospace; padding: 5px 5px 5px 60px; font-size: 14px; word-break: break-all; color: rgb(51, 51, 51); background-color: rgba(128, 128, 128, 0.047); border: 0px solid rgb(136, 136, 136); border-radius: 0px; font-variant-numeric: normal; font-variant-east-asian: normal;"><code class="language-javascript hljshas-numbering" style="display: block; background-color: transparent; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace; font-size: 13px; white-space: pre; border-radius: 0px; overflow-x: auto; word-wrap: normal;"><span class="hljs-function" style="box-sizing: border-box;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">function</span> <span class="hljs-title" style="box-sizing: border-box;">draw</span><span class="hljs-params" style="color: rgb(102, 0, 102); box-sizing: border-box;">()</span>{</span>
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">var</span> canvas = document.getElementById(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'tutorial'</span>);
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> (!canvas.getContext) <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span>;
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">var</span> ctx = canvas.getContext(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"2d"</span>);
ctx.beginPath(); <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//新建一条path</span>
ctx.moveTo(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">50</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">50</span>); <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//把画笔移动到指定的坐标</span>
ctx.lineTo(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">200</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">50</span>);<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//绘制一条从当前位置到指定坐标(200, 50)的直线.</span>
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//闭合路径。会拉一条从当前点到path起始点的直线。如果当前点与起始点重合,则什么都不做</span>
ctx.closePath();
ctx.stroke(); <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//绘制路径。</span>
}
draw();</code><ul class="pre-numbering" style="box-sizing: border-box; line-height: 23px; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; padding-top: 6px; padding-bottom: 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">1</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">2</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">3</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">4</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">5</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">6</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">7</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">8</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">9</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">10</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">11</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">12</li></ul></pre><h2 id="42-绘制三角形边框" style="margin-top: 0.8em; margin-bottom: 0.8em; box-sizing: border-box; font-family: "microsoft yahei"; font-weight: 300; line-height: 1.1; color: rgb(63, 63, 63); font-size: 2.15em; font-variant-numeric: normal; font-variant-east-asian: normal;"><a name="t14" target="_blank" style="box-sizing: border-box; color: rgb(12, 137, 207);"></a>4.2 绘制三角形边框</h2><pre class="prettyprint" name="code" style="white-space: nowrap; box-sizing: border-box; position: relative; overflow-y: hidden; overflow-x: auto; margin-bottom: 1.7em; line-height: 23px; font-family: "Source Code Pro", monospace; padding: 5px 5px 5px 60px; font-size: 14px; word-break: break-all; color: rgb(51, 51, 51); background-color: rgba(128, 128, 128, 0.047); border: 0px solid rgb(136, 136, 136); border-radius: 0px; font-variant-numeric: normal; font-variant-east-asian: normal;"><code class="language-javascript hljshas-numbering" style="display: block; background-color: transparent; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace; font-size: 13px; white-space: pre; border-radius: 0px; overflow-x: auto; word-wrap: normal;"><span class="hljs-function" style="box-sizing: border-box;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">function</span> <span class="hljs-title" style="box-sizing: border-box;">draw</span><span class="hljs-params" style="color: rgb(102, 0, 102); box-sizing: border-box;">()</span>{</span>
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">var</span> canvas = document.getElementById(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'tutorial'</span>);
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> (!canvas.getContext) <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span>;
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">var</span> ctx = canvas.getContext(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"2d"</span>);
ctx.beginPath();
ctx.moveTo(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">50</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">50</span>);
ctx.lineTo(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">200</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">50</span>);
ctx.lineTo(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">200</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">200</span>);
ctx.closePath(); <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//虽然我们只绘制了两条线段,但是closePath会closePath,仍然是一个3角形</span>
ctx.stroke(); <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//描边。stroke不会自动closePath()</span>
}
draw();</code><ul class="pre-numbering" style="box-sizing: border-box; line-height: 23px; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; padding-top: 6px; padding-bottom: 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">1</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">2</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">3</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">4</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">5</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">6</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">7</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">8</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">9</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">10</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">11</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">12</li></ul></pre><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;"><img src="http://o7cqr8cfk.bkt.clouddn.com/17-6-4/20099429.jpg" alt="" title="" style="border: none; box-sizing: border-box; margin-top: 15px; margin-bottom: 15px; max-width: 602px; max-height: 100%; height: auto;"></p><h2 id="43-填充三角形" style="margin-top: 0.8em; margin-bottom: 0.8em; box-sizing: border-box; font-family: "microsoft yahei"; font-weight: 300; line-height: 1.1; color: rgb(63, 63, 63); font-size: 2.15em; font-variant-numeric: normal; font-variant-east-asian: normal;"><a name="t15" target="_blank" style="box-sizing: border-box; color: rgb(12, 137, 207);"></a>4.3 填充三角形</h2><pre class="prettyprint" name="code" style="white-space: nowrap; box-sizing: border-box; position: relative; overflow-y: hidden; overflow-x: auto; margin-bottom: 1.7em; line-height: 23px; font-family: "Source Code Pro", monospace; padding: 5px 5px 5px 60px; font-size: 14px; word-break: break-all; color: rgb(51, 51, 51); background-color: rgba(128, 128, 128, 0.047); border: 0px solid rgb(136, 136, 136); border-radius: 0px; font-variant-numeric: normal; font-variant-east-asian: normal;"><code class="language-javascript hljshas-numbering" style="display: block; background-color: transparent; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace; font-size: 13px; white-space: pre; border-radius: 0px; overflow-x: auto; word-wrap: normal;"><span class="hljs-function" style="box-sizing: border-box;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">function</span> <span class="hljs-title" style="box-sizing: border-box;">draw</span><span class="hljs-params" style="color: rgb(102, 0, 102); box-sizing: border-box;">()</span>{</span>
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">var</span> canvas = document.getElementById(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'tutorial'</span>);
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> (!canvas.getContext) <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span>;
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">var</span> ctx = canvas.getContext(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"2d"</span>);
ctx.beginPath();
ctx.moveTo(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">50</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">50</span>);
ctx.lineTo(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">200</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">50</span>);
ctx.lineTo(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">200</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">200</span>);
ctx.fill(); <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//填充闭合区域。如果path没有闭合,则fill()会自动闭合路径。</span>
}
draw();</code><ul class="pre-numbering" style="box-sizing: border-box; line-height: 23px; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; padding-top: 6px; padding-bottom: 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">1</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">2</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">3</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">4</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">5</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">6</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">7</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">8</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">9</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">10</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">11</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">12</li></ul></pre><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;"><img src="http://o7cqr8cfk.bkt.clouddn.com/17-6-4/83072674.jpg" alt="" title="" style="border: none; box-sizing: border-box; margin-top: 15px; margin-bottom: 15px; max-width: 602px; max-height: 100%; height: auto;"></p><h2 id="44-绘制圆弧" style="margin-top: 0.8em; margin-bottom: 0.8em; box-sizing: border-box; font-family: "microsoft yahei"; font-weight: 300; line-height: 1.1; color: rgb(63, 63, 63); font-size: 2.15em; font-variant-numeric: normal; font-variant-east-asian: normal;"><a name="t16" target="_blank" style="box-sizing: border-box; color: rgb(12, 137, 207);"></a>4.4 绘制圆弧</h2><blockquote style="box-sizing: border-box; margin-bottom: 1.7em; line-height: 27px; padding: 15px 20px; border-left: 10px solid rgba(128, 128, 128, 0.075); background-color: rgba(128, 128, 128, 0.047); border-radius: 0px 5px 5px 0px; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;"><p style="box-sizing: border-box; line-height: 1.5; font-size: 1em; color: rgb(111, 111, 111); word-break: normal;">有两个方法可以绘制圆弧:</p></blockquote><ol style="box-sizing: border-box; line-height: 27px; margin-bottom: 1.7em; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;"><li style="box-sizing: border-box;"><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal;"><code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">arc(x, y, r, startAngle, endAngle, anticlockwise)</code>:</p><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal;">以<code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">(x, y)</code>为圆心,以<code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">r</code>为半径,从<span class="Apple-converted-space"> </span><code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">startAngle</code>弧度开始到<code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">endAngle</code>弧度结束。<code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">anticlosewise</code>是布尔值,<code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">true</code>表示逆时针,<code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">false</code>表示顺时针。(默认是顺时针)</p><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal;">注意:</p><ol style="box-sizing: border-box; line-height: 27px; margin-bottom: 1.7em;"><li style="box-sizing: border-box;">这里的度数都是弧度。</li><li style="box-sizing: border-box;"><code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">0</code>弧度是指的<code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">x</code>轴正方形</li></ol><pre class="prettyprint" name="code" style="white-space: nowrap; box-sizing: border-box; position: relative; overflow-y: hidden; overflow-x: auto; margin-bottom: 1.7em; line-height: 23px; font-family: "Source Code Pro", monospace; padding: 5px 5px 5px 60px; font-size: 14px; word-break: break-all; color: rgb(51, 51, 51); background-color: rgba(128, 128, 128, 0.047); border: 0px solid rgb(136, 136, 136); border-radius: 0px;"><code class="language-javascript hljshas-numbering" style="display: block; background-color: transparent; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace; font-size: 13px; white-space: pre; border-radius: 0px; overflow-x: auto; word-wrap: normal;">radians=(<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">Math</span>.PI/<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">180</span>)*degrees <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//角度转换成弧度</span></code><ul class="pre-numbering" style="box-sizing: border-box; line-height: 23px; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; padding-top: 6px; padding-bottom: 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">1</li></ul></pre></li><li style="box-sizing: border-box;"><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal;"><code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">arcTo(x1, y1, x2, y2, radius)</code>:</p><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal;">根据给定的控制点和半径画一段圆弧,最后再以直线连接两个控制点。</p></li></ol><h3 id="圆弧案例1" style="margin-top: 0.8em; margin-bottom: 0.8em; box-sizing: border-box; font-family: "microsoft yahei"; font-weight: 300; line-height: 1.1; color: rgb(63, 63, 63); font-size: 1.7em; font-variant-numeric: normal; font-variant-east-asian: normal;"><a name="t17" target="_blank" style="box-sizing: border-box; color: rgb(12, 137, 207);"></a>圆弧案例1:</h3><pre class="prettyprint" name="code" style="white-space: nowrap; box-sizing: border-box; position: relative; overflow-y: hidden; overflow-x: auto; margin-bottom: 1.7em; line-height: 23px; font-family: "Source Code Pro", monospace; padding: 5px 5px 5px 60px; font-size: 14px; word-break: break-all; color: rgb(51, 51, 51); background-color: rgba(128, 128, 128, 0.047); border: 0px solid rgb(136, 136, 136); border-radius: 0px; font-variant-numeric: normal; font-variant-east-asian: normal;"><code class="language-javascript hljshas-numbering" style="display: block; background-color: transparent; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace; font-size: 13px; white-space: pre; border-radius: 0px; overflow-x: auto; word-wrap: normal;"><span class="hljs-function" style="box-sizing: border-box;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">function</span> <span class="hljs-title" style="box-sizing: border-box;">draw</span><span class="hljs-params" style="color: rgb(102, 0, 102); box-sizing: border-box;">()</span>{</span>
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">var</span> canvas = document.getElementById(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'tutorial'</span>);
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> (!canvas.getContext) <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span>;
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">var</span> ctx = canvas.getContext(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"2d"</span>);
ctx.beginPath();
ctx.arc(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">50</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">50</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">40</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>, <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">Math</span>.PI / <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span>, <span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">false</span>);
ctx.stroke();
}
draw();</code><ul class="pre-numbering" style="box-sizing: border-box; line-height: 23px; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; padding-top: 6px; padding-bottom: 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">1</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">2</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">3</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">4</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">5</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">6</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">7</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">8</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">9</li></ul></pre><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;"><img src="http://o7cqr8cfk.bkt.clouddn.com/17-6-4/97210404.jpg" alt="" title="" style="border: none; box-sizing: border-box; margin-top: 15px; margin-bottom: 15px; max-width: 602px; max-height: 100%; height: auto;"></p><h3 id="圆弧案例2" style="margin-top: 0.8em; margin-bottom: 0.8em; box-sizing: border-box; font-family: "microsoft yahei"; font-weight: 300; line-height: 1.1; color: rgb(63, 63, 63); font-size: 1.7em; font-variant-numeric: normal; font-variant-east-asian: normal;"><a name="t18" target="_blank" style="box-sizing: border-box; color: rgb(12, 137, 207);"></a>圆弧案例2:</h3><pre class="prettyprint" name="code" style="white-space: nowrap; box-sizing: border-box; position: relative; overflow-y: hidden; overflow-x: auto; margin-bottom: 1.7em; line-height: 23px; font-family: "Source Code Pro", monospace; padding: 5px 5px 5px 60px; font-size: 14px; word-break: break-all; color: rgb(51, 51, 51); background-color: rgba(128, 128, 128, 0.047); border: 0px solid rgb(136, 136, 136); border-radius: 0px; font-variant-numeric: normal; font-variant-east-asian: normal;"><code class="language-javascript hljshas-numbering" style="display: block; background-color: transparent; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace; font-size: 13px; white-space: pre; border-radius: 0px; overflow-x: auto; word-wrap: normal;"><span class="hljs-function" style="box-sizing: border-box;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">function</span> <span class="hljs-title" style="box-sizing: border-box;">draw</span><span class="hljs-params" style="color: rgb(102, 0, 102); box-sizing: border-box;">()</span>{</span>
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">var</span> canvas = document.getElementById(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'tutorial'</span>);
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> (!canvas.getContext) <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span>;
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">var</span> ctx = canvas.getContext(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"2d"</span>);
ctx.beginPath();
ctx.arc(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">50</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">50</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">40</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>, <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">Math</span>.PI / <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span>, <span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">false</span>);
ctx.stroke();
ctx.beginPath();
ctx.arc(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">150</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">50</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">40</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>, -<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">Math</span>.PI / <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span>, <span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">true</span>);
ctx.closePath();
ctx.stroke();
ctx.beginPath();
ctx.arc(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">50</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">150</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">40</span>, -<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">Math</span>.PI / <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span>, <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">Math</span>.PI / <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span>, <span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">false</span>);
ctx.fill();
ctx.beginPath();
ctx.arc(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">150</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">150</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">40</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>, <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">Math</span>.PI, <span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">false</span>);
ctx.fill();
}
draw();</code><ul class="pre-numbering" style="box-sizing: border-box; line-height: 23px; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; padding-top: 6px; padding-bottom: 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">1</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">2</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">3</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">4</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">5</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">6</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">7</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">8</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">9</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">10</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">11</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">12</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">13</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">14</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">15</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">16</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">17</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">18</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">19</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">20</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">21</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">22</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">23</li></ul></pre><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;"><img src="http://o7cqr8cfk.bkt.clouddn.com/17-6-4/62078705.jpg" alt="" title="" style="border: none; box-sizing: border-box; margin-top: 15px; margin-bottom: 15px; max-width: 602px; max-height: 100%; height: auto;"></p><h3 id="圆弧案例3" style="margin-top: 0.8em; margin-bottom: 0.8em; box-sizing: border-box; font-family: "microsoft yahei"; font-weight: 300; line-height: 1.1; color: rgb(63, 63, 63); font-size: 1.7em; font-variant-numeric: normal; font-variant-east-asian: normal;"><a name="t19" target="_blank" style="box-sizing: border-box; color: rgb(12, 137, 207);"></a>圆弧案例3:</h3><pre class="prettyprint" name="code" style="white-space: nowrap; box-sizing: border-box; position: relative; overflow-y: hidden; overflow-x: auto; margin-bottom: 1.7em; line-height: 23px; font-family: "Source Code Pro", monospace; padding: 5px 5px 5px 60px; font-size: 14px; word-break: break-all; color: rgb(51, 51, 51); background-color: rgba(128, 128, 128, 0.047); border: 0px solid rgb(136, 136, 136); border-radius: 0px; font-variant-numeric: normal; font-variant-east-asian: normal;"><code class="language-javascript hljshas-numbering" style="display: block; background-color: transparent; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace; font-size: 13px; white-space: pre; border-radius: 0px; overflow-x: auto; word-wrap: normal;"><span class="hljs-function" style="box-sizing: border-box;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">function</span> <span class="hljs-title" style="box-sizing: border-box;">draw</span><span class="hljs-params" style="color: rgb(102, 0, 102); box-sizing: border-box;">()</span>{</span>
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">var</span> canvas = document.getElementById(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'tutorial'</span>);
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> (!canvas.getContext) <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span>;
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">var</span> ctx = canvas.getContext(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"2d"</span>);
ctx.beginPath();
ctx.moveTo(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">50</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">50</span>);
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//参数1、2:控制点1坐标 参数3、4:控制点2坐标参数4:圆弧半径</span>
ctx.arcTo(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">200</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">50</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">200</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">200</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">100</span>);
ctx.lineTo(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">200</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">200</span>)
ctx.stroke();
ctx.beginPath();
ctx.rect(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">50</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">50</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">10</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">10</span>);
ctx.rect(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">200</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">50</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">10</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">10</span>)
ctx.rect(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">200</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">200</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">10</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">10</span>)
ctx.fill()
}
draw();</code><ul class="pre-numbering" style="box-sizing: border-box; line-height: 23px; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; padding-top: 6px; padding-bottom: 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">1</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">2</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">3</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">4</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">5</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">6</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">7</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">8</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">9</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">10</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">11</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">12</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">13</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">14</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">15</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">16</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">17</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">18</li></ul></pre><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;"><img src="http://o7cqr8cfk.bkt.clouddn.com/17-6-4/95442248.jpg" alt="" title="" style="border: none; box-sizing: border-box; margin-top: 15px; margin-bottom: 15px; max-width: 602px; max-height: 100%; height: auto;"></p><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;"><code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">arcTo</code>方法的说明:</p><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;"> 这个方法可以这样理解。绘制的弧形是由两条切线所决定。</p><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;"> 第 1 条切线:起始点和控制点1决定的直线。</p><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;"> 第 2 条切线:控制点1 和控制点2决定的直线。</p><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;"><span class="Apple-converted-space"> </span><strong style="box-sizing: border-box;">其实绘制的圆弧就是与这两条直线相切的圆弧。</strong></p><h2 id="45-绘制贝塞尔曲线" style="margin-top: 0.8em; margin-bottom: 0.8em; box-sizing: border-box; font-family: "microsoft yahei"; font-weight: 300; line-height: 1.1; color: rgb(63, 63, 63); font-size: 2.15em; font-variant-numeric: normal; font-variant-east-asian: normal;"><a name="t20" target="_blank" style="box-sizing: border-box; color: rgb(12, 137, 207);"></a>4.5 绘制贝塞尔曲线</h2><h3 id="451-什么是贝塞尔曲线" style="margin-top: 0.8em; margin-bottom: 0.8em; box-sizing: border-box; font-family: "microsoft yahei"; font-weight: 300; line-height: 1.1; color: rgb(63, 63, 63); font-size: 1.7em; font-variant-numeric: normal; font-variant-east-asian: normal;"><a name="t21" target="_blank" style="box-sizing: border-box; color: rgb(12, 137, 207);"></a>4.5.1 什么是贝塞尔曲线</h3><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;"> 贝塞尔曲线(Bézier curve),又称贝兹曲线或贝济埃曲线,是应用于二维图形应用程序的数学曲线。</p><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;"> 一般的矢量图形软件通过它来精确画出曲线,贝兹曲线由线段与节点组成,节点是可拖动的支点,线段像可伸缩的皮筋,我们在绘图工具上看到的钢笔工具就是来做这种矢量曲线的。</p><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;"> 贝塞尔曲线是计算机图形学中相当重要的参数曲线,在一些比较成熟的位图软件中也有贝塞尔曲线工具如PhotoShop等。在Flash4中还没有完整的曲线工具,而在Flash5里面已经提供出贝塞尔曲线工具。</p><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;"> 贝塞尔曲线于1962,由法国工程师皮埃尔·贝塞尔(Pierre Bézier)所广泛发表,他运用贝塞尔曲线来为汽车的主体进行设计。贝塞尔曲线最初由Paul de Casteljau于1959年运用de Casteljau演算法开发,以稳定数值的方法求出贝兹曲线。</p><h4 id="一次贝塞尔曲线线性贝塞尔曲线" style="margin-top: 0.8em; margin-bottom: 0.8em; box-sizing: border-box; font-family: "microsoft yahei"; font-weight: 300; line-height: 1.1; color: rgb(63, 63, 63); font-size: 1.25em; font-variant-numeric: normal; font-variant-east-asian: normal;"><a name="t22" target="_blank" style="box-sizing: border-box; color: rgb(12, 137, 207);"></a>一次贝塞尔曲线(线性贝塞尔曲线)</h4><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;"> 一次贝塞尔曲线其实是一条直线。</p><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;"><img src="http://o7cqr8cfk.bkt.clouddn.com/17-6-4/40655077.jpg" alt="" title="" style="border: none; box-sizing: border-box; margin-top: 15px; margin-bottom: 15px; max-width: 602px; max-height: 100%; height: auto;"></p><h4 id="二次贝塞尔曲线" style="margin-top: 0.8em; margin-bottom: 0.8em; box-sizing: border-box; font-family: "microsoft yahei"; font-weight: 300; line-height: 1.1; color: rgb(63, 63, 63); font-size: 1.25em; font-variant-numeric: normal; font-variant-east-asian: normal;"><a name="t23" target="_blank" style="box-sizing: border-box; color: rgb(12, 137, 207);"></a>二次贝塞尔曲线</h4><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;"><img src="http://o7cqr8cfk.bkt.clouddn.com/17-6-4/94917354.jpg" alt="" title="" style="border: none; box-sizing: border-box; margin-top: 15px; margin-bottom: 15px; max-width: 602px; max-height: 100%; height: auto;"></p><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;"><img src="http://o7cqr8cfk.bkt.clouddn.com/17-6-4/35792129.jpg" alt="" title="" style="border: none; box-sizing: border-box; margin-top: 15px; margin-bottom: 15px; max-width: 602px; max-height: 100%; height: auto;"></p><h4 id="三次贝塞尔曲线" style="margin-top: 0.8em; margin-bottom: 0.8em; box-sizing: border-box; font-family: "microsoft yahei"; font-weight: 300; line-height: 1.1; color: rgb(63, 63, 63); font-size: 1.25em; font-variant-numeric: normal; font-variant-east-asian: normal;"><a name="t24" target="_blank" style="box-sizing: border-box; color: rgb(12, 137, 207);"></a>三次贝塞尔曲线</h4><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;"><img src="http://o7cqr8cfk.bkt.clouddn.com/17-6-4/69159470.jpg" alt="" title="" style="border: none; box-sizing: border-box; margin-top: 15px; margin-bottom: 15px; max-width: 602px; max-height: 100%; height: auto;"></p><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;"><img src="http://o7cqr8cfk.bkt.clouddn.com/17-6-4/55999388.jpg" alt="" title="" style="border: none; box-sizing: border-box; margin-top: 15px; margin-bottom: 15px; max-width: 602px; max-height: 100%; height: auto;"></p><h3 id="452-绘制贝塞尔曲线" style="margin-top: 0.8em; margin-bottom: 0.8em; box-sizing: border-box; font-family: "microsoft yahei"; font-weight: 300; line-height: 1.1; color: rgb(63, 63, 63); font-size: 1.7em; font-variant-numeric: normal; font-variant-east-asian: normal;"><a name="t25" target="_blank" style="box-sizing: border-box; color: rgb(12, 137, 207);"></a>4.5.2 绘制贝塞尔曲线</h3><h4 id="绘制二次贝塞尔曲线" style="margin-top: 0.8em; margin-bottom: 0.8em; box-sizing: border-box; font-family: "microsoft yahei"; font-weight: 300; line-height: 1.1; color: rgb(63, 63, 63); font-size: 1.25em; font-variant-numeric: normal; font-variant-east-asian: normal;"><a name="t26" target="_blank" style="box-sizing: border-box; color: rgb(12, 137, 207);"></a>绘制二次贝塞尔曲线</h4><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;"><code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">quadraticCurveTo(cp1x, cp1y, x, y)</code>:</p><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;"><strong style="box-sizing: border-box;">说明:</strong></p><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;"> 参数1和2:控制点坐标</p><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;"> 参数3和4:结束点坐标</p><pre class="prettyprint" name="code" style="white-space: nowrap; box-sizing: border-box; position: relative; overflow-y: hidden; overflow-x: auto; margin-bottom: 1.7em; line-height: 23px; font-family: "Source Code Pro", monospace; padding: 5px 5px 5px 60px; font-size: 14px; word-break: break-all; color: rgb(51, 51, 51); background-color: rgba(128, 128, 128, 0.047); border: 0px solid rgb(136, 136, 136); border-radius: 0px; font-variant-numeric: normal; font-variant-east-asian: normal;"><code class="language-javascript hljshas-numbering" style="display: block; background-color: transparent; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace; font-size: 13px; white-space: pre; border-radius: 0px; overflow-x: auto; word-wrap: normal;"><span class="hljs-function" style="box-sizing: border-box;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">function</span> <span class="hljs-title" style="box-sizing: border-box;">draw</span><span class="hljs-params" style="color: rgb(102, 0, 102); box-sizing: border-box;">()</span>{</span>
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">var</span> canvas = document.getElementById(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'tutorial'</span>);
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> (!canvas.getContext) <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span>;
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">var</span> ctx = canvas.getContext(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"2d"</span>);
ctx.beginPath();
ctx.moveTo(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">10</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">200</span>); <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//起始点</span>
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">var</span> cp1x = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">40</span>, cp1y = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">100</span>;<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//控制点</span>
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">var</span> x = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">200</span>, y = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">200</span>; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 结束点</span>
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//绘制二次贝塞尔曲线</span>
ctx.quadraticCurveTo(cp1x, cp1y, x, y);
ctx.stroke();
ctx.beginPath();
ctx.rect(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">10</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">200</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">10</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">10</span>);
ctx.rect(cp1x, cp1y, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">10</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">10</span>);
ctx.rect(x, y, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">10</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">10</span>);
ctx.fill();
}
draw();</code><ul class="pre-numbering" style="box-sizing: border-box; line-height: 23px; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; padding-top: 6px; padding-bottom: 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">1</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">2</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">3</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">4</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">5</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">6</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">7</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">8</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">9</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">10</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">11</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">12</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">13</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">14</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">15</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">16</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">17</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">18</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">19</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">20</li></ul></pre><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;"><img src="http://o7cqr8cfk.bkt.clouddn.com/17-6-4/31012258.jpg" alt="" title="" style="border: none; box-sizing: border-box; margin-top: 15px; margin-bottom: 15px; max-width: 602px; max-height: 100%; height: auto;"></p><h4 id="绘制三次贝塞尔曲线" style="margin-top: 0.8em; margin-bottom: 0.8em; box-sizing: border-box; font-family: "microsoft yahei"; font-weight: 300; line-height: 1.1; color: rgb(63, 63, 63); font-size: 1.25em; font-variant-numeric: normal; font-variant-east-asian: normal;"><a name="t27" target="_blank" style="box-sizing: border-box; color: rgb(12, 137, 207);"></a>绘制三次贝塞尔曲线</h4><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;"><code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">bezierCurveTo(cp1x, cp1y, cp2x, cp2y, x, y)</code>:</p><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;">说明:</p><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;"> 参数1和2:控制点1的坐标</p><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;"> 参数3和4:控制点2的坐标</p><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;"> 参数5和6:结束点的坐标</p><pre class="prettyprint" name="code" style="white-space: nowrap; box-sizing: border-box; position: relative; overflow-y: hidden; overflow-x: auto; margin-bottom: 1.7em; line-height: 23px; font-family: "Source Code Pro", monospace; padding: 5px 5px 5px 60px; font-size: 14px; word-break: break-all; color: rgb(51, 51, 51); background-color: rgba(128, 128, 128, 0.047); border: 0px solid rgb(136, 136, 136); border-radius: 0px; font-variant-numeric: normal; font-variant-east-asian: normal;"><code class="language-javascript hljshas-numbering" style="display: block; background-color: transparent; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace; font-size: 13px; white-space: pre; border-radius: 0px; overflow-x: auto; word-wrap: normal;"><span class="hljs-function" style="box-sizing: border-box;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">function</span> <span class="hljs-title" style="box-sizing: border-box;">draw</span><span class="hljs-params" style="color: rgb(102, 0, 102); box-sizing: border-box;">()</span>{</span>
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">var</span> canvas = document.getElementById(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'tutorial'</span>);
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> (!canvas.getContext) <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span>;
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">var</span> ctx = canvas.getContext(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"2d"</span>);
ctx.beginPath();
ctx.moveTo(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">40</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">200</span>); <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//起始点</span>
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">var</span> cp1x = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">20</span>, cp1y = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">100</span>;<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//控制点1</span>
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">var</span> cp2x = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">100</span>, cp2y = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">120</span>;<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//控制点2</span>
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">var</span> x = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">200</span>, y = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">200</span>; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 结束点</span>
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//绘制二次贝塞尔曲线</span>
ctx.bezierCurveTo(cp1x, cp1y, cp2x, cp2y, x, y);
ctx.stroke();
ctx.beginPath();
ctx.rect(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">40</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">200</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">10</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">10</span>);
ctx.rect(cp1x, cp1y, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">10</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">10</span>);
ctx.rect(cp2x, cp2y, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">10</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">10</span>);
ctx.rect(x, y, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">10</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">10</span>);
ctx.fill();
}
draw();</code><ul class="pre-numbering" style="box-sizing: border-box; line-height: 23px; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; padding-top: 6px; padding-bottom: 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">1</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">2</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">3</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">4</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">5</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">6</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">7</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">8</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">9</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">10</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">11</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">12</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">13</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">14</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">15</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">16</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">17</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">18</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">19</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">20</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">21</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">22</li></ul></pre><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;"><img src="http://o7cqr8cfk.bkt.clouddn.com/17-6-4/18941618.jpg" alt="" title="" style="border: none; box-sizing: border-box; margin-top: 15px; margin-bottom: 15px; max-width: 602px; max-height: 100%; height: auto;"></p> 转自:<a href="http://blog.csdn.net/u012468376/article/details/73350998" style="text-decoration-line: none; font-family: "microsoft yahei"; font-size: 14px; font-variant-numeric: normal; font-variant-east-asian: normal; line-height: 14px; color: rgb(102, 102, 102);">http://blog.csdn.net/u012468376/article/details/73350998</a><p></p><div><h1 id="五添加样式和颜色" style="margin-top: 0.8em; margin-bottom: 0.8em; box-sizing: border-box; font-family: "microsoft yahei"; font-weight: 300; line-height: 1.1; color: rgb(63, 63, 63); font-size: 2.6em; font-variant-numeric: normal; font-variant-east-asian: normal;">五、添加样式和颜色</h1><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;"> 在前面的绘制矩形章节中,只用到了默认的线条和颜色。</p><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;"> 如果想要给图形上色,有两个重要的属性可以做到。</p><ol style="box-sizing: border-box; line-height: 27px; margin-bottom: 1.7em; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;"><li style="box-sizing: border-box;"><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal;"><code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">fillStyle = color</code></p><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal;">设置图形的填充颜色</p></li><li style="box-sizing: border-box;"><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal;"><code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">strokeStyle = color</code></p><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal;">设置图形轮廓的颜色</p></li></ol><blockquote style="box-sizing: border-box; margin-bottom: 1.7em; line-height: 27px; padding: 15px 20px; border-left: 10px solid rgba(128, 128, 128, 0.075); background-color: rgba(128, 128, 128, 0.047); border-radius: 0px 5px 5px 0px; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;"><p style="box-sizing: border-box; line-height: 1.5; font-size: 1em; color: rgb(111, 111, 111); word-break: normal;">备注:</p></blockquote><pre style="white-space: pre-wrap; box-sizing: border-box; margin-bottom: 10.5px; line-height: 1.45; font-family: "Source Code Pro", monospace; padding: 10px; font-size: 14px; word-break: break-all; color: rgb(51, 51, 51); background-color: rgba(128, 128, 128, 0.047); border: 1px solid rgba(128, 128, 128, 0.075); border-radius: 0px; font-variant-numeric: normal; font-variant-east-asian: normal;"><code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; font-size: 13px; color: inherit; background-color: transparent; white-space: pre; border-radius: 0px; overflow-x: auto; word-wrap: normal;">1. `color` 可以是表示 `css` 颜色值的字符串、渐变对象或者图案对象。
2. 默认情况下,线条和填充颜色都是黑色。
3. 一旦您设置了 `strokeStyle` 或者 `fillStyle` 的值,那么这个新值就会成为新绘制的图形的默认值。如果你要给每个图形上不同的颜色,你需要重新设置 `fillStyle` 或 `strokeStyle` 的值。
</code></pre><h2 id="fillstyle" style="margin-top: 0.8em; margin-bottom: 0.8em; box-sizing: border-box; font-family: "microsoft yahei"; font-weight: 300; line-height: 1.1; color: rgb(63, 63, 63); font-size: 2.15em; font-variant-numeric: normal; font-variant-east-asian: normal;"><a name="t29" target="_blank" style="box-sizing: border-box; color: rgb(12, 137, 207);"></a><code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 31px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">fillStyle</code></h2><pre class="prettyprint" name="code" style="white-space: nowrap; box-sizing: border-box; position: relative; overflow-y: hidden; overflow-x: auto; margin-bottom: 1.7em; line-height: 23px; font-family: "Source Code Pro", monospace; padding: 5px 5px 5px 60px; font-size: 14px; word-break: break-all; color: rgb(51, 51, 51); background-color: rgba(128, 128, 128, 0.047); border: 0px solid rgb(136, 136, 136); border-radius: 0px; font-variant-numeric: normal; font-variant-east-asian: normal;"><code class="language-javascript hljshas-numbering" style="display: block; background-color: transparent; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace; font-size: 13px; white-space: pre; border-radius: 0px; overflow-x: auto; word-wrap: normal;"><span class="hljs-function" style="box-sizing: border-box;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">function</span> <span class="hljs-title" style="box-sizing: border-box;">draw</span><span class="hljs-params" style="color: rgb(102, 0, 102); box-sizing: border-box;">()</span>{</span>
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">var</span> canvas = document.getElementById(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'tutorial'</span>);
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> (!canvas.getContext) <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span>;
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">var</span> ctx = canvas.getContext(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"2d"</span>);
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">for</span> (<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">var</span> i = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>; i < <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">6</span>; i++){
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">for</span> (<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">var</span> j = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>; j < <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">6</span>; j++){
ctx.fillStyle = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'rgb('</span> + <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">Math</span>.floor(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">255</span> - <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">42.5</span> * i) + <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">','</span> +
<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">Math</span>.floor(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">255</span> - <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">42.5</span> * j) + <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">',0)'</span>;
ctx.fillRect(j * <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">50</span>, i * <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">50</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">50</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">50</span>);
}
}
}
draw();</code><ul class="pre-numbering" style="box-sizing: border-box; line-height: 23px; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; padding-top: 6px; padding-bottom: 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">1</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">2</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">3</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">4</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">5</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">6</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">7</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">8</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">9</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">10</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">11</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">12</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">13</li></ul></pre><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;"><img src="http://o7cqr8cfk.bkt.clouddn.com/17-6-4/90462466.jpg" alt="" title="" style="border: none; box-sizing: border-box; margin-top: 15px; margin-bottom: 15px; max-width: 602px; max-height: 100%; height: auto;"></p><h2 id="strokestyle" style="margin-top: 0.8em; margin-bottom: 0.8em; box-sizing: border-box; font-family: "microsoft yahei"; font-weight: 300; line-height: 1.1; color: rgb(63, 63, 63); font-size: 2.15em; font-variant-numeric: normal; font-variant-east-asian: normal;"><a name="t30" target="_blank" style="box-sizing: border-box; color: rgb(12, 137, 207);"></a><code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 31px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">strokeStyle</code></h2><pre class="prettyprint" name="code" style="white-space: nowrap; box-sizing: border-box; position: relative; overflow-y: hidden; overflow-x: auto; margin-bottom: 1.7em; line-height: 23px; font-family: "Source Code Pro", monospace; padding: 5px 5px 5px 60px; font-size: 14px; word-break: break-all; color: rgb(51, 51, 51); background-color: rgba(128, 128, 128, 0.047); border: 0px solid rgb(136, 136, 136); border-radius: 0px; font-variant-numeric: normal; font-variant-east-asian: normal;"><code class="language-javascript hljshas-numbering" style="display: block; background-color: transparent; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace; font-size: 13px; white-space: pre; border-radius: 0px; overflow-x: auto; word-wrap: normal;"><script type=<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"text/javascript"</span>>
<span class="hljs-function" style="box-sizing: border-box;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">function</span> <span class="hljs-title" style="box-sizing: border-box;">draw</span><span class="hljs-params" style="color: rgb(102, 0, 102); box-sizing: border-box;">()</span>{</span>
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">var</span> canvas = document.getElementById(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'tutorial'</span>);
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> (!canvas.getContext) <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span>;
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">var</span> ctx = canvas.getContext(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"2d"</span>);
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">for</span> (<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">var</span> i = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>; i < <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">6</span>; i++){
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">for</span> (<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">var</span> j = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>; j < <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">6</span>; j++){
ctx.strokeStyle = `rgb(${randomInt(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">255</span>)},${randomInt(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">255</span>)},${randomInt(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">255</span>)})`;
ctx.strokeRect(j * <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">50</span>, i * <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">50</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">40</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">40</span>);
}
}
}
draw();
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">/**
作者:李振超 4 Jun 2017 12:12
返回随机的 之间的整数(包括from,也包括to)
*/</span>
<span class="hljs-function" style="box-sizing: border-box;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">function</span> <span class="hljs-title" style="box-sizing: border-box;">randomInt</span><span class="hljs-params" style="color: rgb(102, 0, 102); box-sizing: border-box;">(from, to)</span>{</span>
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">parseInt</span>(<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">Math</span>.random() * (to - from + <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>) + from);
}
<<span class="hljs-regexp" style="color: rgb(0, 136, 0); box-sizing: border-box;">/script></span></code><ul class="pre-numbering" style="box-sizing: border-box; line-height: 23px; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; padding-top: 6px; padding-bottom: 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">1</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">2</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">3</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">4</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">5</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">6</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">7</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">8</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">9</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">10</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">11</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">12</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">13</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">14</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">15</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">16</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">17</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">18</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">19</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">20</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">21</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">22</li></ul></pre><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;"><img src="http://o7cqr8cfk.bkt.clouddn.com/17-6-4/39824191.jpg" alt="" title="" style="border: none; box-sizing: border-box; margin-top: 15px; margin-bottom: 15px; max-width: 602px; max-height: 100%; height: auto;"></p><h2 id="transparency透明度" style="margin-top: 0.8em; margin-bottom: 0.8em; box-sizing: border-box; font-family: "microsoft yahei"; font-weight: 300; line-height: 1.1; color: rgb(63, 63, 63); font-size: 2.15em; font-variant-numeric: normal; font-variant-east-asian: normal;"><a name="t31" target="_blank" style="box-sizing: border-box; color: rgb(12, 137, 207);"></a><code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 31px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">Transparency(透明度)</code></h2><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;"><code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">globalAlpha = transparencyValue</code></p><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;"> 这个属性影响到 canvas 里所有图形的透明度,有效的值范围是 0.0 (完全透明)到 1.0(完全不透明),默认是 1.0。</p><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;"><span class="Apple-converted-space"> </span><code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">globalAlpha</code><span class="Apple-converted-space"> </span>属性在需要绘制大量拥有相同透明度的图形时候相当高效。不过,我认为使用<code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">rgba()</code>设置透明度更加好一些。</p><h2 id="line-style" style="margin-top: 0.8em; margin-bottom: 0.8em; box-sizing: border-box; font-family: "microsoft yahei"; font-weight: 300; line-height: 1.1; color: rgb(63, 63, 63); font-size: 2.15em; font-variant-numeric: normal; font-variant-east-asian: normal;"><a name="t32" target="_blank" style="box-sizing: border-box; color: rgb(12, 137, 207);"></a><code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 31px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">line style</code></h2><h3 id="1-linewidth-value" style="margin-top: 0.8em; margin-bottom: 0.8em; box-sizing: border-box; font-family: "microsoft yahei"; font-weight: 300; line-height: 1.1; color: rgb(63, 63, 63); font-size: 1.7em; font-variant-numeric: normal; font-variant-east-asian: normal;"><a name="t33" target="_blank" style="box-sizing: border-box; color: rgb(12, 137, 207);"></a>1.<span class="Apple-converted-space"> </span><code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 24px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">lineWidth = value</code></h3><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;">线宽。只能是正值。默认是<code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">1.0</code>。</p><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;">起始点和终点的连线为中心,<strong style="box-sizing: border-box;">上下各占线宽的一半</strong></p><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;">“`javascript<span class="Apple-converted-space"> </span><br style="box-sizing: border-box;">ctx.beginPath();<span class="Apple-converted-space"> </span><br style="box-sizing: border-box;">ctx.moveTo(10, 10);<span class="Apple-converted-space"> </span><br style="box-sizing: border-box;">ctx.lineTo(100, 10);<span class="Apple-converted-space"> </span><br style="box-sizing: border-box;">ctx.lineWidth = 10;<span class="Apple-converted-space"> </span><br style="box-sizing: border-box;">ctx.stroke();</p><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;">ctx.beginPath();<span class="Apple-converted-space"> </span><br style="box-sizing: border-box;">ctx.moveTo(110, 10);<span class="Apple-converted-space"> </span><br style="box-sizing: border-box;">ctx.lineTo(160, 10)<span class="Apple-converted-space"> </span><br style="box-sizing: border-box;">ctx.lineWidth = 20;<span class="Apple-converted-space"> </span><br style="box-sizing: border-box;">ctx.stroke()<span class="Apple-converted-space"> </span><br style="box-sizing: border-box;">“`</p><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;"><img src="http://o7cqr8cfk.bkt.clouddn.com/17-6-4/29873575.jpg" alt="" title="" style="border: none; box-sizing: border-box; margin-top: 15px; margin-bottom: 15px; max-width: 602px; max-height: 100%; height: auto;"></p><h3 id="2-linecap-type" style="margin-top: 0.8em; margin-bottom: 0.8em; box-sizing: border-box; font-family: "microsoft yahei"; font-weight: 300; line-height: 1.1; color: rgb(63, 63, 63); font-size: 1.7em; font-variant-numeric: normal; font-variant-east-asian: normal;"><a name="t34" target="_blank" style="box-sizing: border-box; color: rgb(12, 137, 207);"></a>2.<span class="Apple-converted-space"> </span><code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 24px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">lineCap = type</code></h3><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;">线条末端样式。</p><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;">共有3个值:</p><ol style="box-sizing: border-box; line-height: 27px; margin-bottom: 1.7em; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;"><li style="box-sizing: border-box;"><code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">butt</code>:线段末端以方形结束</li><li style="box-sizing: border-box;"><code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">round</code>:线段末端以圆形结束</li><li style="box-sizing: border-box;"><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal;"><code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">square</code>:线段末端以方形结束,但是增加了一个宽度和线段相同,高度是线段厚度一半的矩形区域。</p><pre class="prettyprint" name="code" style="white-space: nowrap; box-sizing: border-box; position: relative; overflow-y: hidden; overflow-x: auto; margin-bottom: 1.7em; line-height: 23px; font-family: "Source Code Pro", monospace; padding: 5px 5px 5px 60px; font-size: 14px; word-break: break-all; color: rgb(51, 51, 51); background-color: rgba(128, 128, 128, 0.047); border: 0px solid rgb(136, 136, 136); border-radius: 0px;"><code class="language-javascript hljshas-numbering" style="display: block; background-color: transparent; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace; font-size: 13px; white-space: pre; border-radius: 0px; overflow-x: auto; word-wrap: normal;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">var</span> lineCaps = [<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"butt"</span>, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"round"</span>, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"square"</span>];
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">for</span> (<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">var</span> i = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>; i < <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3</span>; i++){
ctx.beginPath();
ctx.moveTo(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">20</span> + <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">30</span> * i, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">30</span>);
ctx.lineTo(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">20</span> + <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">30</span> * i, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">100</span>);
ctx.lineWidth = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">20</span>;
ctx.lineCap = lineCaps;
ctx.stroke();
}
ctx.beginPath();
ctx.moveTo(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">30</span>);
ctx.lineTo(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">300</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">30</span>);
ctx.moveTo(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">100</span>);
ctx.lineTo(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">300</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">100</span>)
ctx.strokeStyle = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"red"</span>;
ctx.lineWidth = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>;
ctx.stroke();</code><ul class="pre-numbering" style="box-sizing: border-box; line-height: 23px; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; padding-top: 6px; padding-bottom: 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">1</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">2</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">3</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">4</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">5</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">6</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">7</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">8</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">9</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">10</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">11</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">12</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">13</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">14</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">15</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">16</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">17</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">18</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">19</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">20</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">21</li></ul></pre><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal;"><img src="http://o7cqr8cfk.bkt.clouddn.com/17-6-4/41486892.jpg" alt="" title="" style="border: none; box-sizing: border-box; margin-top: 15px; margin-bottom: 15px; max-width: 602px; max-height: 100%; height: auto;"></p></li></ol><h3 id="3-linejoin-type" style="margin-top: 0.8em; margin-bottom: 0.8em; box-sizing: border-box; font-family: "microsoft yahei"; font-weight: 300; line-height: 1.1; color: rgb(63, 63, 63); font-size: 1.7em; font-variant-numeric: normal; font-variant-east-asian: normal;"><a name="t35" target="_blank" style="box-sizing: border-box; color: rgb(12, 137, 207);"></a>3.<span class="Apple-converted-space"> </span><code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 24px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">lineJoin = type</code></h3><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;">同一个path内,设定线条与线条间接合处的样式。</p><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;">共有3个值<code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">round</code>,<span class="Apple-converted-space"> </span><code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">bevel</code><span class="Apple-converted-space"> </span>和<span class="Apple-converted-space"> </span><code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">miter</code>:</p><ol style="box-sizing: border-box; line-height: 27px; margin-bottom: 1.7em; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;"><li style="box-sizing: border-box;"><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal;"><code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">round</code></p><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal;">通过填充一个额外的,圆心在相连部分末端的扇形,绘制拐角的形状。 圆角的半径是线段的宽度。</p></li><li style="box-sizing: border-box;"><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal;"><code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">bevel</code></p><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal;">在相连部分的末端填充一个额外的以三角形为底的区域, 每个部分都有各自独立的矩形拐角。</p></li><li style="box-sizing: border-box;"><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal;"><code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">miter</code>(默认)</p><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal;">通过延伸相连部分的外边缘,使其相交于一点,形成一个额外的菱形区域。</p></li></ol><pre class="prettyprint" name="code" style="white-space: nowrap; box-sizing: border-box; position: relative; overflow-y: hidden; overflow-x: auto; margin-bottom: 1.7em; line-height: 23px; font-family: "Source Code Pro", monospace; padding: 5px 5px 5px 60px; font-size: 14px; word-break: break-all; color: rgb(51, 51, 51); background-color: rgba(128, 128, 128, 0.047); border: 0px solid rgb(136, 136, 136); border-radius: 0px; font-variant-numeric: normal; font-variant-east-asian: normal;"><code class="language-javascript hljshas-numbering" style="display: block; background-color: transparent; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace; font-size: 13px; white-space: pre; border-radius: 0px; overflow-x: auto; word-wrap: normal;"><span class="hljs-function" style="box-sizing: border-box;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">function</span> <span class="hljs-title" style="box-sizing: border-box;">draw</span><span class="hljs-params" style="color: rgb(102, 0, 102); box-sizing: border-box;">()</span>{</span>
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">var</span> canvas = document.getElementById(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'tutorial'</span>);
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> (!canvas.getContext) <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span>;
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">var</span> ctx = canvas.getContext(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"2d"</span>);
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">var</span> lineJoin = [<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'round'</span>, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'bevel'</span>, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'miter'</span>];
ctx.lineWidth = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">20</span>;
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">for</span> (<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">var</span> i = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>; i < lineJoin.length; i++){
ctx.lineJoin = lineJoin;
ctx.beginPath();
ctx.moveTo(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">50</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">50</span> + i * <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">50</span>);
ctx.lineTo(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">100</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">100</span> + i * <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">50</span>);
ctx.lineTo(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">150</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">50</span> + i * <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">50</span>);
ctx.lineTo(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">200</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">100</span> + i * <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">50</span>);
ctx.lineTo(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">250</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">50</span> + i * <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">50</span>);
ctx.stroke();
}
}
draw();</code><ul class="pre-numbering" style="box-sizing: border-box; line-height: 23px; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; padding-top: 6px; padding-bottom: 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">1</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">2</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">3</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">4</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">5</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">6</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">7</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">8</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">9</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">10</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">11</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">12</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">13</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">14</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">15</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">16</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">17</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">18</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">19</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">20</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">21</li></ul></pre><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;"><img src="http://o7cqr8cfk.bkt.clouddn.com/17-6-5/5058353.jpg" alt="" title="" style="border: none; box-sizing: border-box; margin-top: 15px; margin-bottom: 15px; max-width: 602px; max-height: 100%; height: auto;"></p><h3 id="4-虚线" style="margin-top: 0.8em; margin-bottom: 0.8em; box-sizing: border-box; font-family: "microsoft yahei"; font-weight: 300; line-height: 1.1; color: rgb(63, 63, 63); font-size: 1.7em; font-variant-numeric: normal; font-variant-east-asian: normal;"><a name="t36" target="_blank" style="box-sizing: border-box; color: rgb(12, 137, 207);"></a>4. 虚线</h3><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;">用<span class="Apple-converted-space"> </span><code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">setLineDash</code><span class="Apple-converted-space"> </span>方法和<span class="Apple-converted-space"> </span><code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">lineDashOffset</code><span class="Apple-converted-space"> </span>属性来制定虚线样式.<span class="Apple-converted-space"> </span><code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">setLineDash</code><span class="Apple-converted-space"> </span>方法接受一个数组,来指定线段与间隙的交替;<code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">lineDashOffset</code>属性设置起始偏移量.</p><pre class="prettyprint" name="code" style="white-space: nowrap; box-sizing: border-box; position: relative; overflow-y: hidden; overflow-x: auto; margin-bottom: 1.7em; line-height: 23px; font-family: "Source Code Pro", monospace; padding: 5px 5px 5px 60px; font-size: 14px; word-break: break-all; color: rgb(51, 51, 51); background-color: rgba(128, 128, 128, 0.047); border: 0px solid rgb(136, 136, 136); border-radius: 0px; font-variant-numeric: normal; font-variant-east-asian: normal;"><code class="language-javascript hljshas-numbering" style="display: block; background-color: transparent; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace; font-size: 13px; white-space: pre; border-radius: 0px; overflow-x: auto; word-wrap: normal;"><span class="hljs-function" style="box-sizing: border-box;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">function</span> <span class="hljs-title" style="box-sizing: border-box;">draw</span><span class="hljs-params" style="color: rgb(102, 0, 102); box-sizing: border-box;">()</span>{</span>
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">var</span> canvas = document.getElementById(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'tutorial'</span>);
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> (!canvas.getContext) <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span>;
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">var</span> ctx = canvas.getContext(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"2d"</span>);
ctx.setLineDash([<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">20</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">5</span>]);<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// [实线长度, 间隙长度]</span>
ctx.lineDashOffset = -<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>;
ctx.strokeRect(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">50</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">50</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">210</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">210</span>);
}
draw();</code><ul class="pre-numbering" style="box-sizing: border-box; line-height: 23px; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; padding-top: 6px; padding-bottom: 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">1</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">2</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">3</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">4</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">5</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">6</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">7</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">8</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">9</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">10</li></ul></pre><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;"><img src="http://o7cqr8cfk.bkt.clouddn.com/17-6-5/92471191.jpg" alt="" title="" style="border: none; box-sizing: border-box; margin-top: 15px; margin-bottom: 15px; max-width: 602px; max-height: 100%; height: auto;"></p><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;">备注:</p><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;"><span class="Apple-converted-space"> </span><code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">getLineDash()</code>:返回一个包含当前虚线样式,长度为非负偶数的数组。</p><h1 id="六绘制文本" style="margin-top: 0.8em; margin-bottom: 0.8em; box-sizing: border-box; font-family: "microsoft yahei"; font-weight: 300; line-height: 1.1; color: rgb(63, 63, 63); font-size: 2.6em; font-variant-numeric: normal; font-variant-east-asian: normal;"><a name="t37" target="_blank" style="box-sizing: border-box; color: rgb(12, 137, 207);"></a>六、绘制文本</h1><h2 id="绘制文本的两个方法" style="margin-top: 0.8em; margin-bottom: 0.8em; box-sizing: border-box; font-family: "microsoft yahei"; font-weight: 300; line-height: 1.1; color: rgb(63, 63, 63); font-size: 2.15em; font-variant-numeric: normal; font-variant-east-asian: normal;"><a name="t38" target="_blank" style="box-sizing: border-box; color: rgb(12, 137, 207);"></a>绘制文本的两个方法</h2><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;">canvas 提供了两种方法来渲染文本:</p><ol style="box-sizing: border-box; line-height: 27px; margin-bottom: 1.7em; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;"><li style="box-sizing: border-box;"><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal;"><code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">fillText(text, x, y [, maxWidth])</code></p><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal;">在指定的(x,y)位置填充指定的文本,绘制的最大宽度是可选的.</p></li><li style="box-sizing: border-box;"><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal;"><code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">strokeText(text, x, y [, maxWidth])</code></p><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal;">在指定的(x,y)位置绘制文本边框,绘制的最大宽度是可选的.</p></li></ol><pre class="prettyprint" name="code" style="white-space: nowrap; box-sizing: border-box; position: relative; overflow-y: hidden; overflow-x: auto; margin-bottom: 1.7em; line-height: 23px; font-family: "Source Code Pro", monospace; padding: 5px 5px 5px 60px; font-size: 14px; word-break: break-all; color: rgb(51, 51, 51); background-color: rgba(128, 128, 128, 0.047); border: 0px solid rgb(136, 136, 136); border-radius: 0px; font-variant-numeric: normal; font-variant-east-asian: normal;"><code class="language-javascript hljshas-numbering" style="display: block; background-color: transparent; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace; font-size: 13px; white-space: pre; border-radius: 0px; overflow-x: auto; word-wrap: normal;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">var</span> ctx;
<span class="hljs-function" style="box-sizing: border-box;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">function</span> <span class="hljs-title" style="box-sizing: border-box;">draw</span><span class="hljs-params" style="color: rgb(102, 0, 102); box-sizing: border-box;">()</span>{</span>
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">var</span> canvas = document.getElementById(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'tutorial'</span>);
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> (!canvas.getContext) <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span>;
ctx = canvas.getContext(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"2d"</span>);
ctx.font = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"100px sans-serif"</span>
ctx.fillText(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"天若有情"</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">10</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">100</span>);
ctx.strokeText(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"天若有情"</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">10</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">200</span>)
}
draw();</code><ul class="pre-numbering" style="box-sizing: border-box; line-height: 23px; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; padding-top: 6px; padding-bottom: 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">1</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">2</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">3</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">4</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">5</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">6</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">7</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">8</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">9</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">10</li></ul></pre><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;"><img src="http://o7cqr8cfk.bkt.clouddn.com/17-6-6/87968030.jpg" alt="" title="" style="border: none; box-sizing: border-box; margin-top: 15px; margin-bottom: 15px; max-width: 602px; max-height: 100%; height: auto;"></p><h2 id="给文本添加样式" style="margin-top: 0.8em; margin-bottom: 0.8em; box-sizing: border-box; font-family: "microsoft yahei"; font-weight: 300; line-height: 1.1; color: rgb(63, 63, 63); font-size: 2.15em; font-variant-numeric: normal; font-variant-east-asian: normal;"><a name="t39" target="_blank" style="box-sizing: border-box; color: rgb(12, 137, 207);"></a>给文本添加样式</h2><ol style="box-sizing: border-box; line-height: 27px; margin-bottom: 1.7em; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;"><li style="box-sizing: border-box;"><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal;"><code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">font = value</code></p><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal;">当前我们用来绘制文本的样式。这个字符串使用和<span class="Apple-converted-space"> </span><code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">CSS font</code>属性相同的语法. 默认的字体是<span class="Apple-converted-space"> </span><code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">10px sans-serif</code>。</p></li><li style="box-sizing: border-box;"><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal;"><code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">textAlign = value</code></p><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal;">文本对齐选项. 可选的值包括:<code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">start</code>,<span class="Apple-converted-space"> </span><code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">end</code>,<span class="Apple-converted-space"> </span><code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">left</code>,<span class="Apple-converted-space"> </span><code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">right</code><span class="Apple-converted-space"> </span>or<span class="Apple-converted-space"> </span><code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">center</code>. 默认值是<span class="Apple-converted-space"> </span><code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">start</code>。</p></li><li style="box-sizing: border-box;"><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal;"><code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">textBaseline = value</code></p><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal;">基线对齐选项,可选的值包括:<code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">top</code>,<span class="Apple-converted-space"> </span><code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">hanging</code>,<span class="Apple-converted-space"> </span><code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">middle</code>,<span class="Apple-converted-space"> </span><code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">alphabetic</code>,<span class="Apple-converted-space"> </span><code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">ideographic</code>,<span class="Apple-converted-space"> </span><code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">bottom</code>。默认值是<span class="Apple-converted-space"> </span><code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">alphabetic。</code></p></li><li style="box-sizing: border-box;"><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal;"><code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">direction = value</code></p><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal;">文本方向。可能的值包括:<code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">ltr</code>,<span class="Apple-converted-space"> </span><code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">rtl</code>,<span class="Apple-converted-space"> </span><code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">inherit</code>。默认值是<span class="Apple-converted-space"> </span><code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">inherit。</code></p></li></ol><h1 id="七绘制图片" style="margin-top: 0.8em; margin-bottom: 0.8em; box-sizing: border-box; font-family: "microsoft yahei"; font-weight: 300; line-height: 1.1; color: rgb(63, 63, 63); font-size: 2.6em; font-variant-numeric: normal; font-variant-east-asian: normal;"><a name="t40" target="_blank" style="box-sizing: border-box; color: rgb(12, 137, 207);"></a>七、绘制图片</h1><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;"> 我们也可以在<code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">canvas</code>上直接绘制图片。</p><h2 id="71-由零开始创建图片" style="margin-top: 0.8em; margin-bottom: 0.8em; box-sizing: border-box; font-family: "microsoft yahei"; font-weight: 300; line-height: 1.1; color: rgb(63, 63, 63); font-size: 2.15em; font-variant-numeric: normal; font-variant-east-asian: normal;"><a name="t41" target="_blank" style="box-sizing: border-box; color: rgb(12, 137, 207);"></a>7.1 由零开始创建图片</h2><h3 id="创建img元素" style="margin-top: 0.8em; margin-bottom: 0.8em; box-sizing: border-box; font-family: "microsoft yahei"; font-weight: 300; line-height: 1.1; color: rgb(63, 63, 63); font-size: 1.7em; font-variant-numeric: normal; font-variant-east-asian: normal;"><a name="t42" target="_blank" style="box-sizing: border-box; color: rgb(12, 137, 207);"></a>创建<code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 24px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;"><img></code>元素</h3><pre class="prettyprint" name="code" style="white-space: nowrap; box-sizing: border-box; position: relative; overflow-y: hidden; overflow-x: auto; margin-bottom: 1.7em; line-height: 23px; font-family: "Source Code Pro", monospace; padding: 5px 5px 5px 60px; font-size: 14px; word-break: break-all; color: rgb(51, 51, 51); background-color: rgba(128, 128, 128, 0.047); border: 0px solid rgb(136, 136, 136); border-radius: 0px; font-variant-numeric: normal; font-variant-east-asian: normal;"><code class="language-javascript hljshas-numbering" style="display: block; background-color: transparent; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace; font-size: 13px; white-space: pre; border-radius: 0px; overflow-x: auto; word-wrap: normal;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">var</span> img = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> Image(); <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 创建一个<img>元素</span>
img.src = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'myImage.png'</span>; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 设置图片源地址</span></code><ul class="pre-numbering" style="box-sizing: border-box; line-height: 23px; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; padding-top: 6px; padding-bottom: 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">1</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">2</li></ul></pre><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;">脚本执行后图片开始装载</p><h3 id="绘制img" style="margin-top: 0.8em; margin-bottom: 0.8em; box-sizing: border-box; font-family: "microsoft yahei"; font-weight: 300; line-height: 1.1; color: rgb(63, 63, 63); font-size: 1.7em; font-variant-numeric: normal; font-variant-east-asian: normal;"><a name="t43" target="_blank" style="box-sizing: border-box; color: rgb(12, 137, 207);"></a>绘制<code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 24px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">img</code></h3><pre class="prettyprint" name="code" style="white-space: nowrap; box-sizing: border-box; position: relative; overflow-y: hidden; overflow-x: auto; margin-bottom: 1.7em; line-height: 23px; font-family: "Source Code Pro", monospace; padding: 5px 5px 5px 60px; font-size: 14px; word-break: break-all; color: rgb(51, 51, 51); background-color: rgba(128, 128, 128, 0.047); border: 0px solid rgb(136, 136, 136); border-radius: 0px; font-variant-numeric: normal; font-variant-east-asian: normal;"><code class="language-javascript hljshas-numbering" style="display: block; background-color: transparent; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace; font-size: 13px; white-space: pre; border-radius: 0px; overflow-x: auto; word-wrap: normal;"><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//参数1:要绘制的img参数2、3:绘制的img在canvas中的坐标</span>
ctx.drawImage(img,<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>,<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>); </code><ul class="pre-numbering" style="box-sizing: border-box; line-height: 23px; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; padding-top: 6px; padding-bottom: 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">1</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">2</li></ul></pre><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;">注意:</p><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;"> 考虑到图片是从网络加载,如果<span class="Apple-converted-space"> </span><code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">drawImage</code><span class="Apple-converted-space"> </span>的时候图片还没有完全加载完成,则什么都不做,个别浏览器会抛异常。所以我们应该保证在<span class="Apple-converted-space"> </span><code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">img</code><span class="Apple-converted-space"> </span>绘制完成之后再<span class="Apple-converted-space"> </span><code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">drawImage</code>。</p><pre class="prettyprint" name="code" style="white-space: nowrap; box-sizing: border-box; position: relative; overflow-y: hidden; overflow-x: auto; margin-bottom: 1.7em; line-height: 23px; font-family: "Source Code Pro", monospace; padding: 5px 5px 5px 60px; font-size: 14px; word-break: break-all; color: rgb(51, 51, 51); background-color: rgba(128, 128, 128, 0.047); border: 0px solid rgb(136, 136, 136); border-radius: 0px; font-variant-numeric: normal; font-variant-east-asian: normal;"><code class="language-javascript hljshas-numbering" style="display: block; background-color: transparent; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace; font-size: 13px; white-space: pre; border-radius: 0px; overflow-x: auto; word-wrap: normal;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">var</span> img = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> Image(); <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 创建img元素</span>
img.onload = <span class="hljs-function" style="box-sizing: border-box;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">function</span><span class="hljs-params" style="color: rgb(102, 0, 102); box-sizing: border-box;">()</span>{</span>
ctx.drawImage(img, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>)
}
img.src = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'myImage.png'</span>; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 设置图片源地址</span></code><ul class="pre-numbering" style="box-sizing: border-box; line-height: 23px; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; padding-top: 6px; padding-bottom: 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">1</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">2</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">3</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">4</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">5</li></ul></pre><h2 id="72-绘制-img-标签元素中的图片" style="margin-top: 0.8em; margin-bottom: 0.8em; box-sizing: border-box; font-family: "microsoft yahei"; font-weight: 300; line-height: 1.1; color: rgb(63, 63, 63); font-size: 2.15em; font-variant-numeric: normal; font-variant-east-asian: normal;"><a name="t44" target="_blank" style="box-sizing: border-box; color: rgb(12, 137, 207);"></a>7.2 绘制<span class="Apple-converted-space"> </span><code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 31px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">img</code><span class="Apple-converted-space"> </span>标签元素中的图片</h2><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;"><span class="Apple-converted-space"> </span><code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">img</code><span class="Apple-converted-space"> </span>可以<span class="Apple-converted-space"> </span><code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">new</code><span class="Apple-converted-space"> </span>也可以来源于我们页面的<span class="Apple-converted-space"> </span><code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;"><img></code>标签</p><pre class="prettyprint" name="code" style="white-space: nowrap; box-sizing: border-box; position: relative; overflow-y: hidden; overflow-x: auto; margin-bottom: 1.7em; line-height: 23px; font-family: "Source Code Pro", monospace; padding: 5px 5px 5px 60px; font-size: 14px; word-break: break-all; color: rgb(51, 51, 51); background-color: rgba(128, 128, 128, 0.047); border: 0px solid rgb(136, 136, 136); border-radius: 0px; font-variant-numeric: normal; font-variant-east-asian: normal;"><code class="language-javascript hljshas-numbering" style="display: block; background-color: transparent; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace; font-size: 13px; white-space: pre; border-radius: 0px; overflow-x: auto; word-wrap: normal;"><img src=<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"./美女.jpg"</span> alt=<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">""</span> width=<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"300"</span>><span class="xml" style="box-sizing: border-box;"><span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box;"><<span class="hljs-title" style="box-sizing: border-box; color: rgb(0, 0, 136);">br</span>></span>
<span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box;"><<span class="hljs-title" style="box-sizing: border-box; color: rgb(0, 0, 136);">canvas</span> <span class="hljs-attribute" style="box-sizing: border-box; color: rgb(102, 0, 102);">id</span>=<span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 136, 0);">"tutorial"</span> <span class="hljs-attribute" style="box-sizing: border-box; color: rgb(102, 0, 102);">width</span>=<span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 136, 0);">"600"</span> <span class="hljs-attribute" style="box-sizing: border-box; color: rgb(102, 0, 102);">height</span>=<span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 136, 0);">"400"</span>></span><span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box;"></<span class="hljs-title" style="box-sizing: border-box; color: rgb(0, 0, 136);">canvas</span>></span>
<span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box;"><<span class="hljs-title" style="box-sizing: border-box; color: rgb(0, 0, 136);">script</span> <span class="hljs-attribute" style="box-sizing: border-box; color: rgb(102, 0, 102);">type</span>=<span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 136, 0);">"text/javascript"</span>></span><span class="javascript" style="box-sizing: border-box;">
<span class="hljs-function" style="box-sizing: border-box;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">function</span> <span class="hljs-title" style="box-sizing: border-box;">draw</span><span class="hljs-params" style="color: rgb(102, 0, 102); box-sizing: border-box;">()</span>{</span>
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">var</span> canvas = document.getElementById(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'tutorial'</span>);
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> (!canvas.getContext) <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span>;
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">var</span> ctx = canvas.getContext(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"2d"</span>);
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">var</span> img = document.querySelector(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"img"</span>);
ctx.drawImage(img, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>);
}
document.querySelector(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"img"</span>).onclick = <span class="hljs-function" style="box-sizing: border-box;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">function</span> <span class="hljs-params" style="color: rgb(102, 0, 102); box-sizing: border-box;">()</span>{</span>
draw();
}
</span><span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box;"></<span class="hljs-title" style="box-sizing: border-box; color: rgb(0, 0, 136);">script</span>></span></span></code><ul class="pre-numbering" style="box-sizing: border-box; line-height: 23px; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; padding-top: 6px; padding-bottom: 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">1</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">2</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">3</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">4</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">5</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">6</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">7</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">8</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">9</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">10</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">11</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">12</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">13</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">14</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">15</li></ul></pre><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;">第一张图片就是页面中的<code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;"><img></code>标签</p><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;"><img src="http://o7cqr8cfk.bkt.clouddn.com/17-6-6/84133001.jpg" alt="" title="" style="border: none; box-sizing: border-box; margin-top: 15px; margin-bottom: 15px; max-width: 602px; max-height: 100%; height: auto;"></p><h2 id="73-缩放图片" style="margin-top: 0.8em; margin-bottom: 0.8em; box-sizing: border-box; font-family: "microsoft yahei"; font-weight: 300; line-height: 1.1; color: rgb(63, 63, 63); font-size: 2.15em; font-variant-numeric: normal; font-variant-east-asian: normal;"><a name="t45" target="_blank" style="box-sizing: border-box; color: rgb(12, 137, 207);"></a>7.3 缩放图片</h2><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;"><code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">drawImage()</code><span class="Apple-converted-space"> </span>也可以再添加两个参数:</p><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;"><span class="Apple-converted-space"> </span><code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">drawImage(image, x, y, width, height)</code></p><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;"> 这个方法多了2个参数:<code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">width</code><span class="Apple-converted-space"> </span>和<span class="Apple-converted-space"> </span><code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">height,</code>这两个参数用来控制 当像canvas画入时应该缩放的大小。</p><pre class="prettyprint" name="code" style="white-space: nowrap; box-sizing: border-box; position: relative; overflow-y: hidden; overflow-x: auto; margin-bottom: 1.7em; line-height: 23px; font-family: "Source Code Pro", monospace; padding: 5px 5px 5px 60px; font-size: 14px; word-break: break-all; color: rgb(51, 51, 51); background-color: rgba(128, 128, 128, 0.047); border: 0px solid rgb(136, 136, 136); border-radius: 0px; font-variant-numeric: normal; font-variant-east-asian: normal;"><code class="language-javascript hljshas-numbering" style="display: block; background-color: transparent; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace; font-size: 13px; white-space: pre; border-radius: 0px; overflow-x: auto; word-wrap: normal;">ctx.drawImage(img, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">400</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">200</span>)</code><ul class="pre-numbering" style="box-sizing: border-box; line-height: 23px; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; padding-top: 6px; padding-bottom: 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">1</li></ul></pre><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;"><img src="http://o7cqr8cfk.bkt.clouddn.com/17-6-9/14522939.jpg" alt="" title="" style="border: none; box-sizing: border-box; margin-top: 15px; margin-bottom: 15px; max-width: 602px; max-height: 100%; height: auto;"></p><h2 id="74-切片slice" style="margin-top: 0.8em; margin-bottom: 0.8em; box-sizing: border-box; font-family: "microsoft yahei"; font-weight: 300; line-height: 1.1; color: rgb(63, 63, 63); font-size: 2.15em; font-variant-numeric: normal; font-variant-east-asian: normal;"><a name="t46" target="_blank" style="box-sizing: border-box; color: rgb(12, 137, 207);"></a>7.4 切片(<code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 31px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">slice</code>)</h2><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;"><code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">drawImage(image, sx, sy, sWidth, sHeight, dx, dy, dWidth, dHeight)</code></p><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;"> 第一个参数和其它的是相同的,都是一个图像或者另一个 canvas 的引用。</p><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;">其他8个参数:</p><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;"> 前4个是定义图像源的切片位置和大小,</p><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;"> 后4个则是定义切片的目标显示位置和大小。</p><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;"><img src="http://o7cqr8cfk.bkt.clouddn.com/17-6-9/86448348.jpg" alt="" title="" style="border: none; box-sizing: border-box; margin-top: 15px; margin-bottom: 15px; max-width: 602px; max-height: 100%; height: auto;"></p><h1 id="八状态的保存和恢复" style="margin-top: 0.8em; margin-bottom: 0.8em; box-sizing: border-box; font-family: "microsoft yahei"; font-weight: 300; line-height: 1.1; color: rgb(63, 63, 63); font-size: 2.6em; font-variant-numeric: normal; font-variant-east-asian: normal;"><a name="t47" target="_blank" style="box-sizing: border-box; color: rgb(12, 137, 207);"></a>八、状态的保存和恢复</h1><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;"><code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">Saving and restoring state</code>是绘制复杂图形时必不可少的操作。</p><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;"><code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">save()和restore()</code></p><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;"><span class="Apple-converted-space"> </span><code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">save</code><span class="Apple-converted-space"> </span>和<span class="Apple-converted-space"> </span><code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">restore</code><span class="Apple-converted-space"> </span>方法是用来保存和恢复<span class="Apple-converted-space"> </span><code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">canvas</code><span class="Apple-converted-space"> </span>状态的,都没有参数。</p><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;"><span class="Apple-converted-space"> </span><code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">Canvas</code><span class="Apple-converted-space"> </span>的状态就是当前画面应用的所有样式和变形的一个快照。</p><ol style="box-sizing: border-box; line-height: 27px; margin-bottom: 1.7em; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;"><li style="box-sizing: border-box;"><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal;">关于<span class="Apple-converted-space"> </span><code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">save()</code></p><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal;">Canvas状态存储在栈中,每当<code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">save()</code>方法被调用后,当前的状态就被推送到栈中保存。一个绘画状态包括:</p><ul style="box-sizing: border-box; line-height: 27px; margin-bottom: 1.7em;"><li style="box-sizing: border-box;"><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal;">当前应用的变形(即移动,旋转和缩放)</p></li><li style="box-sizing: border-box;"><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal;"><code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">strokeStyle</code>,<span class="Apple-converted-space"> </span><code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">fillStyle</code>,<span class="Apple-converted-space"> </span><code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">globalAlpha</code>,<span class="Apple-converted-space"> </span><code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">lineWidth</code>,<span class="Apple-converted-space"> </span><code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">lineCap</code>,<span class="Apple-converted-space"> </span><code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">lineJoin</code>,<span class="Apple-converted-space"> </span><code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">miterLimit</code>,<span class="Apple-converted-space"> </span><code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">shadowOffsetX</code>,<span class="Apple-converted-space"> </span><code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">shadowOffsetY</code>,<code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">shadowBlur</code>,<span class="Apple-converted-space"> </span><code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">shadowColor</code>,<span class="Apple-converted-space"> </span><code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">globalCompositeOperation 的值</code></p></li><li style="box-sizing: border-box;"><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal;">当前的裁切路径(<code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">clipping path</code>)</p></li></ul><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal;"></p><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal;"><strong style="box-sizing: border-box;">可以调用任意多次<span class="Apple-converted-space"> </span><code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">save</code>方法。</strong>(类似数组的<code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">push()</code>)</p></li><li style="box-sizing: border-box;"><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal;">关于<code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">restore()</code></p><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal;">每一次调用<span class="Apple-converted-space"> </span><code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">restore</code><span class="Apple-converted-space"> </span>方法,上一个保存的状态就从栈中弹出,所有设定都恢复。(类似数组的<code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">pop()</code>)</p></li></ol><pre class="prettyprint" name="code" style="white-space: nowrap; box-sizing: border-box; position: relative; overflow-y: hidden; overflow-x: auto; margin-bottom: 1.7em; line-height: 23px; font-family: "Source Code Pro", monospace; padding: 5px 5px 5px 60px; font-size: 14px; word-break: break-all; color: rgb(51, 51, 51); background-color: rgba(128, 128, 128, 0.047); border: 0px solid rgb(136, 136, 136); border-radius: 0px; font-variant-numeric: normal; font-variant-east-asian: normal;"><code class="language-javascript hljshas-numbering" style="display: block; background-color: transparent; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace; font-size: 13px; white-space: pre; border-radius: 0px; overflow-x: auto; word-wrap: normal;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">var</span> ctx;
<span class="hljs-function" style="box-sizing: border-box;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">function</span> <span class="hljs-title" style="box-sizing: border-box;">draw</span><span class="hljs-params" style="color: rgb(102, 0, 102); box-sizing: border-box;">()</span>{</span>
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">var</span> canvas = document.getElementById(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'tutorial'</span>);
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> (!canvas.getContext) <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span>;
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">var</span> ctx = canvas.getContext(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"2d"</span>);
ctx.fillRect(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">150</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">150</span>); <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 使用默认设置绘制一个矩形</span>
ctx.save(); <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 保存默认状态</span>
ctx.fillStyle = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'red'</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 在原有配置基础上对颜色做改变</span>
ctx.fillRect(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">15</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">15</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">120</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">120</span>); <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 使用新的设置绘制一个矩形</span>
ctx.save(); <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 保存当前状态</span>
ctx.fillStyle = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'#FFF'</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 再次改变颜色配置</span>
ctx.fillRect(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">30</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">30</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">90</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">90</span>); <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 使用新的配置绘制一个矩形</span>
ctx.restore(); <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 重新加载之前的颜色状态</span>
ctx.fillRect(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">45</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">45</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">60</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">60</span>); <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 使用上一次的配置绘制一个矩形</span>
ctx.restore(); <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 加载默认颜色配置</span>
ctx.fillRect(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">60</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">60</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">30</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">30</span>); <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 使用加载的配置绘制一个矩形</span>
}
draw();</code><ul class="pre-numbering" style="box-sizing: border-box; line-height: 23px; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; padding-top: 6px; padding-bottom: 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">1</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">2</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">3</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">4</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">5</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">6</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">7</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">8</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">9</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">10</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">11</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">12</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">13</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">14</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">15</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">16</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">17</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">18</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">19</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">20</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">21</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">22</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">23</li></ul></pre><h1 id="九变形" style="margin-top: 0.8em; margin-bottom: 0.8em; box-sizing: border-box; font-family: "microsoft yahei"; font-weight: 300; line-height: 1.1; color: rgb(63, 63, 63); font-size: 2.6em; font-variant-numeric: normal; font-variant-east-asian: normal;"><a name="t48" target="_blank" style="box-sizing: border-box; color: rgb(12, 137, 207);"></a>九、变形</h1><h2 id="91-translate" style="margin-top: 0.8em; margin-bottom: 0.8em; box-sizing: border-box; font-family: "microsoft yahei"; font-weight: 300; line-height: 1.1; color: rgb(63, 63, 63); font-size: 2.15em; font-variant-numeric: normal; font-variant-east-asian: normal;"><a name="t49" target="_blank" style="box-sizing: border-box; color: rgb(12, 137, 207);"></a>9.1 translate</h2><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;"><code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">translate(x, y)</code></p><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;"> 用来移动<span class="Apple-converted-space"> </span><code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">canvas</code><span class="Apple-converted-space"> </span>的<strong style="box-sizing: border-box;">原点</strong>到指定的位置</p><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;"><span class="Apple-converted-space"> </span><code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">translate</code>方法接受两个参数。<code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">x</code><span class="Apple-converted-space"> </span>是左右偏移量,<code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">y</code><span class="Apple-converted-space"> </span>是上下偏移量,如右图所示。</p><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;">在做变形之前先保存状态是一个良好的习惯。大多数情况下,调用<span class="Apple-converted-space"> </span><code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">restore</code><span class="Apple-converted-space"> </span>方法比手动恢复原先的状态要简单得多。又如果你是在一个循环中做位移但没有保存和恢复<code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">canvas</code><span class="Apple-converted-space"> </span>的状态,很可能到最后会发现怎么有些东西不见了,那是因为它很可能已经超出<span class="Apple-converted-space"> </span><code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">canvas</code><span class="Apple-converted-space"> </span>范围以外了。</p><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;"> 注意:<code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">translate</code>移动的是<code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">canvas</code>的坐标原点。(坐标变换)</p><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;"><span class="Apple-converted-space"> </span><img src="http://o7cqr8cfk.bkt.clouddn.com/17-6-10/83770925.jpg" alt="" title="" style="border: none; box-sizing: border-box; margin-top: 15px; margin-bottom: 15px; max-width: 602px; max-height: 100%; height: auto;"></p><pre class="prettyprint" name="code" style="white-space: nowrap; box-sizing: border-box; position: relative; overflow-y: hidden; overflow-x: auto; margin-bottom: 1.7em; line-height: 23px; font-family: "Source Code Pro", monospace; padding: 5px 5px 5px 60px; font-size: 14px; word-break: break-all; color: rgb(51, 51, 51); background-color: rgba(128, 128, 128, 0.047); border: 0px solid rgb(136, 136, 136); border-radius: 0px; font-variant-numeric: normal; font-variant-east-asian: normal;"><code class="language-javascript hljshas-numbering" style="display: block; background-color: transparent; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace; font-size: 13px; white-space: pre; border-radius: 0px; overflow-x: auto; word-wrap: normal;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">var</span> ctx;
<span class="hljs-function" style="box-sizing: border-box;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">function</span> <span class="hljs-title" style="box-sizing: border-box;">draw</span><span class="hljs-params" style="color: rgb(102, 0, 102); box-sizing: border-box;">()</span>{</span>
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">var</span> canvas = document.getElementById(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'tutorial1'</span>);
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> (!canvas.getContext) <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span>;
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">var</span> ctx = canvas.getContext(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"2d"</span>);
ctx.save(); <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//保存坐原点平移之前的状态</span>
ctx.translate(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">100</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">100</span>);
ctx.strokeRect(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">100</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">100</span>)
ctx.restore(); <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//恢复到最初状态</span>
ctx.translate(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">220</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">220</span>);
ctx.fillRect(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">100</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">100</span>)
}
draw();</code><ul class="pre-numbering" style="box-sizing: border-box; line-height: 23px; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; padding-top: 6px; padding-bottom: 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">1</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">2</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">3</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">4</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">5</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">6</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">7</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">8</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">9</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">10</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">11</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">12</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">13</li></ul></pre><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;"><img src="http://o7cqr8cfk.bkt.clouddn.com/17-6-10/36692804.jpg" alt="" title="" style="border: none; box-sizing: border-box; margin-top: 15px; margin-bottom: 15px; max-width: 602px; max-height: 100%; height: auto;"></p><h2 id="92-rotate" style="margin-top: 0.8em; margin-bottom: 0.8em; box-sizing: border-box; font-family: "microsoft yahei"; font-weight: 300; line-height: 1.1; color: rgb(63, 63, 63); font-size: 2.15em; font-variant-numeric: normal; font-variant-east-asian: normal;"><a name="t50" target="_blank" style="box-sizing: border-box; color: rgb(12, 137, 207);"></a>9.2 rotate</h2><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;"><code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">rotate(angle)</code></p><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;"> 旋转坐标轴。</p><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;"> 这个方法只接受一个参数:旋转的角度(angle),它是顺时针方向的,以弧度为单位的值。</p><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;"> 旋转的中心是坐标原点。</p><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;"><img src="http://o7cqr8cfk.bkt.clouddn.com/17-6-10/37980271.jpg" alt="" title="" style="border: none; box-sizing: border-box; margin-top: 15px; margin-bottom: 15px; max-width: 602px; max-height: 100%; height: auto;"></p><pre class="prettyprint" name="code" style="white-space: nowrap; box-sizing: border-box; position: relative; overflow-y: hidden; overflow-x: auto; margin-bottom: 1.7em; line-height: 23px; font-family: "Source Code Pro", monospace; padding: 5px 5px 5px 60px; font-size: 14px; word-break: break-all; color: rgb(51, 51, 51); background-color: rgba(128, 128, 128, 0.047); border: 0px solid rgb(136, 136, 136); border-radius: 0px; font-variant-numeric: normal; font-variant-east-asian: normal;"><code class="language-javascript hljshas-numbering" style="display: block; background-color: transparent; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace; font-size: 13px; white-space: pre; border-radius: 0px; overflow-x: auto; word-wrap: normal;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">var</span> ctx;
<span class="hljs-function" style="box-sizing: border-box;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">function</span> <span class="hljs-title" style="box-sizing: border-box;">draw</span><span class="hljs-params" style="color: rgb(102, 0, 102); box-sizing: border-box;">()</span>{</span>
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">var</span> canvas = document.getElementById(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'tutorial1'</span>);
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> (!canvas.getContext) <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span>;
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">var</span> ctx = canvas.getContext(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"2d"</span>);
ctx.fillStyle = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"red"</span>;
ctx.save();
ctx.translate(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">100</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">100</span>);
ctx.rotate(<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">Math</span>.PI / <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">180</span> * <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">45</span>);
ctx.fillStyle = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"blue"</span>;
ctx.fillRect(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">100</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">100</span>);
ctx.restore();
ctx.save();
ctx.translate(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>);
ctx.fillRect(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">50</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">50</span>)
ctx.restore();
}
draw();</code><ul class="pre-numbering" style="box-sizing: border-box; line-height: 23px; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; padding-top: 6px; padding-bottom: 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">1</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">2</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">3</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">4</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">5</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">6</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">7</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">8</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">9</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">10</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">11</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">12</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">13</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">14</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">15</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">16</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">17</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">18</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">19</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">20</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">21</li></ul></pre><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;"><img src="http://o7cqr8cfk.bkt.clouddn.com/17-6-10/40328613.jpg" alt="" title="" style="border: none; box-sizing: border-box; margin-top: 15px; margin-bottom: 15px; max-width: 602px; max-height: 100%; height: auto;"></p><h2 id="93-scale" style="margin-top: 0.8em; margin-bottom: 0.8em; box-sizing: border-box; font-family: "microsoft yahei"; font-weight: 300; line-height: 1.1; color: rgb(63, 63, 63); font-size: 2.15em; font-variant-numeric: normal; font-variant-east-asian: normal;"><a name="t51" target="_blank" style="box-sizing: border-box; color: rgb(12, 137, 207);"></a>9.3 scale</h2><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;"><code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">scale(x, y)</code></p><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;"> 我们用它来增减图形在<span class="Apple-converted-space"> </span><code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">canvas</code><span class="Apple-converted-space"> </span>中的像素数目,对形状,位图进行缩小或者放大。</p><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;"><span class="Apple-converted-space"> </span><code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">scale</code>方法接受两个参数。<code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">x,y</code>分别是横轴和纵轴的缩放因子,它们都必须是正值。值比 1.0 小表示缩 小,比 1.0 大则表示放大,值为 1.0 时什么效果都没有。</p><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;"> 默认情况下,<code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">canvas</code><span class="Apple-converted-space"> </span>的 1 单位就是 1 个像素。举例说,如果我们设置缩放因子是 0.5,1 个单位就变成对应 0.5 个像素,这样绘制出来的形状就会是原先的一半。同理,设置为 2.0 时,1 个单位就对应变成了 2 像素,绘制的结果就是图形放大了 2 倍。</p><h2 id="94-transform变形矩阵" style="margin-top: 0.8em; margin-bottom: 0.8em; box-sizing: border-box; font-family: "microsoft yahei"; font-weight: 300; line-height: 1.1; color: rgb(63, 63, 63); font-size: 2.15em; font-variant-numeric: normal; font-variant-east-asian: normal;"><a name="t52" target="_blank" style="box-sizing: border-box; color: rgb(12, 137, 207);"></a>9.4 transform(变形矩阵)</h2><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;"><code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">transform(a, b, c, d, e, f)</code></p><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;"><img src="http://o7cqr8cfk.bkt.clouddn.com/17-6-10/89391901.jpg" alt="" title="" style="border: none; box-sizing: border-box; margin-top: 15px; margin-bottom: 15px; max-width: 602px; max-height: 100%; height: auto;"></p><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;"><code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">a (m11)</code></p><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;"> Horizontal scaling.</p><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;"><code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">b (m12)</code></p><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;"> Horizontal skewing.</p><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;"><code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">c (m21)</code></p><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;"> Vertical skewing.</p><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;"><code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">d (m22)</code></p><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;"> Vertical scaling.</p><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;"><code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">e (dx)</code></p><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;"> Horizontal moving.</p><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;"><code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">f (dy)</code></p><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;"> Vertical moving.</p><pre class="prettyprint" name="code" style="white-space: nowrap; box-sizing: border-box; position: relative; overflow-y: hidden; overflow-x: auto; margin-bottom: 1.7em; line-height: 23px; font-family: "Source Code Pro", monospace; padding: 5px 5px 5px 60px; font-size: 14px; word-break: break-all; color: rgb(51, 51, 51); background-color: rgba(128, 128, 128, 0.047); border: 0px solid rgb(136, 136, 136); border-radius: 0px; font-variant-numeric: normal; font-variant-east-asian: normal;"><code class="language-javascript hljshas-numbering" style="display: block; background-color: transparent; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace; font-size: 13px; white-space: pre; border-radius: 0px; overflow-x: auto; word-wrap: normal;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">var</span> ctx;
<span class="hljs-function" style="box-sizing: border-box;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">function</span> <span class="hljs-title" style="box-sizing: border-box;">draw</span><span class="hljs-params" style="color: rgb(102, 0, 102); box-sizing: border-box;">()</span>{</span>
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">var</span> canvas = document.getElementById(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'tutorial1'</span>);
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> (!canvas.getContext) <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span>;
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">var</span> ctx = canvas.getContext(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"2d"</span>);
ctx.transform(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>);
ctx.fillRect(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">100</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">100</span>);
}
draw();</code><ul class="pre-numbering" style="box-sizing: border-box; line-height: 23px; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; padding-top: 6px; padding-bottom: 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">1</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">2</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">3</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">4</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">5</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">6</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">7</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">8</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">9</li></ul></pre><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;"><img src="http://o7cqr8cfk.bkt.clouddn.com/17-6-10/15535750.jpg" alt="" title="" style="border: none; box-sizing: border-box; margin-top: 15px; margin-bottom: 15px; max-width: 602px; max-height: 100%; height: auto;"></p><h1 id="十合成" style="margin-top: 0.8em; margin-bottom: 0.8em; box-sizing: border-box; font-family: "microsoft yahei"; font-weight: 300; line-height: 1.1; color: rgb(63, 63, 63); font-size: 2.6em; font-variant-numeric: normal; font-variant-east-asian: normal;"><a name="t53" target="_blank" style="box-sizing: border-box; color: rgb(12, 137, 207);"></a>十、合成</h1><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;"> 在前面的所有例子中、,我们总是将一个图形画在另一个之上,对于其他更多的情况,仅仅这样是远远不够的。比如,对合成的图形来说,绘制顺序会有限制。不过,我们可以利用<span class="Apple-converted-space"> </span><code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">globalCompositeOperation</code><span class="Apple-converted-space"> </span>属性来改变这种状况。</p><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;"><code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">globalCompositeOperation = type</code></p><pre class="prettyprint" name="code" style="white-space: nowrap; box-sizing: border-box; position: relative; overflow-y: hidden; overflow-x: auto; margin-bottom: 1.7em; line-height: 23px; font-family: "Source Code Pro", monospace; padding: 5px 5px 5px 60px; font-size: 14px; word-break: break-all; color: rgb(51, 51, 51); background-color: rgba(128, 128, 128, 0.047); border: 0px solid rgb(136, 136, 136); border-radius: 0px; font-variant-numeric: normal; font-variant-east-asian: normal;"><code class="language-javascript hljshas-numbering" style="display: block; background-color: transparent; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace; font-size: 13px; white-space: pre; border-radius: 0px; overflow-x: auto; word-wrap: normal;"> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">var</span> ctx;
<span class="hljs-function" style="box-sizing: border-box;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">function</span> <span class="hljs-title" style="box-sizing: border-box;">draw</span><span class="hljs-params" style="color: rgb(102, 0, 102); box-sizing: border-box;">()</span>{</span>
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">var</span> canvas = document.getElementById(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'tutorial1'</span>);
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> (!canvas.getContext) <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span>;
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">var</span> ctx = canvas.getContext(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"2d"</span>);
ctx.fillStyle = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"blue"</span>;
ctx.fillRect(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">200</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">200</span>);
ctx.globalCompositeOperation = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"source-over"</span>; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//全局合成操作</span>
ctx.fillStyle = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"red"</span>;
ctx.fillRect(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">100</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">100</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">200</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">200</span>);
}
draw();
<span class="xml" style="box-sizing: border-box;"><span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box;"></<span class="hljs-title" style="box-sizing: border-box; color: rgb(0, 0, 136);">script</span>></span></span></code><ul class="pre-numbering" style="box-sizing: border-box; line-height: 23px; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; padding-top: 6px; padding-bottom: 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">1</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">2</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">3</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">4</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">5</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">6</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">7</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">8</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">9</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">10</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">11</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">12</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">13</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">14</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">15</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">16</li></ul></pre><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;">注:下面的展示中,蓝色是原有的,红色是新的。</p><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;">type `是下面 13 种字符串值之一:</p><h2 id="1-source-overdefault" style="margin-top: 0.8em; margin-bottom: 0.8em; box-sizing: border-box; font-family: "microsoft yahei"; font-weight: 300; line-height: 1.1; color: rgb(63, 63, 63); font-size: 2.15em; font-variant-numeric: normal; font-variant-east-asian: normal;"><a name="t54" target="_blank" style="box-sizing: border-box; color: rgb(12, 137, 207);"></a>1.<span class="Apple-converted-space"> </span><code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 31px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">source-over(default)</code></h2><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;">这是默认设置,新图像会覆盖在原有图像。</p><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;"><img src="http://o7cqr8cfk.bkt.clouddn.com/17-6-10/15850624.jpg" alt="" title="" style="border: none; box-sizing: border-box; margin-top: 15px; margin-bottom: 15px; max-width: 602px; max-height: 100%; height: auto;"></p><h2 id="2-source-in" style="margin-top: 0.8em; margin-bottom: 0.8em; box-sizing: border-box; font-family: "microsoft yahei"; font-weight: 300; line-height: 1.1; color: rgb(63, 63, 63); font-size: 2.15em; font-variant-numeric: normal; font-variant-east-asian: normal;"><a name="t55" target="_blank" style="box-sizing: border-box; color: rgb(12, 137, 207);"></a>2.<span class="Apple-converted-space"> </span><code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 31px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">source-in</code></h2><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;">仅仅会出现新图像与原来图像重叠的部分,其他区域都变成透明的。(包括其他的老图像区域也会透明)</p><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;"><img src="http://o7cqr8cfk.bkt.clouddn.com/17-6-10/37411043.jpg" alt="" title="" style="border: none; box-sizing: border-box; margin-top: 15px; margin-bottom: 15px; max-width: 602px; max-height: 100%; height: auto;"></p><h2 id="3-source-out" style="margin-top: 0.8em; margin-bottom: 0.8em; box-sizing: border-box; font-family: "microsoft yahei"; font-weight: 300; line-height: 1.1; color: rgb(63, 63, 63); font-size: 2.15em; font-variant-numeric: normal; font-variant-east-asian: normal;"><a name="t56" target="_blank" style="box-sizing: border-box; color: rgb(12, 137, 207);"></a>3.<span class="Apple-converted-space"> </span><code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 31px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">source-out</code></h2><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;">仅仅显示新图像与老图像没有重叠的部分,其余部分全部透明。(老图像也不显示)</p><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;"><img src="http://o7cqr8cfk.bkt.clouddn.com/17-6-10/41781103.jpg" alt="" title="" style="border: none; box-sizing: border-box; margin-top: 15px; margin-bottom: 15px; max-width: 602px; max-height: 100%; height: auto;"></p><h2 id="4-source-atop" style="margin-top: 0.8em; margin-bottom: 0.8em; box-sizing: border-box; font-family: "microsoft yahei"; font-weight: 300; line-height: 1.1; color: rgb(63, 63, 63); font-size: 2.15em; font-variant-numeric: normal; font-variant-east-asian: normal;"><a name="t57" target="_blank" style="box-sizing: border-box; color: rgb(12, 137, 207);"></a>4.<span class="Apple-converted-space"> </span><code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 31px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">source-atop</code></h2><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;">新图像仅仅显示与老图像重叠区域。老图像仍然可以显示。</p><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;"><img src="http://o7cqr8cfk.bkt.clouddn.com/17-6-10/29772191.jpg" alt="" title="" style="border: none; box-sizing: border-box; margin-top: 15px; margin-bottom: 15px; max-width: 602px; max-height: 100%; height: auto;"></p><h2 id="5-destination-over" style="margin-top: 0.8em; margin-bottom: 0.8em; box-sizing: border-box; font-family: "microsoft yahei"; font-weight: 300; line-height: 1.1; color: rgb(63, 63, 63); font-size: 2.15em; font-variant-numeric: normal; font-variant-east-asian: normal;"><a name="t58" target="_blank" style="box-sizing: border-box; color: rgb(12, 137, 207);"></a>5.<span class="Apple-converted-space"> </span><code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 31px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">destination-over</code></h2><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;">新图像会在老图像的下面。</p><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;"><img src="http://o7cqr8cfk.bkt.clouddn.com/17-6-10/23645788.jpg" alt="" title="" style="border: none; box-sizing: border-box; margin-top: 15px; margin-bottom: 15px; max-width: 602px; max-height: 100%; height: auto;"></p><h2 id="6-destination-in" style="margin-top: 0.8em; margin-bottom: 0.8em; box-sizing: border-box; font-family: "microsoft yahei"; font-weight: 300; line-height: 1.1; color: rgb(63, 63, 63); font-size: 2.15em; font-variant-numeric: normal; font-variant-east-asian: normal;"><a name="t59" target="_blank" style="box-sizing: border-box; color: rgb(12, 137, 207);"></a>6.<span class="Apple-converted-space"> </span><code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 31px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">destination-in</code></h2><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;">仅仅新老图像重叠部分的老图像被显示,其他区域全部透明。</p><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;"><img src="http://o7cqr8cfk.bkt.clouddn.com/17-6-10/78166139.jpg" alt="" title="" style="border: none; box-sizing: border-box; margin-top: 15px; margin-bottom: 15px; max-width: 602px; max-height: 100%; height: auto;"></p><h2 id="7-destination-out" style="margin-top: 0.8em; margin-bottom: 0.8em; box-sizing: border-box; font-family: "microsoft yahei"; font-weight: 300; line-height: 1.1; color: rgb(63, 63, 63); font-size: 2.15em; font-variant-numeric: normal; font-variant-east-asian: normal;"><a name="t60" target="_blank" style="box-sizing: border-box; color: rgb(12, 137, 207);"></a>7.<span class="Apple-converted-space"> </span><code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 31px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">destination-out</code></h2><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;">仅仅老图像与新图像没有重叠的部分。 注意显示的是老图像的部分区域。</p><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;"><img src="http://o7cqr8cfk.bkt.clouddn.com/17-6-10/27996302.jpg" alt="" title="" style="border: none; box-sizing: border-box; margin-top: 15px; margin-bottom: 15px; max-width: 602px; max-height: 100%; height: auto;"></p><h2 id="8-destination-atop" style="margin-top: 0.8em; margin-bottom: 0.8em; box-sizing: border-box; font-family: "microsoft yahei"; font-weight: 300; line-height: 1.1; color: rgb(63, 63, 63); font-size: 2.15em; font-variant-numeric: normal; font-variant-east-asian: normal;"><a name="t61" target="_blank" style="box-sizing: border-box; color: rgb(12, 137, 207);"></a>8.<span class="Apple-converted-space"> </span><code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 31px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">destination-atop</code></h2><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;">老图像仅仅仅仅显示重叠部分,新图像会显示在老图像的下面。</p><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;"><img src="http://o7cqr8cfk.bkt.clouddn.com/17-6-10/73933570.jpg" alt="" title="" style="border: none; box-sizing: border-box; margin-top: 15px; margin-bottom: 15px; max-width: 602px; max-height: 100%; height: auto;"></p><h2 id="9-lighter" style="margin-top: 0.8em; margin-bottom: 0.8em; box-sizing: border-box; font-family: "microsoft yahei"; font-weight: 300; line-height: 1.1; color: rgb(63, 63, 63); font-size: 2.15em; font-variant-numeric: normal; font-variant-east-asian: normal;"><a name="t62" target="_blank" style="box-sizing: border-box; color: rgb(12, 137, 207);"></a>9.<span class="Apple-converted-space"> </span><code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 31px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">lighter</code></h2><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;">新老图像都显示,但是重叠区域的颜色做加处理</p><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;"><img src="http://o7cqr8cfk.bkt.clouddn.com/17-6-10/65148759.jpg" alt="" title="" style="border: none; box-sizing: border-box; margin-top: 15px; margin-bottom: 15px; max-width: 602px; max-height: 100%; height: auto;"></p><h2 id="10-darken" style="margin-top: 0.8em; margin-bottom: 0.8em; box-sizing: border-box; font-family: "microsoft yahei"; font-weight: 300; line-height: 1.1; color: rgb(63, 63, 63); font-size: 2.15em; font-variant-numeric: normal; font-variant-east-asian: normal;"><a name="t63" target="_blank" style="box-sizing: border-box; color: rgb(12, 137, 207);"></a>10.<span class="Apple-converted-space"> </span><code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 31px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">darken</code></h2><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;">保留重叠部分最黑的像素。(每个颜色位进行比较,得到最小的)</p><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;"><code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">blue: #0000ff</code></p><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;"><code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">red: #ff0000</code></p><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;">所以重叠部分的颜色:<code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">#000000</code></p><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;"><img src="http://o7cqr8cfk.bkt.clouddn.com/17-6-10/88895274.jpg" alt="" title="" style="border: none; box-sizing: border-box; margin-top: 15px; margin-bottom: 15px; max-width: 602px; max-height: 100%; height: auto;"></p><h2 id="11-lighten" style="margin-top: 0.8em; margin-bottom: 0.8em; box-sizing: border-box; font-family: "microsoft yahei"; font-weight: 300; line-height: 1.1; color: rgb(63, 63, 63); font-size: 2.15em; font-variant-numeric: normal; font-variant-east-asian: normal;"><a name="t64" target="_blank" style="box-sizing: border-box; color: rgb(12, 137, 207);"></a>11.<span class="Apple-converted-space"> </span><code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 31px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">lighten</code></h2><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;">保证重叠部分最量的像素。(每个颜色位进行比较,得到最大的)</p><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;"><code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">blue: #0000ff</code></p><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;"><code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">red: #ff0000</code></p><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;">所以重叠部分的颜色:<code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">#ff00ff</code></p><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;"><img src="http://o7cqr8cfk.bkt.clouddn.com/17-6-10/21148678.jpg" alt="" title="" style="border: none; box-sizing: border-box; margin-top: 15px; margin-bottom: 15px; max-width: 602px; max-height: 100%; height: auto;"></p><h2 id="12-xor" style="margin-top: 0.8em; margin-bottom: 0.8em; box-sizing: border-box; font-family: "microsoft yahei"; font-weight: 300; line-height: 1.1; color: rgb(63, 63, 63); font-size: 2.15em; font-variant-numeric: normal; font-variant-east-asian: normal;"><a name="t65" target="_blank" style="box-sizing: border-box; color: rgb(12, 137, 207);"></a>12.<span class="Apple-converted-space"> </span><code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 31px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">xor</code></h2><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;">重叠部分会变成透明</p><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;"><img src="http://o7cqr8cfk.bkt.clouddn.com/17-6-10/44617053.jpg" alt="" title="" style="border: none; box-sizing: border-box; margin-top: 15px; margin-bottom: 15px; max-width: 602px; max-height: 100%; height: auto;"></p><h2 id="13-copy" style="margin-top: 0.8em; margin-bottom: 0.8em; box-sizing: border-box; font-family: "microsoft yahei"; font-weight: 300; line-height: 1.1; color: rgb(63, 63, 63); font-size: 2.15em; font-variant-numeric: normal; font-variant-east-asian: normal;"><a name="t66" target="_blank" style="box-sizing: border-box; color: rgb(12, 137, 207);"></a>13.<span class="Apple-converted-space"> </span><code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 31px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">copy</code></h2><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;">只有新图像会被保留,其余的全部被清除(边透明)<img src="http://o7cqr8cfk.bkt.clouddn.com/17-6-10/11205920.jpg" alt="" title="" style="border: none; box-sizing: border-box; margin-top: 15px; margin-bottom: 15px; max-width: 602px; max-height: 100%; height: auto;"></p><h1 id="十一裁剪路径" style="margin-top: 0.8em; margin-bottom: 0.8em; box-sizing: border-box; font-family: "microsoft yahei"; font-weight: 300; line-height: 1.1; color: rgb(63, 63, 63); font-size: 2.6em; font-variant-numeric: normal; font-variant-east-asian: normal;"><a name="t67" target="_blank" style="box-sizing: border-box; color: rgb(12, 137, 207);"></a>十一、裁剪路径</h1><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;"><code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">clip()</code></p><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;"> 把已经创建的路径转换成裁剪路径。</p><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;"> 裁剪路径的作用是遮罩。只显示裁剪路径内的区域,裁剪路径外的区域会被隐藏。</p><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;"> 注意:<code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">clip()</code>只能遮罩在这个方法调用之后绘制的图像,如果是<code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">clip()</code>方法调用之前绘制的图像,则无法实现遮罩。</p><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;"><img src="http://o7cqr8cfk.bkt.clouddn.com/17-6-10/53022912.jpg" alt="" title="" style="border: none; box-sizing: border-box; margin-top: 15px; margin-bottom: 15px; max-width: 602px; max-height: 100%; height: auto;"></p><pre class="prettyprint" name="code" style="white-space: nowrap; box-sizing: border-box; position: relative; overflow-y: hidden; overflow-x: auto; margin-bottom: 1.7em; line-height: 23px; font-family: "Source Code Pro", monospace; padding: 5px 5px 5px 60px; font-size: 14px; word-break: break-all; color: rgb(51, 51, 51); background-color: rgba(128, 128, 128, 0.047); border: 0px solid rgb(136, 136, 136); border-radius: 0px; font-variant-numeric: normal; font-variant-east-asian: normal;"><code class="language-javascript hljshas-numbering" style="display: block; background-color: transparent; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace; font-size: 13px; white-space: pre; border-radius: 0px; overflow-x: auto; word-wrap: normal;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">var</span> ctx;
<span class="hljs-function" style="box-sizing: border-box;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">function</span> <span class="hljs-title" style="box-sizing: border-box;">draw</span><span class="hljs-params" style="color: rgb(102, 0, 102); box-sizing: border-box;">()</span>{</span>
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">var</span> canvas = document.getElementById(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'tutorial1'</span>);
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> (!canvas.getContext) <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span>;
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">var</span> ctx = canvas.getContext(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"2d"</span>);
ctx.beginPath();
ctx.arc(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">20</span>,<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">20</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">100</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>, <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">Math</span>.PI * <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span>);
ctx.clip();
ctx.fillStyle = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"pink"</span>;
ctx.fillRect(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">20</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">20</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">100</span>,<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">100</span>);
}
draw();</code><ul class="pre-numbering" style="box-sizing: border-box; line-height: 23px; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; padding-top: 6px; padding-bottom: 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">1</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">2</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">3</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">4</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">5</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">6</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">7</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">8</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">9</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">10</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">11</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">12</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">13</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">14</li></ul></pre><h1 id="十二动画" style="margin-top: 0.8em; margin-bottom: 0.8em; box-sizing: border-box; font-family: "microsoft yahei"; font-weight: 300; line-height: 1.1; color: rgb(63, 63, 63); font-size: 2.6em; font-variant-numeric: normal; font-variant-east-asian: normal;"><a name="t68" target="_blank" style="box-sizing: border-box; color: rgb(12, 137, 207);"></a>十二、动画</h1><h2 id="动画的基本步骤" style="margin-top: 0.8em; margin-bottom: 0.8em; box-sizing: border-box; font-family: "microsoft yahei"; font-weight: 300; line-height: 1.1; color: rgb(63, 63, 63); font-size: 2.15em; font-variant-numeric: normal; font-variant-east-asian: normal;"><a name="t69" target="_blank" style="box-sizing: border-box; color: rgb(12, 137, 207);"></a>动画的基本步骤</h2><ol style="box-sizing: border-box; line-height: 27px; margin-bottom: 1.7em; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;"><li style="box-sizing: border-box;"><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal;"><strong style="box-sizing: border-box;">清空<code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">canvas</code></strong></p><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal;">再绘制每一帧动画之前,需要清空所有。清空所有最简单的做法就是<code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">clearRect()</code>方法</p></li><li style="box-sizing: border-box;"><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal;"><strong style="box-sizing: border-box;">保存<code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">canvas</code>状态</strong></p><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal;">如果在绘制的过程中会更改<code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">canvas</code>的状态(颜色、移动了坐标原点等),又在绘制每一帧时都是原始状态的话,则最好保存下<code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">canvas</code>的状态</p></li><li style="box-sizing: border-box;"><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal;"><strong style="box-sizing: border-box;">绘制动画图形</strong></p><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal;">这一步才是真正的绘制动画帧</p></li><li style="box-sizing: border-box;"><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal;"><strong style="box-sizing: border-box;">恢复<code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">canvas</code>状态</strong></p><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal;">如果你前面保存了<code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">canvas</code>状态,则应该在绘制完成一帧之后恢复<code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">canvas</code>状态。</p></li></ol><h2 id="控制动画" style="margin-top: 0.8em; margin-bottom: 0.8em; box-sizing: border-box; font-family: "microsoft yahei"; font-weight: 300; line-height: 1.1; color: rgb(63, 63, 63); font-size: 2.15em; font-variant-numeric: normal; font-variant-east-asian: normal;"><a name="t70" target="_blank" style="box-sizing: border-box; color: rgb(12, 137, 207);"></a>控制动画</h2><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;">我们可用通过<code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">canvas</code>的方法或者自定义的方法把图像会知道到<code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">canvas</code>上。正常情况,我们能看到绘制的结果是在脚本执行结束之后。例如,我们不可能在一个<span class="Apple-converted-space"> </span><code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">for</code><span class="Apple-converted-space"> </span>循环内部完成动画。</p><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;">也就是,为了执行动画,我们需要一些可以定时执行重绘的方法。</p><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;">一般用到下面三个方法:</p><ol style="box-sizing: border-box; line-height: 27px; margin-bottom: 1.7em; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;"><li style="box-sizing: border-box;"><code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">setInterval()</code></li><li style="box-sizing: border-box;"><code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">setTimeout()</code></li><li style="box-sizing: border-box;"><code style="box-sizing: border-box; font-family: "Source Code Pro", monospace; padding: 2px 4px; font-size: 14px; background-color: rgba(128, 128, 128, 0.075); white-space: nowrap; border-radius: 0px;">requestAnimationFrame()</code></li></ol><h2 id="案例1太阳系" style="margin-top: 0.8em; margin-bottom: 0.8em; box-sizing: border-box; font-family: "microsoft yahei"; font-weight: 300; line-height: 1.1; color: rgb(63, 63, 63); font-size: 2.15em; font-variant-numeric: normal; font-variant-east-asian: normal;"><a name="t71" target="_blank" style="box-sizing: border-box; color: rgb(12, 137, 207);"></a>案例1:太阳系</h2><pre class="prettyprint" name="code" style="white-space: nowrap; box-sizing: border-box; position: relative; overflow-y: hidden; overflow-x: auto; margin-bottom: 1.7em; line-height: 23px; font-family: "Source Code Pro", monospace; padding: 5px 5px 5px 60px; font-size: 14px; word-break: break-all; color: rgb(51, 51, 51); background-color: rgba(128, 128, 128, 0.047); border: 0px solid rgb(136, 136, 136); border-radius: 0px; font-variant-numeric: normal; font-variant-east-asian: normal;"><code class="language-javascript hljshas-numbering" style="display: block; background-color: transparent; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace; font-size: 13px; white-space: pre; border-radius: 0px; overflow-x: auto; word-wrap: normal;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">let</span> sun;
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">let</span> earth;
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">let</span> moon;
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">let</span> ctx;
<span class="hljs-function" style="box-sizing: border-box;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">function</span> <span class="hljs-title" style="box-sizing: border-box;">init</span><span class="hljs-params" style="color: rgb(102, 0, 102); box-sizing: border-box;">()</span>{</span>
sun = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> Image();
earth = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> Image();
moon = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> Image();
sun.src = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"sun.png"</span>;
earth.src = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"earth.png"</span>;
moon.src = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"moon.png"</span>;
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">let</span> canvas = document.querySelector(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"#solar"</span>);
ctx = canvas.getContext(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"2d"</span>);
sun.onload = <span class="hljs-function" style="box-sizing: border-box;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">function</span> <span class="hljs-params" style="color: rgb(102, 0, 102); box-sizing: border-box;">()</span>{</span>
draw()
}
}
init();
<span class="hljs-function" style="box-sizing: border-box;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">function</span> <span class="hljs-title" style="box-sizing: border-box;">draw</span><span class="hljs-params" style="color: rgb(102, 0, 102); box-sizing: border-box;">()</span>{</span>
ctx.clearRect(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">300</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">300</span>); <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//清空所有的内容</span>
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">/*绘制 太阳*/</span>
ctx.drawImage(sun, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">300</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">300</span>);
ctx.save();
ctx.translate(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">150</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">150</span>);
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//绘制earth轨道</span>
ctx.beginPath();
ctx.strokeStyle = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"rgba(255,255,0,0.5)"</span>;
ctx.arc(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">100</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span> * <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">Math</span>.PI)
ctx.stroke()
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">let</span> time = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">Date</span>();
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//绘制地球</span>
ctx.rotate(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span> * <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">Math</span>.PI / <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">60</span> * time.getSeconds() + <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span> * <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">Math</span>.PI / <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">60000</span> * time.getMilliseconds())
ctx.translate(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">100</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>);
ctx.drawImage(earth, -<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">12</span>, -<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">12</span>)
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//绘制月球轨道</span>
ctx.beginPath();
ctx.strokeStyle = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"rgba(255,255,255,.3)"</span>;
ctx.arc(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">40</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span> * <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">Math</span>.PI);
ctx.stroke();
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//绘制月球</span>
ctx.rotate(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span> * <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">Math</span>.PI / <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">6</span> * time.getSeconds() + <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span> * <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">Math</span>.PI / <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">6000</span> * time.getMilliseconds());
ctx.translate(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">40</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>);
ctx.drawImage(moon, -<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3.5</span>, -<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3.5</span>);
ctx.restore();
requestAnimationFrame(draw);
}</code><ul class="pre-numbering" style="box-sizing: border-box; line-height: 23px; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; padding-top: 6px; padding-bottom: 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">1</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">2</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">3</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">4</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">5</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">6</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">7</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">8</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">9</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">10</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">11</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">12</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">13</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">14</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">15</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">16</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">17</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">18</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">19</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">20</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">21</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">22</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">23</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">24</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">25</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">26</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">27</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">28</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">29</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">30</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">31</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">32</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">33</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">34</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">35</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">36</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">37</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">38</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">39</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">40</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">41</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">42</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">43</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">44</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">45</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">46</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">47</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">48</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">49</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">50</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">51</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">52</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">53</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">54</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">55</li></ul></pre><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;"><img src="http://o7cqr8cfk.bkt.clouddn.com/17-6-10/65211642.jpg" alt="" title="" style="border: none; box-sizing: border-box; margin-top: 15px; margin-bottom: 15px; max-width: 602px; max-height: 100%; height: auto;"></p><h2 id="案例2模拟时钟" style="margin-top: 0.8em; margin-bottom: 0.8em; box-sizing: border-box; font-family: "microsoft yahei"; font-weight: 300; line-height: 1.1; color: rgb(63, 63, 63); font-size: 2.15em; font-variant-numeric: normal; font-variant-east-asian: normal;"><a name="t72" target="_blank" style="box-sizing: border-box; color: rgb(12, 137, 207);"></a>案例2:模拟时钟</h2><pre class="prettyprint" name="code" style="white-space: nowrap; box-sizing: border-box; position: relative; overflow-y: hidden; overflow-x: auto; margin-bottom: 1.7em; line-height: 23px; font-family: "Source Code Pro", monospace; padding: 5px 5px 5px 60px; font-size: 14px; word-break: break-all; color: rgb(51, 51, 51); background-color: rgba(128, 128, 128, 0.047); border: 0px solid rgb(136, 136, 136); border-radius: 0px; font-variant-numeric: normal; font-variant-east-asian: normal;"><code class="language-html hljshas-numbering" style="display: block; background-color: transparent; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace; font-size: 13px; white-space: pre; border-radius: 0px; overflow-x: auto; word-wrap: normal;"><span class="hljs-doctype" style="color: rgb(102, 0, 102); box-sizing: border-box;"><!DOCTYPE html></span>
<span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box;"><<span class="hljs-title" style="box-sizing: border-box; color: rgb(0, 0, 136);">html</span> <span class="hljs-attribute" style="box-sizing: border-box; color: rgb(102, 0, 102);">lang</span>=<span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 136, 0);">"en"</span>></span>
<span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box;"><<span class="hljs-title" style="box-sizing: border-box; color: rgb(0, 0, 136);">head</span>></span>
<span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box;"><<span class="hljs-title" style="box-sizing: border-box; color: rgb(0, 0, 136);">meta</span> <span class="hljs-attribute" style="box-sizing: border-box; color: rgb(102, 0, 102);">charset</span>=<span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 136, 0);">"UTF-8"</span>></span>
<span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box;"><<span class="hljs-title" style="box-sizing: border-box; color: rgb(0, 0, 136);">title</span>></span>Title<span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box;"></<span class="hljs-title" style="box-sizing: border-box; color: rgb(0, 0, 136);">title</span>></span>
<span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box;"><<span class="hljs-title" style="box-sizing: border-box; color: rgb(0, 0, 136);">style</span>></span><span class="css" style="box-sizing: border-box;">
<span class="hljs-tag" style="color: rgb(0, 0, 0); box-sizing: border-box;">body</span> <span class="hljs-rules" style="box-sizing: border-box;">{
<span class="hljs-rule" style="box-sizing: border-box;"><span class="hljs-attribute" style="box-sizing: border-box;">padding</span>:<span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 102, 102);"> <span class="hljs-number" style="box-sizing: border-box;">0</span></span></span>;
<span class="hljs-rule" style="box-sizing: border-box;"><span class="hljs-attribute" style="box-sizing: border-box;">margin</span>:<span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 102, 102);"> <span class="hljs-number" style="box-sizing: border-box;">0</span></span></span>;
<span class="hljs-rule" style="box-sizing: border-box;"><span class="hljs-attribute" style="box-sizing: border-box;">background-color</span>:<span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 102, 102);"> <span class="hljs-function" style="box-sizing: border-box;">rgba(<span class="hljs-number" style="box-sizing: border-box;">0</span>, <span class="hljs-number" style="box-sizing: border-box;">0</span>, <span class="hljs-number" style="box-sizing: border-box;">0</span>, <span class="hljs-number" style="box-sizing: border-box;">0.1</span>)</span>
</span></span></span>}
<span class="hljs-tag" style="color: rgb(0, 0, 0); box-sizing: border-box;">canvas</span> <span class="hljs-rules" style="box-sizing: border-box;">{
<span class="hljs-rule" style="box-sizing: border-box;"><span class="hljs-attribute" style="box-sizing: border-box;">display</span>:<span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 102, 102);"> block</span></span>;
<span class="hljs-rule" style="box-sizing: border-box;"><span class="hljs-attribute" style="box-sizing: border-box;">margin</span>:<span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 102, 102);"> <span class="hljs-number" style="box-sizing: border-box;">200</span>px auto</span></span>;
<span class="hljs-rule" style="box-sizing: border-box;">}</span></span>
</span><span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box;"></<span class="hljs-title" style="box-sizing: border-box; color: rgb(0, 0, 136);">style</span>></span>
<span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box;"></<span class="hljs-title" style="box-sizing: border-box; color: rgb(0, 0, 136);">head</span>></span>
<span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box;"><<span class="hljs-title" style="box-sizing: border-box; color: rgb(0, 0, 136);">body</span>></span>
<span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box;"><<span class="hljs-title" style="box-sizing: border-box; color: rgb(0, 0, 136);">canvas</span> <span class="hljs-attribute" style="box-sizing: border-box; color: rgb(102, 0, 102);">id</span>=<span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 136, 0);">"solar"</span> <span class="hljs-attribute" style="box-sizing: border-box; color: rgb(102, 0, 102);">width</span>=<span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 136, 0);">"300"</span> <span class="hljs-attribute" style="box-sizing: border-box; color: rgb(102, 0, 102);">height</span>=<span class="hljs-value" style="box-sizing: border-box; color: rgb(0, 136, 0);">"300"</span>></span><span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box;"></<span class="hljs-title" style="box-sizing: border-box; color: rgb(0, 0, 136);">canvas</span>></span>
<span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box;"><<span class="hljs-title" style="box-sizing: border-box; color: rgb(0, 0, 136);">script</span>></span><span class="javascript" style="box-sizing: border-box;">
init();
<span class="hljs-function" style="box-sizing: border-box;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">function</span> <span class="hljs-title" style="box-sizing: border-box;">init</span><span class="hljs-params" style="color: rgb(102, 0, 102); box-sizing: border-box;">()</span>{</span>
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">let</span> canvas = document.querySelector(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"#solar"</span>);
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">let</span> ctx = canvas.getContext(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"2d"</span>);
draw(ctx);
}
<span class="hljs-function" style="box-sizing: border-box;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">function</span> <span class="hljs-title" style="box-sizing: border-box;">draw</span><span class="hljs-params" style="color: rgb(102, 0, 102); box-sizing: border-box;">(ctx)</span>{</span>
requestAnimationFrame(<span class="hljs-function" style="box-sizing: border-box;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">function</span> <span class="hljs-title" style="box-sizing: border-box;">step</span><span class="hljs-params" style="color: rgb(102, 0, 102); box-sizing: border-box;">()</span>{</span>
drawDial(ctx); <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//绘制表盘</span>
drawAllHands(ctx); <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//绘制时分秒针</span>
requestAnimationFrame(step);
});
}
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">/*绘制时分秒针*/</span>
<span class="hljs-function" style="box-sizing: border-box;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">function</span> <span class="hljs-title" style="box-sizing: border-box;">drawAllHands</span><span class="hljs-params" style="color: rgb(102, 0, 102); box-sizing: border-box;">(ctx)</span>{</span>
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">let</span> time = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">new</span> <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">Date</span>();
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">let</span> s = time.getSeconds();
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">let</span> m = time.getMinutes();
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">let</span> h = time.getHours();
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">let</span> pi = <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">Math</span>.PI;
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">let</span> secondAngle = pi / <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">180</span> * <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">6</span> * s;<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//计算出来s针的弧度</span>
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">let</span> minuteAngle = pi / <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">180</span> * <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">6</span> * m + secondAngle / <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">60</span>;<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//计算出来分针的弧度</span>
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">let</span> hourAngle = pi / <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">180</span> * <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">30</span> * h + minuteAngle / <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">12</span>;<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//计算出来时针的弧度</span>
drawHand(hourAngle, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">60</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">6</span>, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"red"</span>, ctx);<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//绘制时针</span>
drawHand(minuteAngle, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">106</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4</span>, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"green"</span>, ctx);<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//绘制分针</span>
drawHand(secondAngle, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">129</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span>, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"blue"</span>, ctx);<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//绘制秒针</span>
}
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">/*绘制时针、或分针、或秒针
* 参数1:要绘制的针的角度
* 参数2:要绘制的针的长度
* 参数3:要绘制的针的宽度
* 参数4:要绘制的针的颜色
* 参数4:ctx
* */</span>
<span class="hljs-function" style="box-sizing: border-box;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">function</span> <span class="hljs-title" style="box-sizing: border-box;">drawHand</span><span class="hljs-params" style="color: rgb(102, 0, 102); box-sizing: border-box;">(angle, len, width, color, ctx)</span>{</span>
ctx.save();
ctx.translate(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">150</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">150</span>); <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//把坐标轴的远点平移到原来的中心</span>
ctx.rotate(-<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">Math</span>.PI / <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span> + angle);<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//旋转坐标轴。 x轴就是针的角度</span>
ctx.beginPath();
ctx.moveTo(-<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>);
ctx.lineTo(len, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>);<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// 沿着x轴绘制针</span>
ctx.lineWidth = width;
ctx.strokeStyle = color;
ctx.lineCap = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"round"</span>;
ctx.stroke();
ctx.closePath();
ctx.restore();
}
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">/*绘制表盘*/</span>
<span class="hljs-function" style="box-sizing: border-box;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">function</span> <span class="hljs-title" style="box-sizing: border-box;">drawDial</span><span class="hljs-params" style="color: rgb(102, 0, 102); box-sizing: border-box;">(ctx)</span>{</span>
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">let</span> pi = <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">Math</span>.PI;
ctx.clearRect(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">300</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">300</span>); <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//清除所有内容</span>
ctx.save();
ctx.translate(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">150</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">150</span>); <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//一定坐标原点到原来的中心</span>
ctx.beginPath();
ctx.arc(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">148</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span> * pi); <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//绘制圆周</span>
ctx.stroke();
ctx.closePath();
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">for</span> (<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">let</span> i = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>; i < <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">60</span>; i++){<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//绘制刻度。</span>
ctx.save();
ctx.rotate(-pi / <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span> + i * pi / <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">30</span>);<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//旋转坐标轴。坐标轴x的正方形从 向上开始算起</span>
ctx.beginPath();
ctx.moveTo(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">110</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>);
ctx.lineTo(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">140</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>);
ctx.lineWidth = i % <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">5</span> ? <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span> : <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4</span>;
ctx.strokeStyle = i % <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">5</span> ? <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"blue"</span> : <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"red"</span>;
ctx.stroke();
ctx.closePath();
ctx.restore();
}
ctx.restore();
}
</span><span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box;"></<span class="hljs-title" style="box-sizing: border-box; color: rgb(0, 0, 136);">script</span>></span>
<span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box;"></<span class="hljs-title" style="box-sizing: border-box; color: rgb(0, 0, 136);">body</span>></span>
<span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box;"></<span class="hljs-title" style="box-sizing: border-box; color: rgb(0, 0, 136);">html</span>></span></code><ul class="pre-numbering" style="box-sizing: border-box; line-height: 23px; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; padding-top: 6px; padding-bottom: 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">1</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">2</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">3</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">4</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">5</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">6</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">7</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">8</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">9</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">10</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">11</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">12</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">13</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">14</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">15</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">16</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">17</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">18</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">19</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">20</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">21</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">22</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">23</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">24</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">25</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">26</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">27</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">28</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">29</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">30</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">31</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">32</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">33</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">34</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">35</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">36</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">37</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">38</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">39</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">40</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">41</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">42</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">43</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">44</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">45</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">46</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">47</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">48</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">49</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">50</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">51</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">52</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">53</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">54</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">55</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">56</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">57</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">58</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">59</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">60</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">61</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">62</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">63</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">64</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">65</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">66</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">67</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">68</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">69</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">70</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">71</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">72</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">73</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">74</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">75</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">76</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">77</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">78</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">79</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">80</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">81</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">82</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">83</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">84</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">85</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">86</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">87</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">88</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">89</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">90</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">91</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">92</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">93</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">94</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">95</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">96</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">97</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">98</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">99</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">100</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">101</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">102</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">103</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">104</li><li style="box-sizing: border-box; padding-right: 5px; padding-left: 5px; list-style: none; margin-left: 0px;">105</li></ul></pre><p style="margin-bottom: 1.7em; box-sizing: border-box; line-height: 27px; word-break: normal; color: rgb(63, 63, 63); font-family: "microsoft yahei"; font-variant-numeric: normal; font-variant-east-asian: normal;"><img src="http://o7cqr8cfk.bkt.clouddn.com/17-6-10/61345876.jpg" alt="" title="" style="border: none; box-sizing: border-box; margin-top: 15px; margin-bottom: 15px; max-width: 602px; max-height: 100%; height: auto;"></p></div>
页:
[1]