Permalink
Newer
Older
100644 50 lines (40 sloc) 1.52 KB
rmagick
New
Dec 31, 2005
1
# Make a 32x32 animated GIF that resembles the OS X animation.
2
# This produces a very small (~2.6Kb) GIF file.
3
4
require 'RMagick'
5
6
puts <<END_INFO
7
This example creates an animated GIF that resembles the OS X "waiting" icon.
8
You can view the GIF with the command
9
10
animate spinner.gif
11
END_INFO
12
13
14
NFRAMES = 12 # number of frames in the animation
15
DIM = 32 # width & height of image in pixels
16
DELAY = 100.0 / (NFRAMES/2) # 2 rotations per second
17
18
# 'frame' is a single frame in the animation.
19
frame = Magick::Image.new(DIM, DIM) {self.background_color = 'transparent'}
20
21
# 'spinner' will contain the frames that make up the animated GIF
22
spinner = Magick::ImageList.new
23
24
# 'level' is the change from darkest gray to white
25
level = Magick::QuantumRange / 2 / NFRAMES
rmagick
New
Dec 31, 2005
26
27
NFRAMES.times do |x|
28
gc = Magick::Draw.new
29
gc.translate(DIM/2, DIM/2)
30
gc.stroke('none')
31
intensity = 0.58 * Magick::QuantumRange
rmagick
New
Dec 31, 2005
32
fill_color = Magick::Pixel.new(intensity, intensity, intensity).to_color
33
gc.fill(fill_color)
34
angle = x * (360/NFRAMES)
35
NFRAMES.times do
36
gc.rotate(angle)
rmagick
Mar 23, 2007
37
intensity = [intensity, Magick::QuantumRange].min
rmagick
New
Dec 31, 2005
38
fill_color = Magick::Pixel.new(intensity, intensity, intensity).to_color
39
gc.fill(fill_color)
40
gc.roundrectangle(7, -1, 15, 0, 1, 1)
41
angle = -(360/NFRAMES)
42
intensity += level
43
end
44
spinner << frame.copy
45
gc.draw(spinner)
46
end
47
48
spinner.delay = DELAY
49
spinner.compression = Magick::LZWCompression
50
spinner.write('spinner.gif')