Created
April 9, 2014 12:25
-
-
Save leaysgur/10263553 to your computer and use it in GitHub Desktop.
Use layout smart with spacing at compass sprite-map
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# For Compass v0.13 | |
# See also http://stackoverflow.com/questions/16793278/generate-sprites-with-compass-with-smart-layout-and-spacing | |
module Compass | |
module SassExtensions | |
module Sprites | |
module Layout | |
class Smart < SpriteLayout | |
def layout! | |
calculate_positions! | |
end | |
private | |
def calculate_positions! | |
fitter = ::Compass::SassExtensions::Sprites::RowFitter.new(@images) | |
current_y = 0 | |
width = 0 | |
height = 0 | |
last_row_spacing = 9999 | |
fitter.fit!.each do |row| | |
current_x = 0 | |
row_height = 0 | |
row.images.each_with_index do |image, index| | |
extra_y = [image.spacing - last_row_spacing, 0].max | |
if index > 0 | |
last_image = row.images[index-1] | |
current_x += [image.spacing, last_image.spacing].max | |
end | |
image.left = current_x | |
image.top = current_y + extra_y | |
current_x += image.width | |
width = [width, current_x].max | |
row_height = [row_height, extra_y + image.height+image.spacing].max | |
end | |
current_y += row.height | |
height = [height, current_y].max | |
last_row_spacing = row_height - row.height | |
current_y += last_row_spacing | |
end | |
@width = width | |
@height = height | |
end | |
end | |
end | |
module LayoutMethods | |
def compute_image_positions! | |
case layout | |
when SMART | |
@images, @width, @height = Layout::Smart.new(@images, @kwargs).properties | |
when DIAGONAL | |
require 'compass/sass_extensions/sprites/layout/diagonal' | |
@images, @width, @height = Layout::Diagonal.new(@images, @kwargs).properties | |
when HORIZONTAL | |
require 'compass/sass_extensions/sprites/layout/horizontal' | |
@images, @width, @height = Layout::Horizontal.new(@images, @kwargs).properties | |
else | |
require 'compass/sass_extensions/sprites/layout/vertical' | |
@images, @width, @height = Layout::Vertical.new(@images, @kwargs).properties | |
end | |
end | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment