Sha256: 0d5c77fe5ec8961b731b598809ed82b4d01652fdf7a628f23b4f37d26f87e8b2

Contents?: true

Size: 1.48 KB

Versions: 6

Compression:

Stored size: 1.48 KB

Contents

# Make a 32x32 animated GIF that resembles the OS X animation.
# This produces a very small (~2.6Kb) GIF file.

require 'rmagick'

puts <<END_INFO
This example creates an animated GIF that resembles the OS X "waiting" icon.
You can view the GIF with the command

    animate spinner.gif
END_INFO

NFRAMES = 12                    # number of frames in the animation
DIM = 32                        # width & height of image in pixels
DELAY = 100.0 / (NFRAMES / 2) # 2 rotations per second

# 'frame' is a single frame in the animation.
frame = Magick::Image.new(DIM, DIM) { self.background_color = 'transparent' }

# 'spinner' will contain the frames that make up the animated GIF
spinner = Magick::ImageList.new

# 'level' is the change from darkest gray to white
level = Magick::QuantumRange / 2 / NFRAMES

NFRAMES.times do |x|
  gc = Magick::Draw.new
  gc.translate(DIM / 2, DIM / 2)
  gc.stroke('none')
  intensity = 0.58 * Magick::QuantumRange
  fill_color = Magick::Pixel.new(intensity, intensity, intensity).to_color
  gc.fill(fill_color)
  angle = x * (360 / NFRAMES)
  NFRAMES.times do
    gc.rotate(angle)
    intensity = [intensity, Magick::QuantumRange].min
    fill_color = Magick::Pixel.new(intensity, intensity, intensity).to_color
    gc.fill(fill_color)
    gc.roundrectangle(7, -1, 15, 0, 1, 1)
    angle = -(360 / NFRAMES)
    intensity += level
  end
  spinner << frame.copy
  gc.draw(spinner)
end

spinner.delay = DELAY
spinner.compression = Magick::LZWCompression
spinner.write('spinner.gif')

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
rmagick-4.1.0.rc2 examples/spinner.rb
rmagick-4.1.0.rc1 examples/spinner.rb
rmagick-4.0.0 examples/spinner.rb
rmagick-3.2.0 examples/spinner.rb
rmagick-3.1.0 examples/spinner.rb
rmagick-3.0.0 examples/spinner.rb