Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                
              
            
!

CSS

              
                body {
  background-color: black;
  display: flex;
  height: 100vh;
  overflow: hidden;
  align-items: center;
  justify-content: center;
}

              
            
!

JS

              
                //genuary2024 - use a physics library;
//press space to save
//press any other key to remove the mirrors and see the truth

//I used matter.js for a bit before I got extremely frustrated with the limitations of not being able to get the gravity to be a single point, so I wrote this instead. I liked playing with the library, but it didn't do what I wanted. I looked at others, but just did this after hitting some time constraints.

//There is only one "truth" dust particle, and the rest are reflected. Think of a hall of mirrors. It might look like a particle collides, but it isn't acutally. When they do collide they become stuck.
let nRot, r, threshold, bodies, centerOfMe, r1, g1, b1, eye;
function setup() {
  cnv = createCanvas(500, 500);
  centerOfMe = createVector(0, 0);
  bodies = [];
  stroke(255);
  nRot = int(random(5, 19)); //number of mirrors
  r = int(random(4, 20));
  threshold = int(random(2, 10)); //how close before stuck
  rectMode(CENTER);
  r1 = random(50, 100);
  g1 = random(50, 100);
  b1 = random(80, 100);
  timeMod = int(random(2, 30));
  eye = random(width / 4, width / 3);
}

function draw() {
  background(r1 / 2, g1 / 2, b1 / 2);
  translate(width / 2, height / 2);
  noStroke();
  fill(r1, g1, b1);
  circle(centerOfMe.x, centerOfMe.y, r);
  for (let i = 0; i < bodies.length; i++) {
    let particle = bodies[i];
    if (!particle.stuck) {
      particle.attracted(centerOfMe);
      particle.update();
    }
    let d = dist(particle.pos.x, particle.pos.y, centerOfMe.x, centerOfMe.y);
    if (d < r / 2 + threshold) {
      particle.stuck = true;
    }
    for (let j = 0; j < bodies.length; j++) {
      if (i != j) {
        if (particle.checkCollision(bodies[j])) {
          break;
        }
      }
    }

    particle.display();
  }
  if (frameCount % timeMod === 0 && bodies.length < 300) {
    bodies.push(new Dust(random(-width / 1.5, r), random(-height / 1.5, r)));
  }
  border();
}

function border() {
  push();
  noFill();
  stroke(r1 / 1.5, g1 / 1.5, b1 / 1.5);
  strokeWeight(4);
  rect(0, 0, width - 20, height - 20);
  pop();
}

function mousePressed() {
  frameCount = 0;
  setup();
  draw();
}

function keyPressed() {
  if (keyCode === 32) {
    save(cnv, "together", "jpg");
  }
}

function windowResized() {
  c = min(500, min(windowWidth, windowHeight));
  resizeCanvas(c, c);
}

class Dust {
  constructor(x, y) {
    this.pos = createVector(x, y);
    this.vel = createVector(random(-0.5, 1), 0);
    this.acc = createVector();
    this.stuck = false;
    this.G = random(1, 9);
    this.fc = frameCount / 100;
    this.size = int(random(1, 5));
    this.color = color(
      150 + r1 * sin(this.fc),
      120 + g1 * sin(this.fc),
      150 + b1 * sin(this.fc)
    );
  }

  attracted(target) {
    let force = p5.Vector.sub(target, this.pos);
    let distanceSq = constrain(force.magSq(), 50, 2000);
    let strength = this.G / distanceSq;
    force.setMag(strength);
    this.acc.add(force);
  }
  checkCollision(friend) {
    let distance = dist(this.pos.x, this.pos.y, friend.pos.x, friend.pos.y);
    let d2 = dist(centerOfMe.x, centerOfMe.y, this.pos.x, this.pos.y);
    if (distance < threshold && d2 < eye) {
      this.stuck = true;
      return true;
    }
    return false;
  }

  update() {
    if (!this.stuck) {
      this.vel.add(this.acc);
      this.pos.add(this.vel);
      this.acc.mult(0);
    }
  }

  display() {
    strokeWeight(this.size);
    if (keyIsPressed && keyCode != 32) {
      push();
      stroke(this.color);
      point(this.pos.x, this.pos.y);
      pop();
    } else {
      for (let i = 0; i < nRot; i++) {
        push();
        stroke(this.color);
        rotate((i * TAU) / nRot);
        point(this.pos.x, this.pos.y);
        pop();
      }
    }
  }
}

              
            
!
999px

Console