// The Cycle of Life -- by John Clavin // September 26, 2016 -- Version 3 var human; function preload() { human = loadImage("human2.jpg"); } var dust; var controlCounter; function setup() { createCanvas(532, 640); frameRate(30); controlCounter = 0; rectMode(CENTER); noStroke(); dust = new Dust(); human.loadPixels(); for (var i = 0; i < 2880; i++) { var rndX = int(random(532)); var rndY = int(random(640)); var pixelNumber = (rndY * width + rndX) * 4; var redAmount = human.pixels[pixelNumber]; var greenAmount = human.pixels[pixelNumber+1]; var blueAmount = human.pixels[pixelNumber+2]; var redA = redAmount + random(-20, 20); var greenA = greenAmount + random(-25, 25); var blueA = blueAmount + random(-40, 5); var nuColor = color(redA, greenA, blueA); dust.addParticle(createVector(rndX, rndY), nuColor); } } function draw() { background(240); dust.run(); } function Dust() { this.particles = []; this.addParticle = function(pos, a) { var p = new Particle(pos, a); this.particles.push(p); } this.run = function() { controlCounter++; if(controlCounter > 650) { controlCounter = 0; for (var j = 0; j < this.particles.length; j++) { var rndX = int(random(532)); var rndY = int(random(640)); var pixelNumber = (rndY * width + rndX) * 4; var rAmount = human.pixels[pixelNumber]; var gAmount = human.pixels[pixelNumber+1]; var bAmount = human.pixels[pixelNumber+2]; this.particles[j].reset(rndX, rndY, rAmount, gAmount, bAmount); } } for (var i = 0; i < this.particles.length; i++) { this.particles[i].control(controlCounter); this.particles[i].update(); this.particles[i].render(); } } } function Particle(pos, pC) { this.photoPosition = pos; this.randomPosition = createVector(random(532), random(640)); this.randomForce = p5.Vector.random2D(); this.particleColor = pC; this.renderFlag1 = false; this.renderFlag2 = false; this.rectWidth = random(6, 28); this.rectHeight = random(6, 28); this.sizeCounter = 9; this.update = function() { if(random(100) < 3) { this.randomForce = p5.Vector.random2D(); this.randomForce.mult(random(2.0)); } this.photoForce = p5.Vector.sub(this.photoPosition, this.randomPosition); var distance = this.photoForce.mag(); this.photoForce.normalize(); if(distance < 5) { this.photoForce.mult(0.01); } else { this.photoForce.mult(2.8); } } this.control = function(cc) { var cCounter = cc; if(cCounter < 200) { this.randomPosition.add(this.randomForce); } if(cCounter > 200 && cCounter < 550) { this.randomPosition.add(this.photoForce); } if(cCounter > 280) { this.renderFlag1 = true; this.renderFlag2 = false; } if(cCounter > 550) { this.randomPosition.add(this.randomForce); this.renderFlag1 = false; this.renderFlag2 = true; } } this.reset = function(rX, rY, rA, gA, bA) { this.photoPosition.x = rX; this.photoPosition.y = rY; var redA = rA + random(-20, 20); var greenA = gA + random(-25, 25); var blueA = bA + random(-40, 5); this.particleColor = color(redA, greenA, blueA); this.renderFlag1 = false; this.renderFlag2 = false; } this.render = function() { fill(this.particleColor); if(this.renderFlag1) { if(this.sizeCounter < 100) { this.sizeCounter++; } } if(this.renderFlag2) { if(this.sizeCounter > 10) { this.sizeCounter--; } } var variableX = this.rectWidth * this.sizeCounter * 0.01; var variableY = this.rectHeight * this.sizeCounter * 0.01; rect(this.randomPosition.x, this.randomPosition.y, variableX, variableY); } }