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

              
                
              
            
!

CSS

              
                html, body {
  height: 100%;
  margin: 0;
}
              
            
!

JS

              
                const P = Pencil;

const gravity = new P.Position(0, 0.5);
const stiffness = 0.4;
const friction = 0.01;

// Set the scene
const scene = new P.Scene();
scene.options.fill = new P.LinearGradient([0, 0], [0, scene.height], {
  0: "#666",
  1: "#333",
});

// Set the pool
const radius = Math.min(scene.width, scene.height) / 2.5;
const pool = new P.Circle(scene.center, radius, {
  fill: "#fff",
});
scene.add(pool);

// Return a random radius
const getRadius = () => P.Math.random(pool.radius / 15, pool.radius / 6);

// List of colors
const colors = [
  "#1278CC", "#5A7D99", "#30FFEF", "#FF795B", "#CC1912",
];
// Generate some balls
const balls = P.RegularPolygon.getRotatingPoints(30, radius / 6).map((point) => {
  const ball = new P.Circle(point, getRadius());
  const third = -ball.radius / 3;
  const gradientPosition = [third, third];
  const color = colors[Math.floor(P.Math.random(0, colors.length))];
  ball.options.fill = new P.RadialGradient(gradientPosition, ball.radius * 1.5, {
    0: (new P.Color(color)).lightness(0.7),
    0.7: color,
    1: (new P.Color(color)).lightness(0.4),
  });
  ball.draggable();
  return ball;
});
pool.add(...balls);

// Render once for the codepen thumbnail
scene.render();

const onDraw = () => {
  // Loops through all balls
  balls.forEach((ball) => {
    // Copy the current position
    const prev = ball.position.clone();

    if (!ball.isDragged) {
      // Verlet integration
      if (ball.previousPosition) {
        ball.position.add(
          ball.position.clone()
            .subtract(ball.previousPosition)
            .multiply(1 - friction),
        );
      }

      // Add gravity
      ball.position.add(gravity);

      // Pool boundary constraint
      const distanceToOrigin = ball.position.distance([0, 0]);
      if (distanceToOrigin > pool.radius - ball.radius) {
        const pushBack = ball.position.clone()
          .divide(distanceToOrigin)
          .multiply(distanceToOrigin - (pool.radius - ball.radius))
          .multiply(-stiffness);
        ball.position.add(pushBack);
      }

      // Collision constraint
      balls.forEach((other) => {
        if (other !== ball) {
          const distanceToOther = ball.position.distance(other.position);
          if (distanceToOther < ball.radius + other.radius) {
            const pushBack = ball.position.clone()
              .subtract(other.position)
              .divide(distanceToOther)
              .multiply(distanceToOther - (ball.radius + other.radius))
              .multiply(-stiffness / 2);
            ball.position.add(pushBack);
            other.position.subtract(pushBack);
          }
        }
      });
    }

    // Save previous position for next frame
    ball.previousPosition = prev;
  });
};

// Delay to give more weight to the explosion
setTimeout(() => {
  // Tell the scene to render each frame and call onDraw
  scene
    .startLoop()
    .on("draw", onDraw, true);
}, 300);

              
            
!
999px

Console