如何使用 HTML5 jquery 在鼠标位置 canvas 上放置一个元素
How to drop an element on canvas at mouse position using HTML5 jquery
我是 HTML5 的新手,希望在松开鼠标按钮时将元素放在鼠标位置的 canvas/div 上。
我使用 appendChild 在目标上添加元素,它落在 DIV 的起始位置,是问题所在。
HTML5: dragover(), drop(): how get current x,y coordinates? 我找到了这个,但是它没有在特定位置添加元素的代码。
据我了解你的问题,你想确定发生放置事件时光标在元素上的相对位置。
如果是这样,光标在块元素上的位置可以通过以下方式计算:
+--- body --------
|
|
| +-- div ---
| |
| | x
x - is the cursor
- 根据
drop
事件确定光标位置
- 页面上
<div>
位置的确定(左上角)
cursorX
- divX
= X position
(相对于<div>
)
cursorY
- divY
= Y position
(相对于<div>
)
! 请注意,您 无法将 DOM 个节点 添加到 <canvas>
.
我是 HTML5 的新手,希望在松开鼠标按钮时将元素放在鼠标位置的 canvas/div 上。
我使用 appendChild 在目标上添加元素,它落在 DIV 的起始位置,是问题所在。
HTML5: dragover(), drop(): how get current x,y coordinates? 我找到了这个,但是它没有在特定位置添加元素的代码。
据我了解你的问题,你想确定发生放置事件时光标在元素上的相对位置。 如果是这样,光标在块元素上的位置可以通过以下方式计算:
+--- body -------- | | | +-- div --- | | | | x
x - is the cursor
- 根据
drop
事件确定光标位置 - 页面上
<div>
位置的确定(左上角) cursorX
-divX
=X position
(相对于<div>
)cursorY
-divY
=Y position
(相对于<div>
)
! 请注意,您 无法将 DOM 个节点 添加到 <canvas>
.