xxxxxxxxxx
67
let roseImages = [];
let roses = [];
let dispersionSpeed = 2;
let mic;
let volThreshold = 0.1;
function preload() {
for (let i = 1; i < 5; i++) {
roseImages[i] = loadImage('flowor/rose' + i + '.png');
}
}
function setup() {
createCanvas(800, 800);
mic = new p5.AudioIn();
mic.start();
}
function draw() {
background(0);
let vol = mic.getLevel();
if (vol > volThreshold) {
createRandomRose();
}
for (let rose of roses) {
rose.disperse();
rose.show();
}
}
function createRandomRose() {
let randomX = random(width);
let randomY = random(height);
let r = random(10, 50);
let newRose = new Rose(randomX, randomY, r);
roses.push(newRose);
}
class Rose {
constructor(x, y, r) {
this.x = x;
this.y = y;
this.r = r;
this.imgIndex = floor(random(1, 5));
this.dispersionDir = createVector(random(-1, 1), random(-1, 1)).normalize();
this.transparent = 255;
}
disperse() {
this.x += this.dispersionDir.x * dispersionSpeed;
this.y += this.dispersionDir.y * dispersionSpeed;
this.r += 1;
this.transparent -= 1;
}
show() {
let imgWidth = (roseImages[this.imgIndex].width / 25) * (this.r / 10);
let imgHeight = (roseImages[this.imgIndex].height / 25) * (this.r / 10);
tint(255, this.transparent);
image(roseImages[this.imgIndex], this.x, this.y, imgWidth, imgHeight);
}
}