Cache static LÖVE layers with an offscreen canvas
Bake unchanging geometry into a LÖVE canvas once, then draw it as one image each frame to cut draw calls.
18 Oct 2026, 08:57 UTC

The problem: redrawing static geometry every frame
You build a tiled background or a static UI panel in love.draw. The tiles never move, yet LÖVE reissues every rectangle, sprite, and line to the GPU each frame. On a desktop build the cost may be invisible, but on mobile or web targets those hundreds of draw calls can drop the frame rate well before any game logic runs.
The useful takeaway is simple: if the geometry does not change, draw it once into an offscreen canvas, then draw that canvas as a single image every frame.
What an offscreen canvas does in LÖVE
love.graphics.newCanvas creates a render target: a texture in video memory that LÖVE can draw into just like the screen. Calling love.graphics.setCanvas(canvas) switches the active target, so subsequent drawing commands go to the texture instead of the window. Calling love.graphics.setCanvas() with no arguments returns rendering to the main display. love.graphics.clear always affects whichever target is currently active.
Canvas contents persist until you clear or redraw them, which makes a canvas suitable for retaining a static layer across frames without reissuing the source draw commands.
Worked example: cache a tile layer
This example assumes LÖVE 11.x. It creates an 800 by 600 canvas, draws a grid of tiles into it during love.load, and draws the cached result in love.draw.
local canvasWidth, canvasHeight = 800, 600
local backgroundCanvas
function love.load()
love.graphics.setDefaultFilter("nearest", "nearest")
backgroundCanvas = love.graphics.newCanvas(canvasWidth, canvasHeight)
love.graphics.setCanvas(backgroundCanvas)
love.graphics.clear(0.1, 0.1, 0.15, 1)
for x = 0, canvasWidth - 1, 32 do
for y = 0, canvasHeight - 1, 32 do
love.graphics.setColor(0.2, 0.3, 0.4, 1)
love.graphics.rectangle("fill", x, y, 30, 30)
end
end
love.graphics.setCanvas()
end
function love.draw()
love.graphics.setColor(1, 1, 1, 1)
love.graphics.draw(backgroundCanvas, 0, 0)
end
Place this in your project's main.lua or a required module; no special permissions are needed. The placeholders canvasWidth and canvasHeight should match the area you want to cache.
After love.graphics.setCanvas() runs with no arguments, rendering has returned to the window. You can verify this by checking that love.graphics.getCanvas() returns nil. If you forget the reset, later drawing commands go to the offscreen texture and appear to vanish from the screen.
Trade-offs and limits
- Video memory. Each canvas consumes video memory sized by its width and height. Creating many large canvases at runtime can exhaust VRAM or cause stalls on constrained devices.
- Clearing every frame defeats the purpose. If you clear and redraw the canvas every frame, you pay the canvas switch cost plus the original draw calls. Only clear when the layer actually changes.
- Driver limits. On some older drivers or LÖVE versions, canvas dimensions must be powers of two. Prefer modest, consistent sizes unless you verify your target hardware supports arbitrary dimensions.
- Static content only. If the layer changes every frame, a canvas adds overhead without reducing draw calls.
Check the result
- Run the project and confirm the cached tiles appear on screen.
- Add
print(love.graphics.getCanvas())after the reset inlove.load; expectnil. - Monitor frame time as you add more static layers. If frame time rises sharply, reduce canvas dimensions or reuse one canvas for multiple layers instead of allocating many.
Start with one unchanging background or UI layer. Cache it once, draw it once per frame, and verify the render target resets to nil. If drawing ever disappears after you introduce a canvas, the first thing to check is whether you left setCanvas pointing at the offscreen texture.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.