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.

Constant Summary collapse

OPACITY =

RMagick 2.13.4 replaced ‘MaxRGB’ with ‘QuantumRange’.

Magick.const_defined?(:MaxRGB) ? Magick::MaxRGB : Magick::QuantumRange

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


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

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.



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

def filename
  @filename
end

#heightObject (readonly)

Public: Gets the the height of the combined image.



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

def height
  @height
end

#nameObject (readonly)

Public: Gets the name of the sprite.



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

def name
  @name
end

#pathObject (readonly)

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



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

def path
  @path
end

#spacingObject (readonly)

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



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

def spacing
  @spacing
end

#verticalObject (readonly) Also known as: vertical?

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



31
32
33
# File 'lib/spriteful/sprite.rb', line 31

def vertical
  @vertical
end

#widthObject (readonly)

Public: Gets the the width of the combined image.



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

def width
  @width
end

Instance Method Details

#blobObject

Public: Gets the binary contents generated by the sprite.

Returns a String.



91
92
93
# File 'lib/spriteful/sprite.rb', line 91

def blob
  File.binread(tmp_path)
end

#cleanupObject

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

Returns nothing.



99
100
101
# File 'lib/spriteful/sprite.rb', line 99

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.



71
72
73
74
75
76
77
78
79
# File 'lib/spriteful/sprite.rb', line 71

def combine!
  combined = Magick::Image.new(width, height)
  combined.opacity = OPACITY
  @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.



109
110
111
112
# File 'lib/spriteful/sprite.rb', line 109

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.



84
85
86
# File 'lib/spriteful/sprite.rb', line 84

def tmp_path
  @tmpfile.path
end