Galaxy 3d

Processing.js

by schrodingers

HTML

<canvas></canvas>

CSS

</style> <script type="text/javascript"> window.addEventListener('load', function() {
  var scripts=document.body.getElementsByTagName('script');
  var canvases=document.body.getElementsByTagName('canvas');
  new Processing(canvases[0], scripts[0].text);
}

, false);
// Here prevent javascript in body from throwing error </script> <style>

JavaScript

/*
 title: // 3D galaxy. Spiral simple
 date: 2015-12-18
 */

ArrayList<Particle> pts;
color[] spring = {#ff6699, #ff9900, #ffcc00, #9adb1b, #00cc66, #00cccc, #00ccff, #6633cc, #ff66cc};
color col;
int incr; // amount of particles to emit first time
void setup() {
  size(800, 600, P3D);
  //col = spring[(int)random(spring.length)];
  pts = new ArrayList<Particle>();
}

void draw() {  
  background(44);
  incr = (int)random(22, 51);
  //fill(44, 95);
  //rect(0, 0, width, height);
  translate(width/2, height/2);
  rotateX(radians(1+mouseY/3));
  rotateY(radians(1+mouseX/3));
  rotateZ(radians(90));
  for (int i = 0; i < incr; i++) {
    float zDiff = random(-0.09, 0.098);
    float xyDiff = random(-50, 50);
  //  float xyDiff = noise(i+frameCount)*220;
    pts.add(new Particle(new PVector(sin(radians(xyDiff+frameCount*3))*1.5, zDiff, cos(radians(xyDiff+frameCount*3))*1.5)));
    pts.add(new Particle(new PVector(-sin(radians(xyDiff+frameCount*3))*1.5, zDiff, -cos(radians(xyDiff+frameCount*3))*1.5)));
  }
  for (int i = 0; i < pts.size()-1; i++) {
    Particle p = pts.get(i);
    pushMatrix();
    p.display();
    popMatrix();
    p.move();
  }
}
class Particle {
  PVector loc, vel;
  float maxLife;
  float life;
  float col;

  Particle(PVector v) {
    loc = new PVector(0, 0, 0);
    life = random(200, 222);
    maxLife = life;

    vel = v;
  }
  void display() {
    translate(loc.x, loc.y, loc.z);
    col = map(life, 0, maxLife, 0, 255);
    stroke(map(life, 0, maxLife, 50, 25), col, map(life, 0, maxLife, 255, 50), 115+col);
    strokeWeight(map(life, 0, maxLife, 5, 1));
    point(0, 0, 0);
    life--;
    if (life <= 0) {
      pts.remove(this);
    }
  }
  void move() {
    loc.add(vel);
  }
}
// Utilities functions
void keyPressed() {
  if (key == 's' || key == 'S') {
    saveFrame("out/pic-##.png"); 
    println("Picture saved as " + "pic-"+ frameCount +".png");
  }
}