Skip to content

Instantly share code, notes, and snippets.

@nossidge
Created March 7, 2013 15:11
Show Gist options
  • Save nossidge/5108711 to your computer and use it in GitHub Desktop.
Save nossidge/5108711 to your computer and use it in GitHub Desktop.
BlurredWiggleRectangle
// Save as animated GIF
// Requires gifAnimation Processing Library
// http://www.extrapixel.ch/processing/gifAnimation/
import gifAnimation.*;
GifMaker gifExport;
boolean saveToGif = true;
// The time between GIF frames
int frameDelay = 50;
// The pixel size for the canvas
float canvasDimRatio = 1;
int xCanvasDimension = int(300*canvasDimRatio);
int yCanvasDimension = xCanvasDimension;
// Declare the Shape array
ArrayList<Shape> rectArray = new ArrayList<Shape>();
// Wiggle stuff
int[] wiggleDegrees = {-4,-3,-2,-1,0,1,2,3,4,3,2,1,0,-1,-2,-3};
int wiggleCounter = 0;
//////////////////////////////////////////////////////////////////////
void setup(){
frameRate(10);
size(xCanvasDimension,yCanvasDimension);
createCircles();
gifCreateNew();
}
//////////////////////////////////////////////////////////////////////
void createCircles() {
rectArray = new ArrayList<Shape>();
Shape newRect;
float rectCount = 16;
float rectDistanceBetween = 23;
float rectDim = rectDistanceBetween * rectCount;
for (int i = 0; i < rectCount; i++) {
newRect = new Shape(xCanvasDimension/2 - rectDim/2, yCanvasDimension/2 - rectDim/2,
//rectDim, rectDim, 0, 0, color(255,100), color(0));
//rectDim, rectDim, 0, 0, color(random(0,255),100), color(0));
//rectDim, rectDim, 0, 0, color(random(0,255),random(0,255),random(0,255),100), color(0));
//rectDim, rectDim, 0, 0, color(random(0,255),random(0,255),random(0,255),100),
// color(random(0,255),random(0,255),random(0,255),100));
//rectDim, rectDim, 0, 0, color(0,0, 55 + (200/rectCount)*i ,100), color(0));
//rectDim, rectDim, 0, 0, color(0,0, 55 + (200/rectCount)*i ), color(0));
//rectDim, rectDim, 0, 0, color(55 + (200/rectCount)*(rectCount-i) ), color(0));
rectDim, rectDim, 0, 0, color(55 + (200/rectCount)*i ), color(0));
rectArray.add(newRect);
rectDim = rectDim - rectDistanceBetween;
}
}
//////////////////////////////////////////////////////////////////////
void draw() {
//background(0);
background(255);
// Wiggle
if (wiggleCounter >= wiggleDegrees.length) {
wiggleCounter = 0;
}
int shapeCounter = 0;
for(Shape iRect:rectArray) {
translate(width/2, height/2);
rotate(radians(wiggleDegrees[ (shapeCounter+wiggleCounter) % wiggleDegrees.length]));
translate(-width/2, -height/2);
iRect.drawShape();
shapeCounter++;
//filter(BLUR,2);
}
wiggleCounter++;
filter(BLUR,8);
// end wiggle
gifAddFrame();
if (frameCount==wiggleDegrees.length) {
gifSave();
}
}
//////////////////////////////////////////////////////////////////////
void keyPressed(){
println("pressed " + int(key) );
switch( int(key) ) {
case 114: saveToGif = true; gifCreateNew(); break; // _R_ecord new gif
case 115: gifSave(); break; // _S_ave
case 110: createCircles(); break; // _N_ew pattern
}
}
void stop() {
gifSave();
}
//////////////////////////////////////////////////////////////////////
void gifCreateNew() {
if (saveToGif) {
gifExport = new GifMaker(this, System.currentTimeMillis() + ".gif");
gifExport.setRepeat(0);
}
}
void gifAddFrame() {
if (saveToGif) {
gifExport.setDelay(frameDelay);
gifExport.addFrame();
}
}
void gifSave() {
if (saveToGif) {
gifExport.setDelay(frameDelay);
gifExport.finish();
saveToGif = false;
}
}
//////////////////////////////////////////////////////////////////////
// NOT the same Shape class as before
// This one draws from x & y pixel pos, not cell
class Shape extends PVector {
String defaultShape = "rect";
//String defaultShape = "ellipse";
PVector shapeDim = new PVector();
PVector shapeVel = new PVector();
PVector originalPos = new PVector();
PVector originalDim = new PVector();
Boolean inOriginalPos = true;
Boolean offScreen = false;
Boolean killed = false;
color fill;
color stroke;
int zLayer; // White should be on top, so draw them last
int drawOrigin = CORNER; // Supports CORNER and CENTER only
////////////////////////////////////////////
Shape(float xPos, float yPos,
float xCellW, float yCellH,
float xVel, float yVel,
color inputFill, color inputStroke) {
this.set(xPos, yPos, 0.0);
originalPos.set(xPos, yPos, 0.0);
shapeDim.set(xCellW, yCellH, 0.0);
originalDim.set(xCellW, yCellH, 0.0);
shapeVel.set(xVel, yVel, 0.0);
this.fill = inputFill;
this.stroke = inputStroke;
}
////////////////////////////////////////////
// CENTER is more accurate, but CORNER is more weird
void setColors() {
fill(this.fill);
stroke(this.stroke);
ellipseMode(this.drawOrigin);
rectMode(this.drawOrigin);
strokeWeight(1);
strokeWeight(2);
strokeWeight(0);
noStroke();
}
void setDrawOrigin(int originType){
this.drawOrigin = originType;
}
////////////////////////////////////////////
void drawShape() {
drawShape(this.shapeDim.x, this.shapeDim.y);
}
void drawShape(float dimensions) {
drawShape(dimensions, dimensions);
}
void drawShape(float xWidth, float yHeight) {
if (defaultShape == "rect") {
drawRect();
} else if (defaultShape == "ellipse") {
drawEllipse(xWidth,yHeight);
} else if (defaultShape == "quad") {
drawRandomQuad(xWidth,yHeight);
} else {
drawRandomQuad(xWidth,yHeight);
}
}
////////////////////////////////////////////
void drawEllipse() {
drawEllipse(this.shapeDim.x, this.shapeDim.y);
}
void drawEllipse(float dimensions) {
drawEllipse(dimensions, dimensions);
}
void drawEllipse(float xWidth, float yHeight) {
setColors();
// If CORNER
float xPos = this.x;
float yPos = this.y;
if (drawOrigin==CENTER) {
xPos = this.x + (this.originalDim.x / 2);
yPos = this.y + (this.originalDim.y / 2);
}
ellipse(xPos, yPos, xWidth, yHeight);
}
////////////////////////////////////////////
void drawRect() {
drawRect(this.shapeDim.x, this.shapeDim.y);
}
void drawRect(float dimensions) {
drawRect(dimensions, dimensions);
}
void drawRect(float xWidth, float yHeight) {
setColors();
// If CORNER
float xPos = this.x;
float yPos = this.y;
if (drawOrigin==CENTER) {
xPos = this.x + (this.originalDim.x / 2);
yPos = this.y + (this.originalDim.y / 2);
}
rect(xPos, yPos, xWidth, yHeight);
}
////////////////////////////////////////////
void drawRandomQuad() {
drawRandomQuad(this.shapeDim.x, this.shapeDim.y);
}
void drawRandomQuad(float dimensions) {
drawRandomQuad(dimensions, dimensions);
}
void drawRandomQuad(float xWidth, float yHeight) {
PVector TopLeft = new PVector(this.x, this.y, this.z);
PVector TopRight = new PVector(TopLeft.x + xWidth, TopLeft.y, this.z);
PVector BottomLeft = new PVector(TopLeft.x, TopLeft.y + yHeight, this.z);
PVector BottomRight = new PVector(BottomLeft.x + xWidth, BottomLeft.y, this.z);
float TopPointX = random(TopLeft.x, TopRight.x);
float BottomPointX = random(BottomLeft.x, BottomRight.x);
float LeftPointY = random(TopLeft.y, BottomLeft.y);
float RightPointY = random(TopRight.y, BottomRight.y);
setColors();
strokeWeight(2);
stroke(0);
ellipse(TopLeft.x, TopLeft.y, xWidth, yHeight);
strokeWeight(2);
//fill(110,110,110,123);
beginShape();
vertex(TopPointX, TopLeft.y); //Top
vertex(TopRight.x, RightPointY); //Right
vertex(BottomPointX, BottomLeft.y); //Bottom
vertex(TopLeft.x, LeftPointY); //Left
vertex(TopPointX, TopLeft.y); //Top
endShape();
}
////////////////////////////////////////////
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment