Page 1 of 1

Question about canvas behavior

Posted: Tue Aug 18, 2015 2:07 pm
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 ^^

Re: Question about canvas behavior

Posted: Tue Aug 18, 2015 2:43 pm
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.

Re: Question about canvas behavior

Posted: Tue Aug 18, 2015 2:52 pm
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.

Re: Question about canvas behavior

Posted: Tue Aug 18, 2015 3:13 pm
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.

Re: Question about canvas behavior

Posted: Tue Aug 18, 2015 3:17 pm
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.

Re: Question about canvas behavior

Posted: Tue Aug 18, 2015 3:25 pm
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.