拖动鼠标时更改 HTML5 Canvas 上的光标

发布于 2024-10-17 01:05:42 字数 183 浏览 2 评论 0原文

我使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

夕色琉璃 2024-10-24 01:05:42

当鼠标按钮在元素上按下时,使用 :active CSS 伪类更改光标:

#draw:active { 
    cursor: url(image/pencil.cur);
}

工作示例:

Use the :active CSS pseudo-class to change the cursor when the mouse button is down over the element:

#draw:active { 
    cursor: url(image/pencil.cur);
}

Working example: http://jsfiddle.net/nXv63/

情丝乱 2024-10-24 01:05:42

对于任何仍在寻找此 webkit 错误解决方案的人:https://bugs.webkit。 org/show_bug.cgi?id=105355,这就是我所做的。

我将此属性添加到 body 元素中,以使页面中的所有文本都不可选择,仅此而已。

body {
    -webkit-touch-callout: none;

    -webkit-user-select: none;

    -moz-user-select: none;

    user-select: none;
}

问题是,如果您确实需要一个元素可供选择,则必须更改其 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.

body {
    -webkit-touch-callout: none;

    -webkit-user-select: none;

    -moz-user-select: none;

    user-select: none;
}

The problem is that if you really need an element to be selectable, you will have to change its CSS to make it so.

我可以为此想到两种方法,其中一种可以实现您想要的:

  1. 使用一些 JavaScript 来更改光标,与此 jQuery 片段相同:

    $('canvas#draw').css('cursor', 'url(image/another-cursor.cur)');
    

    如果您在按下鼠标按钮时使用它,并在释放鼠标按钮时恢复原始状态,我想这将达到您想要的效果。

  2. 在画布本身上绘制“光标”,并让它跟踪鼠标在画布上的位置。这样,您可以绘制 Photoshop 风格的圆圈(等)来指示绘图的位置。我不确定这种方法的性能如何(我没有尝试过),但我想它应该没问题。只需使用在画布上移动鼠标时触发的事件,这可能是您已经用来跟踪油漆应该放置的位置的事件。

Two approaches I can think of for this, one of which might accomplish what you want:

  1. Use some JavaScript to change the cursor, along the same lines as this jQuery fragment:

    $('canvas#draw').css('cursor', 'url(image/another-cursor.cur)');
    

    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.

  2. 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.

等数载,海棠开 2024-10-24 01:05:42

在拖动操作启用时向画布添加一个“拖动”类(并在 mouseUp 上将其删除)

然后添加以下样式:

canvas {cursor: default;}
canvas.dragging {cursor: crosshair;}

Add a "dragging" class to the canvas while the dragging action is on (and remove it on mouseUp)

Then add this style:

canvas {cursor: default;}
canvas.dragging {cursor: crosshair;}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文