canvas?

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
pielago
Party member
Posts: 142
Joined: Fri Jun 14, 2013 10:41 am

canvas?

Post by pielago »

Okay wiki example..
whats?
I want to do one and i can't! I follow the wiki and still can't idk why?
so the canvas you must draw it in love.load() ? not in love.draw() ?

Code: Select all

function love.load()
    canvas = love.graphics.newCanvas() --i get this to create a canvas--
    love.graphics.setCanvas(canvas)--i get this as well lol part of top 
    love.graphics.setColor(230,240,120) 
    love.graphics.rectangle('fill',0,0,100,100)-- why is this rectangle here??? 

    love.graphics.setCanvas()--is this the closing  canvas???? or what is it????
end

function love.draw()  
    love.graphics.setColor(255,255,255)
    love.graphics.draw(canvas, 200,100, 0, .5,.5) --------where is this one coming from??? and why????
end
kind of get how to start making one but if i want to make 2??? and how to draw to each one? of them???
can someone explain me this process but with an easy example????
got lost with the draw(canvas,200,100,0,.5,.5)???????
User avatar
Plu
Inner party member
Posts: 722
Joined: Fri Mar 15, 2013 9:36 pm

Re: canvas?

Post by Plu »

Basically what is happening is the following:

Code: Select all

function love.load()
canvas = love.graphics.newCanvas() -- Make a new canvas. This is basically just a blank image that you can draw on. 
love.graphics.setCanvas(canvas) -- make the canvas active; so that any love.graphics call will draw to the newly created blank image instead of to the screen
love.graphics.setColor(230,240,120)  -- set the drawing color
love.graphics.rectangle('fill',0,0,100,100)-- draw a rectangle. because we've activated the canvas, this draw call is now made to the canvas, so instead of a blank image it now contains a colorful rectangle; it's part of the example
love.graphics.setCanvas() -- this will tell love that we no longer want to draw to the canvas; any draw call you make now will draw to the screen again
end

function love.draw()  
    love.graphics.setColor(255,255,255) -- set the drawing color back to white
    love.graphics.draw(canvas, 200,100, 0, .5,.5) -- draw the canvas we created (which contains a colorful rectangle, since we drew that to the canvas) to the screen, but scaled down 50%
end
This is what is happening. A Canvas is a blank image you can draw to, and it's draw state will be remembered. However, a canvas by default is not shown on the screen, unless you paint it to the screen in love.draw by using love.graphics.draw( canvas ).

If you want to have multiple canvas, you have to call love.graphics.newCanvas() multiple times, store each of them in a variable, and then use love.graphics.setCanvas( var ) to draw something on one. Once you're prepped all of them, you can simply start painting them to the screen by using love.graphics.draw( whicheverCanvasYouWant ).
User avatar
micha
Inner party member
Posts: 1083
Joined: Wed Sep 26, 2012 5:13 pm

Re: canvas?

Post by micha »

A canvas is an off-screen render target. You can imagine a canvas as an extra screen. Instead of drawing onto the main window, you can draw anything there. And then, the canvas itself is like an image and can be drawn.

Image your screen as a piece of paper. By creating a new canvas, you put another piece of paper next to it. For every draw that you do (image, rectangle, circle,...) you can choose to either draw it on the screen (first piece of paper) or on the canvas (second piece of paper). The player will only see the screen.
With this line:

Code: Select all

love.graphics.setCanvas(canvas)
You select, where you draw. If you put a canvas as argument, then you will draw on this canvas. If you put nothing as argument, then you draw onto the screen.

By doing this:

Code: Select all

love.graphics.draw(canvas, 200,100, 0, .5,.5)
You regard the canvas as an image itself and draw it to the currently selected target (which is the screen in this case).

What do you need a canvas for? If you happen to draw something complicated, that does not change over time, then a canvas can increase performance. Let's say you have a complicated background, made of 1000 single images. Instead of drawing these 1000 images each frame, you can simply draw them onto a canvas once and then each frame just draw the canvas.

Edit: Oh, Plu was faster.
pielago
Party member
Posts: 142
Joined: Fri Jun 14, 2013 10:41 am

Re: canvas?

Post by pielago »

well i want to learn about canvas so i can move on to lighting! i hear canvas are good for it ..
but if there is another way to do it? ill be willing to learn it i want to make a few canvas some visible others not
also are canvas name layers? or layers its something different???? btw thanks for replying so fast :)
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 144 guests