Class: CssSpriter::Sprite

Inherits:
Object
  • Object
show all
Defined in:
lib/css-spriter/sprite.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSprite

Returns a new instance of Sprite.



5
6
7
8
# File 'lib/css-spriter/sprite.rb', line 5

def initialize
  @images = []
  @locations = {}
end

Instance Attribute Details

#imagesObject (readonly)

Returns the value of attribute images.



3
4
5
# File 'lib/css-spriter/sprite.rb', line 3

def images
  @images
end

#max_heightObject (readonly)

Returns the value of attribute max_height.



3
4
5
# File 'lib/css-spriter/sprite.rb', line 3

def max_height
  @max_height
end

Instance Method Details

#append(image) ⇒ Object



10
11
12
13
# File 'lib/css-spriter/sprite.rb', line 10

def append( image )
  @images << image
  @max_height = @images.map{ |i| i.height }.max
end

#append_file(filename) ⇒ Object



15
16
17
# File 'lib/css-spriter/sprite.rb', line 15

def append_file( filename )
  append( CssSpriter::Image.from_file( filename ))
end

#locationsObject



19
20
21
22
23
24
25
26
27
# File 'lib/css-spriter/sprite.rb', line 19

def locations
  @images.inject(0) do |x, image|
    @locations[image.name.to_sym] = { :x => -(x),
      :width => image.width,
      :height => image.height}
    image.width + x
  end
  @locations
end

#write(output_filename) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/css-spriter/sprite.rb', line 29

def write( output_filename )
  return if @images.empty?

  sprite_height = @images.map{ |i| i.height }.max
  sprite_width = @images.inject(0){|sum, image| sum + image.width }

  sprite = ChunkyPNG::Image.new(sprite_width, sprite_height)

  current_x = 0

  images.each do |image|
    sprite = sprite.replace(image, current_x, 0)
    current_x += image.width
  end

  sprite.save( output_filename, :best_compression )
end