for ( A starting point, usually a counter variable at 0. , A condition. The code will keep looping while this is true. Usually this sets a max the counter will reach before it stops. , An instruction to do each loop, usually just adding one to the counter. ) { Code to repeat each loop }
function setup(){ createCanvas(400, 400); var counter; //draws ellipses across screen for (counter=0; counter <=width; counter++){ fill(255, 100); noStroke(); //draw each ellipse, using 'counter' for the x position ellipse(counter, 50, 20, 20); } }
function setup(){ createCanvas(400, 400); var counter; //draws ellipses across screen for (counter=0; counter <=width; counter++){ fill(random(255), random(255), random(255)); noStroke(); //draw an ellipse if 'counter' is divisible by 20 if(counter%20 == 0) ellipse(counter, 50, 20, 20); } }
//noprotect function setup(){ createCanvas(400, 400); background(0); var counterx; var countery; //loop that moves down screen vertically //to draw each line of dots for (countery=0; countery <=height; countery++){ //loop that moves across each line, drawing dots for (counterx=0; counterx<=width; counterx++){ if(counterx%20 == 0 && countery%20==0){ fill(random(255), random(255), random(255)); noStroke(); ellipse(counterx, countery, 20, 20); } } } }
//noprotect function setup(){ createCanvas(400, 400); } function draw(){ background(0); var counterx; var countery; for (countery=0; countery<=height; countery++){ for (counterx=0; counterx<=width; counterx++){ if (counterx%20 == 0 && countery%20==0){ fill(counterx, countery, 0, 100); noStroke(); //create a variable called 'distance' that //contains the distance between the mouse //and the current ellipse being drawn... var distance = dist (counterx, countery, mouseX, mouseY); //...and use that value to change the value of //a variable called 'size' which we'll use as //the width and height of the ellipse var size = map(distance, 0, 500, 50, 5); ellipse(counterx, countery, size, size); } } } }
//create a new variable named 'capture' //which will store a video-capturing 'object' //that grabs frames from the camera var capture; function setup() { createCanvas(640, 480); //create a video-capturing 'object' //stored in our 'capture' variable capture = createCapture(VIDEO); //set 'capture' to capture a 640x480 frame capture.size(640, 480); //If we don't "hide" the capture, P5 will just //display it onscreen wherever it wants. //We'll 'hide' it so we can control where/how it's displayed capture.hide(); } function draw() { //draw frame to screen, aligned to top-left (0,0) //and as wide and tall as our canvas (640, 480) image(capture, 0, 0, 640, 480); }
var capture;
function setup() {
createCanvas(640, 480);
capture = createCapture(VIDEO);
capture.size(640, 480);
capture.hide();
}
function draw() {
image(capture, 0, 0, 640, 480);
filter(POSTERIZE,3); // add a visual effect
}
index | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 |
contents | pixel #0's red value | pixel #0's green value | pixel #0's blue value | pixel #0's alpha value | pixel #1's red value | pixel #1's green value | pixel #1's blue value | pixel #1's alpha value | pixel #2's red value | pixel #2's green value | pixel #2's blue value | pixel #2's alpha value |
index | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 |
contents | pixel #0's red value | pixel #0's green value | pixel #0's blue value | pixel #0's alpha value | pixel #1's red value | pixel #1's green value | pixel #1's blue value | pixel #1's alpha value | pixel #2's red value | pixel #2's green value | pixel #2's blue value | pixel #2's alpha value |
Pixel #0 | Pixel #1 | Pixel #2 |
//I have an array named 'image.pixels,' which contains the color data of an image named 'image' image.pixels[0] = 255; //set the red value for the first pixel, pixel #0, to 255 image.pixels[1] = 0; //set the green value for the first pixel, pixel #0, to 0 image.pixels[2] = 100; //set the blue value for the first pixel, pixel #0, to 100 int counter=0; // we'll use this variable in a loop below to move through each pixel for (counter = 0; counter < image.width*image.height; counter+=4){ //a for loop that moves through every pixel in 'image' image.pixels[counter*4] = random(255); //Set the current pixel's red value to a random number image.pixels[counter*4+1] = random(255); //Set the current pixel's green value to a random number image.pixels[counter*4+2] = random(255); //Set the current pixel's blue value to a random number }
//noprotect var capture; //creates a variable to store the current video frame var vidframe; function setup() { createCanvas(640, 480); capture = createCapture(VIDEO); capture.size(640, 480); capture.hide(); //set the size of the 'vidframe' to store frames captured by 'capture' vidframe = createGraphics(640, 480); } function draw() { //draws the captured frame into our 'vidframe' image vidframe.image(capture, 0, 0, 640, 480); //creates an array of pixel color information that we can tweak and change vidframe.loadPixels(); var counter;// this variable will take us through the loop to edit each pixel //use a for loop to travel through the array of pixel data and make changes for (counter = 0; counter < vidframe.width*vidframe.height; counter++){ //grab the colors for the current pixel var r = vidframe.pixels[counter*4]; //store the red value of this pixel var g = vidframe.pixels[counter*4+1]; //store the green value of this pixel var b = vidframe.pixels[counter*4+2]; //store the blue value of this pixel //check the total color values of the current pixel //If it's dark, replace the pixel with neon green! if (r+g+b < 150){ vidframe.pixels[counter*4] = 0; //the red value vidframe.pixels[counter*4+1] = 255; //the green value vidframe.pixels[counter*4+2] = 0; //the blue value } } //updatePixels() locks in any changes we made to the pixels vidframe.updatePixels(); //draw our "vidframe" image onto our canvas image (vidframe, 0, 0, 640, 480); }
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 |
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 |
0 | 1 | 2 | 3 | 4 | 5 | |
0 | ||||||
1 | ||||||
2 | ||||||
3 | ||||||
4 |
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 |
0 | 1 | 2 | 3 | 4 | 5 | |
0 | ||||||
1 | ||||||
2 | ||||||
3 | ||||||
4 |
//noprotect var capture; var vidframe; function setup(){ createCanvas(640, 480); capture = createCapture(VIDEO); capture.size(640, 480); capture.hide(); vidframe = createGraphics(640, 480); } function draw(){ background(0); //draws the captured frame into our 'vidframe' image vidframe.image(capture,0,0,640,480); //creates an array of pixel color information that we can tweak and change vidframe.loadPixels(); var counter; //Use a loop to travel through every pixel of the video image. //Notice we're counting up 10 at a time in these loops //That's because we don't need to look at every pixel, //just every 10th pixel, so we can draw a circle. for (counter = 0; counter < width*height; counter +=10){ //Use the modulus in an 'if' statement to make sure //we draw ellipses only every 10 rows if (int(counter/width)%10==0){ //IF we're on row 0, 10, 20, 30, etc... var r = vidframe.pixels[counter*4]; //store red value of this pixel var g = vidframe.pixels[counter*4+1];//store green value of this pixel var b = vidframe.pixels[counter*4+2];//store blue value of this pixel // use that color to draw next shape fill(color(r,g,b)); // calculate what ‘column’ we’re on // by taking the modulus of our current // position in the pixel array over // the width of the screen var column = int(counter % width); // calculate what ‘row’ we’re on // by dividing our current // position in the pixel array by // the width of the screen var row = int(counter / width); //draw the 10x10 circle at the pixel position ellipse(column, row, 10, 10); } } }
//noprotect var capture; var vidframe; function setup(){ createCanvas(640, 480); capture = createCapture(VIDEO); capture.size(640, 480); capture.hide(); vidframe = createGraphics(640, 480); } function draw(){ background(0); //draws the captured frame into our 'vidframe' image vidframe.image(capture,0,0,640,480); //creates an array of pixel color information that we can tweak and change vidframe.loadPixels(); var counter; //Use a loop to travel through every pixel of the video image. //Notice we're counting up 10 at a time in these loops //That's because we don't need to look at every pixel, //just every 10th pixel, so we can draw a circle. for (counter = 0; counter < width*height; counter +=10){ //Use the modulus in an 'if' statement to make sure //we draw ellipses only every 10 rows if (int(counter/width)%10==0){ //IF we're on row 0, 10, 20, 30, etc... var r = vidframe.pixels[counter*4]; //store red value of this pixel var g = vidframe.pixels[counter*4+1];//store green value of this pixel var b = vidframe.pixels[counter*4+2];//store blue value of this pixel // use that color to draw next shape, first adding an alpha value fill(color(r,g,b, 100)); noStroke(); // calculate what ‘column’ we’re on // by taking the modulus of our current // position in the pixel array over // the width of the screen var column = int(counter % width); // calculate what ‘row’ we’re on // by dividing our current // position in the pixel array by // the width of the screen var row = int(counter / width); //draw the circle at the pixel position, with randomized size ellipse(column, row, random(20,50), random(20,50)); } } }