3 ms·
The reminds me a piece of CSS I wrote over the weekend for a hobby project. There is a game canvas where the playfield is 16:9 and you always see the whole fiel
by zamadatix 4y ago
The reminds me a piece of CSS I wrote over the weekend for a hobby project. There is a game canvas where the playfield is 16:9 and you always see the whole field (For simplicities' sake think of Pong where it doesn't make sense to display only part of the field and changing the field dimensions to fit the screen dimensions alters gameplay too much). So the problem I came to was "I don't know what the dimensions of the screen are but I know I want this canvas to be 16:9 and fill the screen". Now there are two possibilities at this point 1) just do it in JS 2) find out what wizardry I need to learn today. I went for route #2 for the fun of it.
I quickly found and then ran into issues with aspect-ratio. Either the canvas could get smooshed at certain sizes if I set width: 100vw and max-height: 100vh (despite setting the aspect-ratio) or it wouldn't do anything but be the default 300x150 canvas if not specified. There may well be a way to fix this but I got bored and just as I was switching to write the JS I had couldn't help but think "this feels doable with calc() and min()/max()". So I popped open an Excel sheet, created some random widths and heights, and started messing around for formulas that worked but didn't rely on variables or explicit conditionals. What I came to was:
--Canvas-Width: min(100vw, calc(100vh * 16 / 9));
--Canvas-Height: min(100vh, calc(100vw * 9 / 16));
Which is basically using min() to be the "if we are vertically/horizontally constrained" conditional. At this point since I had the height and knew it was of 100vh I also had a lazy way out on vertically centering with another calc().
It was definitely fun to tinker to a solution but if anyone actually knows CSS and how to make the built in aspect-ratio work the same way on canvas I wouldn't mind hearing it :).
- amenghra 4y agoToday, you can use aspect-ratio (https://developer.mozilla.org/en-US/docs/Web/CSS/aspect-ratio https://developer.mozilla.org/en-US/docs/Web/CSS/aspect-rati...). I had to once do something similar and I ran into 4 cases (which can be collapsed into 2 cases): add vertical borders if the window is very wide or a little narrow. Add horizontal borders if the window is a little wide or very narrow.
- zamadatix 4y agoPer the above I ran into issues with Canvas and aspect-ratio, demo here https://jsfiddle.net/6m0v9n3d/1/ https://jsfiddle.net/6m0v9n3d/1/ (try vertically smashing it like it was being played on an ultrawide and it won't hold the 16:9 aspect ratio anymore). Trying to size vertically results in the opposite issue. Trying not to size it to the viewport or parent results in the canvas staying the default size.
- amenghra 4y agoYou are setting `width: 100vw;` which is why it doesn't work. Try something like this: https://jsfiddle.net/pgaz7cmx/ https://jsfiddle.net/pgaz7cmx/, which sets both, width and max-width.
- zamadatix 4y agoHuzzah! I don't think I ever would have thought to set both width and max-width (to > 100 vw no less!). Makes sense now that I see it though, thanks!
- amenghra 4y agomax-width gets set to vh, not vw. I do dislike this solution because the ratio is encoded twice. I’m sure there are other ways to achieve the same, some are perhaps more elegant?