Davidobot / love.js

LÖVE ported to the web using Emscripten, updated to the latest Emscripten and LÖVE (v11.5)
MIT License
605 stars 27 forks source link

Mouse coordinates are wrong when requestFullScreen called #50

Open misch27 opened 2 years ago

misch27 commented 2 years ago

The canvas coordinates in requestFullScreen are the same as the canvas before full screen mode, although they need to be adjusted.

Found a similar problem here https://stackoverflow.com/questions/43853119/javascript-wrong-mouse-position-when-drawing-on-canvas/43873988

luttje commented 1 week ago

For future reference:

I fixed this problem by calling requestFullScreen on a new div I wrapped around the game canvas.

Step-by-step

  1. Open index.html of your built game
  2. Find the canvas https://github.com/Davidobot/love.js/blob/c4f04e185033a7c9fbefa9be3bec88c41a90421b/src/release/index.html#L17
  3. Wrap a new div around it and give it the id 'game':
    <div id="game">
        <canvas id="canvas" oncontextmenu="event.preventDefault()"></canvas>
    </div>
  4. Modify the goFullScreen function: https://github.com/Davidobot/love.js/blob/c4f04e185033a7c9fbefa9be3bec88c41a90421b/src/release/index.html#L22-23 to:
    function goFullScreen() {
        var canvas = document.getElementById("game");
  5. The above will work, but it won't make the canvas full-size. To do that you can add a new class for #canvas in the <style> tag at the top:
    #canvas {
        max-height: 100%;
        max-width: 100%;
    }

A downside of this is that the canvas wont scale, thus your game wont know that the window changed size. It'll just remain the aspect ratio it was at boot. See this answer for a solution that doesn't have that problem: https://github.com/Davidobot/love.js/issues/88#issuecomment-2005117581 (in combination with full-screen mode: love.window.setMode(600, 920, { fullscreen = true }))