Edit: Ninja'd by MrFariator but I think this post may still be useful.
A shader will work quite well; but if you really must avoid it, one alternative is a stencil.
Code: Select all
local totalTime = 2
local objects = {}
local speed = 600
local effect = false
local canvas = love.graphics.newCanvas()
local T = 0
local function newObject()
return {love.math.random(0, love.graphics.getWidth()-1),
love.math.random(0, love.graphics.getHeight()-1)}
end
for i = 1, 10 do
objects[i] = newObject()
end
function love.keypressed(k)
if k == "escape" then return love.event.quit() end
if k == "space" then T = totalTime; effect = true end
end
local function circle()
local hw, hh = love.graphics.getWidth()/2, love.graphics.getHeight()/2
if T > 0 then
love.graphics.circle("fill", hw, hh, T/totalTime*math.sqrt(hw*hw + hh*hh))
end
end
function love.update(dt)
if T > 0 then
T = T - dt
end
for i = 1, #objects do
local obj = objects[i]
obj[1] = obj[1] + (math.random() - 0.5) * dt * speed
obj[2] = obj[2] + (math.random() - 0.5) * dt * speed
end
end
function love.draw()
love.graphics.setCanvas(canvas)
love.graphics.clear(.5, .5, .5)
for i = 1, #objects do
local obj = objects[i]
love.graphics.rectangle("fill", obj[1], obj[2], 8, 8)
end
love.graphics.setCanvas()
if effect then
love.graphics.stencil(circle)
love.graphics.setStencilTest("gequal", 1)
end
love.graphics.draw(canvas)
love.graphics.print(T)
end
Edit: Another alternative is to use two canvases. This is better in that any antialiasing that the circle gets, goes to the final result; however I don't think there's any antialiasing for filled circles anyway. In the example above, modify the initialization to create two canvases instead of one:
Code: Select all
local canvasRender = love.graphics.newCanvas()
local canvasCrop = love.graphics.newCanvas()
then modify love.draw as follows:
Code: Select all
function love.draw()
love.graphics.setCanvas(canvasRender)
love.graphics.clear(.5, .5, .5)
for i = 1, #objects do
local obj = objects[i]
love.graphics.rectangle("fill", obj[1], obj[2], 8, 8)
end
love.graphics.setCanvas()
if effect then
love.graphics.setCanvas(canvasCrop)
love.graphics.clear(0,0,0,0)
circle()
love.graphics.setCanvas(canvasRender)
love.graphics.setBlendMode("multiply", "premultiplied")
love.graphics.draw(canvasCrop)
love.graphics.setCanvas()
love.graphics.setBlendMode("alpha", "alphamultiply")
end
love.graphics.draw(canvasRender)
love.graphics.print(T)
end