lineCap(路徑樣式屬性)
context.lineCap=capStyle // butt (default), round, square
設定線起點和終點的上限樣式。
-
butt ,預設的 lineCap 樣式,顯示不超出線的起點和終點的平方上限。
-
圓形,顯示超出線的起點和終點的圓形帽。
-
方形,顯示延伸超出線的起點和終點的方形帽。
<!doctype html>
<html>
<head>
<style>
body{ background-color:white; }
#canvas{border:1px solid red; }
</style>
<script>
window.onload=(function(){
// canvas related variables
var canvas=document.getElementById("canvas");
var ctx=canvas.getContext("2d");
ctx.lineWidth=15;
// lineCap default: butt
ctx.lineCap='butt';
drawLine(50,40,200,40);
// lineCap: round
ctx.lineCap='round';
drawLine(50,70,200,70);
// lineCap: square
ctx.lineCap='square';
drawLine(50,100,200,100);
// utility function to draw a line
function drawLine(startX,startY,endX,endY){
ctx.beginPath();
ctx.moveTo(startX,startY);
ctx.lineTo(endX,endY);
ctx.stroke();
}
// For demo only,
// Rulers to show which lineCaps extend beyond endpoints
ctx.lineWidth=1;
ctx.strokeStyle='red';
drawLine(50,20,50,120);
drawLine(200,20,200,120);
}); // end window.onload
</script>
</head>
<body>
<canvas id="canvas" width=300 height=200></canvas>
</body>
</html>