For a title screen, I want to draw some text on the screen, such that the fill pattern on the letters is an image rather than a solid color. Think of something like this:
My first thought was to use stencils for it, but that didn't work out well. After trying a few different things, I settled on drawing the background to one canvas, drawing the text to another, and using mapPixel.
Code: Select all
function MaskImageWithText(image, font, text, x, y)
local texture_x = x or 0
local texture_y = x or 0
local text_drawable = love.graphics.newText(font, text)
local text_width = text_drawable:getWidth()
local text_height = text_drawable:getHeight()
local text_canvas = love.graphics.newCanvas(text_width, text_height)
local texture_canvas = love.graphics.newCanvas(text_width, text_height)
local texture_quad = love.graphics.newQuad(texture_x, texture_y, text_width, text_height, image:getDimensions())
love.graphics.setCanvas(texture_canvas)
love.graphics.setColor(1.0, 1.0, 1.0, 1.0)
love.graphics.draw(image, texture_quad, 0, 0)
love.graphics.setCanvas()
love.graphics.setCanvas(text_canvas)
love.graphics.setColor(1.0, 1.0, 1.0, 0.0)
love.graphics.rectangle('fill', 0, 0, text_width, text_height)
love.graphics.setColor(0.0, 0.0, 0.0, 1.0)
love.graphics.draw(text_drawable, 0, 0)
love.graphics.setCanvas()
local texture_image_data = texture_canvas:newImageData()
local text_image_data = text_canvas:newImageData()
text_image_data:mapPixel(
function(x,y,r,g,b,a)
if a ~= 0 then
return texture_image_data:getPixel(x,y)
else
return r,g,b,a
end
end
)
return love.graphics.newImage(text_image_data)
end
function love.load()
textureImage = love.graphics.newImage('graphics/propaganda_goo.jpg')
textureImage:setWrap('repeat', 'repeat')
texturedText = MaskImageWithText(
textureImage,
love.graphics.newFont('fonts/Turtles.ttf', 128),
'TURTLES'
)
end
function love.draw()
tX = math.floor(WINDOW_WIDTH/2 - texturedText:getWidth()/2)
tY = math.floor(WINDOW_HEIGHT/2 - texturedText:getHeight()/2)
love.graphics.setColor(1.0, 1.0, 1.0, 1.0)
love.graphics.draw(texturedText, tX, tY)
end
I've attached the .love archive for anyone who wants to play with it.