Idea
For this assignment, I was inspired by how fireflies blink synchronously in the night. At first look, it might seem like they are just flashing their light randomly, but they are actually communicating with each other through the blinking.
I thought this was very ingenious of them, so I want to replicate this into coding. My idea is having a main firefly interacting with other fireflies. Whenever the main firefly (cursor) is near any other fireflies, they will start talking (blinking!) with each other.
Implementation
To execute my idea, I use two class – one for the main firefly and one for the rest of the swarm. I use the class Swarm as a subclass of Firefly to reduce repetitive coding, using some shared variables (eg. glowing, size, x, y) and overwrite the move function for the difference in the movement between the main firefly and the swarm.
First, let’s talk about the main firefly. I took reference from delayed mouse following code by kjhollen (https://editor.p5js.org/kjhollen/sketches/rJItdyEt-). For the continuous glowing light, I increase and reduce the size of the shadow to create effects of glowing as in the code below
glowing(){ //blinking of firefly light //if the glow size is bigger than 40, set glow to false and decrease size if (this.size>40){ this.glow = false; } //if size is negative, set glow to true and increase the size if (this.size < 0){ this.glow = true; } if (this.glow == true){ this.size += 1; }else if (this.glow == false){ this.size -= 1; } }
For the Swarm, most remain similar. The main difference is the swarm only glow when the cursor is near, to which I implement this code to check the condition
blinking(){ // blink if main firefly is within 100 distance let distance = ((this.x - mouseX)**2 + (this.y - mouseY)**2)**(1/2); if(distance < 100){ this.on = true; }else{ this.on = false; } }
I use an array to keep track of all (40) fireflies on the screen:
let fireflies = []; //list of all fireflies function setup() { createCanvas(400, 400); colorMode(HSB,360,100,100,100); let x = 50; let y = 50; //create 40 fireflies with random positions for(i= 0; i<40; i++){ fireflies.push(new Swarm(x,y)); x = random(300); y = random(300); } }
Final Piece
Reflection
For this assignment, I am happy with how my vision turned out like what I imagined. The fireflies glow together creates an eye-pleasing composition and interesting piece in my opinion. However, I would like to improve on the glowing pattern – the blink still happens randomly without a clear pattern. In addition, I would also like to try different representation of fireflies to further replicate the experience.