GWT Canvas:如何更改线条颜色
由于 GWT 中的画布绘制已经遍布整个地图,所以让我明确地说我正在使用这个:
import com.google.gwt.canvas.client.Canvas;
问题是,如果我绘制一条黑线然后更改为红色,则第一行也会更改为红色。
// draw line in black
context.moveTo(xScale(-0.5), yScale(0.0));
context.lineTo(xScale(15.0), yScale(0.0));
context.stroke();
// change to red
context.setStrokeStyle(CssColor.make(255,0,0));
context.moveTo(xScale(0.0), yScale(20.0));
context.lineTo(xScale(0.0), yScale(-20.0));
context.stroke();
// both lines appear in red
改变笔颜色的正确方法是什么?
Since the canvas drawing in GWT has been all over the map, let me be explicit and say I'm using this:
import com.google.gwt.canvas.client.Canvas;
The problem is that if I draw a black line and then change to red, the first line is changed to red also.
// draw line in black
context.moveTo(xScale(-0.5), yScale(0.0));
context.lineTo(xScale(15.0), yScale(0.0));
context.stroke();
// change to red
context.setStrokeStyle(CssColor.make(255,0,0));
context.moveTo(xScale(0.0), yScale(20.0));
context.lineTo(xScale(0.0), yScale(-20.0));
context.stroke();
// both lines appear in red
What is the correct method for changing pen color?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
更多
发布评论
评论(1)
在每个具有不同颜色的新形状/线条之前调用
context.beginPath()
应该可以解决您的问题。基本上 beginPath() 推动了状态
Calling
context.beginPath()
before each new shape/line with different color should fix your problem.Basically beginPath() pushed the state