问题:
我通过beginPath方法来创建2个三角形的路径,然后统一用stroke来描边。结果只能画出第2个三角形,为什么第1个三角形画不出来呢?
代码:
<body>
<canvas width="600" height="300"></canvas>
<script>
window.onload=function(){
var ctx = document.querySelector('canvas').getContext('2d');
ctx.lineWidth=4;
// 新建路径1
ctx.beginPath();
ctx.moveTo(100,20);
ctx.lineTo(200,100);
ctx.lineTo(100,100);
ctx.lineTo(100,20);
ctx.closePath();
// 新建路径2
ctx.beginPath();
ctx.moveTo(200,40);
ctx.lineTo(400,200);
ctx.lineTo(200,200);
ctx.lineTo(200,40);
ctx.closePath();
// 统一描边
ctx.stroke();
};
</script>
</body>
截图:
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
两种方式:1.分开描边
2.把中间那团去掉(因为描边从beginPath到closepath结束)
中间那个closePath()去掉貌似就可以了