Visual Programming For Absolute beginners
What Is Processing?
- from Wikipedia:
Processing is an open source programming language and integrated development environment (IDE) built for the electronic arts, new media art, and visual design communities with the purpose of teaching the fundamentals of computer programming in a visual context, and to serve as the foundation for electronic sketchbooks.
-Processing is based on Java but uses simplified syntax and assumes a graphical end product.
What can you do with Processing?
-Processing is really cool.
-You can build entire programs that run on Mac, PC, and Linux and work just like any other computer program (this is only possible with the IDE)
-You can drop a processing program (called a "sketch") into any webpage with a few lines of HTML.
-You can learn programming without wanting to bang your head against the wall!
-Processing is really cool.
-You can build entire programs that run on Mac, PC, and Linux and work just like any other computer program (this is only possible with the IDE)
-You can drop a processing program (called a "sketch") into any webpage with a few lines of HTML.
-You can learn programming without wanting to bang your head against the wall!
What do You Need to Work with Processing?
-Processing IDE (Recommended)
-Openprocessing.org account
-Build and display directly in the browser (You'll just need a plain text editor)
Processing is simple, but it's still programming. You'll need to know some basics of programming to use it effectively.
Programming Tips
-Think like a machine. Be 100% explicit.
-Make your sketch as simple as possible.
-If something's not working, don't be discouraged--it could be something as simple as a missing ')' in your code. The Processing IDE will help you keep from making these kinds of mistakes but even pros make silly errors every once in a while.
-Be lazy. Try to find a way to do what you want with just a few lines of code--in most cases, there's a way.
The basics of Processing:
SETUP() // THE SETUP FUNCTION IS CALLED ONCE, IT WILL NEVER BE CALLED AGAIN. EVERYTHING YOU WANT TO DO BEFORE YOUR PROGRAM STARTS REALLY RUNNING NEEDS TO BE IN THE SETUP FUNCTION.
DRAW() // THE DRAW FUNCTION WILL BE CALLED OVER AND OVER AGAIN
UNTIL YOU TELL IT TO STOP. THIS IS HOW ELEMENTS ARE ANIMATED FROM ONE FRAME TO THE NEXT IN PROCESSING.
More Functions
void mousePressed()
void newCircle()
void rotateSquare()
The Setup() Function
void setup(){
size(200,200);
background(255,255,255,100);
stroke(0,0,0,100);
strokeWeight(3);
fill(150,150,150,50);
frameRate(40);
}
What the Setup() Function Means:
void setup(){
size(200,200); // width in pixels, height in pixels
background(255,255,255,100); // background red value, background green value, background blue value, alpha/opacity (0-100)
stroke(0,0,0,100); // stroke color (same values as background)
strokeWeight(3); // stroke width in pixels
fill(150,150,150,50); // fill color (same values as background)
frameRate(40) // number of times per second the draw function should be called (100 by default)
}
Only the size element is absolutely required; other elements may be supplied in another function later on.
A Note on Color
-Colors in Processing can be represented a lot of different ways--usually R,G,B,A is a really useful way to do it.
-They can also be represented with hex codes, but using a hex code doesn't allow you to set the opacity. Hex codes are actually R,G,B codes in disguise anyway (for instance, #55FAD3)
-You can also use gray values (supply a number between 0 and 100, 0 being white and 100 being black) but why limit yourself to 100 shades of gray when you can use R,G,B to represent anything between white (255,255,255) and black (0,0,0)?
The Draw() Function
void draw(){
// do something
}
Draw a Circle
ellipse(50,50,10,10); // X coordinate of the center, Y coordinate of the center, width in pixels, and height in pixels
Whenever you want to draw a circle on the canvas, you need to call the ellipse() function. This function can make ellipses and circles, so it needs a center point, a width, and a height.
Draw a Line
line(50,50,150,150); // X coordinate of first endpoint, Y coordinate of first endpoint, X coordinate of second endpoint, Y coordinate of second endpoint
line() takes four parameters--the first two are one endpoint, the second two are another endpoint.
Draw a Rectangle
rect(50,50,100,100); // X coordinate of top left corner, Y coordinate of top left corner, width in pixels, height in pixels
rect() takes four parameters, the first two are the top left corner of the rectangle, the third is its width, and the fourth is its height.
OK. let's Make Something.
void setup(){
size(500,500);
background(255,0,0,100);
fill(255,255,255,100);
stroke(0,0,0,100);
}
void draw(){
rect(100,100,50,200);
ellipse(150,250,75,150);
line(150,150,480,480);
}
Wow, that's ugly.Some more Programming Tips:
variables - variables are really important. Here's how you declare a variable:
int x = 1; // integers are whole numbers (they can be negative or positive) float y = 4.0; // floats are decimal numbers (also negative or positive) String chicken = "Roger is the chicken's name"; // Processing doesn't use a lot of text, but you can display text in various fonts--store text as strings color c = color(255, 204, 0); // colors are not very useful as variables, but it's possible to store a color in a variable
More about Variables:
Variables don't need to be a single letter. Try to name your variables words/letters beginning with a lower case letter (generally, symbols other than "_" do not belong in variable names).
Examples of common variable naming conventions are:
w inputText numberOfChickens charleys_Phone_Number
Width and height
width and height - these variables always return the canvas' width and height--use them any time you want to make your sketch scalable, because if you change the size of the canvas, width and height will respond to new canvas sizes and react appropriately.
For example:
rect((width/2) - 15,(height/2) - 15, 30, 30);
Random
random(100);
random() generates a semi-random float between 0 and whatever number you specify. This is great for generating a new random color or position.
Try adding the following into setup() and then void():
background(random(255),255,255,100);
MouseX and MouseY
mouseX & mouseY
These dynamic variables always store the location of the user's mouse, you can use them to produce shapes that change based on where the user's mouse is at any given point in time.
Try adding the following to the end of your draw() function:
stroke(0,0,0,100);
line(0, 0, mouseX, mouseY);
Now let's practice:
The Code:
void setup(){
size(1000, 1000);
background(255, 255, 255, 100);
fill(255, 100, 100, 5);
stroke(0, 0, 0, 100);
}
void draw(){
rect(0, 0, mouseX, mouseY);
}
The Solution:
void setup(){
size(1000, 1000);
background(255, 255, 255, 100);
fill(255, 100, 100); // the opacity is set to 100% to show the rectangle
stroke(0, 0, 0, 100);
}
void draw(){
background(255, 255, 255, 100);
rect(0, 0, mouseX, mouseY);
}
More Practice (a little Harder):
(You can use the same sketch, just empty out your draw() function)
The Code:
void setup(){
size(1000, 1000);
background(255, 255, 255, 100);
fill(255, 100, 100);
noStroke();
}
void draw(){
fill(random(255), random(255), random(255), random(50));
ellipse(mouseX, mouseY, 30, 30);
}
Solution:
void setup(){
size(1000, 1000);
background(255, 255, 255, 100);
fill(255, 100, 100);
noStroke();
}
void draw(){
fill(random(255), random(255), random(255), 30 + random(20));
ellipse(mouseX, mouseY, 30, 30);
}
More Practice:
a vertical line
-3px wide (use the strokeWeight() attribute)-that is a random shade of green-and 25% opaque
The Code:
void setup(){
size(1000, 1000);
background(255, 255, 255, 100);
strokeWeight(3);
}
void draw(){
stroke(0, random(255), 0, 25);
line(mouseX, 0, mouseX, height);
}
make it a random shade of gray.
Yes, We Have No Variables!
stroke(random(255), 25);
The Solution:
float g = random(255);
stroke(g, g, g, 25);
More Practice:
The Solution:
void setup(){
size(1000, 1000);
background(255, 255, 255, 100);
strokeWeight(3);
}
void draw(){
float g = random(255);
stroke(g, g, g, 25);
line(mouseX, 0, mouseX, height);
stroke(0, random(255), 0, 25);
line(0, mouseY, width, mouseY);
}
Arrays
float colorOptions[] = new float[];
Background Information for Arrays:
String fruits[] = ["apples","oranges","grapefruits","peaches","bananas"]
Dealing with Arrays
So how do I add an Item to an Array?
int[] colorOptions = {30, 100, 200, 230};
Loops
Loop (Step 1)
void setup(){
size(800,800);
background(255,255,255,100);
stroke(0,0,0,100);
strokeWeight(1);
}
Loop (Step 2)
int counter = 0; float[] colorOptions = {244, 35, 0, 130, 200};
void setup(){ size(800,800); background(255,255,255,100); stroke(0,0,0,100); strokeWeight(1); }
Loop (Step 3)
int counter = 0; float[] colorOptions = {244, 35, 0, 130, 200};
void setup(){ size(800,800); background(255,255,255,100); stroke(0,0,0,100); strokeWeight(1); }
void draw(){ rect(counter + 30, 30, 20, 20);
}
Loop (Step 4)
(to illustrate the loop)
int counter = 0; float[] colorOptions = {244, 35, 0, 130, 200};
void setup(){ size(800,800); background(255,255,255,100); stroke(0,0,0,100); strokeWeight(1); }
void draw(){ for (int i = 0; i < colorOptions.length; i++){ rect(counter + 30, 30, 20, 20); counter += 30; } noLoop(); }
For Loops Explained:
for (int i = 0; i < colorOptions.length; i++){
rect(counter + 30, 30, 20, 20);
counter += 30;
}
2) i < colorOptions.length -- Keep going as long as i is less than the number of items in
colorOptions[]
Loop (step 5)
Loop (step 5 Part 2)
corresponding color in colorOptions[]
int counter = 0; float[] colorOptions = {244, 35, 0, 130, 200};
void setup(){ size(800,800); background(255,255,255,100); stroke(0,0,0,100); strokeWeight(1); }
void draw(){ for (int i = 0; i < colorOptions.length; i++){
fill(colorOptions[i], colorOptions[i], colorOptions[i], 100); rect(counter + 30, 30, 20, 20); counter += 30; } noLoop(); }
Loop (Step 6)
int counter = 0; float[] colorOptions = {244, 35, 0, 130, 200};
void setup(){ size(800,800); background(255,255,255,100); stroke(0,0,0,100); strokeWeight(1); }
void draw(){ for (int i = 0; i < colorOptions.length; i++){
fill(colorOptions[i], colorOptions[i], colorOptions[i], 100); rect(counter + 30, 30, 20, 20); counter += 30; } }
Visual Programming For Absolute beginners
By Clinton Daniel McKay
Visual Programming For Absolute beginners
This presentation will guide you through the basics of programming with the Processing language. It covers drawing, making responsive sketches, arrays, loops, and sharing Processing sketches--as well as the basics you'll need for most any programming language.
- 2,037