[SOLVED] Drawing on canvas works wrong

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
DGM
Prole
Posts: 16
Joined: Tue Mar 20, 2018 6:42 am

[SOLVED] Drawing on canvas works wrong

Post by DGM »

Hello again,

More trouble with canvases. Again, this is with v11.2 on 64-bit Windows 10.

I'm attempting to draw a white square with a smaller blue square inside of it to a canvas, then draw the result to the screen. Instead what I get is a solid blue square with no hint of white. Here's the full code of the test program:

Code: Select all

function love.load()
	love.window.setMode(1280, 720)
	love.graphics.setDefaultFilter("nearest", "nearest")

	canvas_1 = love.graphics.newCanvas(500, 500)
end

function love.update()
end

function love.draw()

	love.graphics.setCanvas(canvas_1)
	love.graphics.clear()
	love.graphics.setColor(255/255, 255/255, 255/255, 1)
	love.graphics.rectangle("fill", 0, 0, 499, 499)
	love.graphics.setColor(0/255, 0/255, 255/255, 1)
	love.graphics.rectangle("fill", 100, 100, 300, 300)
	love.graphics.setCanvas()

	love.graphics.clear()
	love.graphics.draw(canvas_1, 350, 100, 0, 1, 1, 0, 0, 0, 0)
end
Any ideas?
Last edited by DGM on Tue Jan 22, 2019 2:20 am, edited 1 time in total.
User avatar
pgimeno
Party member
Posts: 3656
Joined: Sun Oct 18, 2015 2:58 pm

Re: Drawing on canvas works wrong

Post by pgimeno »

You need to draw the canvas to the screen with white colour, otherwise you're tinting it :)

The last clear() isn't necessary, by the way.
DGM
Prole
Posts: 16
Joined: Tue Mar 20, 2018 6:42 am

Re: [SOLVED] Drawing on canvas works wrong

Post by DGM »

That fixed it, thanks.

But why is the last clear unnecessary? I get that I don't need it here since the image doesn't change, but aren't I normally supposed to clear the screen between frames?

EDIT: One more question. Does the tint apply to everything drawn with the graphics.draw() function? I just realized my forum account lets me edit the docs and I feel like adding a warning.
pedrosgali
Party member
Posts: 107
Joined: Wed Oct 15, 2014 5:00 pm
Location: Yorkshire, England

Re: [SOLVED] Drawing on canvas works wrong

Post by pedrosgali »

You get one free clear at the start of the draw loop as love clears the screen for you ready to draw the new frame. As for the tint, yes it applies to everything drawn. You can use this to your advantage though, I draw all my UI assets in greyscale and let the user pick the UI colour.

Code: Select all

if not wearTheseGlasses() then
  chewing_on_trashcan = true
end
DGM
Prole
Posts: 16
Joined: Tue Mar 20, 2018 6:42 am

Re: [SOLVED] Drawing on canvas works wrong

Post by DGM »

pedrosgali wrote: Tue Jan 22, 2019 6:38 am You get one free clear at the start of the draw loop as love clears the screen for you ready to draw the new frame.
But the free clear applies only to the screen and not to the canvases, correct? That makes sense.

As for the tint, yes it applies to everything drawn. You can use this to your advantage though, I draw all my UI assets in greyscale and let the user pick the UI colour.
Thanks. I've added a note to the draw() function doc.
User avatar
pgimeno
Party member
Posts: 3656
Joined: Sun Oct 18, 2015 2:58 pm

Re: [SOLVED] Drawing on canvas works wrong

Post by pgimeno »

Anything drawn with this function will be tinted according to the currently selected color. Set it to pure white to preserve the object's original colors.
Hm, that's only true when using the default shader, which is the one that tints it. When using a custom shader, it depends on whether the shader supports the colour parameter.
DGM
Prole
Posts: 16
Joined: Tue Mar 20, 2018 6:42 am

Re: [SOLVED] Drawing on canvas works wrong

Post by DGM »

Thanks, I've changed the text to mention that. Anything else I should know?
User avatar
pgimeno
Party member
Posts: 3656
Joined: Sun Oct 18, 2015 2:58 pm

Re: [SOLVED] Drawing on canvas works wrong

Post by pgimeno »

LGTM :)
Post Reply

Who is online

Users browsing this forum: Semrush [Bot] and 4 guests