Skip to content

Commit 7d5d67f

Browse files
committed
handy library examples
1 parent 554950c commit 7d5d67f

File tree

11 files changed

+504
-1
lines changed

11 files changed

+504
-1
lines changed

examples/grid_method/luciernagas.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ def draw_flyers
8484
flyer.rotation = to_rotation if flyer.rotation < to_rotation
8585
end
8686
# add tail position
87-
flyer.positions << flyer.pos.dup
87+
flyer.positions << flyer.pos.copy
8888
flyer.positions.shift while flyer.positions.size > @tail_length
8989
# set flyer position
9090
flyer.pos.x = flyer.pos.x + @speed * cos(flyer.rotation)
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# The Handy Library by Jo Wood et al giCentre
2+
3+
The Handy library allows you to produce graphics with a hand-drawn appearance in Processing sketches. This can be customised to produce a variety of styles including pencil, watercolour and ink and marker pen appearance.
4+
5+
### Obtaining Library
6+
7+
Get it [here][web] install in `~/.propane/libraries` or local to sketch
8+
9+
[web]:https://www.gicentre.net/handy
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Simple demo Rakefile to autorun samples in current directory
2+
# adjust path to k9 executable, and or opts as required
3+
4+
SAMPLES_DIR="./"
5+
6+
desc 'run demo'
7+
task :default => [:demo]
8+
9+
desc 'demo'
10+
task :demo do
11+
samples_list.shuffle.each{|sample| run_sample sample}
12+
end
13+
14+
def samples_list
15+
files = []
16+
Dir.chdir(SAMPLES_DIR)
17+
Dir.glob("*.rb").each do |file|
18+
files << File.join(SAMPLES_DIR, file)
19+
end
20+
return files
21+
end
22+
23+
def run_sample(sample_name)
24+
puts "Running #{sample_name}...quit to run next sample"
25+
open("|jruby #{sample_name}", "r") do |io|
26+
while l = io.gets
27+
puts(l.chop)
28+
end
29+
end
30+
end
13.4 KB
Binary file not shown.
31.2 KB
Binary file not shown.
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#!/usr/bin/env jruby
2+
require 'propane'
3+
# Displays 4 sketchy rectangles with different hachuring styles.
4+
# Version for propane by Martin Prout
5+
# Author Jo Wood.
6+
class HachureStyles < Propane::App
7+
load_library :handy
8+
java_import 'org.gicentre.handy.HandyRenderer'
9+
10+
attr_reader :handy
11+
12+
def settings
13+
size(300, 200)
14+
end
15+
16+
def setup
17+
sketch_title 'Simple Hachure Styles'
18+
@handy = HandyRenderer.new(self)
19+
fill(206,76,52)
20+
handy.set_hachure_perturbation_angle(15)
21+
end
22+
23+
def draw
24+
background(247,230,197)
25+
handy.set_roughness(1)
26+
handy.set_fill_gap(0.5)
27+
handy.set_fill_weight(0.1)
28+
handy.rect(50, 30, 80, 50)
29+
handy.set_fill_gap(3)
30+
handy.set_fill_weight(2)
31+
handy.rect(170, 30, 80, 50)
32+
handy.set_fill_gap(5)
33+
handy.set_is_alternating(true)
34+
handy.rect(50, 120, 80, 50)
35+
handy.set_roughness(3)
36+
handy.set_fill_weight(1)
37+
handy.set_is_alternating(false)
38+
handy.rect(170, 120, 80, 50)
39+
no_loop # No need to redraw.
40+
end
41+
end
42+
43+
HachureStyles.new
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#!/usr/bin/env jruby
2+
require 'propane'
3+
# After a processing.rb example by kitao.
4+
class HandyBallCollision < Propane::App
5+
load_libraries :handy, :balls
6+
java_import 'org.gicentre.handy.HandyRenderer'
7+
BALL_NUM = 6
8+
BALL_COLORS = [[255, 0, 0], [255, 255, 0], [64, 64, 255]].freeze
9+
10+
attr_reader :handy, :balls
11+
12+
def settings
13+
size(400, 400)
14+
end
15+
16+
def setup
17+
sketch_title 'Handy Ball Collision'
18+
@handy = HandyRenderer.new(self)
19+
@balls = (0...BALL_NUM).map { |idx| Ball.new(self, idx) }
20+
end
21+
22+
def draw
23+
background(234, 215, 182)
24+
fill(0, 255, 0)
25+
draw_borders(handy)
26+
balls.each(&:draw)
27+
end
28+
29+
def draw_borders(renderer)
30+
renderer.rect(20, 20, 360, 20)
31+
renderer.rect(20, 360, 360, 20)
32+
renderer.rect(20, 40, 20, 320)
33+
renderer.rect(360, 40, 20, 320)
34+
end
35+
end
36+
37+
HandyBallCollision.new
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
BALL_COLORS = [[255, 0, 0], [255, 255, 0], [64, 64, 255]].freeze
2+
3+
# Bouncing ball
4+
class Ball
5+
attr_reader :sketch, :position, :delta, :bounds
6+
7+
def initialize(sketch, id)
8+
@sketch = sketch
9+
@position = Vec2D.new rand(100..300), rand(100..300)
10+
@delta = Vec2D.new rand(-6..6), rand(-6..6)
11+
@size = rand(60..100)
12+
radius = @size / 2.0
13+
@color = BALL_COLORS[id % BALL_COLORS.size]
14+
@bounds = Boundary.new(40 + radius, 360 - radius)
15+
end
16+
17+
def draw
18+
@position += delta
19+
if bounds.exclude? position.x
20+
position.x = position.x > bounds.upper ? bounds.upper : bounds.lower
21+
delta.x *= -1
22+
end
23+
if bounds.exclude? position.y
24+
position.y = position.y > bounds.upper ? bounds.upper : bounds.lower
25+
delta.y *= -1
26+
end
27+
sketch.fill(*@color)
28+
sketch.handy.ellipse(position.x, position.y, @size, @size)
29+
end
30+
end
31+
32+
# Useful helper class
33+
Boundary = Struct.new(:lower, :upper) do
34+
def exclude?(val)
35+
!(lower..upper).cover? val
36+
end
37+
end
Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
#!/usr/bin/env jruby
2+
require 'propane'
3+
# Displays 4 sketchy rectangles with different hachuring styles.
4+
# Version for propane by Martin Prout
5+
# Author Jo Wood.
6+
class PresetStyleDemo < Propane::App
7+
load_libraries :handy, :gicentre_utils
8+
java_import 'org.gicentre.handy.HandyPresets'
9+
java_import 'org.gicentre.handy.HandyRenderer'
10+
java_import 'org.gicentre.utils.move.ZoomPan'
11+
#*****************************************************************************************
12+
# Simple sketch to show handy shape drawing in four different present styles. H key toggles
13+
# sketchy rendering on or off. Left and right arrows change the hachure angle. Image zoomed
14+
# and panned with mouse drag.
15+
# @author Jo Wood, giCentre, City University London.
16+
# @version JRubyArt translated by Martin Prout
17+
#
18+
# *****************************************************************************************
19+
20+
# self file is part of Handy sketchy drawing library. Handy is free software: you can
21+
# redistribute it and/or modify it under the terms of the GNU Lesser General Public License
22+
# as published by the Free Software Foundation, either version 3 of the License, or (at your
23+
# option) any later version.
24+
#
25+
# Handy is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY
26+
# without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
27+
# See the GNU Lesser General Public License for more details.
28+
#
29+
# You should have received a copy of the GNU Lesser General Public License along with self
30+
# source code (see COPYING.LESSER included with self source code). If not, see
31+
# http://www.gnu.org/licenses/.
32+
#
33+
34+
# ----------------------------- Object variables ------------------------------
35+
36+
attr_reader :pencil, :marker, :water, :cPencil, :border, :zoomer, :angle
37+
attr_reader :isHandy, :sketchyFont, :normalFont
38+
39+
# ---------------------------- Processing methods -----------------------------
40+
def settings
41+
size(1200, 800)
42+
# Should work with all Processing 3 renderers.
43+
# size(1200, 800, P2D)
44+
# size(1200, 800, P3D)
45+
# size(1200, 800, FX2D)
46+
pixelDensity(displayDensity) # Use platform's maximum display density.
47+
end
48+
49+
def setup
50+
sketch_title 'Preset Styles Demo'
51+
@zoomer = ZoomPan.new(self)
52+
@angle = -42
53+
@isHandy = true
54+
@pencil = HandyPresets.create_pencil(self)
55+
@water = HandyPresets.create_water_and_ink(self)
56+
@marker = HandyPresets.create_marker(self)
57+
@cPencil = HandyPresets.create_coloured_pencil(self)
58+
@border = HandyRenderer.new(self)
59+
pencil.set_hachure_angle(angle)
60+
water.set_hachure_angle(angle)
61+
marker.set_hachure_angle(angle)
62+
cPencil.set_hachure_angle(angle)
63+
@sketchyFont = load_font(data_path('HumorSans-32.vlw'))
64+
@normalFont = create_font('sans-serif', 32)
65+
end
66+
67+
def draw
68+
background(255)
69+
zoomer.transform
70+
pencil.set_seed(1234)
71+
water.set_seed(1234)
72+
marker.set_seed(1234)
73+
cPencil.set_seed(1234)
74+
stroke(0)
75+
stroke_weight(1)
76+
textAlign(RIGHT,BOTTOM)
77+
if isHandy
78+
text_font(sketchyFont)
79+
else
80+
text_font(normalFont)
81+
end
82+
srand(10)
83+
no_fill
84+
border.rect(10, 10, width / 2 - 20, height / 2 - 20)
85+
draw_shapes(pencil, 0, 0, width / 2, height / 2)
86+
fill(60)
87+
text('Pencil', width / 2 - 20, height / 2-15)
88+
no_fill
89+
border.rect(width / 2 + 10, 10,width / 2 - 20, height / 2 - 20)
90+
draw_shapes(water, width / 2, 0, width / 2, height / 2)
91+
fill(60)
92+
text('Ink and watercolour', width - 20,height / 2-15)
93+
no_fill
94+
border.rect(10, height / 2 + 10, width / 2 - 20, height / 2 - 20)
95+
draw_shapes(marker, 0, height / 2, width / 2, height / 2)
96+
fill(60)
97+
text('Marker pen', width / 2 - 20, height - 15)
98+
no_fill
99+
border.rect(width / 2 + 10, height / 2 + 10, width / 2 - 20, height / 2 - 20)
100+
draw_shapes(cPencil,width/2,height / 2,width/2,height / 2)
101+
fill(60)
102+
text('Coloured pencil', width - 20,height - 15)
103+
no_loop
104+
end
105+
106+
def key_pressed
107+
case key
108+
when 'h', 'H'
109+
@isHandy = !isHandy
110+
pencil.setIsHandy(isHandy)
111+
water.setIsHandy(isHandy)
112+
marker.setIsHandy(isHandy)
113+
cPencil.setIsHandy(isHandy)
114+
loop
115+
when 'r', 'R'
116+
zoomer.reset
117+
loop
118+
else
119+
return unless key == CODED
120+
end
121+
case key_code
122+
when LEFT
123+
@angle -= 1
124+
pencil.set_hachure_angle(angle)
125+
water.set_hachure_angle(angle)
126+
marker.set_hachure_angle(angle)
127+
cPencil.set_hachure_angle(angle)
128+
loop
129+
when RIGHT
130+
@angle += 1
131+
pencil.set_hachure_angle(angle)
132+
water.set_hachure_angle(angle)
133+
marker.set_hachure_angle(angle)
134+
cPencil.set_hachure_angle(angle)
135+
loop
136+
end
137+
end
138+
139+
def mouse_dragged
140+
loop
141+
end
142+
143+
private
144+
145+
def draw_shapes(handy, x, y, w, h)
146+
minSize = w / 10
147+
maxSize = min(w, h) / 4
148+
30.times do
149+
colour = color(rand(100..200), rand(60..200), rand(100..200), 120)
150+
fill(colour)
151+
shape_choice = rand
152+
if (shape_choice < 0.33)
153+
handy.rect(x + rand(minSize..w - maxSize),y + rand(minSize..h - maxSize),rand(minSize..maxSize), rand(minSize..maxSize))
154+
elsif shape_choice < 0.66
155+
x1 = x + rand(minSize..w - maxSize)
156+
y1 = y + rand(minSize..h - maxSize)
157+
x2 = x1 + rand(50..maxSize)
158+
y2 = y1 + rand(-10..10)
159+
x3 = (x1 + x2) / 2
160+
y3 = y1 - rand(minSize..maxSize)
161+
handy.triangle(x1, y1, x2, y2, x3, y3)
162+
else
163+
handy.ellipse(x + rand(minSize..w - maxSize), y + rand(minSize..h - maxSize),rand(minSize..maxSize), rand(minSize..maxSize))
164+
end
165+
end
166+
end
167+
end
168+
169+
PresetStyleDemo.new
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#!/usr/bin/env jruby
2+
require 'propane'
3+
# Displays 4 sets of sketchy rectangles each with their own preset styles.
4+
# Version 2.0, 4th April, 2016
5+
# Author Jo Wood
6+
class PresetStyles < Propane::App
7+
load_library :handy
8+
java_import 'org.gicentre.handy.HandyPresets'
9+
10+
attr_reader :h1, :h2, :h3, :h4
11+
FIVE = 5
12+
13+
def settings
14+
size(300, 200)
15+
end
16+
17+
def setup
18+
sketch_title 'Preset Styles Test'
19+
@h1 = HandyPresets.create_pencil(self)
20+
@h2 = HandyPresets.create_coloured_pencil(self)
21+
@h3 = HandyPresets.create_water_and_ink(self)
22+
@h4 = HandyPresets.create_marker(self)
23+
end
24+
25+
def draw
26+
background(247, 230, 197)
27+
fill(0, 255, 0)
28+
FIVE.times do
29+
fill(206 + rand(-30..30), 76 + rand(-30..30), 52 + rand(-30..30), 160)
30+
h1.rect(rand(10..200), rand(10..50), 80, 50)
31+
h2.rect(rand(310..520), rand(10..50), 80, 50)
32+
h3.rect(rand(10..200), rand(100..140), 80, 50)
33+
h4.rect(rand(310..520), rand(100..140), 80, 50)
34+
end
35+
no_loop
36+
end
37+
end
38+
39+
PresetStyles.new

0 commit comments

Comments
 (0)