<!DOCTYPE html>
<html>
<body>
<canvas id="myCanvas" width="600" height="400"></canvas>
<script>
var canvas = document.getElementById('myCanvas');
var context = canvas.getContext('2d');
// square linecap
context.beginPath();
context.moveTo(200, 200);
context.lineTo(canvas.width - 200, 100);
context.lineWidth = 20;
context.strokeStyle = 'red';
context.lineCap = 'square';
context.stroke();
// butt linecap
context.beginPath();
context.moveTo(200, 250);
context.lineTo(400, 250);
context.lineWidth = 10;
context.strokeStyle = '#0000ff';
context.lineCap = 'butt';
context.stroke();
// round linecap
context.beginPath();
context.moveTo(200, 300);
context.lineTo(400, 300);
context.lineWidth =15;
context.strokeStyle = 'green';
context.lineCap = 'round';
context.stroke();
</script>
</body>
</html>
You must define lineCap ,linewidth ,strokeStyle properties before stroke( ).