Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

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

Behavior

Save Automatically?

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 Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

HTML

              
                
              
            
!

CSS

              
                
              
            
!

JS

              
                const NUM_OBJECTS = 30;
const OBJECT_SIZE = 15;
const MAX_SPEED = 5;

let objects = [];
let num_collisions = 0;

function setup() {
  createCanvas(500, 500);
  for (let i=0; i<NUM_OBJECTS; i++) {
    let theta = random(0, TWO_PI);
    let speed = random(0, MAX_SPEED);
    let collision = false;
    let new_obj = 
    {
      x: random(OBJECT_SIZE, width-OBJECT_SIZE),
      y: random(OBJECT_SIZE, height-OBJECT_SIZE),
      dx: speed * cos(theta),
      dy: speed * sin(theta),
      diameter: OBJECT_SIZE
    }
    for (let j=0; j<objects.size; j++) {
      if (collides(new_obj, objects[j])) {
        collision = true;
      }
    }
    if (collision) {
      i -= 1;
    } else {
      objects.push(new_obj);
    }
  }
}

function draw() {
  background(200);
  fill(0);
  text(num_collisions, 10, 10);
  for (let i=0; i<NUM_OBJECTS; i++) {
    objects[i].x += objects[i].dx;
    objects[i].y += objects[i].dy;
    if (objects[i].x - objects[i].diameter / 2 < 0 || objects[i].x + objects[i].diameter / 2 > width) {
      objects[i].dx = -objects[i].dx;
      objects[i].x += objects[i].dx;
    }
    if (objects[i].y - objects[i].diameter / 2 < 0 || objects[i].y + objects[i].diameter / 2 > height) {
      objects[i].dy = -objects[i].dy;
      objects[i].y += objects[i].dy;
    }
    for (let j=i+1; j<NUM_OBJECTS; j++) {
      if (collides(objects[i], objects[j])) {
        objects[i].y -= objects[i].dy;
        objects[i].x -= objects[i].dx;
        num_collisions += 1;
        tmp_dx = objects[i].dx;
        tmp_dy = objects[i].dy;
        objects[i].dx = objects[j].dx;
        objects[i].dy = objects[j].dy;
        objects[j].dx = tmp_dx;
        objects[j].dy = tmp_dy;
      }
    }
    ellipse(objects[i].x, objects[i].y, objects[i].diameter);
  }
}

function collides(obj1, obj2) {
  let d = distance(obj1, obj2);
  let r1 = obj1.diameter / 2;
  let r2 = obj2.diameter / 2;
  return (r1 + r2 >= d);
}

function distance(obj1, obj2) {
  let dx = obj1.x - obj2.x;
  let dy = obj1.y - obj2.y;
  return sqrt(dx*dx + dy*dy);
}
              
            
!
999px

Console