拖动鼠标时更改 HTML5 Canvas 上的光标
我使用 Canvas Tag 制作了画笔类型的应用程序。我希望当鼠标位于画布上时光标发生变化,
<canvas id="draw" style="cursor: url(image/pencil.cur)">
我已经完成了这一点,但我不知道在拖动鼠标绘制图像时如何更改光标
I have made a paint brush type of application using the Canvas Tag . I wanted that when the mouse is on the canvas the Cursor Changes ,
<canvas id="draw" style="cursor: url(image/pencil.cur)">
I have accomplished this but i cant figure out how shall i change the cursor while I Drag the mouse for drawing the image
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
当鼠标按钮在元素上按下时,使用
:active
CSS 伪类更改光标:工作示例:
Use the
:active
CSS pseudo-class to change the cursor when the mouse button is down over the element:Working example: http://jsfiddle.net/nXv63/
对于任何仍在寻找此 webkit 错误解决方案的人:https://bugs.webkit。 org/show_bug.cgi?id=105355,这就是我所做的。
我将此属性添加到 body 元素中,以使页面中的所有文本都不可选择,仅此而已。
问题是,如果您确实需要一个元素可供选择,则必须更改其 CSS 才能实现。
For any one still looking for a solution to this webkit bug: https://bugs.webkit.org/show_bug.cgi?id=105355, this is what I did.
I added this property to the body element to make all text unselectable in my page and that's it.
The problem is that if you really need an element to be selectable, you will have to change its CSS to make it so.
我可以为此想到两种方法,其中一种可以实现您想要的:
使用一些 JavaScript 来更改光标,与此 jQuery 片段相同:
如果您在按下鼠标按钮时使用它,并在释放鼠标按钮时恢复原始状态,我想这将达到您想要的效果。
在画布本身上绘制“光标”,并让它跟踪鼠标在画布上的位置。这样,您可以绘制 Photoshop 风格的圆圈(等)来指示绘图的位置。我不确定这种方法的性能如何(我没有尝试过),但我想它应该没问题。只需使用在画布上移动鼠标时触发的事件,这可能是您已经用来跟踪油漆应该放置的位置的事件。
Two approaches I can think of for this, one of which might accomplish what you want:
Use some JavaScript to change the cursor, along the same lines as this jQuery fragment:
If you were to use that in the event for when the mouse button is pressed, and restore the original when it is released, I imagine that would have the exact effect you desire.
Draw the "cursor" on the canvas itself, and have it track the mouse position on the canvas. This way, you could draw a Photoshop-style circle (etc) to indicate where the drawing is going to take place. I'm not sure how performance might be with this approach (I've not tried it), but I imagine it ought to be fine. Just use the event which is fired on mouse move for the canvas, which is presumably what you're already using to track where the paint ought to be placed.
在拖动操作启用时向画布添加一个“拖动”类(并在 mouseUp 上将其删除)
然后添加以下样式:
Add a "dragging" class to the canvas while the dragging action is on (and remove it on mouseUp)
Then add this style:
在这里工作正常:
http://jsfiddle.net/Mutant_Tractor/DMBWC/1/
Works fine here:
http://jsfiddle.net/Mutant_Tractor/DMBWC/1/