Utilisez closePath() pour fermer la forme
Nous dessinons d'abord un rectangle en utilisant la méthode la plus courante.
Code JavaScriptCopier le contenu dans le presse-papiers
-
- "fr">
-
- "UTF-8">
-
-
-
-
- "zh">
-
- "UTF-8">
- 绘制矩形
-
-
"toile-warp">
-
- 你的浏览器居然不支持Canvas?!赶快换一个吧!!
-
-
- <script> </span></li>
<li>
<span> window.onload = </span><span class="keyword">fonction</span><span>(){ </span>
</li>
<li class="alt">
<span> </span><span class="keyword">var</span><span> canvas = document.getElementById(</span><span class="string">"canvas"</span><span>); </span>
</li>
<li><span> canvas.width = 800 ; </span></li>
<li class="alt"><span> canvas.height = 600 ; </span></li>
<li>
<span> </span><span class="keyword">var</span><span> context = canvas.getContext(</span><span class="string">"2d"</span><span>); </span>
</li>
<li class="alt"><span> </span></li>
<li><span> context.beginPath(); </span></li>
<li class="alt"><span> context.moveTo(150,50); </span></li>
<li><span> context.lineTo(650,50); </span></li>
<li class="alt"><span> context.lineTo(650 550); </span></li>
<li><span> context.lineTo(150 550); </span></li>
<li class="alt">
<span> context.lineTo(150,50); </span><span class="comment">//绘制最后一笔使图像闭合 </span><span> </span>
</li>
<li><span> context.lineWidth = 5; </span></li>
<li class="alt">
<span> context.StrokeStyle = </span><span class="string">"noir"</span><span>; </span>
</li>
<li><span> context.Stroke(); </span></li>
<li class="alt"><span> </span></li>
<li><span> } </span></li>
<li class="alt"><span></script>
-