Skip to content

Instantly share code, notes, and snippets.

@zeroeth
Created May 6, 2018 05:38
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zeroeth/49fbc296671efadcf1cfe6c35a49a17a to your computer and use it in GitHub Desktop.
Save zeroeth/49fbc296671efadcf1cfe6c35a49a17a to your computer and use it in GitHub Desktop.
create an array of a gif animation with random start offsets.
# cat jelly spreader
require 'chunky_png'
tile_count_width = 20
tile_count_height = 10
tile_count = tile_count_width + tile_count_height
# load frames
print "Loading frames..."
frame_image_names = Dir.glob("rolls/*.png").sort
frame_images = []
frame_image_names.each do |name|
frame_images.push ChunkyPNG::Image.from_file name
end
print "done!\n"
frame_count = frame_images.length
puts "#{frame_count} frames!"
# create matrix of starting offsets (and flips)
print "Randomizing rolls..."
frame_infos = Array.new(tile_count_width) { Array.new(tile_count_height) { Hash.new } }
frame_infos.each_with_index do |frame_width_row, width_index|
frame_width_row.each_with_index do |frame_info, height_index|
frame_info['offset' ] = Random.rand frame_count
frame_info['flipped' ] = [true, false].sample
frame_info['w_offset'] = width_index * frame_images.first.width
frame_info['h_offset'] = height_index * frame_images.first.height
end
end
print "done!\n"
# paste in frame offsets into each master frame
print "Rolling around...\n"
frame_width = tile_count_width * frame_images.first.width
frame_height = tile_count_height * frame_images.first.height
frame_count.times.each do |master_frame_index|
puts "#{frame_width}x#{frame_height}[#{master_frame_index}] roll space"
rollyspace = ChunkyPNG::Image.new frame_width, frame_height, ChunkyPNG::Color::TRANSPARENT
frame_infos.each do |frame_width_row|
frame_width_row.each do |frame_info|
image_x = frame_info['w_offset']
image_y = frame_info['h_offset']
frame_offset = (frame_info['offset'] + master_frame_index) % frame_count
image = frame_images[frame_offset]
if frame_info['flipped'] == true
rollyspace.compose! image.flip_vertically, image_x, image_y
else
rollyspace.compose! image, image_x, image_y
end
end
end
rollyspace.save("masterroll/rool#{'%02d' % master_frame_index}.png");
end
print "done!\n"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment