Processing – pictures that move
Since 2001, Processing has been the language for art, games and graphics made from code: Ben Fry and Casey Reas built it at MIT so that designers and artists could learn to program. A program there is called a sketch, and it has two parts: setup runs once at the start, draw runs after that again and again, about 60 times a second. The processing gem brings exactly this language to Ruby – with the same names as the original:
require "processing"
using Processing
setup do
size 400, 300
end
draw do
background 250, 240, 220
stroke 90, 60, 40
fill 230, 120, 40
ellipse 200, 130, 140, 140
fill 255
rect 60, 230, 280, 40
endThe sketch's window opens below the cell. require "processing" loads the gem, and using Processing switches its commands on – size, ellipse, fill only exist in a file that says so. (That is a refinement, like Pptx::Lengths in lesson 66: a module that adds methods only where you switch it on with using.) So every cell here starts with these two lines: each is a sketch of its own, like a file of its own.
Coordinates start top left at 0, 0, x goes right, y goes down. ellipse 200, 130, 140, 140 draws a circle centred at 200/130, 140 pixels across, rect a rectangle from its top-left corner. Colours are three numbers from 0 to 255 – red, green, blue –, a single number is a grey. fill sets the fill, stroke the outline, and both hold for everything that comes after.
The methods are named as in the original Processing: mouseX, not mouse_x. That way the many examples from books and the web fit almost unchanged. Now something moves:
require "processing"
using Processing
setup do
size 400, 300
end
draw do
background 30
noStroke
fill 255, 200, 0
x = frameCount * 2 % width
ellipse x, 150, 50, 50
endframeCount counts the frames since the start. Because draw runs again and again and x is a little bigger each time, the circle travels; % width sends it back to the left at the right edge. And background 30 at the start of draw? Without it every frame paints over the last one, and the circle leaves a trail. Try it: delete the line.
Move the mouse over the next window and press the button:
require "processing"
using Processing
setup do
size 400, 300
end
draw do
background 30
noStroke
if mousePressed
fill 255, 80, 80
else
fill 255, 160, 0
end
ellipse mouseX, mouseY, 40, 40
endmouseX and mouseY are where the mouse is right now (on a phone: the finger), mousePressed is true while a button is down. For drawings that turn, you move the coordinate system itself: translate puts the origin somewhere else, rotate turns it (in radians – TWO_PI is a full turn), and push/pop remember the state and bring it back. colorMode HSB describes colours as hue, saturation and brightness – that way you can go once round the colour wheel:
require "processing"
using Processing
setup do
size 400, 300
colorMode HSB, 360, 100, 100
noStroke
end
draw do
background 0, 0, 12
translate width / 2, height / 2
12.times do |i|
push
rotate TWO_PI * i / 12 + frameCount * 0.01
fill i * 30, 80, 100
ellipse 80, 0, 60, 22
pop
end
endA sketch is plain Ruby: variables, arrays, hashes, blocks. Besides draw there are blocks for events – mousePressed do … end runs once per click, keyPressed do … end once per key (key says which). createVector makes a vector, an arrow with x and y that you can add: position plus speed gives the new position. Click into the water a few times:
require "processing"
using Processing
bubbles = []
setup do
size 400, 300
noStroke
end
draw do
background 20, 40, 80
bubbles.each do |bubble|
bubble[:position].add(bubble[:speed])
fill 255, 255, 255, 120
circle bubble[:position].x, bubble[:position].y, bubble[:size]
end
bubbles.reject! { |bubble| bubble[:position].y < -20 }
end
mousePressed do
bubbles << {
position: createVector(mouseX, mouseY),
speed: createVector(random(-1, 1), random(-3, -1)),
size: random(10, 40)
}
endEach bubble is a hash with position, speed and size. random(-1, 1) is a random number between -1 and 1, the fourth number in fill the opacity (0 see-through, 255 solid), and reject! clears away the bubbles that floated out at the top – otherwise the array would keep growing.
The real gem draws with OpenGL, through xord's C++ libraries rays and reflexion. A browser does not have them – here a stand-in in pure Ruby with the same commands paints into a <canvas> (just like Numo in lesson 74). So every sketch in this lesson is exactly the one you start at home:
gem install processing ruby sketch.rb
Then a window of its own opens. On Windows the install needs RubyInstaller's MSYS2 DevKit, because rays is compiled. At home the gem can do more: load pictures (loadImage), shaders, 3D and the camera (createCapture) – here those commands raise NotImplementedError. And if you prefer Ruby names, write using Processing(snake_case: true) and you may say mouse_x and color_mode as well.
One more thing that might surprise you at home: the window only opens if the sketch has a draw block (or one for the mouse or the keyboard). A sketch with only setup is done at once – and so here too: no draw, no window.
setup, and while the mouse button is pressed, draw draws a line from where the mouse just was (pmouseX, pmouseY) to where it is now. Without background in draw, everything stays.# Chunky's paint program: while the mouse is pressed,
# a line from (pmouseX, pmouseY) to (mouseX, mouseY)
require "processing"
using Processing
setup do
size 400, 300
background 255
end
draw do
end