Module:Sprite
Documentation for this module may be created at Module:Sprite/doc
local p = {}
function p.getSpritePosition( pos, sheetsize, size )
local spritesPerRow = sheetsize / size
local row = math.floor(pos / spritesPerRow)
local col = pos % spritesPerRow
local x = col * size
local y = row * size
return { ['x'] = x, ['y'] = y }
end
function p.get(f)
local args = f
if f == mw.getCurrentFrame() then
args = require( 'Module:ProcessArgs' ).merge( true )
else
f = mw.getCurrentFrame()
end
local url = args.url
local default = {
scale = 1,
sheetsize = 256,
size = 16,
pos = 1,
align = 'text-top'
}
local scale = args.scale or default.scale
local sheetsize = args.sheetsize or default.sheetsize
local size = args.size or default.size
local pos = args.pos or default.pos
local align = args.align or default.align
local spritePosition = p.getSpritePosition( pos, sheetsize, size )
local fileUrl = f:expandTemplate{title = 'FileUrl', args = {[1] = url} }
local styles = {
"vertical-align: " .. align,
"background-position: " .. -spritePosition.x * scale .. 'px ' .. -spritePosition.y * scale .. 'px',
"background-size: " .. sheetsize * scale,
"display: inline-block",
"width: " .. size * scale .. "px",
"height: " .. size * scale .. "px",
-- "transform: scale(" .. scale .. ")"
}
local sprite = mw.html.create( 'span' ):addClass( 'sprite' )
sprite:addClass( 'pixelated' )
sprite:cssText( table.concat( styles, ";" ) )
sprite:attr( 'data-bgimage', fileUrl )
local root = mw.html.create( 'span' )
root:node( sprite )
return tostring( root )
end
return p