class DynamicSprites::Generator
Generates sprites
Constants
- VALID_EXTENSIONS
Array of file extensions used for creating sprites.
Public Class Methods
new(filename, path, layout, geometry)
click to toggle source
Initializer
filename - Pathname where generated file should be placed to. path - Pathname of directory containing source images. layout - sprite layout name as a String
# File lib/dynamic-sprites/generator.rb, line 16 def initialize(filename, path, layout, geometry) @filename = filename @layout = layout @files = Dir.glob(File.join(path, '*')).sort.select { |e| VALID_EXTENSIONS.include?(File.extname(e)) } @images = Magick::ImageList.new(*@files) @geometry = Magick::Geometry.from_s(geometry) rescue default_geometry end
Public Instance Methods
mixin_call()
click to toggle source
Returns a call to sass mixin function with default attributes
# File lib/dynamic-sprites/generator.rb, line 39 def mixin_call call = "img.sprite\n @include dynamic-sprite" call << "-horizontal" if @layout == "horizontal" arguments = [ @filename.inspect, classess, "#{100.0 / (@images.length - 1)}%", "100%", "#{100.0 * @geometry.height / @geometry.width}%" ] call << "(#{arguments.join(', ')})" end
run!()
click to toggle source
Main method for sprites generation
# File lib/dynamic-sprites/generator.rb, line 26 def run! # @canvas.opacity = Magick::MaxRGB geometry = @geometry tile = grid @images.montage do |c| c.geometry = geometry if geometry c.background_color = 'transparent' c.tile = tile end.write(@filename) end
Private Instance Methods
classess()
click to toggle source
Changes array of files into String containing classes derived from file names.
# File lib/dynamic-sprites/generator.rb, line 64 def classess '(' + @files.map{ |f| File.basename(f).split('.').first.inspect }.join(', ') + ')' end
default_geometry()
click to toggle source
# File lib/dynamic-sprites/generator.rb, line 68 def default_geometry Magick::Geometry.new(@images.first.columns, @images.first.rows) end
grid()
click to toggle source
# File lib/dynamic-sprites/generator.rb, line 54 def grid if @layout == 'horizontal' Magick::Geometry.new(@images.length, 1) else Magick::Geometry.new(1, @images.length) end end