更改画布元素的颜色
我正在尝试动态更改画布上绘制的线条的颜色...
ctx.moveTo(0, 0);
ctx.lineTo(0, 200);
ctx.strokeStyle = "Grey"
可能是鼠标悬停事件或按下按钮或鼠标单击事件,我想更改线条的颜色或使其加粗。是否可以通过添加事件来更改颜色,或者是否可以为特定元素上的事件提供样式?
I am trying to change the color of line drawn on canvas dynamically...
ctx.moveTo(0, 0);
ctx.lineTo(0, 200);
ctx.strokeStyle = "Grey"
It could be mouseover event or press botton or mouse click event, I want to change the color of line or make it bold. Is it possible to change the color by adding event or is it possible to give style on an event on particular element?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
非常接近。从某种意义上说,您无法真正“更改”画布上元素的颜色,因为
如果画布上有更复杂的场景,则必须重新绘制整个场景。有许多 Javascript 库可以扩展 canvas 标签的基本功能,并提供其他绘图功能。您可能想看一下 Processing,它看起来相当令人印象深刻。
Very close. In a sense, you can't really "change" the color of an element on the canvas because it has no scene graph, or, in other words, it has no history of what has been drawn on the canvas. To change the color of a line, you would have to redraw the line.
If the canvas had a more complex scene going on, you would have to redraw the entire scene. There are numerous Javascript libraries that extend the base features of the canvas tag, and provide other drawing capabilities. You may want to take a look at Processing, it looks quite impressive.
我遇到了同样的问题,我通过移动具有不同画布元素的另一种颜色的另一条线来做到这一点,因此它给出了动态改变其颜色的线的外观。
希望这能解决您的问题......:)
I was having the same problem, I did it by moving another line with another color of different canvas element, so it gives appearance of line changing its color dynamically.
Hope this solves your problem.... :)