Purescript MouseEvent 获取鼠标的 x y
Purescript MouseEvent to get x y of mouse
我是 purescript 的新手,正在尝试使 3d 立方体根据鼠标事件旋转。但是我无法在鼠标移动事件中获得鼠标指针的 x 和 y 坐标。我在下面附上我的代码,它有一个事件监听器。有人可以帮我获取鼠标的 x 和 y 坐标,或者可以告诉我为鼠标编写事件侦听器的更好方法。
node <- querySelector "#canvas"
for_ node $ addEventListener "mousedown" $ void do
modifyRef drag \d -> true
xz <- getPageX
log (show xz)
x <- liftEff $ Window.screenX =<< window
y <- liftEff $ Window.screenX =<< window
modifyRef old_x \ox -> toNumber x
modifyRef old_y \oy -> toNumber y
log (show y)
for_ node $ addEventListener "mouseup" $ void do
modifyRef drag \d -> false
for_ node $ addEventListener "mouseout" $ void do
modifyRef drag \d -> false
for_ node $ addEventListener "mousemove" $ void do
--log "Mouse Moved!"
x <- liftEff $ Window.screenX =<< window
y <- liftEff $ Window.screenX =<< window
ox <- readRef old_x
oy <- readRef old_y
modifyRef dX \dx -> (toNumber x - ox) * 2.0 * pi / 600.0
modifyRef dY \dy -> (toNumber y - oy) * 2.0 * pi / 650.0
dx <- readRef dX
dy <- readRef dY
dg <- readRef drag
if dg == true then do
modifyRef alpha \al -> al + dx
modifyRef beta \bt -> bt + dy
modifyRef old_x \ox -> toNumber x
modifyRef old_y \oy -> toNumber y
--modifyRef gamma \ga -> ga + 3.0 * pi/180.0
else
pure unit
您正在使用 window.screenX
和 window.screenY
,而 MouseEvent
类型中的 is not what you want. You need to use screenX
and screenY
。
我是 purescript 的新手,正在尝试使 3d 立方体根据鼠标事件旋转。但是我无法在鼠标移动事件中获得鼠标指针的 x 和 y 坐标。我在下面附上我的代码,它有一个事件监听器。有人可以帮我获取鼠标的 x 和 y 坐标,或者可以告诉我为鼠标编写事件侦听器的更好方法。
node <- querySelector "#canvas"
for_ node $ addEventListener "mousedown" $ void do
modifyRef drag \d -> true
xz <- getPageX
log (show xz)
x <- liftEff $ Window.screenX =<< window
y <- liftEff $ Window.screenX =<< window
modifyRef old_x \ox -> toNumber x
modifyRef old_y \oy -> toNumber y
log (show y)
for_ node $ addEventListener "mouseup" $ void do
modifyRef drag \d -> false
for_ node $ addEventListener "mouseout" $ void do
modifyRef drag \d -> false
for_ node $ addEventListener "mousemove" $ void do
--log "Mouse Moved!"
x <- liftEff $ Window.screenX =<< window
y <- liftEff $ Window.screenX =<< window
ox <- readRef old_x
oy <- readRef old_y
modifyRef dX \dx -> (toNumber x - ox) * 2.0 * pi / 600.0
modifyRef dY \dy -> (toNumber y - oy) * 2.0 * pi / 650.0
dx <- readRef dX
dy <- readRef dY
dg <- readRef drag
if dg == true then do
modifyRef alpha \al -> al + dx
modifyRef beta \bt -> bt + dy
modifyRef old_x \ox -> toNumber x
modifyRef old_y \oy -> toNumber y
--modifyRef gamma \ga -> ga + 3.0 * pi/180.0
else
pure unit
您正在使用 window.screenX
和 window.screenY
,而 MouseEvent
类型中的 is not what you want. You need to use screenX
and screenY
。