lineCap 是 Canvas 2D API 指定如何繪制每一條線段末端的屬性。有3個可能的值,分別是:butt, round and square。默認(rèn)值是 butt。
畫三條帶有(butt,round,square)端蓋的線:
JavaScript:
<!DOCTYPE html>
<html>
<head>
<title>HTML canvas lineCap 屬性的使用(菜鳥教程 cainiaoplus.com)</title>
</head>
<body>
<canvas id="myCanvas" width="300" height="150" style="border:1px solid #d3d3d3;">
您的瀏覽器不支持 HTML5 canvas 標(biāo)簽。
</canvas>
<script>
var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");
ctx.beginPath();
ctx.lineWidth = 10;
ctx.lineCap = "butt";
ctx.moveTo(20, 20);
ctx.lineTo(200, 20);
ctx.stroke();
ctx.beginPath();
ctx.lineCap = "round";
ctx.moveTo(20, 40);
ctx.lineTo(200, 40);
ctx.stroke();
ctx.beginPath();
ctx.lineCap = "square";
ctx.moveTo(20, 60);
ctx.lineTo(200, 60);
ctx.stroke();
</script>
</body>
</html>測試看看 ?/?IEFirefoxOperaChromeSafari
Internet Explorer 9、Firefox、Opera、Chrome 和 Safari 支持 lineCap 屬性。
注意:Internet Explorer 8 及之前的版本不支持 <canvas> 元素。
lineCap屬性設(shè)置或返回一條線的端蓋樣式。
注意:"round" 和 "square" 值會使線條略微變長。
| 默認(rèn)值: | butt |
|---|---|
| JavaScript 語法: | context.lineCap="butt|round|square"; |
| 值 | 描述 |
|---|---|
| butt | 默認(rèn)。向線條的每個末端添加平直的邊緣。 |
| round | 向線條的每個末端添加圓形線帽。 |
| square | 向線條的每個末端添加正方形線帽。 |