Advertisement
Guest User

Particles with images

a guest
May 20th, 2018
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. num = 300;
  2. p = [];
  3. max_spd = 2;
  4.  
  5. particle_image = 0;
  6.  
  7. function setup() {
  8.     createCanvas(windowWidth, windowHeight);
  9.     colorMode(HSB);
  10.     for (let i = 0; i < num; i++) {
  11.         p.push(new Particle(createVector(random(width), random(height)), 100, 150));
  12.         stroke(255);
  13.     }
  14.     particle_image = loadImage("32861069_10156294632646585_6523526685164306432_n.png");
  15. }
  16.  
  17. function draw() {
  18.     background(0);
  19.     for (var i = 0; i < num; i++) {
  20.         p[i].update(p, i);
  21.     }
  22. }
  23.  
  24. class Particle {
  25.  
  26.     constructor(pos, r, mr) {
  27.         this.pos = pos;
  28.         this.r = r;
  29.         this.mr = mr;
  30.         this.spd = 2;
  31.         this.vel = createVector(random(-1, 1), random(-1, 1));
  32.     }
  33.  
  34.     update(p, i) {
  35.         var h = map(this.pos.x, 0, width, 0, 255);
  36.         this.pos.add(this.vel);
  37.  
  38.         if (this.pos.x < -10) this.pos.x = width; // if it goes backwards it will end up on far side of screen
  39.         if (this.pos.x > width + 10) this.pos.x = 0; //
  40.         if (this.pos.y < -10) this.pos.y = height;
  41.         if (this.pos.y > height + 10) this.pos.y = 0;
  42.  
  43.         this.vel.x = constrain(this.vel.x + random(-this.spd, this.spd), -max_spd, max_spd);
  44.         this.vel.y = constrain(this.vel.y + random(-this.spd, this.spd), -max_spd, max_spd);
  45.  
  46.         for (var j = i + 1; j < p.length; j++) {
  47.             var ang = atan2(this.pos.y - p[j].pos.y, this.pos.x - p[j].pos.x);
  48.             var d = this.pos.dist(p[j].pos);
  49.             if (d < this.r) {
  50.                 stroke(h, 255, map(d, 0, this.r, 255, 0));
  51.                 strokeWeight(map(d, 0, this.r, 3, 0));
  52.                 line(this.pos.x, this.pos.y, p[j].pos.x, p[j].pos.y);
  53.  
  54.                 var force = map(d, 0, this.r, 4, 0);
  55.                 this.vel.x += force * cos(ang);
  56.                 this.vel.y += force * sin(ang);
  57.             }
  58.         }
  59.  
  60.         var ang = atan2(this.pos.y -mouseY, this.pos.x -mouseX);
  61.         var d = this.pos.dist(createVector(mouseX, mouseY));
  62.         if (d < this.mr) {
  63.             var force = map(d, 0, this.mr, 20, 0);
  64.             this.vel.x += force * cos(ang);
  65.             this.vel.y += force * sin(ang);
  66.         }
  67.  
  68.         noStroke();
  69.         //fill(h, 255, 255);
  70.         //ellipse(this.pos.x, this.pos.y, 5, 5);
  71.         var image_scale = 20;
  72.         var image_x = this.pos.x  - (particle_image.width/image_scale)/2;
  73.         var image_y = this.pos.y  - (particle_image.height/image_scale)/2;
  74.         var image_w = particle_image.width/image_scale;
  75.         var image_h = particle_image.height/image_scale;
  76.         image(particle_image, image_x, image_y, image_w, image_h);
  77.     }
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement