I create games using HTML5/CSS/JS, but I am struggling to figure out how to make them scale to different screen resolutions. It seems like a simple task, but I can't seem to grasp it.
SOLVED
var RATIO = 480 / 800; // Initial ratio.
function resize() {
var DEVICE_WIDTH = window.innerWidth,
DEVICE_HEIGHT = window.innerHeight,
ratio = DEVICE_WIDTH / DEVICE_HEIGHT,
scale = 1;
// Adjusting scale based on window size changes
if (ratio > RATIO) {
scale = DEVICE_HEIGHT / 800; // Divide by original viewport height
} else {
scale = DEVICE_WIDTH / 480; // Divide by original viewport width
}
}
// Now you just need to apply this scale factor in your draw method for every element (x and y coordinates, width and height)
I hope this solution helps someone who was in the same boat as me :)