使用fill()方法对图形进行填充,配合fillStyle使用,设置填充的颜色
代码:
<body>
<canvas id="canvas" width="500" height="500"></canvas>
</body>
</html>
<script>
var canvas = document.getElementById('canvas');
var ctx = canvas.getContext('2d');
ctx.moveTo(50,50);
ctx.lineTo(100,50);
ctx.lineTo(100,100);
ctx.lineTo(50,100);
ctx.closePath();
// 设置填充的颜色
ctx.fillStyle = 'blue';
// 填充
ctx.fill();
</script>
效果: