Skip to content

Commit de3122e

Browse files
committed
more samples
1 parent be8223e commit de3122e

File tree

173 files changed

+3036
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

173 files changed

+3036
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# frozen_string_literal: true
2+
java_import 'monkstone.vecmath.ShapeRender'
3+
# An object that wraps the PShape
4+
class Wiggler
5+
include Propane::Proxy
6+
attr_reader :original, :x, :y, :s, :yoff, :xoff
7+
def initialize width, height
8+
@x = width/2
9+
@y = height/2
10+
@yoff = 0
11+
# The "original" locations of the vertices make up a circle
12+
@original = (0...16).map{ |a| Vec2D.from_angle(PI * a / 8) * 100 }
13+
# Now make the PShape with those vertices
14+
@s = create_shape
15+
renderer = ShapeRender.new(s) # Prefix with Sketch classname
16+
s.begin_shape
17+
s.fill(127)
18+
s.stroke(0)
19+
s.stroke_weight(2)
20+
original.map{ |v| v.to_vertex(renderer) }
21+
s.end_shape(CLOSE)
22+
end
23+
24+
def wiggle
25+
@xoff = 0
26+
# Apply an offset to each vertex
27+
rad = ->(pos){ (Vec2D.from_angle(2 * PI * noise(xoff, yoff)) * 4) + pos }
28+
original.each_with_index do |pos, i|
29+
# Calculate a new vertex location based on noise around "original" location
30+
r = rad.call(pos)
31+
# Set the location of each vertex to the new one
32+
s.set_vertex(i, r.x, r.y)
33+
# increment perlin noise x value
34+
@xoff += 0.5
35+
end
36+
# Increment perlin noise y value
37+
@yoff += 0.02
38+
end
39+
40+
def display
41+
push_matrix
42+
translate(x, y)
43+
shape(s)
44+
pop_matrix
45+
end
46+
end
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
#!/usr/bin/env jruby
2+
require 'propane'
3+
# Animated Sprite (Shifty + Teddy)
4+
# by James Paterson, rubified by Martin Prout.
5+
#
6+
# Hold down the mouse button to change animations.
7+
# Demonstrates loading, displaying, and animating GIF images.
8+
# It would be easy to write a program to display
9+
# animated GIFs, but would not allow as much control over
10+
# the display sequence and rate of display.
11+
class AnimatedSprite < Propane::App
12+
DRAG = 30.0
13+
SHIFTY = 'PT_Shifty_%s.gif'
14+
TEDDY = 'PT_Teddy_%s.gif'
15+
attr_reader :xpos, :ypos, :animation1, :animation2
16+
17+
def setup
18+
sketch_title 'Animated Sprite'
19+
background(255, 204, 0)
20+
frame_rate(24)
21+
@animation1 = Animation.new(SHIFTY, 38)
22+
@animation2 = Animation.new(TEDDY, 60)
23+
@ypos = height * 0.25
24+
@xpos = 0
25+
end
26+
27+
def draw
28+
dx = mouse_x - xpos
29+
@xpos = xpos + dx / DRAG
30+
# Display the sprite at the position xpos, ypos
31+
if mouse_pressed?
32+
background(153, 153, 0)
33+
animation1.display(xpos - animation1.image_width / 2, ypos)
34+
else
35+
background(255, 204, 0)
36+
animation2.display(xpos - animation2.image_width / 2, ypos)
37+
end
38+
end
39+
40+
def settings
41+
size(640, 360)
42+
end
43+
end
44+
45+
# the animation class
46+
class Animation
47+
include Propane::Proxy
48+
attr_reader :images, :fcount, :frame
49+
50+
def initialize(image_format, count)
51+
@fcount = count
52+
@frame = 0
53+
@images = (0...count).map do |i|
54+
load_image(data_path(format(image_format, i.to_s.rjust(4, '0'))))
55+
end
56+
end
57+
58+
def display(xpos, ypos)
59+
@frame = (frame + 1) % fcount
60+
image(images[frame], xpos, ypos)
61+
end
62+
63+
def image_width
64+
images[0].width
65+
end
66+
end
67+
68+
69+
70+
AnimatedSprite.new

0 commit comments

Comments
 (0)