Use closePath() to close the shape
First we draw a rectangle using the most common method.
JavaScript CodeCopy content to clipboard
-
- "en">
-
- "UTF-8">
-
-
-
-
- "zh">
-
- "UTF-8">
- 绘制矩形
-
-
"canvas-warp">
-
- 你的浏览器居然不支持Canvas?!赶快换一个吧!!
-
-
- <script> </span></li>
<li>
<span> window.onload = </span><span class="keyword">function</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">"black"</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>
-
-
-
-
Run result:
At first glance, there is nothing wrong with it, but children's shoes with good eyesight have discovered that there is a problem when the last stroke is closed, resulting in a gap in the upper left corner. This situation is caused by setting lineWidth. If the default is 1 stroke, there will be no problem. But the larger the strokes and the wider the lines, the more obvious this gap will be. So how to avoid this situation?
The title has already spoiled it, use clothPath() to close the shape.
JavaScript CodeCopy content to clipboard
-
- "zh">
-
- "UTF-8">
- Draw a rectangle
-
-
"canvas-warp">
-
- Your browser doesn’t support Canvas? ! Change it quickly! !
-
-
- <script> </span></li>
<li class="alt">
<span> window.onload = </span><span class="keyword">function</span><span>(){ </span>
</li>
<li>
<span> </span><span class="keyword">var</span><span> canvas = document.getElementById(</span><span class="string">"canvas"</span><span>); </span>
</li>
<li class="alt"><span> canvas.width = 800; </span></li>
<li><span> canvas.height = 600; </span></li>
<li class="alt">
<span> </span><span class="keyword">var</span><span> context = canvas.getContext(</span><span class="string">"2d"</span><span>); </span>
</li>
<li><span> </span></li>
<li class="alt"><span> context.beginPath(); </span></li>
<li><span> context.moveTo(150,50); </span></li>
<li class="alt"><span> context.lineTo(650,50); </span></li>
<li><span> context.lineTo(650,550); </span></li>
<li class="alt"><span> context.lineTo(150,550); </span></li>
<li>
<span> context.lineTo(150,50); </span><span class="comment">//You don’t need to draw the last stroke </span><span> </span>
</li>
<li class="alt">
<span> context.closePath(); </span><span class="comment">//Use closePath() to close the graphic </span><span> </span>
</li>
<li><span> </span></li>
<li class="alt"><span> context.lineWidth = 5; </span></li>
<li>
<span> context.strokeStyle = </span><span class="string">"black"</span><span>; </span>
</li>
<li class="alt"><span> context.stroke(); </span></li>
<li><span> </span></li>
<li class="alt"><span> } </span></li>
<li><span></script>
-
-
-