Page 1 of 1

Nesting canvases

Posted: Mon May 12, 2014 7:49 pm
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

Re: Nesting canvases

Posted: Mon May 12, 2014 8:59 pm
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.