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

              
                <div class="titles">
  <h1>Click and hold to generate circles</h1>
  <h3>The velocity of each circle is based on its mass. Smaller circles move faster than large ones.</h3>
</div>
<canvas class="canvasElm"></canvas>
              
            
!

CSS

              
                html,
body,
h1,
h3 {
  margin: 0;
  padding: 0;
  border: 0;
}

html {
  box-sizing: border-box;
  font-size: 62.5%; /* reseting base font-size to 10px */
  scroll-behavior: smooth;
}
*,
*:before,
*:after {
  box-sizing: inherit;
}

* {
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

body {
  font-size: 1.6em; /* base font size is 16px */
  line-height: 1.4;
  font-family: -apple-system, sans-serif, BlinkMacSystemFont, "Segoe UI",
    Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji",
    "Segoe UI Symbol";
  background-color: white;
  color: rgba(0, 0, 0, 0.9);
  min-height: 100vh;
  /* mobile viewport bug fix */
  min-height: -webkit-fill-available;
}

/* ———————————————————————— */

.canvasElm {
  box-shadow: 0 0 40px 1px rgba(0, 0, 0, 0.1);
  background: #222222;
  position: absolute;
  top: 0;
  left: 0;
}

.titles {
  position: absolute;
  top: 24px;
  left: 24px;
  color: rgba(255, 255, 255, 1);
  max-width: 240px;
  line-height: 1.2;
  z-index: 2;
  pointer-events: none;
}
.titles h1 {
  font-size: 24px;
  margin-bottom: 8px;
}
.titles h3 {
  font-size: 16px;
  font-weight: 400;
  opacity: 0.6;
}

              
            
!

JS

              
                let canvasElm = document.querySelector(".canvasElm");

canvasElm.width = window.innerWidth;
canvasElm.height = window.innerHeight;

let gravity = 0.5;
let friction = 0.01;
let bounce = 0.7;

let circleColor = "rgba(0, 0, 0, 0)";

function randomBetween(min, max) {
  return Math.random() * (max - min) + min;
}

let ctx = canvasElm.getContext("2d");

class Particle {
  constructor(x, y, dx, dy, size) {
    this.x = x;
    this.y = y;
    this.dx = dx;
    this.dy = dy;
    this.size = size;
  }

  draw() {
    ctx.beginPath();
    ctx.strokeStyle = "rgba(0, 240, 255, 1)";
    ctx.lineWidth = 2;
    ctx.fillStyle = circleColor;
    ctx.arc(this.x, this.y, this.size, 0, Math.PI * 2);
    ctx.fill();
    ctx.stroke();

    this.update();
  }

  friction() {
    if (this.dx > 0) {
      this.dx -= friction;
    } else {
      this.dx += friction;
    }
  }

  update() {
    this.x += this.dx;
    this.y += this.dy;
    this.dy += gravity;

    // Side walls detection
    if (this.x + this.size > canvasElm.width || this.x - this.size < 0) {
      this.dx *= -1;
      this.friction();
    }

    // Prevents circles from sticking to the right side
    if (this.x + this.size > canvasElm.width) {
      this.x = canvasElm.width - this.size;
    }
    // Prevents circles from sticking to the left side
    if (this.x - this.size < 0) {
      this.x = this.size;
    }

    // Detect bottom side
    if (this.y + this.size + this.dy > canvasElm.height) {
      // reposition circle to bottom of the page
      this.y = canvasElm.height - this.size;

      this.dy *= -bounce;
      this.friction();
    }

    // Detect top side
    if (this.y + this.size <= 0) {
      this.y = 0 + this.size;
      this.dy *= -bounce;
    }

    // stop the bouncing if the ball's velocity is between 0 & -2
    if (this.dy < 0 && this.dy > -2.2) {
      this.dy = 0;
    }

    // stop moving on X axis if x velocity after
    if (Math.abs(this.dx) < 0.1) {
      this.dx = 0;
    }
  }
}

// Create particle objects
let particlesCount = 7;
let particlesArray = [];

for (let i = 0; i < particlesCount; i++) {
  let size = randomBetween(10, 30);
  let x = randomBetween(size, canvasElm.width - size);
  let y = randomBetween(size, canvasElm.height / 2);
  // let y = canvasElm.height / 2;
  // let dx = 20 / size * 15;
  let dx = (randomBetween(-25, 25) / size) * 15;
  // let dx = 20;
  // let dy = 20 / size * 15;
  let dy = (randomBetween(-15, 15) / size) * 15;
  // let dy = 25;
  particlesArray.push(new Particle(x, y, dx, dy, size));
}

// Preview circle size and position on click and hold
let previewCircle = {
  x: 0,
  y: 0,
  dx: 0,
  dy: 0,
  size: 0
};
let countDownTimer;

document.addEventListener("mousedown", (e) => {
  countDownTimer = setInterval(() => {
    previewCircle.size += 0.3;
  }, 5);

  // Update preview circle position and size
  previewCircle.x = e.offsetX;
  previewCircle.y = e.offsetY;
});
document.addEventListener("mousemove", (e) => {
  previewCircle.x = e.offsetX;
  previewCircle.y = e.offsetY;
});
document.addEventListener("mouseup", (e) => {
  clearInterval(countDownTimer);
  let size = previewCircle.size;
  let x = e.offsetX;
  let y = e.offsetY;
  let dx = (randomBetween(-25, 25) / size) * 15;
  let dy = (randomBetween(-15, 15) / size) * 15;
  particlesArray.push(new Particle(x, y, dx, dy, size));
  previewCircle.size = 0;
});

// Resize canvas on window resize
window.addEventListener("resize", () => {
  canvasElm.height = window.innerHeight;
  canvasElm.width = window.innerWidth;
});

function animate() {
  ctx.clearRect(0, 0, canvasElm.width, canvasElm.height);

  // Preview circle
  new Particle(
    previewCircle.x,
    previewCircle.y,
    previewCircle.dx,
    previewCircle.dy,
    previewCircle.size
  ).draw();

  for (let i = 0; i < particlesArray.length; i++) {
    particlesArray[i].draw();
  }
  requestAnimationFrame(animate);
}
animate();

              
            
!
999px

Console