Class: Spriteful::Sprite

Inherits:
Object
  • Object
show all
Defined in:
lib/spriteful/sprite.rb

Overview

Public: the ‘Sprite’ class is responsible for combining a directory of images into a single one, and providing the required information about the related images.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source_dir, destination, options = {}) ⇒ Sprite

Public: Initialize a Sprite.

source_dir - the source directory where the sprite images are located. destination - the destination directory where the sprite should be saved. options - additional Hash of options.

:horizontal - flag to turn the sprite into the horizontal
              orientation.
:spacing - spacing in pixels that should be placed between
           the images in the sprite. Defaults to 0.
:optimize - flag to optimize SVG to be inlined


42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/spriteful/sprite.rb', line 42

def initialize(source_dir, destination, options = {})
  source_pattern = File.join(source_dir, '*{.png,.svg}')
  sources = Dir[source_pattern].sort

  if sources.size == 0
    raise EmptySourceError, "No image sources found at '#{source_dir}'."
  end

  @vertical    = !options[:horizontal]
  @spacing     = options[:spacing] || 0
  @optimize    = options[:optimize]

  @name     = options[:name] || File.basename(source_dir)
  @filename = "#{name}.png"
  @path     = File.expand_path(File.join(destination, @filename))
  @list     = Magick::ImageList.new(*sources) { self.background_color = 'none' }
  @images   = initialize_images(@list)

  @height, @width = detect_dimensions
  @tmpfile = Tempfile.new(filename)
end

Instance Attribute Details

#filenameObject (readonly)

Public: Gets the filename of the sprite.



16
17
18
# File 'lib/spriteful/sprite.rb', line 16

def filename
  @filename
end

#heightObject (readonly)

Public: Gets the the height of the combined image.



25
26
27
# File 'lib/spriteful/sprite.rb', line 25

def height
  @height
end

#nameObject (readonly)

Public: Gets the name of the sprite.



13
14
15
# File 'lib/spriteful/sprite.rb', line 13

def name
  @name
end

#pathObject (readonly)

Public: returns the path where the sprite will be saved.



10
11
12
# File 'lib/spriteful/sprite.rb', line 10

def path
  @path
end

#spacingObject (readonly)

Public: Gets the the spacing between the images in the sprite.



19
20
21
# File 'lib/spriteful/sprite.rb', line 19

def spacing
  @spacing
end

#verticalObject (readonly) Also known as: vertical?

Public: Gets the flag to check if the sprite is vertical or not.



28
29
30
# File 'lib/spriteful/sprite.rb', line 28

def vertical
  @vertical
end

#widthObject (readonly)

Public: Gets the the width of the combined image.



22
23
24
# File 'lib/spriteful/sprite.rb', line 22

def width
  @width
end

Instance Method Details

#blobObject

Public: Gets the binary contents generated by the sprite.

Returns a String.



88
89
90
# File 'lib/spriteful/sprite.rb', line 88

def blob
  File.binread(tmp_path)
end

#cleanupObject

Public: Execute any post generation code to free any resources used by the sprite.

Returns nothing.



96
97
98
# File 'lib/spriteful/sprite.rb', line 96

def cleanup
  @tmpfile.unlink
end

#combine!Object

Public: combines the source images into a single one, storing the combined image into the sprite path.

Returns nothing.



68
69
70
71
72
73
74
75
76
# File 'lib/spriteful/sprite.rb', line 68

def combine!
  combined = Magick::Image.new(width, height)
  combined.opacity = Magick::MaxRGB
  @images.each do |image|
    combined.composite!(image.source, image.left.abs, image.top.abs, Magick::SrcOverCompositeOp)
  end
  @tmpfile.write(combined.to_blob { self.format = 'png' })
  @tmpfile.close
end

#each_imageObject Also known as: images

Public: exposes the source images found in the ‘source’ directory.

Yields an ‘Image’ object on each interation.

Returns an ‘Enumerator’ if no block is given.



106
107
108
109
# File 'lib/spriteful/sprite.rb', line 106

def each_image
  return to_enum(__method__) unless block_given?
  @images.each { |image| yield image }
end

#tmp_pathObject

Public: Gets the temporary path where the sprite is stored.

Returns a String.



81
82
83
# File 'lib/spriteful/sprite.rb', line 81

def tmp_path
  @tmpfile.path
end