Class: DynamicSprites::Generator

Inherits:
Object
  • Object
show all
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

Constructor Details

#initialize(filename, path, layout, geometry) ⇒ 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, 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

Instance Method Details

#mixin_callObject

Returns a call to sass mixin function with default attributes



39
40
41
42
43
44
45
46
47
48
49
50
# 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!Object

Main method for sprites generation



26
27
28
29
30
31
32
33
34
35
# 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