Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Interactive Cubist Portrait • Pixel by Pixel Midterm Project, NYU ITP Spring 2018
import java.util.*;
import processing.video.*;
Capture camera;
int totalRects = 10;
Rectangle[] rectangles = new Rectangle[totalRects];
int x, y, w, h, a;
void setup() {
fullScreen();
frameRate(120);
camera = new Capture(this, width, height);
camera.start();
for (int i = 0; i < rectangles.length; i++) {
getRectValues();
rectangles[i] = new Rectangle(x, y, w, h, a);
}
}
void draw() {
if (camera.available()) camera.read();
pushMatrix();
scale(-1, 1);
//blendMode(LIGHTEST); // creates a surreal painting effect when combined with blendMode(DARKEST) below
image(camera, -width, 0);
popMatrix();
for (int i = 0; i < rectangles.length; i++) {
if (rectangles[i].dragging == true) {
rectangles[i].update(mouseX, mouseY);
}
rectangles[i].display();
}
}
void getRectValues() {
x = int(random(width*.75));
y = int(random(height*.75));
float widthFactor = random(3, 7);
float heightFactor = random(3, 7);
w = int(width/widthFactor);
h = int(height/heightFactor);
a = int(random(160, 255));
}
void mousePressed() {
for (int i = 0; i < rectangles.length; i++) {
rectangles[i].pressed(mouseX, mouseY);
}
}
void mouseReleased() {
for (int i = 0; i < rectangles.length; i++) {
rectangles[i].notPressed();
}
}
void keyPressed() {
if (key == 's') {
Date d = new Date();
save("screenshots/screen_" + d.getTime() +".jpg");
}
if (key == CODED) {
if (keyCode == UP) {
// from: http://learningprocessing.com/examples/chp09/example-09-11-array-append (Thank you, Shiffman!)
getRectValues();
Rectangle r = new Rectangle(x, y, w, h, a);
rectangles = (Rectangle[]) append(rectangles, r);
}
}
if (key == CODED) {
if (keyCode == DOWN) {
// from: https://forum.processing.org/two/discussion/12987/how-can-i-remove-an-object-from-an-array-of-objects
rectangles = (Rectangle[]) shorten(rectangles);
}
}
}
class Rectangle {
PGraphics crop;
int x, y, w, h, a, offsetX, offsetY;
boolean dragging;
float factor;
Rectangle(int x, int y, int w, int h, int a) {
this.x = x;
this.y = y;
this.w = w;
this.h = h;
this.a = a;
this.offsetX = -(w/2);
this.offsetY = -(h/2);
this.dragging = false;
factor = random(0.1, 1);
crop = createGraphics(w, h);
}
void update(int px, int py) {
this.x = width - px + this.offsetX;
this.y = py + this.offsetY;
}
void display() {
pushMatrix();
translate(width, 0);
scale(-1, 1);
stroke(0);
strokeWeight(2);
//noStroke(); // use with surreal painting effect in combo with the two blend modes and comment out stroke & strokeWeight
noFill();
rect(this.x, this.y, this.w, this.h);
crop.beginDraw();
crop.copy(camera, this.x, this.y, int(this.w*factor), int(this.h*factor), 0, 0, this.w, this.h);
crop.endDraw();
tint(255, this.a);
//blendMode(DARKEST); // creates a surreal painting effect when combined with blendMode(DARKEST) below
image(crop, this.x, this.y);
popMatrix();
}
void pressed(int px, int py) {
int mx = width-px; // from: https://forum.processing.org/one/topic/translating-mouse-coordinates.html
if (mx > this.x && mx < this.x + this.w && py > this.y && py < this.y + this.h) {
this.dragging = true;
this.offsetX = this.x - mx;
this.offsetY = this.y - py;
this.update(px, py);
}
}
void notPressed() {
this.dragging = false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.