Class: DynamicSprites::Generator
- Inherits:
-
Object
- Object
- DynamicSprites::Generator
- Defined in:
- lib/dynamic-sprites/generator.rb
Overview
Generates sprites
Constant Summary collapse
- VALID_EXTENSIONS =
Array of file extensions used for creating sprites.
%w(.png .jpg .jpeg .gif .ico)
Instance Method Summary collapse
-
#initialize(filename, path, layout) ⇒ Generator
constructor
Initializer.
-
#mixin_call ⇒ Object
Returns a call to sass mixin function with default attributes.
-
#run! ⇒ Object
Main method for sprites generation.
Constructor Details
#initialize(filename, path, layout) ⇒ Generator
Initializer
filename - Pathname where generated file should be placed to. path - Pathname of directory containing source images. layout - sprite layout name as a String
16 17 18 19 20 21 22 |
# File 'lib/dynamic-sprites/generator.rb', line 16 def initialize(filename, path, layout) @filename = filename @layout = layout @files = Dir.glob(File.join(path, '*')).sort.select { |e| VALID_EXTENSIONS.include?(File.extname(e)) } @images = load(@files) @canvas = canvas end |
Instance Method Details
#mixin_call ⇒ Object
Returns a call to sass mixin function with default attributes
43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/dynamic-sprites/generator.rb', line 43 def mixin_call call = "dynamic-sprite" call << "-horizontal" if @layout == "horizontal" arguments = [ @filename, @files.map{ |f| File.basename(f) }, "#{100 / (@files.size - 1)}%", "100%", "#{100 * @images.first[:height] / @images.first[:width]}%" ] call << "(#{arguments.join(', ')})" end |
#run! ⇒ Object
Main method for sprites generation
26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/dynamic-sprites/generator.rb', line 26 def run! @canvas.opacity = Magick::MaxRGB offset_x = 0 offset_y = 0 @images.each do |image| @canvas.composite!(image[:image], offset_x, offset_y, Magick::SrcOverCompositeOp) if @layout == 'horizontal' offset_x += image[:width] else offset_y += image[:height] end end @canvas.write(@filename) end |