Drawing one canvas on top of another?
Posted: Sun Sep 19, 2021 4:30 am
Right now, I'm working on an effect where the player leaves a trail as they walk. I want the trail to be persistent so I don't clear it every frame, but I also want it to have some transparency and the problem is that the trail keeps drawing on top of itself every frame until it's no longer transparent.
I had a (maybe weird) solution, where I'd have a second canvas that tracks where the player's already walked, and multiply the trail in the first canvas by the value (0 or 1) in the second canvas to determine whether the trail needs to be drawn or not.
For example, I want to add to the trail at the player's current position: But I don't want to add to it if they've already been there, so I keep track of where they've been in black: I then multiply the trail by the value of the path canvas to mask out any areas they've already been: My idea was to draw the trail to a temporary canvas, save the second canvas as an ImageData, then draw the ImageData to the temporary canvas using love.graphics.newImage like so:
I know the path canvas is updating correctly, because if I draw it directly, it looks correct. However nothing shows if I try converting it to an ImageData and drawing it onto another canvas like so:
Do you all know is there a way to draw a Canvas on top of another Canvas? Or if not, is there a better way to do what I'm thinking?
I had a (maybe weird) solution, where I'd have a second canvas that tracks where the player's already walked, and multiply the trail in the first canvas by the value (0 or 1) in the second canvas to determine whether the trail needs to be drawn or not.
For example, I want to add to the trail at the player's current position: But I don't want to add to it if they've already been there, so I keep track of where they've been in black: I then multiply the trail by the value of the path canvas to mask out any areas they've already been: My idea was to draw the trail to a temporary canvas, save the second canvas as an ImageData, then draw the ImageData to the temporary canvas using love.graphics.newImage like so:
Code: Select all
love.graphics.setCanvas(tempcanvas)
pathdata = pathcanvas:newImageData( ) --convert the path canvas into ImageData
love.graphics.setColor(1, 1, 1, .5)
love.graphics.circle('fill', x, y, 10) --draw a transparent circle to the tempcanvas
love.graphics.setBlendMode("multiply","premultiplied") --switch to a multiply mode
love.graphics.newImage(delaydata) --draw the path on top of the tempcanvas
Code: Select all
love.graphics.setCanvas(tempcanvas)
pathdata = pathcanvas:newImageData( ) --convert the path canvas into ImageData
love.graphics.newImage(delaydata) --draw the path on top of the tempcanvas