module SpriteAnimation
Constants
- VERSION
Public Instance Methods
animate_sprite(image_src, frame_count, params = {})
click to toggle source
Returns an image_tag inside a div, an populates the image with data so it can be animated via javascript Params:
image_src
-
a sprite sheet source, just as you would pass to image_tag
frame_count
-
a string representing the number of frames for the sheet
params
-
a hash containing two optional parameters for the animation
scale
-
a decimal number that will multiply the frame size. Default: 1
orientation
-
a symbol representing the orientation of your sheet.
Can be: :vertical or :horizontal. If not given, it will try to guess.
# File lib/sprite_animation/sprite_animation_helper.rb, line 15 def animate_sprite(image_src, frame_count, params = {}) scale = params[:scale] || 1 img_width, img_height = ImageSize.size(image_src).map { |d| d*scale } orientation = params[:orientation] || guess_orientation(img_width, img_height) frame = send(orientation,*[img_width,img_height,frame_count]) image = image_tag(image_url(image_src), class: "animated", frameCount: frame_count, frameLength: frame[:length], frameSide: frame[:margin], style: frame[:style]) content_tag(:div, image, style: animated_div_style(frame[:width],frame[:height])) end
Private Instance Methods
animated_div_style(frame_width, frame_height)
click to toggle source
# File lib/sprite_animation/sprite_animation_helper.rb, line 38 def animated_div_style(frame_width, frame_height) "width: #{frame_width}px; height: #{frame_height}px; display: block; overflow: hidden" end
guess_orientation(img_width, img_height)
click to toggle source
# File lib/sprite_animation/sprite_animation_helper.rb, line 69 def guess_orientation(img_width, img_height) img_height > img_width ? :vertical : :horizontal end
horizontal(img_width, img_height, frame_count)
click to toggle source
# File lib/sprite_animation/sprite_animation_helper.rb, line 45 def horizontal(img_width, img_height, frame_count) frame_width = img_width / frame_count.to_i frame_height = img_height { width: frame_width, height: frame_height, margin: "left", length: frame_width, style: "height: " + img_height.to_s + "px" } end
vertical(img_width, img_height, frame_count)
click to toggle source
# File lib/sprite_animation/sprite_animation_helper.rb, line 57 def vertical(img_width, img_height, frame_count) frame_width = img_width frame_height = img_height / frame_count.to_i { width: frame_width, height: frame_height, margin: "top", length: frame_height, style: "width: " + img_width.to_s + "px" } end