问题
I am new to purescript and was trying to make a 3d cube rotate on mouse events. But I cannot get x and y coordinates of mouse pointer on mouse move event. I am attaching my code below which have a event listener. Can somebody help me in getting x and y coordinates of mouse or can tell me better way to write event listener for mouse.
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
回答1:
You are using window.screenX
and window.screenY
, which is not what you want. You need to use screenX and screenY from the MouseEvent
type instead.
来源:https://stackoverflow.com/questions/48825081/purescript-mouseevent-to-get-x-y-of-mouse