web前端技术博客
您当前的位置:web前端 > html

canvas 绘制多条不同颜色的折线 使用beginPath

作者:只会切图的前端 发布时间:2021-03-16 10:02:36 浏览量:13

绘制多条不同颜色的折线
注意:
需使用beginPath 创建一个新的路径
stroke 根据当前的画线样式,绘制已存在的路径
不使用beginPath ,stroke将会重新绘制路径,覆盖之前的颜色

var canvas=document.getElementById('canvas');
var context=canvas.getContext('2d');
context.lineWidth=10;

context.beginPath();
context.lineTo(50,100);
context.lineTo(150,200);
context.lineTo(50,300);
context.strokeStyle="red";
context.stroke();

context.beginPath();
context.lineTo(150,100);
context.lineTo(250,200);
context.lineTo(150,300);
context.strokeStyle="blue";
context.stroke();

发表评论
验证码:
联系我
粤ICP备17092958号