Skip to content

Instantly share code, notes, and snippets.

@Achie72
Created August 10, 2025 08:40
Show Gist options
  • Select an option

  • Save Achie72/88b0ff2917c077e2638ade1da606ad5e to your computer and use it in GitHub Desktop.

Select an option

Save Achie72/88b0ff2917c077e2638ade1da606ad5e to your computer and use it in GitHub Desktop.
Löve 2D canvas scaling
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