Skip to content

Commit d29c786

Browse files
committed
Create some uses of buffer to create_graphics
1 parent ad3c551 commit d29c786

File tree

6 files changed

+74
-54
lines changed

6 files changed

+74
-54
lines changed

examples/buffer/buffer.rb

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Using the JRubyArt convenience method buffer to create_graphics you
2+
# do not need to begin_draw, end_draw since the buffer method wraps them
3+
# around user provided block. In practice you probably won't use this method
4+
# much, because you should avoid creating offscreen buffers in the draw loop.
5+
# This sketch is OK because you create the offscreen graphics in the setup.
6+
7+
attr_reader :pg
8+
9+
def setup
10+
sketch_title 'Create graphics using :buffer'
11+
@pg = buffer(60, 70, P2D) do |buf|
12+
buf.background 51
13+
buf.no_fill
14+
buf.stroke 255
15+
buf.rect 0, 0, 59, 69
16+
end
17+
end
18+
19+
def draw
20+
fill 0, 12
21+
rect 0, 0, width, height
22+
image pg, mouse_x - 60, mouse_y - 70
23+
end
24+
25+
def settings
26+
size 640, 380, P2D
27+
end

examples/grid_method/luciernagas.rb

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -52,16 +52,15 @@ def draw
5252
end
5353

5454
def draw_lights
55-
lights = create_graphics width, height, P2D
56-
lights.begin_draw
57-
@flyers.each do |flyer|
58-
lights.push_matrix
59-
position = flyer.pos
60-
lights.translate position.x, position.y
61-
lights.image @spotlight, -@spotlight.width / 2, -@spotlight.height / 2
62-
lights.pop_matrix
55+
lights = buffer(width, height, P2D) do |buffer|
56+
@flyers.each do |flyer|
57+
buffer.push_matrix
58+
position = flyer.pos
59+
buffer.translate position.x, position.y
60+
buffer.image @spotlight, -@spotlight.width / 2, -@spotlight.height / 2
61+
buffer.pop_matrix
62+
end
6363
end
64-
lights.end_draw
6564
image_mask.mask lights
6665
image image_mask, 0, 0
6766
end
@@ -152,14 +151,12 @@ def find_nearest_rotation(from, to)
152151

153152
def create_spotlight
154153
size = 60
155-
spotlight = create_graphics size, size, P2D
156-
spotlight.begin_draw
157-
spotlight.no_stroke
158-
spotlight.fill 255, 60
159-
# spotlight.fill 255, 40
160154
half_size = size / 2
161-
spotlight.ellipse half_size, half_size, half_size, half_size
162-
spotlight.filter BLUR, 4
163-
spotlight.end_draw
164-
spotlight
155+
spotlight = buffer(size, size, P2D) do |buffer|
156+
buffer.no_stroke
157+
buffer.fill 255, 60
158+
# spotlight.fill 255, 40
159+
buffer.ellipse half_size, half_size, half_size, half_size
160+
buffer.filter BLUR, 4
161+
end
165162
end

processing_app/basics/structure/create_graphics.rb

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,30 @@
1-
# Create Graphics.
2-
#
3-
# The createGraphics() function creates an object from the PGraphics class
4-
# (PGraphics is the main graphics and rendering context for Processing).
1+
# Create Graphics.
2+
#
3+
# The createGraphics() function creates an object from the PGraphics class
4+
# (PGraphics is the main graphics and rendering context for Processing).
55
# The beginDraw() method is necessary to prepare for drawing and endDraw() is
6-
# necessary to finish. Use this class if you need to draw into an off-screen
6+
# necessary to finish. Use this class if you need to draw into an off-screen
77
# graphics buffer or to maintain two contexts with different properties.
8+
attr_reader :pg
89

910
def setup
1011
sketch_title 'Create Graphics'
1112
@pg = create_graphics 400, 200
1213
end
1314

14-
def draw
15+
def draw
1516
fill 0, 12
16-
rect 0, 0, width, height
17+
rect 0, 0, width, height
1718
fill 255
1819
no_stroke
19-
ellipse mouse_x, mouse_y, 60, 60
20-
@pg.begin_draw
21-
@pg.background 51
22-
@pg.no_fill
23-
@pg.stroke 255
24-
@pg.ellipse mouse_x-60, mouse_y-60, 60, 60
25-
@pg.end_draw
26-
image @pg, 120, 60
20+
ellipse mouse_x, mouse_y, 60, 60
21+
pg.begin_draw
22+
pg.background 51
23+
pg.no_fill
24+
pg.stroke 255
25+
pg.ellipse mouse_x - 60, mouse_y - 60, 60, 60
26+
pg.end_draw
27+
image pg, 120, 60
2728
end
2829

2930
def settings

processing_app/demos/graphics/trefoil.rb

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,11 @@ def setup
1313
texture_mode(NORMAL)
1414
no_stroke
1515
# Creating offscreen surface for 3D rendering.
16-
@pg = create_graphics(32, 512, P3D)
17-
pg.begin_draw
18-
pg.background(0, 0)
19-
pg.noStroke
20-
pg.fill(255, 0, 0, 200)
21-
pg.end_draw
16+
@pg = buffer(32, 512, P3D) do |buf|
17+
buf.background(0, 0)
18+
buf.no_stroke
19+
buf.fill(255, 0, 0, 200)
20+
end
2221
# Saving trefoil surface into a PShape3D object
2322
@trefoil = create_trefoil(350, 60, 15, pg)
2423
end
@@ -29,7 +28,7 @@ def draw
2928
pg.ellipse(rand(0.0..pg.width), rand(0.0..pg.height), 4, 4)
3029
pg.end_draw
3130
ambient(250, 250, 250)
32-
pointLight(255, 255, 255, 0, 0, 200)
31+
point_light(255, 255, 255, 0, 0, 200)
3332
push_matrix
3433
translate(width / 2, height / 2, -200)
3534
rotate_x(frame_count * PI / 500)

processing_app/topics/drawing/pulses.rb

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#
2-
# Pulses.
3-
#
2+
# Pulses.
3+
#
44
# Software drawing instruments can follow a rhythm or abide by rules independent
55
# of drawn gestures. This is a form of collaborative drawing in which the draftsperson
66
# controls some aspects of the image and the software controls others.
@@ -20,8 +20,8 @@ def draw
2020
# Draw only when mouse is pressed
2121
if mouse_pressed?
2222
@angle += 5
23-
val = cos(angle.radians) * 12.0
24-
(0 ... 360).step(75) do |a|
23+
val = DegLut.cos(angle) * 12.0
24+
(0...360).step(75) do |a|
2525
xoff = cos(a.radians) * val
2626
yoff = sin(a.radians) * val
2727
fill(0)
@@ -35,4 +35,3 @@ def draw
3535
def settings
3636
size(640, 360)
3737
end
38-
Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,27 @@
11
#
22
# Blur Filter
3-
#
3+
#
44
# Change the default shader to apply a simple, custom blur filter.
5-
#
5+
#
66
# Press the mouse to switch between the custom and default shader.
77
#
88

99
attr_reader :blur
1010

1111
def setup
1212
sketch_title 'Blur filter'
13-
@blur = load_shader(data_path('blur.glsl'))
14-
# @blur = load_shader('blur.glsl')
13+
@blur = load_shader(data_path('blur.glsl'))
14+
# @blur = load_shader('blur.glsl')
1515
stroke(255, 0, 0)
1616
rectMode(CENTER)
1717
end
1818

1919
def draw
20-
filter(blur)
21-
rect(mouse_x, mouse_y, 150, 150)
20+
filter(blur)
21+
rect(mouse_x, mouse_y, 150, 150)
2222
ellipse(mouse_x, mouse_y, 100, 100)
2323
end
2424

25-
26-
27-
2825
def settings
2926
size(640, 360, P2D)
3027
end

0 commit comments

Comments
 (0)