Flutter

While googling illustrations for my first idea for this assignment (I’ve been changing my mind a lot with these projects lately), I found an image of a butterfly fluttering, composed of nine frames.

“Monarch Butterflies in Motion”

Given that we have been focusing on visualization in motion, I thought it would be interesting (and conventiently straightforward) to create an animation of the butterfly in movement (Processing sketch here). The frames in the image I found allow said movement to be smooth and realistic. By creating a butterfly class, the animation can contain as many butterflies as desired with varying characteristics. For these objects in particular, motion and position offer the possibilities for variation, as the code shows.

PImage bf1, bf2, bf3, bf4, bf5, bf6, bf7, bf8, bf9;
PImage bg;
Butterfly bfA, bfB, bfC, bfD, bfE, bfF, bfG, bfH, bfI, bfJ, bfK, bfL, bfM, bfN;
int counter;
boolean start;

void setup(){
 size(1000, 1000);
 bf1 = loadImage("bf1.png");
 bf2 = loadImage("bf2.png");
 bf3 = loadImage("bf3.png");
 bf4 = loadImage("bf4.png");
 bf5 = loadImage("bf5.png");
 bf6 = loadImage("bf6.png");
 bf7 = loadImage("bf7.png");
 bf8 = loadImage("bf8.png");
 bf9 = loadImage("bf9.png");
 //frames of butterfly animation (9 in total)
 bfA = new Butterfly(0, 450, 1, 10, 10);
 bfB = new Butterfly(200, 350, 2, 10, 2);
 bfC = new Butterfly(300, 300, 3, 5, 5);
 bfD = new Butterfly(400, 500, 1, 15, 10);
 bfE = new Butterfly(50, 400, 3, 10, 10);
 bfF = new Butterfly(300, 600, 2, 10, 5);
 bfG = new Butterfly(200, 700, 4, 5, 10);
 bfH = new Butterfly(100, 800, 3, 15, 15);
 bfI = new Butterfly(10, 900, 1, 10, 10);
 bfJ = new Butterfly(50, 750, 2, 10, 10);
 bfK = new Butterfly(400, 1000, 2, 10, 10);
 bfL = new Butterfly(100, 650, 3, 10, 10);
 bfM = new Butterfly(10, 850, 2, 5, 10);
 bfN = new Butterfly(10, 700, 4, 10, 5);
 //butterflies (objects); can be increased or decreased, unlike animation frames
 counter = 0;
 //counter increases with each draw() loop
 start = false;
 //controls start of animation
 
 int i = int(random(6));
 switch(i){
 case 0:
 bg = loadImage("white.jpg");
 break;
 case 1:
 bg = loadImage("meadow.jpg");
 break;
 case 2:
 bg = loadImage("beach.jpg");
 break;
 case 3:
 bg = loadImage("house.jpg");
 break;
 case 4: 
 bg = loadImage("city.jpg");
 break;
 case 5: 
 bg = loadImage("water.jpg");
 break;
 }
 //random number and switch determine which background will be showed alongside the butterflies (6 options)
}

void draw(){
 if(start == false){
 background(255);
 fill(255,140,0);
 textSize(50);
 text("Flutter", 420, 480);
 //displays "title screen"
 if(mousePressed){
 start = true;
 }
 //animation starts until mouse is pressed
 }
 else{
 background(bg);
 bfA.fly();
 bfB.fly();
 bfC.fly();
 bfD.fly();
 bfE.fly();
 bfF.fly();
 bfG.fly();
 bfH.fly();
 bfI.fly();
 bfJ.fly();
 bfK.fly();
 bfL.fly();
 bfM.fly();
 bfN.fly();
 counter++;
 };
}

class Butterfly{
 int X;
 int Y;
 int FRAME;
 int SPEED;
 int FORWARDS;
 int UPWARDS;
 
 Butterfly(int x, int y, int speed, int forwards, int upwards){
 X = x;
 Y = y;
 FRAME = int(random(1, 10));
 SPEED = speed;
 FORWARDS = forwards;
 UPWARDS = upwards;
 //original position of butterfly, speed (related to counter), and forward/upward movement are uniquely assigned for each object 
 //FRAME is randomly assigned and determines which is the starting frame for the animation of each butterfly
 }
 
 void fly(){
 if(FRAME == 1){
 image(bf1, X, Y);
 }
 else if(FRAME == 2){
 image(bf2, X, Y);
 }
 else if(FRAME == 3){
 image(bf3, X, Y);
 }
 else if(FRAME == 4){
 image(bf4, X, Y);
 }
 else if(FRAME == 5){
 image(bf5, X, Y);
 }
 else if(FRAME == 6){
 image(bf6, X, Y);
 }
 else if(FRAME == 7){
 image(bf7, X, Y);
 }
 else if(FRAME == 8){
 image(bf8, X, Y);
 }
 else if(FRAME == 9){
 image(bf9, X, Y);
 };
 //image to display per loop is determined by FRAME
 if(counter%SPEED == 0){
 X = X + FORWARDS;
 Y = Y - UPWARDS;
 //SPEED determines after how many draw() loops the position of the butterfly changes; FORWARDS and UPWARDS determine the horizontal and vertical components of the displacement
 if(FRAME == 9){
 FRAME = 1;
 }
 //this "if" statement allows the cycle of animation frames to restart after the ninth one is displayed
 else{
 FRAME++;
 };
 //"else" statement allows progression of animation frames
 };
 }
}

I separated the frames in the original image and made their white background transparent (not perfectly, as the white traces in the animation give away). I also decided to allow the background to change randomly, to play with realism (hopefully to augment it, in most cases). Most background are realistic. The white background is the basic, plain, void one. The under-the-sea background is just for the laughs or the intense confusion, whichever compels the user first.

 

1 thought on “Flutter”

Comments are closed.