Question about canvas behavior

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
ZappD0S
Prole
Posts: 3
Joined: Tue Aug 18, 2015 1:46 pm

Question about canvas behavior

Post by ZappD0S »

Hi everyone,
I recently started to learn LÖVE and I stumbled on a problem. Here's the code i wrote:

Code: Select all

function love.load()
    canvas = love.graphics.newCanvas()
    love.graphics.setCanvas(canvas)
    canvas:clear()
    love.graphics.setColor(85, 34, 159, 255)
    love.graphics.rectangle('fill', 0, 0, 400, 400)
end

function love.draw()
    love.graphics.draw(canvas)
end
This code produces a black screen and the rectangle isn't there, but if I add the function setCanvas() at the end of the load() function it seems to work.So why do I need to set the render target back to the screen before the load() function ends?

Thanks in andvance ^^
User avatar
Jasoco
Inner party member
Posts: 3728
Joined: Mon Jun 22, 2009 9:35 am
Location: Pennsylvania, USA
Contact:

Re: Question about canvas behavior

Post by Jasoco »

Because you need to set the render target back to the window before drawing to the window.

Set the target to the canvas
Draw to the canvas
Set target back to screen
Draw canvas to screen

Quite simple really.
User avatar
Evine
Citizen
Posts: 72
Joined: Wed May 28, 2014 11:46 pm

Re: Question about canvas behavior

Post by Evine »

Code: Select all

function love.load()
    canvas = love.graphics.newCanvas(800,600) -- I would recommend setting a known canvas size.
    love.graphics.setCanvas(canvas)
    canvas:clear()
    love.graphics.setColor(85, 34, 159, 255)
    love.graphics.rectangle('fill', 0, 0, 400, 400)
end

function love.draw()
    love.graphics.setCanvas() -- Set the draw target back to the screen, You can do it in love.load as well.
    love.graphics,setColor() -- Set the drawing color back to white
    love.graphics.draw(canvas) 
end
The Problem is that you never set the active canvas back to the screen so you end up drawing the canvas to the canvas and nothing to the screen.
Artal, A .PSD loader: https://github.com/EvineDev/Artal
ZappD0S
Prole
Posts: 3
Joined: Tue Aug 18, 2015 1:46 pm

Re: Question about canvas behavior

Post by ZappD0S »

Sorry, I forgot to specify that this code works only if setCanvas() is in the load() function. If I put in draw() it doesn't work as well.
User avatar
slime
Solid Snayke
Posts: 3181
Joined: Mon Aug 23, 2010 6:45 am
Location: Nova Scotia, Canada
Contact:

Re: Question about canvas behavior

Post by slime »

The default [wiki]love.run[/wiki] causes the currently active canvas (which normally ends up being the main screen) to be cleared directly before love.draw is called. If you keep your own canvas active until love.draw, then it will be cleared instead of the screen.
ZappD0S
Prole
Posts: 3
Joined: Tue Aug 18, 2015 1:46 pm

Re: Question about canvas behavior

Post by ZappD0S »

Thank you! I figured that there was a similar reason but since i'm new to this engine i wasn't sure and I preferred to ask.
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 6 guests