Nesting canvases

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
User avatar
Cluke009
Prole
Posts: 15
Joined: Tue Aug 20, 2013 7:13 pm

Nesting canvases

Post by Cluke009 »

Is it possible to nest multiple canvases? I have been playing around with it and it doesnt seem to be the case.

I am trying to apply a uniform scaling to multiple canvases and putting them inside a container canvas seemed to make the most sense. I would only need to adjust scaling to the outer most canvas. Is there another way I should be doing this, am I totally misunderstanding canvases?

Code: Select all

function love.load()
    canvas = lg.newCanvas(320,240)
    lg.setCanvas(canvas)
        lg.clear(canvas)
        canvas2 = lg.newCanvas(320,240)

        lg.setCanvas(canvas2)
            lg.clear(canvas2)
            lg.rectangle('fill', 0,0, 32, 32)
        lg.setCanvas()
        lg.draw(canvas2, 0, 0, 0)

        lg.rectangle('fill', 128,128, 32, 32)

    lg.setCanvas()
end

function love.draw()
    love.graphics.draw(canvas, 0, 0, 0, 2, 2)
end
User avatar
micha
Inner party member
Posts: 1083
Joined: Wed Sep 26, 2012 5:13 pm

Re: Nesting canvases

Post by micha »

You can draw one canvas onto another one. This should do, what you want to achieve:

Code: Select all

function love.load()
  canvas = lg.newCanvas(320,240)
  canvas2 = lg.newCanvas(320,240)

  lg.setCanvas(canvas2)
  lg.clear(canvas2)
  lg.rectangle('fill', 0,0, 32, 32)

  lg.setCanvas(canvas)
  lg.draw(canvas2, 0, 0, 0)
end

function love.draw()
  love.graphics.draw(canvas, 0, 0, 0, 2, 2)
end
But canvases are not nested. You maybe misunderstood the concept here. A canvas is just an extra space on that you can draw things. You can of course organize your canvases in a way, that they have a hierarchy and one is always drawn onto the other one. But you don't have to.

For your special case, by the way, I suggest using love.graphics.scale. That way, everything will be scaled by factor two and you don't really need this extra canvas.
Post Reply

Who is online

Users browsing this forum: No registered users and 7 guests