/* // Interactive Particle Generator // Coding Challenge #particleTrain // Diego de la Fuente Curaqueo // Santiago de Chile, 2017 */ let particulas = []; let mic, miclvl; function setup() { createCanvas(windowWidth, windowHeight, P2D); let p = new Particula(); for (let i = 0; i < 200; i++) { particulas[i] = new Particula(); } mic = new p5.AudioIn() mic.start(); miclvl=0; } function draw() { background(0); miclvl = constrain(lerp(miclvl, mic.getLevel() * 1.5, .6), 0, 1); for (let i = 0; i < particulas.length; i++) { particulas[i].mostrar(); particulas[i].animar(); particulas[i].setPorte(miclvl*50); particulas[i].reaparecer(mouseX, mouseY); } } class Particula { constructor() { this.pos = createVector(width / 2, height / 2); this.vel = createVector(random(-3, 3), random(-3, 3)); this.acel = createVector(1, 1); this.forma = int(random(100)); this.rot = random(0, 45); this.rotInc = random(-1, 1); this.diam = random(10, 30); this.rd = random(20); this.alpha = random(24, 48); this.color = color(0, random(127, 196), random(196, 255), this.alpha); } mostrar() { noStroke(); fill(this.color); push(); translate(this.pos.x, this.pos.y); rotate(this.rot); if (this.forma > 50) { rectMode(CENTER); rect(0, 0, this.diam, this.diam); }else{ ellipseMode(CENTER); ellipse(0, 0, this.diam, this.diam+this.rd); } pop(); this.rot += this.rotInc; } animar() { this.acel.set(random(-2, 2), random(-2, 2)); this.vel.add(this.acel.x, this.acel.y); this.pos.add(this.vel.x, this.vel.y); this.diam -= this.rd; this.diam = constrain(this.diam, 0, 50); } reaparecer(_x, _y) { //evalua condiciones para reaparecer (fuera de canvas o demasiado pequeño) let fuera = boolean((this.pos.x > width + this.diam || this.pos.x < 0 - this.diam && this.pos.y > height + this.diam || this.pos.y < 0 - this.diam) || this.diam < 1) //reconstruye la partícula if (fuera) { this.pos.set(_x, _y); this.vel.set(random(-2, 2), random(-2, 2)); this.acel.set(1, 1); this.rot = random(0, 45); this.rotInc = random(-1, 1); this.diam = random(10, 30); this.rd = random(20); this.forma = int(random(100)); } } setPorte(tam){ this.diam += tam; } }