Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ebiten.CursorPosition() does not always return (0,0) on mobile, when the game is compiled to WASM and runs in browser. #3087

Closed
2 of 11 tasks
Duxo opened this issue Sep 8, 2024 · 5 comments

Comments

@Duxo
Copy link

Duxo commented Sep 8, 2024

Ebitengine Version

v2.7.8

Operating System

  • Windows
  • macOS
  • Linux
  • FreeBSD
  • OpenBSD
  • Android
  • iOS
  • Nintendo Switch
  • PlayStation 5
  • Xbox
  • Web Browsers

Go Version (go version)

1.23.1

What steps will reproduce the problem?

Game: (built with env GOOS=js GOARCH=wasm go build -o test.wasm)

package main

import (
	"fmt"
	"log"

	"github.com/hajimehoshi/ebiten/v2"
)

type Game struct{}

func (g *Game) Layout(w int, h int) (int, int) {
	return w, h
}

func (g *Game) Update() error {
	fmt.Println(ebiten.CursorPosition())
	return nil
}

func (g *Game) Draw(screen *ebiten.Image) {}

func main() {
	err := ebiten.RunGame(&Game{})
	if err != nil {
		log.Fatal(err)
	}
}


HTML:

<!DOCTYPE html>

<html>

<head>
    <style>
        #mobile-input {
            position: absolute;
            height: 100%;
            width: 100%;
        }
    </style>
</head>

<body>
    <div id="mobile-input"></div>
    <script src="wasm/wasm_exec.js"></script>
    <script>
        const mobileInput = document.getElementById('mobile-input')
        const go = new Go();
        WebAssembly.instantiateStreaming(fetch("wasm/test.wasm"), go.importObject).then(result => {
            go.run(result.instance)
            mobileInput.addEventListener('touchstart', (event) => { mobileInput.style.zIndex = '-1' })
        })
    </script>
</body>

</html>

What is the expected result?

I expected that ebiten.CursorPosition() will always return (0,0) on mobile.

What happens instead?

After the mobile screen is touched, the ebiten.CursorPosition() returns the coordinates of the touch.

Anything else you feel useful to add?

The problem is present in Firefox and Chrome.

@hajimehoshi
Copy link
Owner

hajimehoshi commented Sep 8, 2024

CursorPosition can return a mouse cursor position if you use a mouse for Android. There is no assumption that CursorPosition returns (0, 0) by default.

@hajimehoshi
Copy link
Owner

For touches, use the APIs for touches. https://pkg.go.dev/github.com/hajimehoshi/ebiten/v2#TouchID

@hajimehoshi hajimehoshi closed this as not planned Won't fix, can't repro, duplicate, stale Sep 8, 2024
@hajimehoshi hajimehoshi added wontfix and removed bug labels Sep 8, 2024
@Duxo
Copy link
Author

Duxo commented Sep 9, 2024

The problem is that the CursorPosition() returned a different value than (0,0) on Android without the mouse. Also, the documentation says: "CursorPosition always returns (0, 0) on mobiles."

Sorry for sounding assertive; it is the most compact way I managed to write it.

@hajimehoshi hajimehoshi reopened this Sep 9, 2024
@hajimehoshi hajimehoshi added bug and removed wontfix labels Sep 9, 2024
@hajimehoshi
Copy link
Owner

hajimehoshi commented Sep 9, 2024

Ah, yes, that seems an issue.

Also I was misunderstanding that Ebitengine detected a mouse for Android apps but was actually not (yet). I'm sorry, I'll take a look.

@hajimehoshi
Copy link
Owner

The confusing point is

  • CursorPosition should return (0, 0) on Android apps.
  • CursorPosition should return a mouse cursor position on Android browsers.

I'll update the comment.

@hajimehoshi hajimehoshi added this to the v2.8.0 milestone Sep 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants