HTML5 Canvas 绘制五彩线条
我试图在画布上绘制两条平行线,但似乎后者的属性覆盖了前者。请提出可能存在问题的建议:
<html>
<head>
<script type="application/javascript">
function draw() {
var canvas = document.getElementById('canvas');
var ctx = canvas.getContext('2d');
// draw a 10 pix green line
ctx.strokeStyle='#00cc00';
ctx.lineWidth=10;
ctx.moveTo(100,0);
ctx.lineTo(100,1000);
ctx.stroke();
// draw a 20 pix red line
ctx.strokeStyle='#cc0000';
ctx.lineWidth=20;
ctx.moveTo(140,0);
ctx.lineTo(140,1000);
ctx.stroke();
}
</script>
</head>
<body onload="draw()">
<div><canvas id="canvas" width="1000" height="1000"></canvas></div>
</body>
</html>
I am trying to draw two parallel lines on the canvas, but it seems like properties of the latter overwrites the former. Please suggest what could be wrong :
<html>
<head>
<script type="application/javascript">
function draw() {
var canvas = document.getElementById('canvas');
var ctx = canvas.getContext('2d');
// draw a 10 pix green line
ctx.strokeStyle='#00cc00';
ctx.lineWidth=10;
ctx.moveTo(100,0);
ctx.lineTo(100,1000);
ctx.stroke();
// draw a 20 pix red line
ctx.strokeStyle='#cc0000';
ctx.lineWidth=20;
ctx.moveTo(140,0);
ctx.lineTo(140,1000);
ctx.stroke();
}
</script>
</head>
<body onload="draw()">
<div><canvas id="canvas" width="1000" height="1000"></canvas></div>
</body>
</html>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在绘制每条线之前调用 ctx.beginPath 。当进行强
lines
调用时,第一行仍然是当前路径的一部分,因此它会再次以新颜色绘制。Call
ctx.beginPath
before drawing each line. When the strongstroke
call is made, the first line is still part of the current path so it gets drawn again in the new color.