Created
August 10, 2025 08:40
-
-
Save Achie72/88b0ff2917c077e2638ade1da606ad5e to your computer and use it in GitHub Desktop.
Löve 2D canvas scaling
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function love.draw() | |
| local width, height = love.graphics.getDimensions() | |
| local gameScale = canvasWidth / canvasHeight | |
| local windowScale = width / height | |
| local sw, sh = width/canvasWidth, height/canvasHeight | |
| if windowScale > gameScale then | |
| drawScale = sh | |
| else | |
| drawScale = sw | |
| end | |
| local hSpace = width - (canvasWidth * drawScale) | |
| local vSpace = height - (canvasHeight * drawScale) | |
| local drawOffsetHorizontal = hSpace / 2 | |
| local drawOffsetVertical = vSpace / 2 | |
| love.graphics.setCanvas(gameCanvas) | |
| love.graphics.clear(0,0,0,1) | |
| for _,particle in ipairs(particle_collection) do | |
| particle:draw() | |
| end | |
| -- try to draw player | |
| player:draw() | |
| draw_enemies() | |
| love.graphics.setCanvas() | |
| love.graphics.draw(gameCanvas, drawOffsetHorizontal, drawOffsetVertical, 0, drawScale, drawScale) | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment