简体   繁体   中英

Purescript MouseEvent to get x y of mouse

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM