diff --git a/internal/input/input_js.go b/internal/input/input_js.go index 986da047d..1b31f98fb 100644 --- a/internal/input/input_js.go +++ b/internal/input/input_js.go @@ -227,23 +227,23 @@ func OnKeyUp(e *js.Object) { theInput.keyUp(code) } -func OnMouseDown(e *js.Object, scale float64, left, top int) { +func OnMouseDown(e *js.Object) { e.Call("preventDefault") button := e.Get("button").Int() theInput.mouseDown(button) - setMouseCursorFromEvent(e, scale, left, top) + setMouseCursorFromEvent(e) } -func OnMouseUp(e *js.Object, scale float64, left, top int) { +func OnMouseUp(e *js.Object) { e.Call("preventDefault") button := e.Get("button").Int() theInput.mouseUp(button) - setMouseCursorFromEvent(e, scale, left, top) + setMouseCursorFromEvent(e) } -func OnMouseMove(e *js.Object, scale float64, left, top int) { +func OnMouseMove(e *js.Object) { e.Call("preventDefault") - setMouseCursorFromEvent(e, scale, left, top) + setMouseCursorFromEvent(e) } func OnTouchStart(e *js.Object, scale float64, left, top int) { @@ -261,11 +261,9 @@ func OnTouchMove(e *js.Object, scale float64, left, top int) { theInput.updateTouches(touchEventToTouches(e, scale, left, top)) } -func setMouseCursorFromEvent(e *js.Object, scale float64, left, top int) { +func setMouseCursorFromEvent(e *js.Object) { x, y := e.Get("clientX").Int(), e.Get("clientY").Int() - x -= left - y -= top - theInput.setMouseCursor(int(float64(x)/scale), int(float64(y)/scale)) + theInput.setMouseCursor(x, y) } func touchEventToTouches(e *js.Object, scale float64, left, top int) []*Touch { diff --git a/internal/ui/ui_js.go b/internal/ui/ui_js.go index 80e0049a9..d5b70b7d7 100644 --- a/internal/ui/ui_js.go +++ b/internal/ui/ui_js.go @@ -78,7 +78,12 @@ func ScreenPadding() (x0, y0, x1, y1 float64) { } func AdjustedCursorPosition() (x, y int) { - return input.Get().CursorPosition() + x, y = input.Get().CursorPosition() + rect := canvas.Call("getBoundingClientRect") + x -= rect.Get("left").Int() + y -= rect.Get("top").Int() + scale := currentUI.getScale() + return int(float64(x) / scale), int(float64(y) / scale) } func IsCursorVisible() bool { @@ -253,18 +258,9 @@ func initialize() error { canvas.Call("addEventListener", "keyup", input.OnKeyUp) // Mouse - canvas.Call("addEventListener", "mousedown", func(e *js.Object) { - rect := canvas.Call("getBoundingClientRect") - input.OnMouseDown(e, currentUI.getScale(), rect.Get("left").Int(), rect.Get("top").Int()) - }) - canvas.Call("addEventListener", "mouseup", func(e *js.Object) { - rect := canvas.Call("getBoundingClientRect") - input.OnMouseUp(e, currentUI.getScale(), rect.Get("left").Int(), rect.Get("top").Int()) - }) - canvas.Call("addEventListener", "mousemove", func(e *js.Object) { - rect := canvas.Call("getBoundingClientRect") - input.OnMouseMove(e, currentUI.getScale(), rect.Get("left").Int(), rect.Get("top").Int()) - }) + canvas.Call("addEventListener", "mousedown", input.OnMouseDown) + canvas.Call("addEventListener", "mouseup", input.OnMouseUp) + canvas.Call("addEventListener", "mousemove", input.OnMouseMove) canvas.Call("addEventListener", "contextmenu", func(e *js.Object) { e.Call("preventDefault") })