quaddamage

A big hairy idea

Apr 30th, 2016
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var img1, img2;
  2. var imgString1 = "https://s3-us-west-2.amazonaws.com/s.cdpn.io/151574/earth4.jpg";
  3. var imgString2 = "https://s3-us-west-2.amazonaws.com/s.cdpn.io/151574/earth5.jpg";
  4. var music;
  5. var agents = [];
  6. var col = [];
  7. var fieldIntensity = 10,
  8.   noiseScale = 800,
  9.   radius = 300,
  10.   stepSize = 1;
  11. var a = 0.25,
  12.   bg = img1,
  13.   c = 0;
  14. var step = 15;
  15. var targetHue = [255, 0, 0];
  16.  
  17. function preload() {
  18.   img1 = loadImage(imgString1);
  19.   img2 = loadImage(imgString2);
  20. }
  21.  
  22. function setup() {
  23.   myCanvas = createCanvas(img1.width, img1.height);
  24.   background(img2);
  25.   createStuff2();
  26. }
  27.  
  28. function draw() {
  29.   for (var i = 0; i < agents.length; i++) {
  30.     agents[i].update();
  31.     agents[i].paint();
  32.   }
  33.   if (frameCount % 100 === 0) c++;
  34.   noStroke();
  35. }
  36.  
  37. function createStuff2() {
  38.   agents = [];
  39.   for (var x = width / 2 - radius; x < width / 2 + radius; x += step) {
  40.     for (var y = height / 2 - radius; y < height / 2 + radius; y += step) {
  41.       var distance = dist(x, y, width / 2, height / 2);
  42.       var pix = img2.get(x, y);
  43.       if (distance < radius && (pix[0] + pix[1] + pix[2] > 30)) {
  44.         agents.push(new Agent(createVector(x, y), pix));
  45.       }
  46.     }
  47.   }
  48. }
  49.  
  50. function Agent(position, couleur) {
  51.   this.angle = random(TWO_PI);
  52.   this.stepSize = stepSize;
  53.   this.position = position;
  54.   this.outside = false;
  55.   this.pix = couleur;
  56.  
  57.   this.update = function() {
  58.     this.angle = noise(this.position.x / noiseScale, this.position.y / noiseScale) * fieldIntensity;
  59.     this.position.x += cos(this.angle) * this.stepSize / 2;
  60.     this.position.y += sin(this.angle) * this.stepSize / 2;
  61.   }
  62.  
  63.   this.paint = function() {
  64.     var col2 = color(this.pix[0] + int((targetHue[0] - this.pix[0]) * c / 20), this.pix[1] + int((targetHue[1] - this.pix[1]) * c / 20), this.pix[2] + int((targetHue[2] - this.pix[2]) * c / 20), a * 255);
  65.     fill(col2);
  66.     noStroke();
  67.     ellipse(this.position.x, this.position.y, this.stepSize, this.stepSize);
  68.   }
  69. }
  70.  
  71. function init() {
  72.   background(img2);
  73.   noiseSeed(random(9999));
  74.   c = 0;
  75.   createStuff2();
  76. }
  77.  
  78. function windowResized() {
  79.   //resizeCanvas(windowWidth, windowHeight-4);
  80. }
  81.  
  82. function keyTyped() {
  83.   if (key == 'r') init();
  84.   if (key === 's') saveCanvas(myCanvas, 'hairy_ideas' + frameCount + ".png", 'png');
  85. }
  86.  
  87. function keyPressed() {
  88.  
  89.   if (keyCode === UP_ARROW) {
  90.     fieldIntensity *= 2;
  91.     if (fieldIntensity > 3000) fieldIntensity = 3000;
  92.     init();
  93.   }
  94.   if (keyCode === DOWN_ARROW) {
  95.     fieldIntensity /= 2;
  96.     if (fieldIntensity < 5) fieldIntensity = 5;
  97.     init();
  98.   }
  99.   if (keyCode === RIGHT_ARROW) {
  100.     noiseScale += 100;
  101.     if (noiseScale > 1000) fieldIntensity = 1000;
  102.     init();
  103.   }
  104.   if (keyCode === LEFT_ARROW) {
  105.     noiseScale -= 100;
  106.     if (noiseScale < 100) fieldIntensity = 100;
  107.     init();
  108.   }
  109. }
Advertisement
Add Comment
Please, Sign In to add comment