Introducing Dragonfly Cloud! Learn More

Question: How do you use a canvas in Love2D?


In Love2D, a canvas is an off-screen render target that you can draw to, instead of drawing directly to the screen. This can be useful for a variety of purposes, such as creating complex visual effects, performing off-screen rendering for performance benefits, or creating reusable graphical assets dynamically.

Here's how you can create and use a canvas in Love2D:

  1. Creating a Canvas:

    You need to create a new canvas object with Optionally, you can specify the width and height, otherwise it uses the window size by default.

local myCanvas =, 600) -- Creates a canvas of 800x600 pixels
  1. Drawing to the Canvas:

    Before drawing anything to the canvas, you need to set it as the active render target using -- Now any drawing operation will render to myCanvas instead of the screen.
  1. Drawing on the Screen:

    After you've drawn on the canvas, you need to switch back to the main screen by calling with no arguments. -- Switches back to the main screen

Then, you can draw the canvas onto the screen just like any other drawable object., 0, 0) -- Draws the canvas at the top-left corner of the screen
  1. Clearing the Canvas:

    If you need to clear the canvas, perhaps to redraw it each frame, you can use while the canvas is the active render target. -- Clears the canvas -- Perform your rendering operations here
  1. Using Multiple Canvases:

    You can switch between multiple canvases if you need to create more complex scenes or effects.

local canvas1 = local canvas2 = -- Draw something on canvas1 -- Draw something else on canvas2 -- Don't forget to reset the rendering target to the main screen -- Now you can draw both canvases on the screen, 0, 0), 0, 0)

Keep in mind that all the usual Love2D drawing functions can be used when a canvas is the active render target, but they will affect the canvas, not the screen. Canvases are powerful tools in Love2D for managing complex rendering tasks.

Was this content helpful?

White Paper

Free System Design on AWS E-Book

Download this early release of O'Reilly's latest cloud infrastructure e-book: System Design on AWS.

Free System Design on AWS E-Book

Start building today 

Dragonfly is fully compatible with the Redis ecosystem and requires no code changes to implement.