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.

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

            
              <script>
  if ('paintWorklet' in CSS && 'registerProperty' in CSS && 'CSSUnitValue' in window) {
    CSS.registerProperty({
      name: '--dot-spacing',
      syntax: '<length>',
      initialValue: '20px',
      inherits: false
    });
    CSS.registerProperty({
      name: '--dot-fade-offset',
      syntax: '<percentage>',
      initialValue: '0%',
      inherits: false
    });
    CSS.registerProperty({
      name: '--dot-color',
      syntax: '<color>',
      initialValue: '#fff',
      inherits: false
    });

    // load this CodePen's JS file as the paint worklet
    CSS.paintWorklet.addModule('/lonekorean/pen/aYoJPv.js');
  } else {
    document.querySelector('html').classList.add('no-support');
  }
</script>

<div class="polka-dot"></div>

<div class="warning">
  <i class="fas fa-exclamation-triangle"></i><br>
  This Houdini demo requires the <strong>CSS Paint API</strong>, <strong>CSS Properties and Values API</strong>, and <strong>Typed OM</strong>.<br>
  Use <strong>Chrome</strong> and go to <strong>chrome://flags</strong> and enable <strong>Experimental Web Platform features</strong>.<br>
  Also make sure you're serving over <strong>https</strong> or <strong>localhost</strong>.
</div>

            
          
!

CSS

            
              *, *::before, *::after {
  box-sizing: border-box;
}

html, body {
  height: 100%;
}

body {
  margin: 0;
  background: #000;
  font: 1rem/1.25 'Mukta Mahee', sans-serif;
}

.polka-dot {
  --dot-spacing: 20px;
  --dot-fade-offset: 0%;
  --dot-color: #40e0d0;
  background-image: paint(polka-dot-fade);

  position: absolute;
  top: 40px;
  right: 40px;
  bottom: 40px;
  left: 40px;
}

.warning {
  display: none;
  position: absolute;
  top: 10px;
  left: 10px;
  right: 10px;
  padding: 20px;
  border-top: 4px dashed #e8590c;
  background-color: #ffe8cc;
  color: #e8590c;
  text-align: center;
}

.no-support .warning {
  display: block;
}

.warning .fas {
  margin-bottom: 1rem;
  font-size: 1.5rem;
}

            
          
!

JS

            
              /*
This CodePen's JS file has been "hijacked" to serve as the paint worklet file. The if check below ensures the code only runs when referenced as such. JavaScript for the page is at the top of the HTML. I'm only doing this to squeeze everything into one CodePen.

IMPORTANT: If you fork, update the CodePen JS file path in the HTML.

ALSO IMPORTANT: If you edit code here, disable cache and reload. Live preview won't pick up changes in the paint worklet.
*/

if (typeof registerPaint !== 'undefined') {
  class PolkaDotFadePainter {
    static get inputProperties() {
      return ['--dot-spacing', '--dot-fade-offset', '--dot-color'];
    }

    paint(ctx, size, props) {
      let spacing = props.get('--dot-spacing').value;
      let fadeOffset = props.get('--dot-fade-offset').value;
      let color = props.get('--dot-color').toString();

      ctx.fillStyle = color;
      for (let y = 0; y < size.height + spacing; y += spacing) {
        for (let x = 0; x < size.width + spacing; x += spacing * 2) {
          // every other row shifts x to create staggered dots
          let staggerX = x + ((y / spacing) % 2 === 1 ? spacing : 0);

          // calculate dot radius based on horizontal position and fade offset
          let fadeRelativeX = staggerX - size.width * fadeOffset / 100;
          let radius = spacing * Math.max(Math.min(1 - fadeRelativeX / size.width, 1), 0);

          // draw dot
          ctx.beginPath();
          ctx.arc(staggerX, y, radius, 0, 2 * Math.PI);
          ctx.fill();
        }
      }
    }
  }

  registerPaint('polka-dot-fade', PolkaDotFadePainter);
}

            
          
!
999px

Console