Skip to content

Commit 01e4cd6

Browse files
committed
add hype examples
1 parent ebdae80 commit 01e4cd6

File tree

3 files changed

+145
-0
lines changed

3 files changed

+145
-0
lines changed

data_path/data/arrow.svg

+13
Loading

data_path/magnetic_field.rb

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#!/usr/bin/env jruby -v -W2
2+
# frozen_string_literal: true
3+
require 'propane'
4+
# Iconic ruby-processing example for Propane
5+
class MagneticField < Propane::App
6+
7+
load_library :hype
8+
include_package 'hype'
9+
# Use Hype namespace
10+
module Hype
11+
java_import 'hype.extended.layout.HGridLayout'
12+
java_import 'hype.extended.behavior.HMagneticField'
13+
java_import 'hype.extended.behavior.HSwarm'
14+
end
15+
16+
attr_reader :pool, :pool_swarm, :field, :swarm
17+
NUM_MAGNETS = 10
18+
19+
def settings
20+
size(640, 640)
21+
end
22+
23+
def setup
24+
sketch_title 'Magnetic Field'
25+
H.init(self)
26+
H.background(color('#000000'))
27+
@field = Hype::HMagneticField.new
28+
NUM_MAGNETS.times do
29+
if rand > 0.5
30+
# x, y, north polarity / strength = 3 / repel
31+
field.add_pole(rand(0..width), rand(0..height), 3)
32+
else
33+
# x, y, south polarity / strength = -3 / attract
34+
field.add_pole(rand(0..width), rand(0..height), -3)
35+
end
36+
end
37+
38+
@pool = HDrawablePool.new(2_500)
39+
pool.auto_add_to_stage
40+
.add(HShape.new(data_path('arrow.svg')).enable_style(false).anchor_at(H::CENTER))
41+
.layout(Hype::HGridLayout.new.start_x(-60).start_y(-60).spacing(16, 16).cols(50))
42+
.on_create do |obj|
43+
obj.no_stroke.anchor(-20, -20)
44+
field.add_target(obj)
45+
end
46+
.requestAll
47+
48+
@swarm = Hype::HSwarm.new.add_goal(width / 2, height / 2).speed(7).turn_ease(0.03).twitch(20)
49+
@pool_swarm = HDrawablePool.new(NUM_MAGNETS)
50+
pool_swarm.auto_add_to_stage
51+
.add(HRect.new(5))
52+
.on_create do |obj|
53+
obj.no_stroke.no_fill.loc(rand(0..width), rand(0..width)).visibility(false)
54+
swarm.add_target(obj)
55+
end
56+
.requestAll
57+
end
58+
59+
def draw
60+
pool_swarm.each_with_index do |d, i|
61+
p = field.pole(i)
62+
p.x = d.x
63+
p.y = d.y
64+
end
65+
H.draw_stage
66+
end
67+
end
68+
69+
MagneticField.new

regular/method_chaining.rb

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#!/usr/bin/env jruby -v -W2
2+
# frozen_string_literal: true
3+
require 'propane'
4+
# Iconic ruby-processing example for Propane
5+
class MethodChaining < Propane::App
6+
load_library :hype
7+
include_package 'hype'
8+
9+
def settings
10+
size(640, 640)
11+
end
12+
13+
def setup
14+
sketch_title('Method Chaining')
15+
H.init(self)
16+
H.background(color('#242424'))
17+
rect1 = HRect.new(100)
18+
rect1.rounding(10) # set corner rounding
19+
rect1.stroke_weight(6) # set stroke weight
20+
rect1.stroke(color('#000000'), 150) # set stroke color and alpha
21+
rect1.fill(color('#FF6600')) # set fill color
22+
rect1.anchor_at(H::CENTER) # set where anchor point is / key point for rotation and positioning
23+
rect1.rotation(45) # set rotation of the rect
24+
rect1.loc(100, height / 2) # set x and y location
25+
H.add(rect1)
26+
27+
# here's the same code / with method chaining
28+
29+
rect2 = HRect.new(100)
30+
rect2.rounding(10)
31+
.stroke_weight(6)
32+
.stroke(color('#000000'), 150)
33+
.fill(color('#FF9900'))
34+
.anchor_at(H::CENTER)
35+
.rotation(45)
36+
.loc(247, height / 2)
37+
H.add(rect2)
38+
39+
# here's the same code / minus the hard returns and indentation (tabs are bad)
40+
41+
rect3 = HRect.new(100)
42+
rect3.rounding(10).stroke_weight(6).stroke(color('#000000'), 150).fill(color('#FFCC00')).anchor_at(H::CENTER).rotation(45).loc(394, height / 2)
43+
H.add(rect3)
44+
45+
H.draw_stage # paint the stage
46+
47+
# here is the non HYPE version / basic processing syntax
48+
49+
push_matrix
50+
stroke_weight(6)
51+
stroke(color('#000000'), 150)
52+
fill(color('#FF3300'))
53+
translate(width - 100, (height / 2))
54+
rotate(45.radians)
55+
rect(0, 0, 100, 100, 10, 10, 10, 10)
56+
pop_matrix
57+
stroke_weight(1)
58+
stroke(color('#0095a8'))
59+
line(0, height / 2, width, height / 2)
60+
end
61+
end
62+
63+
MethodChaining.new

0 commit comments

Comments
 (0)