Difference between revisions of "Dance in the Dark.pde"

From Robert-Depot
Jump to: navigation, search
(New page: //Dance in the Dark import processing.video.*; Capture video; PGraphics buffer; PImage img; int brightestX = 0; // X-coordinate of the brightest video pixel int brightestY = 0; // Y-coor...)
 
(No difference)

Latest revision as of 15:13, 5 June 2010

//Dance in the Dark

import processing.video.*; Capture video; PGraphics buffer; PImage img;

int brightestX = 0; // X-coordinate of the brightest video pixel int brightestY = 0; // Y-coordinate of the brightest video pixel int lastX, lastY;

int currentFrame = 0; PImage[] frames = new PImage[12]; int lastTime = 0;



void setup() { size(700, 500, P3D); video = new Capture(this, width, height, 30); noStroke(); smooth(); buffer = createGraphics(700, 500, P3D);

background(102);

for (int i = 0; i < frames.length; i++) { frames[i] = get(); // Create a blank frame }


}




void draw() { background(0);

int currentTime = millis(); if (currentTime > lastTime+30) {

lastTime = currentTime; //Setting the refresh rate of the recording }

//replaces mousepressing line drawing if (video.available()) { int threshold = 1; video.read(); image(video, 0, 0, width, height); // Draw the webcam video onto the screen

img=buffer.get(); image(buffer, 0, 0, 700, 500); // RDT: draw the offscreen buffer on top of the webcam video nextFrame();

//filter(BLUR, 2); float brightestValue = 0; // Brightness of the brightest video pixel // Search for the brightest pixel: For each row of pixels in the video image and // for each pixel in the yth row, compute each pixel's index in the video video.loadPixels(); int index = 0;


lastX = brightestX; lastY = brightestY;

for (int y = 0; y < video.height; y++) { for (int x = 0; x < video.width; x++) { // Get the color stored in the pixel int pixelValue = video.pixels[index]; // Determine the brightness of the pixel float pixelBrightness = brightness(pixelValue); // If that value is brighter than any previous, then store the // brightness of that pixel, as well as its (x,y) location if (pixelBrightness > brightestValue) { brightestValue = pixelBrightness; brightestY = y; brightestX = x; } index++; } }


//fill(255, 100, 50, 70); if (brightestValue > threshold) { // If the test location is brighter than noStroke(); //fill (255); // the threshold set the fill to black

variableEllipse(lastX, lastY, brightestX, brightestY); nextFrame(); } else { // Otherwise, noStroke(); //fill(255,0); // set the fill to random variableEllipse(lastX, lastY, brightestX, brightestY); nextFrame(); }

}

}

void nextFrame() { frames[currentFrame] = buffer.get(); // Get the display window currentFrame++; // Increment to next frame if (currentFrame >= frames.length) { currentFrame = 0; } image(frames[currentFrame], 0, 0); }


void variableEllipse(int x, int y, int px, int py) { float speed = abs(x-px) + abs(y-py);

stroke(speed);

// RDT: draw to the offscreen buffer buffer.beginDraw(); buffer.noStroke(); buffer.fill(random(0,255), random(0,255), random(0,255), 160); buffer.ellipse(px, py, speed/4, speed/4); buffer.endDraw();

}

if mouseClicked(){ setup(); }