Jose Jimenez
Jose Jimenez

Reputation: 48

Canvas problem, cannot get it to draw a rectangle

I have the following code that draws a blue rectangle and a red line over it over a black background.

function love.load()
    love.window.setMode(300,200,{fullscreen=false})
end

function love.draw()
    love.graphics.setColor(0, 0, 255, 255)
    love.graphics.rectangle("fill", 20, 20, 100, 20)
    love.graphics.setColor(255, 0, 0, 255)
    love.graphics.line(70, 30, 120, 30)
end

Not Using a Canvas

I tried to move the drawing to a canvas so my code changed to

local canvas

function love.load()
    love.window.setMode(300,200,{fullscreen=false})
    canvas = love.graphics.newCanvas(300, 200)
end

function love.draw()
    love.graphics.setCanvas(canvas)
    love.graphics.setColor(0, 0, 255, 255)
    love.graphics.rectangle("fill", 20, 20, 100, 20)
    love.graphics.setColor(255, 0, 0, 255)
    love.graphics.line(70, 30, 120, 30)
    love.graphics.setCanvas()
    love.graphics.draw(canvas)
end

Using a Canvas

But the second version only draws a red line on a black background.

Am I doing something wrong?

I am on Windows 10 Enterprise 64 bits, Lua 5.3.5, Löve 11.2.0.Mysterious Mysteries.

Upvotes: 1

Views: 736

Answers (1)

PaulR
PaulR

Reputation: 726

You need to put the line:

love.graphics.setColor(1, 1, 1, 1)

… before the love.graphics.draw(canvas) line to reset the colours the canvas can be painted with. With this you can filter the colours/change the alpha channel of the whole canvas.

Btw, colour values are in now the range 0 -> 1 since v11.

There is a code example in the docs with a note about this here...

https://love2d.org/wiki/Canvas

Upvotes: 2

Related Questions