Skip to content

Commit d2ae948

Browse files
author
monkstone
committed
improve padded number format
1 parent aaf56b5 commit d2ae948

File tree

3 files changed

+47
-48
lines changed

3 files changed

+47
-48
lines changed

samples/processing_app/topics/advanced_data/threads_two.rb

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
##################
22
# From an original sketch on openprocessing by Chinchbug
33
# http://www.openprocessing.org/sketch/29577
4+
#
5+
# Note: ruby syntax 'rjust' for padding a number string
46
##################
57
FRAMES = 116
8+
IMG_F = 'a%s copy.jpg'
9+
MESS_F = 'loading frames (%s of 115)'
610

7-
attr_reader :ang, :curr, :frames, :done_loading
11+
attr_reader :ang, :curr, :frames
812

913
def setup
1014
size(305, 395)
@@ -21,22 +25,21 @@ def setup
2125
def load_image_thread
2226
thread do # supply a block in ruby-processing rather than use reflection
2327
FRAMES.times do |i|
24-
frames << load_image("a#{i.to_s.rjust(3, '0')} copy.jpg") # ruby syntax
28+
frames << load_image(format(IMG_F, i.to_s.rjust(3, '0')))
2529
@curr = i
26-
delay(75) # just slows down this thread, the main draw cycle is unaffected
30+
delay(75) # slows down this thread, the main draw cycle is unaffected
2731
end
2832
@curr = 0
2933
frame_rate(30)
30-
@done_loading = true
3134
end
3235
end
3336

3437
def draw
35-
if done_loading
36-
display_animation
37-
else
38+
if frames.length < FRAMES
3839
display_loading ang
3940
@ang += 0.1
41+
else
42+
display_animation
4043
end
4144
end
4245

@@ -57,5 +60,5 @@ def display_loading(ang)
5760
fill(0)
5861
rect(120, 150, 170, 100)
5962
fill(128)
60-
text("loading frames (#{curr} of 115)", 200, 200)
63+
text(format(MESS_F, curr), 200, 200)
6164
end
Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,61 @@
11
#
22
# Animated Sprite (Shifty + Teddy)
3-
# by James Paterson, rubified by Martin Prout.
4-
#
3+
# by James Paterson, rubified by Martin Prout.
4+
#
55
# Hold down the mouse button to change animations.
66
# Demonstrates loading, displaying, and animating GIF images.
7-
# It would be easy to write a program to display
8-
# animated GIFs, but would not allow as much control over
9-
# the display sequence and rate of display.
7+
# It would be easy to write a program to display
8+
# animated GIFs, but would not allow as much control over
9+
# the display sequence and rate of display.
1010
#
1111

1212
DRAG = 30.0
13+
SHIFTY = 'PT_Shifty_%s.gif'
14+
TEDDY = 'PT_Teddy_%s.gif'
15+
1316
attr_reader :xpos, :ypos, :animation1, :animation2
1417

1518
def setup
1619
size(640, 360)
1720
background(255, 204, 0)
1821
frame_rate(24)
19-
@animation1 = Animation.new('PT_Shifty_', 38)
20-
@animation2 = Animation.new('PT_Teddy_', 60)
22+
@animation1 = Animation.new(SHIFTY, 38)
23+
@animation2 = Animation.new(TEDDY, 60)
2124
@ypos = height * 0.25
2225
@xpos = 0
2326
end
2427

25-
def draw
28+
def draw
2629
dx = mouse_x - xpos
2730
@xpos = xpos + dx / DRAG
2831
# Display the sprite at the position xpos, ypos
2932
if mouse_pressed?
3033
background(153, 153, 0)
31-
animation1.display(xpos - animation1.get_width / 2, ypos)
34+
animation1.display(xpos - animation1.image_width / 2, ypos)
3235
else
3336
background(255, 204, 0)
34-
animation2.display(xpos - animation2.get_width / 2, ypos)
37+
animation2.display(xpos - animation2.image_width / 2, ypos)
3538
end
3639
end
3740

38-
class Animation
39-
attr_reader :images, :image_count, :frame
40-
41-
def initialize(image_prefix, count)
42-
@image_count = count
41+
# the animation class
42+
class Animation
43+
attr_reader :images, :fcount, :frame
44+
45+
def initialize(image_format, count)
46+
@fcount = count
4347
@frame = 0
44-
@images = (0 ... image_count).map {
45-
|i| load_image(format('%s%04d%s', image_prefix, i, '.gif'))
46-
}
48+
@images = (0...count).map do |i|
49+
load_image(format(image_format, i.to_s.rjust(4, '0')))
50+
end
4751
end
4852

4953
def display(xpos, ypos)
50-
@frame = (frame + 1) % image_count
54+
@frame = (frame + 1) % fcount
5155
image(images[frame], xpos, ypos)
5256
end
53-
54-
def get_width
57+
58+
def image_width
5559
images[0].width
5660
end
5761
end

samples/processing_app/topics/animation/sequential.rb

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,30 @@
11
#
22
# Sequential
3-
# by James Paterson.
4-
#
5-
# Displaying a sequence of images creates the illusion of motion.
6-
# Twelve images are loaded and each is displayed individually in a loop.
3+
# by James Paterson.
4+
#
5+
# Displaying a sequence of images creates the illusion of motion.
6+
# Twelve images are loaded and each is displayed individually in a loop.
77
#
88

99
NUM_FRAMES = 12 # The number of frames in the animation
1010
attr_reader :frame, :images
1111

12+
IMGFORM = 'PT_anim%s.gif'
13+
1214
def setup
13-
size(640, 360)
15+
size(800, 360)
1416
frame_rate(24)
1517
@frame = 0
16-
@images = []
17-
images << load_image('PT_anim0000.gif')
18-
images << load_image('PT_anim0001.gif')
19-
images << load_image('PT_anim0002.gif')
20-
images << load_image('PT_anim0003.gif')
21-
images << load_image('PT_anim0004.gif')
22-
images << load_image('PT_anim0005.gif')
23-
images << load_image('PT_anim0006.gif')
24-
images << load_image('PT_anim0007.gif')
25-
images << load_image('PT_anim0008.gif')
26-
images << load_image('PT_anim0009.gif')
27-
images << load_image('PT_anim0010.gif')
28-
images << load_image('PT_anim0011.gif')
18+
@images = (0...NUM_FRAMES).map do |i|
19+
load_image(format(IMGFORM, i.to_s.rjust(4, '0')))
20+
end
2921
end
3022

3123
def draw
3224
background(0)
3325
@frame = (frame + 1) % NUM_FRAMES # Use % to cycle through frames
3426
offset = 0
35-
(-100 ... width).step(images[0].width) do |i|
27+
(-100...width).step(images[0].width) do |i|
3628
image(images[(frame + offset) % NUM_FRAMES], i, -20)
3729
offset += 2
3830
image(images[(frame + offset) % NUM_FRAMES], i, height / 2)

0 commit comments

Comments
 (0)