Page 1 of 1

[solved] Canvases

Posted: Fri Nov 02, 2018 10:47 pm
by Purple Fedora
If I want to draw something to a canvas, do I need to put the draw functions to draw something on to the canvas in the love.draw function? And how do I draw something to a canvas?

Re: Canvases

Posted: Sat Nov 03, 2018 12:23 am
by MrFariator

Code: Select all

-- create a canvas somewhere outside love.draw
local myCanvas = love.graphics..newCanvas ( width, height, {format="normal"}, 1 )

-- set the canvas inside love.draw, and clear its contents if need be
love.graphics.setCanvas ( myCanvas )
love.graphics.clear()

-- draw things to canvas with love.graphics.draw, love.graphics.rectangle, or other methods here

-- unset the canvas
love.graphics.setCanvas()

-- draw the canvas to screen
love.graphics.draw ( myCanvas, x, y, rotation, scaleX, scaleY )
When you draw stuff between the "set" and "unset" steps, everything you draw ends up on the canvas.

Re: Canvases

Posted: Sat Nov 03, 2018 6:47 am
by zorg
To actually answer part of your question, unlike drawing to the screen (more or less) directly, you can render stuff to a canvas outside of love.draw... but unless you're just pre-rendering some static stuff, i'd (erase, and) draw to canvases in love.draw too.

Re: Canvases

Posted: Sat Nov 03, 2018 5:13 pm
by Purple Fedora
MrFariator wrote: Sat Nov 03, 2018 12:23 am

Code: Select all

-- create a canvas somewhere outside love.draw
local myCanvas = love.graphics..newCanvas ( width, height, {format="normal"}, 1 )

-- set the canvas inside love.draw, and clear its contents if need be
love.graphics.setCanvas ( myCanvas )
love.graphics.clear()

-- draw things to canvas with love.graphics.draw, love.graphics.rectangle, or other methods here

-- unset the canvas
love.graphics.setCanvas()

-- draw the canvas to screen
love.graphics.draw ( myCanvas, x, y, rotation, scaleX, scaleY )
When you draw stuff between the "set" and "unset" steps, everything you draw ends up on the canvas.
I have seen your code but why do things get discoloured?

Re: Canvases

Posted: Sat Nov 03, 2018 6:27 pm
by zorg
Because you forgot to setColor(1,1,1) before drawing the canvas to the screen; the graphics state remembers the last set color, and if you're not careful, it can tint things you don't want tinted.