-
Notifications
You must be signed in to change notification settings - Fork 454
/
Rakefile
36 lines (28 loc) · 1.22 KB
/
Rakefile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
desc 'recreate sprite sheets'
task 'resprite' do
require 'sprite_factory'
SpriteFactory.run!('images/sprites', :layout => :packed, :output_style => 'images/sprites.js', :margin => 5, :nocomments => true) do |images|
SpriteHelper.javascript_style("SPRITES", images)
end
SpriteFactory.run!('images/background', :layout => :vertical, :output_style => 'images/background.js', :margin => 5, :nocomments => true) do |images|
SpriteHelper.javascript_style("BACKGROUND", images)
end
end
#------------------------------------------------------------------------------
module SpriteHelper
# slightly unusual use of sprite-factory to generate a javascript object structure instead of CSS attributes...
def self.javascript_style(variable, images)
maxname = images.keys.inject(0) {|n,key| [n,key.length].max }
rules = []
images.each do |name, i|
name = name.upcase
whitespace = ' '*(maxname-name.length)
x = '%4d' % i[:cssx]
y = '%4d' % i[:cssy]
w = '%4d' % i[:cssw]
h = '%4d' % i[:cssh]
rules << " #{name}: #{whitespace}{ x: #{x}, y: #{y}, w: #{w}, h: #{h} }"
end
"var #{variable} = {\n#{rules.join(",\n")}\n};"
end
end