xxxxxxxxxx
116
// Array to store the flower objects
let flowers = [];
// Sound variables
let song;
let amplitude;
let offset = 1000;
// Variables for symmetry
let maxaxis = 0;
let minaxis = 4;
// Function to preload the audio file
function preload() {
song = loadSound('Interstellar.mp3');
}
// Function to handle key presses
function keyPressed() {
if (key === ' ') {
save("test.svg");
}
}
// Setup function to initialize canvas and objects
function setup() {
createCanvas(windowWidth, windowHeight);
angleMode(DEGREES);
background(0, 0);
song.play(); // Start the audio
amplitude = new p5.Amplitude(); // Initialize amplitude for audio analysis
// Create a flower object at the center of the canvas
flowers.push(new Flower(width / 2, height / 2));
}
// Draw function which updates every frame
function draw() {
background(0, 8);
// Get the audio level
let level = amplitude.getLevel();
// Map the audio level to the axis of symmetry
minaxis = round(map(level, 0, 1, 4, 30))
maxaxis = max(minaxis,maxaxis);
increment = map(level*0.8, 0, 1, 0.0008, 0.3);
offset = map(level, 0, 1, 0, 1000);
// Update and display the flower
for (let flower of flowers) {
flower.update();
flower.display();
}
}
// Flower class
class Flower {
constructor(x, y) {
this.pos = createVector(x, y); // Position vector
this.symmetry = int(random(maxaxis, maxaxis+1)); // Random symmetry
this.angle = 360 / this.symmetry; // Angle based on symmetry
// Perlin noise values
this.xoff = random(0, 1000);
this.yoff = this.xoff + offset;
// Map noise values for x and y coordinates
this.mx = map(noise(this.xoff), 0, 1, -width*0.6, width*0.6);
this.my = map(noise(this.yoff), 0, 1, -height*0.6, height*0.6);
this.px = this.mx;
this.py = this.my;
this.timer = 0; // Timer for symmetry change
}
update() {
// Update previous positions
this.px = this.mx;
this.py = this.my;
// Update current positions based on noise
this.mx = map(noise(this.xoff), 0, 1, -width*0.8, width*0.8);
this.my = map(noise(this.yoff), 0, 1, -height*0.8, height*0.8);
// Increment the noise values
this.xoff += increment;
this.yoff += increment;
this.timer++;
// Change symmetry based on timer
if (this.timer % 100 === 0) {
increment = random(0.01, 0.02);
this.symmetry = int(random(maxaxis, maxaxis+1));
this.angle = 360 / this.symmetry;
}
}
display() {
push();
translate(this.pos.x, this.pos.y);
// Get color based on timer
let col = color(map(this.timer % 200, 0, 255, 100, 255),
map(sin(this.timer * 0.05), -1, 1, 100, 255),
map(cos(this.timer * 0.05), -1, 1, 100, 255));
stroke(col);
let sw = 2;
strokeWeight(sw);
// Draw the symmetry lines
for (let i = 0; i < this.symmetry; i++) {
rotate(this.angle);
line(this.mx, this.my, this.px, this.py);
push();
scale(1, -1);
line(this.mx, this.my, this.px, this.py);
pop();
}
pop();
}
}