PuzzleScript icon indicating copy to clipboard operation
PuzzleScript copied to clipboard

Remove margin from body on mobile

Open sftrabbit opened this issue 3 years ago • 1 comments

By default, most browsers apply a default margin of 8px to the body element. On mobile, PuzzleScript applies the following style the body element:

        var style = {
            height: "100%",
            overflow: "hidden",
            position: "fixed",
            width: "100%"
        }

The position: "fixed" creates a new document flow root, which means that any children with position: absolute are going to be positioned with the 8px offset, including the game canvas.

This can be seen by simulating a mobile device (or using a real mobile device) and inspecting the body and game canvas elements (note the orange margin at the top and left):

Screenshot 2022-12-22 at 23 13 42

Here's another example where I've set the viewport size so that the game should fit perfectly on the screen, but you can see everything is offset slightly:

Screenshot 2022-12-22 at 23 38 42

This caused two issues:

  • The game canvas is slightly outside the viewport.
  • Touch coordinates computed by relMouseCoords in inputoutput.js were offset by 8px. This was most noticeable when using the PuzzleScript Plus fork with support for touch controls.

The fix is just to set the body margin to 0 and make sure the mobile pull-out menu is still positioned correctly. After the fix, the above game fits on screen as expected:

Screenshot 2022-12-22 at 23 40 34

sftrabbit avatar Dec 22 '22 23:12 sftrabbit

I do also have code that I think makes relMouseCoords more robust, even if the margin remains, but a) it's not necessary if we just remove the margin, and b) I don't have 100% confidence that it works in all cases and on all devices.

sftrabbit avatar Dec 22 '22 23:12 sftrabbit