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

              
                <!--  
  I love particles!
  Isn't this magical?

  Controls:
  Click -> Toggle blend.
  Space -> Pause
  Left -> slow
  Right -> fast
-->

<canvas />
              
            
!

CSS

              
                html,
body {
  width: 100%;
  height: 100%;
  margin: 0;
  overflow: hidden;
  background: black;
}

canvas {
  position: relative;
  width: 100%;
  height: 100%;
}

* {
  -webkit-tap-highlight-color: transparent;
  user-select: none;
}

              
            
!

JS

              
                const initialize = () => {
  const canvas = document.querySelector('canvas');

  const pixelRatio = window.devicePixelRatio;
  const multiplier = 40000;
  const duration = 0.6;
  const geometry = [{ x: 0, y: 0, z: 0 }];
  const pointSize = ((window.innerHeight / 800) * 2).toFixed(1);

  let step = 0.004;
  let forward = true;
  let blend = true;

  const attributes = [
    {
      name: 'aPositionStart',
      data: (i, total) => [0, -0.5 + (1 * (i / total)), 0],
    },
    {
      name: 'aControlPointOne',
      data: i => [Math.sin(i), Math.sin(i), 0],
    },
    {
      name: 'aControlPointTwo',
      data: i => [-Math.sin(i), Math.sin(i), 0],
    },
    {
      name: 'aPositionEnd',
      data: (i, total) => [0, -0.5 + (1 * (i / total)), 0],
    },
    {
      name: 'aColor',
      data: (i, total) => getHSL(((i / total) * 1.0), 0.5, 0.5),
    },
    {
      name: 'aOffset',
      data: i => [i * ((1 - duration) / (multiplier - 1))],
    },
  ];

  const uniforms = [
    {
      name: 'uProgress',
      type: 'float',
      value: 0.00,
    },
  ];

  const vertexShader = `
    attribute vec3 aPositionStart;
    attribute vec3 aControlPointOne;
    attribute vec3 aControlPointTwo;
    attribute vec3 aPositionEnd;
    attribute float aOffset;
    attribute vec3 aColor;

    uniform float uProgress;
    uniform mat4 uMVP;

    varying vec3 vColor;

    vec3 bezier4(vec3 a, vec3 b, vec3 c, vec3 d, float t) {
      return mix(mix(mix(a, b, t), mix(b, c, t), t), mix(mix(b, c, t), mix(c, d, t), t), t);
    }

    float easeInOutQuint(float t){
      return t < 0.5 ? 16.0 * t * t * t * t * t : 1.0 + 16.0 * (--t) * t * t * t * t;
    }

    void main () {
      float tProgress = easeInOutQuint(min(1.0, max(0.0, (uProgress - aOffset)) / ${duration}));
      vec3 newPosition = bezier4(aPositionStart, aControlPointOne, aControlPointTwo, aPositionEnd, tProgress);
      gl_PointSize = ${pointSize};
      gl_Position = uMVP * vec4(newPosition, 1.0);
      vColor = aColor;
    }
  `;

  const fragmentShader = `
    precision mediump float;

    varying vec3 vColor;

    void main() {
      gl_FragColor = vec4(vColor, 1.0);
    }
  `;

  const onSetup = (gl) => {
    gl.blendFunc(gl.SRC_ALPHA, gl.ONE);
    gl.enable(gl.BLEND);
    canvas.addEventListener('click', () => {
      if (blend) {
        gl.disable(gl.BLEND);
        blend = false;
      } else {
        gl.enable(gl.BLEND);
        blend = true;
      }
    });
  };

  const onRepeat = () => {
    if (uniforms[0].value > 1) {
      forward = false;
    } else if (uniforms[0].value < 0) {
      forward = true;
    }
    uniforms[0].value += forward ? step : -step;
  };

  canvas.addEventListener('keydown', (e) => {
    if (e.keyCode === 37) step -= 0.001;
    if (e.keyCode === 39) step += 0.001;
    if (e.keyCode === 32) step = step !== 0 ? 0 : 0.004;
  });

  const options = {
    onSetup,
    onRepeat,
    pixelRatio,
  };

  starlings(
    canvas,
    geometry,
    multiplier,
    attributes,
    uniforms,
    vertexShader,
    fragmentShader,
    options,
  );
};

const h2r = (p, q, t) => {
  if (t < 0) t += 1;
  if (t > 1) t -= 1;
  if (t < 1 / 6) return p + (q - p) * 6 * t;
  if (t < 1 / 2) return q;
  if (t < 2 / 3) return p + (q - p) * 6 * (2 / 3 - t);
  return p;
};

const getHSL = (h, s, l) => {
  h = ((h % 1) + 1) % 1;
  s = Math.max(0, Math.min(1, s));
  l = Math.max(0, Math.min(1, l));
  if (s === 0) return [l, l, l];
  const p = l <= 0.5 ? l * (1 + s) : l + s - (l * s);
  const q = (2 * l) - p;
  return [h2r(q, p, h + 1 / 3), h2r(q, p, h), h2r(q, p, h - 1 / 3)];
};

initialize();

              
            
!
999px

Console