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 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

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

              
                <p>Bubbles (drag-n drop them)</p>
<div class="grain">
    <canvas id="world"></canvas>
</div>
              
            
!

CSS

              
                body {
  margin: 0;
  padding: 0;
  font-family: monospace;
  align-items: center;
  justify-content: center;
}

p {
  margin-top: 20px;
  text-align: center;
}

.grain, canvas {
  display: flex;
  background: black url("http://api.thumbr.it/whitenoise-560x800.png?background=0f1517ff&noise=626262&density=67&opacity=15") repeat fixed center;
  width: 600px;
  height: 800px;
  justify-content: center;
  margin: 0 auto;
}
              
            
!

JS

              
                class Bubbles {
  constructor(options) {
    this.options = {
      element: document.getElementById('world'),
      radius: [1, 50], // random range of body radii
      xVarianceRange: [-0.5, 0.5], // random range of x velocity scaling on bodies
      yVarianceRange: [0.5, 1], // random range of y velocity scaling on bodies
      airFriction: 0.03, // air friction of bodies
      opacity: 1, // opacity of bodies
      collisions: true, // do bodies collide or pass through
      pixelsPerBody: 3000, // viewport pixels required for each body added
      colors: ['#ffffff','#cebcbc','#e6edd3']
    };

    
    // resize
    this.resizeDelay = 400;
    this.resizeTimeout = null;

    // Matter.js objects
    this.engine = null;
    this.render = null;
    this.runner = null;
    this.bodies = null;

    this.setSize();
    this.initScene();
    this.initEvents();
  }

  setSize() {
    this.viewportWidth = document.documentElement.clientWidth;
    this.viewportHeight = document.documentElement.clientHeight;
  }

  initScene() {
    // engine
    this.engine = Matter.Engine.create(); // handles the physics
    this.engine.world.gravity.y = 0;

    // world
    this.world = this.engine.world; // stores the bodies

    // render
    this.render = Matter.Render.create({
      canvas: this.options.element,
      engine: this.engine,
      options: {
        width: 600,
        height: 800,
        background: 'transparent',
        wireframes: false,
        showAngleIndicator: false,
      },
    });
    Matter.Render.run(this.render);

    // runner
    this.runner = Matter.Runner.create();
    Matter.Runner.run(this.runner, this.engine);
    
    // resize init
    this.resizeTimeout = null;

    this.initBodies();
  }

  initBodies() {
    this.bodies = [];
    const totalBodies = Math.round(this.viewportWidth * this.viewportHeight / this.options.pixelsPerBody);
    for (let i = 0; i <= totalBodies; i++) {
      const x = this.random([0, this.viewportWidth]);
      const y = this.random([0, this.viewportHeight]);
      const body = this.circle(x, y);
      this.bodies.push(body);
    }
    Matter.World.add(this.engine.world, this.bodies);
  }

  addBody(x, y) {
    const body = this.circle(x, y);
    Matter.World.add(this.engine.world, body);
    console.log(body);
  }

  random(range) {
    const [min, max] = range;
    return Math.random() * (max - min) + min;
  }

  circle(x, y) {
    const radius = this.random(this.options.radius);
    const color = this.options.colors[this.bodies.length % this.options.colors.length];

    return Matter.Bodies.circle(x, y, radius, {
      render: {
        fillStyle: 'transparent',
        strokeStyle: color,
        lineWidth: 1,
        opacity: this.options.opacity,
      },
      frictionAir: this.options.airFriction,
      collisionFilter: {
        group: this.options.collisions ? 1 : -1,
      },
      plugin: {
        wrap: { // garbage-recycle method with wrap
          min: { x: 0, y: 0 },
          max: { x: this.viewportWidth, y: this.viewportHeight },
        },
      },
    });
  }

  initEvents() {
    this.addEventListeners();
    this.dragNDrop();
  }

  dragNDrop() {
    this.mouseConstraint = Matter.MouseConstraint.create(this.engine, {
      element: this.options.element,
      constraint: {
        render: {
          visible: false,
        },
        stiffness: 0.1,
      },
    });
    Matter.World.add(this.world, this.mouseConstraint);

    this.mouseConstraint.mouse.element.removeEventListener('mousewheel', this.mouseConstraint.mouse.mousewheel);
    this.mouseConstraint.mouse.element.removeEventListener('DOMMouseScroll', this.mouseConstraint.mouse.mousewheel);

  }

  shutdown() {
    Matter.Engine.clear(this.engine);
    Matter.Render.stop(this.render);
    Matter.Runner.stop(this.runner);
    this.removeEventListeners();
  }

  addEventListeners() {
    window.addEventListener('resize', this.onResizeThrottled.bind(this));
  }

  removeEventListeners() {
    window.removeEventListener('resize', this.onResizeThrottled);
  }

  onResizeThrottled() {
    if (!this.resizeTimeout) {
      this.resizeTimeout = setTimeout(this.onResize.bind(this), this.resizeDelay);
    }
  }

  onResize() {
    this.shutdown();
    this.initScene();
  }
}

window.addEventListener('DOMContentLoaded', () => {
  new Bubbles();
});

              
            
!
999px

Console