Module:Sprite: Difference between revisions
imported>Ttv pedro270707 mNo edit summary |
imported>Ttv pedro270707 mNo edit summary |
||
Line 1: | Line 1: | ||
local p = {} | 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) | function p.get(f) | ||
local args = f | local args = f | ||
Line 17: | Line 26: | ||
align = 'text-top' | 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 fileUrl = f:expandTemplate{title = 'FileUrl', args = {[1] = url} } | ||
Line 22: | Line 37: | ||
local sprite = mw.html.create( 'span' ):addClass( 'sprite' ) | local sprite = mw.html.create( 'span' ):addClass( 'sprite' ) | ||
sprite:addClass( 'pixelated' ) | sprite:addClass( 'pixelated' ) | ||
sprite:cssText( 'transform: scale(' . | sprite:cssText( 'transform: scale(' .. scale .. ')' ) | ||
sprite:css( 'vertical-align', align ) | |||
sprite:css( 'background-position', spritePosition.x .. 'px ' .. spritePosition.y .. 'px' ) | |||
sprite:attr( 'data-bgimage', fileUrl ) | sprite:attr( 'data-bgimage', fileUrl ) | ||
Revision as of 00:52, 8 March 2023
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 sprite = mw.html.create( 'span' ):addClass( 'sprite' ) sprite:addClass( 'pixelated' ) sprite:cssText( 'transform: scale(' .. scale .. ')' ) sprite:css( 'vertical-align', align ) sprite:css( 'background-position', spritePosition.x .. 'px ' .. spritePosition.y .. 'px' ) sprite:attr( 'data-bgimage', fileUrl ) local root = mw.html.create( 'span' ) root:node( sprite ) return tostring( root ) end return p