fix: preserve viewport aspect ratio in fullscreen, fix touch pos, and inspect resolution#1106
fix: preserve viewport aspect ratio in fullscreen, fix touch pos, and inspect resolution#1106imaginarny wants to merge 8 commits into
Conversation
|
|
…and trigger resize on init
|
Hey, from looking at the changes I think they do, but I won't be able to test in next day or a two. As soon as I can, I'll send my findings here. |
|
Actually there are two things that I found. The first one is a bug, because removing it on init assumes the initial scale is always 1, which is not the case if the canvas is already resized using CSS (like in my game). The second thing is that the resize observer schedules the resize on the next input tick (similar to our discussion in the PR for releasing input on blur) which leaves stale state. After a canvas size change (triggered by changing CSS without resizing the window), the first mouse move can run Please bear in mind that I probably don't have the same understanding of the engine as you do, but from my experience it is good to be very careful about clearing up stale state. If you don't think this will have a huge performance impact I would keep it. |
It follows up on the changes from #1096 and its
canvasToViewport()calculation.Before that, touch coords were way off in fullscreen, when canvas is set to fixed size and letterbox disabled. Although, fullscreen is aspect-fit resized so letterboxing is still present there and
onResize()andupdateViewport()don't account for that.Mouse pos was correct because it scaled the viewport back to the correct ratio on each mouse move. After the mentioned PR, touch coords were only off because it calculated with as if stretched viewport instead (since mentioned functions skipped it).
The mouse correction code was removed and viewport code was updated instead to scale and recalculate the ratio in that case as well which happens only when resize happens. So both mouse and touch are calculated with the viewport already updated with the correct ratio and scale.
🔥 Unexpected bonus: fixed the inspect resolution along at last, since the correctly updated viewport now allows that
Summary
kaplay()opt setup wherewidth&heightare defined, andletterboxdisabled. Other options are basically unaffected/behave the same.There are many playtests for those, which start with
mouseXY. I added two more:mouseSizeFixed- with kaplay opts mentionedmouseSizeAligned- same, but when canvas is not in the original posWill mention how to test touch here as well, that is also included in those two files: