// Self Portrait 3 // September 21, 2017 -- John Clavin var human; var move = false; var controlCounter; var numOfObjects = 2500; function preload() { human = loadImage("johnclavincolor.png"); } function setup() { createCanvas(599, 702); frameRate(30); background(248); controlCounter = 0; rectMode(CENTER); noStroke(); painting = new Painting(); human.loadPixels(); for (var i = 0; i < numOfObjects; i++) { var rndX = floor(random(600)); var rndY = floor(random(703)); var pixelNumber = (rndY * width + rndX) * 4; var redAmount = human.pixels[pixelNumber]; var greenAmount = human.pixels[pixelNumber + 1]; var blueAmount = human.pixels[pixelNumber + 2]; var truColor = color(redAmount, greenAmount, blueAmount); painting.addObject(createVector(rndX, rndY), truColor); } //fill(0); var attractor = createVector(random(10, 260), random(10, 300)); //ellipse(attractor.x, attractor.y, 20, 20); painting.addAttractor(attractor); attractor = createVector(random(340, 580), random(10, 300)); //ellipse(attractor.x, attractor.y, 20, 20); painting.addAttractor(attractor); attractor = createVector(random(10, 260), random(300, 680)); //ellipse(attractor.x, attractor.y, 20, 20); painting.addAttractor(attractor); attractor = createVector(random(340, 580), random(300, 680)); //ellipse(attractor.x, attractor.y, 20, 20); painting.addAttractor(attractor); attractor = createVector(random(100, 500), random(100, 600)); //fill(250, 0, 0); //ellipse(attractor.x, attractor.y, 20, 20); painting.addAttractor(attractor); for (var j = 0; j < numOfObjects; j++) { painting.addRandColor(j); painting.assignAttractors(j); } } function draw() { painting.run(); if (frameCount > 80) { move = true; } if (frameCount > 110) { noLoop(); } } function Painting() { this.paintObjects = []; this.attractors = []; this.addObject = function(pos, truColor) { var p = new PaintObject(pos, truColor); this.paintObjects.push(p); } this.addRandColor = function(oNumber) { var r = this.paintObjects[floor(random(numOfObjects))].truColor; this.paintObjects[oNumber].randomColor = r; } this.addAttractor = function(a) { this.attractors.push(a); } this.assignAttractors = function(aNumber) { var pA1 = this.attractors[0]; var pA2 = this.attractors[1]; var pA3 = this.attractors[2]; var pA4 = this.attractors[3]; var pA5 = this.attractors[4]; this.paintObjects[aNumber].attractor1 = pA1; this.paintObjects[aNumber].attractor2 = pA2; this.paintObjects[aNumber].attractor3 = pA3; this.paintObjects[aNumber].attractor4 = pA4; this.paintObjects[aNumber].attractor5 = pA5; } this.run = function() { controlCounter++; for (var i = 0; i < this.paintObjects.length; i++) { this.paintObjects[i].update(); this.paintObjects[i].calcAttraction(); this.paintObjects[i].render(); } } } function PaintObject(pos, truColor) { this.humanPos = pos; this.truColor = truColor; this.randomColor; this.objectColor; this.movingPos = this.humanPos; this.velocity = createVector(0, 0); this.attractor1; this.attractor2; this.attractor3; this.attractor4; this.attractor5; this.calcAttraction = function() { if (move === true) { var force1 = p5.Vector.sub(this.attractor1, this.movingPos); var force2 = p5.Vector.sub(this.attractor2, this.movingPos); var force3 = p5.Vector.sub(this.attractor3, this.movingPos); var force4 = p5.Vector.sub(this.attractor4, this.movingPos); var force5 = p5.Vector.sub(this.attractor5, this.movingPos); force1.normalize(); force2.normalize(); force3.normalize(); force4.normalize(); force5.normalize(); force1.mult(random(1.0, 3.0)); force2.mult(random(1.0, 3.0)); force3.mult(random(1.0, 3.0)); force4.mult(random(1.0, 3.0)); force5.mult(random(1.0, 3.0)); var subTotal_1 = p5.Vector.sub(force1, force2); var subTotal_2 = p5.Vector.sub(force3, force4); var subTotal_3 = p5.Vector.sub(subTotal_1, subTotal_2); var forceTotal = p5.Vector.add(subTotal_3, force5) this.velocity = forceTotal; } else { this.velocity = createVector(0, 0); } } this.update = function() { var moveAmt = 1; this.x_decision = floor(random(3, 6)); if (this.x_decision == 3) { this.spreadto_x = -moveAmt; } else if (this.x_decision == 4) { this.spreadto_x = 0; } else { this.spreadto_x = moveAmt; } this.y_decision = floor(random(3, 6)); if (this.y_decision == 3) { this.spreadto_y = -moveAmt; } else if (this.y_decision == 4) { this.spreadto_y = 0; } else { this.spreadto_y = moveAmt; } this.spreadto_x += this.movingPos.x; this.spreadto_y += this.movingPos.y; this.movingPos.x = this.spreadto_x; this.movingPos.y = this.spreadto_y; this.movingPos.add(this.velocity); } this.render = function() { this.objectColor = this.truColor; //this.objectColor = this.randomColor; var dotSize = 3; fill(this.objectColor); ellipse(this.movingPos.x, this.movingPos.y, dotSize, dotSize); } }