Skip to content

Instantly share code, notes, and snippets.

@avnik
Created November 16, 2016 20:44
Show Gist options
  • Save avnik/633c1fc49a19b0406dca51bfcf6ed8fd to your computer and use it in GitHub Desktop.
Save avnik/633c1fc49a19b0406dca51bfcf6ed8fd to your computer and use it in GitHub Desktop.
let get_mouse_pos (canvas:Html.canvasElement) event =
let rect = canvas##getBoundingClientRect() in
let top = rect##.top
and bottom = rect##.bottom
and right = rect##.right
and left = rect##.left
and clientX = event##.clientX
and clientY = event##.clientY
and width = canvas##.width
and height = canvas##.height in
let x = (clientX - left)/(right - left)*width
and y = (clientY - top)/(bottom - top)*height
in x, y
;;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment