Class: Sprites::Generator

Inherits:
Object
  • Object
show all
Includes:
Magick
Defined in:
lib/sprites/generator.rb

Instance Method Summary collapse

Constructor Details

#initialize(files_or_paths, output, root, options = {}) ⇒ Generator

parameters: files_or_path: can be Dir.glob-able paths or an array of filenames

ie: ['../images/icons/icon1_out.png', '../images/icons/icon1_over.png'] or '../images/icons/icon*.png'

output: filename where the generated sprite will be stored

ie: '../images/sprites/icons.png'

options: set a variety of options

- delimiter: characters by which multiple variations of images will be found
  ie: '_' for 'icon_hover.png'
  ie: '-' for 'icon-hover.png'
      if icon is the basename
- align: 
- sprite_location: will be available as variable in the liquid template, if this is not set, the template will use output as sprite_location
- tile: if set to ie. '100x100' it will center every image on a 100 by 100 tile
- template: Liquid template for each sprite, use this to build the css for your sprites
          these variables are available:
          - top: distance to top border of sprite in pixels 
          - left: distance to left border of sprite in pixels 
          - width: width of current image in pixels 
          - height: height of current image in pixels 
          - basename: filename or basename of variations
            ie: with variations: icon_out.png, icon_over.png  => icon
            ie: without variations: icon.png => icon.png
          - file_basename: always the name of the current image without extension
            ie: icon_over
          - filename: icon_over.png
          - full_filename: ../images/icons/icon_over.ong
          - variations: number of variations as number
          - variation: the current variation as zero based number
          - sprite_location: path to sprite


34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/sprites/generator.rb', line 34

def initialize(files_or_paths, output, root, options = {})
  @files = find_files(files_or_paths)
  return if @files.nil? || @files.empty?
  @root             = root || ''
  @output           = output
  @delimiter        = options[:delimiter] || '-'
  @distribution     = (options[:distribution] || :smart).to_sym
  @analyzed         = find_files_for_mode
  @template         = Liquid::Template.parse(options[:template] || '')
  @sprite_location  = options[:sprite_location] || @output
  @background       = options[:background] || '#FFFFFF00'
  @tile_size        = options[:tile]
  @alignment        = options[:alignment] ? Magick.const_get("#{camelize(options[:alignment])}Gravity") : Magick::CenterGravity
end

Instance Method Details

#createObject



49
50
51
52
53
54
55
56
57
# File 'lib/sprites/generator.rb', line 49

def create
  raise 'No files found.' if @files.nil? || @files.empty?
  build_tile
  destination = @root.nil? || @root.empty? ? @output : File.join(@root, @output)
  image, css  = build_sprite_and_css
  background  = @background
  image.write(destination){ self.background_color = background }
  css
end