Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ 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

Auto Save

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 id="app">
      <section id="piece"></section>
      <article id="description">
        <h1>Bouncy Perlin Noise.</h1>
        <p>Use the first slider to increase the amount of points.</p>
        <p>Use the second slider to modify the intensity of the audio input.</p>
        <p>Part of a series of works started in June, 2019.</p>
        <a
          href="https://instagram.com/2xAA"
          target="_blank"
          rel="noopener nofollow noreferrer"
          >Instagram</a
        >
        ×
        <a
          href="https://twitter.com/_2xAA"
          target="_blank"
          rel="noopener nofollow noreferrer"
          >Twitter</a
        >
        ×
        <a
          href="https://wray.pro"
          target="_blank"
          rel="noopener nofollow noreferrer"
          >wray.pro</a
        >
      </article>
    </div>
              
            
!

CSS

              
                @import url("https://v36p9.codesandbox.io/style.css");

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

body {
  display: flex;
  align-items: flex-start;
  justify-content: center;
  text-align: center;
}

#app {
  display: flex;
  justify-content: center;
  align-items: center;
  flex-direction: column;
  min-height: 100%;
  max-width: 500px;
  padding: 0.5em;
  box-sizing: border-box;
}

#piece {
  display: flex;
  justify-content: center;
  align-items: center;
  flex-direction: column;
}

#piece > * {
  margin-bottom: 2em;
}

#piece canvas {
  max-width: 500px;
  max-height: 500px;
  width: 100%;
}

h1 {
  display: inline-block;
}
              
            
!

JS

              
                // It makes me very sad we still have to do this in 2019
const AudioContext = window.AudioContext || window.webkitAudioContext;

let raf;
let audioContext;

if (raf) {
  cancelAnimationFrame(raf);
}

const perlin3 = new tumult.Perlin3("week2" + Date.now());

const piece = document.body.querySelector("#piece");
const canvas = document.createElement("canvas");
const context = canvas.getContext("2d");

let dpr;
let canvasSize;
let canvasSizeDpr;
let cells = 64;
let cellSize;
let cellSizeHalf;
let points;
let energyDivision = 10;

let loop;

piece.appendChild(canvas);

function setupValues() {
  dpr = window.devicePixelRatio;
  canvasSize = 500;
  canvasSizeDpr = canvasSize * dpr;
  cellSize = canvasSizeDpr / cells;
  cellSizeHalf = cellSize / 2;
  points = [];

  canvas.width = canvas.height = canvasSize * dpr;
  canvas.style.maxWidth = `${canvasSize}px`;
  canvas.style.maxHeight = `${canvasSize}px`;
  context.strokeStyle = "#fff";
  context.lineWidth = dpr;
  generatePoints();
}

function generatePoints() {
  let y = 0;
  let x = 0;

  for (let i = 0; i < cells * cells; ++i) {
    points[i] = [x, y];

    if (i % cells === cells - 1) {
      x = 0;
      y += cellSize;
    } else {
      x += cellSize;
    }
  }
}

(async function() {
  let stream = null;

  try {
    stream = await navigator.mediaDevices.getUserMedia({
      audio: {
        echoCancellation: false
      }
    });
  } catch (err) {
    throw new Error(err);
  }

  if (audioContext) audioContext.close();
  audioContext = new AudioContext({
    latencyHint: "interactive"
  });

  const source = audioContext.createMediaStreamSource(stream);

  const meyda = new Meyda.createMeydaAnalyzer({
    audioContext,
    source,
    bufferSize: 512,
    windowingFunction: "rect"
  });

  function drawGrid(delta) {
    const energy = meyda.get("energy");
    context.beginPath();
    for (let i = 0; i < points.length; ++i) {
      const mod1 = i % cells === 0;
      const mod2 = i % cells === cells - 1;

      if (mod1) {
        context.beginPath();
      }
      const x = points[i][0];
      const y = points[i][1];

      const noise = perlin3.gen(x / 255, y / 255, delta / 1000);
      const size = noise * (energy / energyDivision);

      let posX;
      let posY;

      if (mod1) {
        posX = x + cellSizeHalf;
        posY = y + cellSizeHalf;
      } else if (mod2) {
        posX = x + cellSizeHalf;
        posY = y + cellSizeHalf;
      } else {
        posX = x + cellSizeHalf + cellSizeHalf * size;
        posY = y + cellSizeHalf + cellSizeHalf * size;
      }
      
      // Adding 0.5 is a slight hack for Canvas2D.
      // Makes pixels render on "real" pixels,
      // rather than sub-pixel aliasing everything
      posX = Math.floor(posX) + 0.5;
      posY = Math.floor(posY) + 0.5;

      context.lineTo(posX, posY);
      if (mod2) {
        context.stroke();
      }
    }
  }

  setupValues();

  loop = delta => {
    raf = requestAnimationFrame(loop);
    context.fillRect(0, 0, canvasSizeDpr, canvasSizeDpr);
    drawGrid(delta);
  };
  raf = requestAnimationFrame(loop);
})();

const input = document.createElement("input");
input.type = "range";
input.min = 4;
input.max = 128;
input.value = cells;
input.step = 4;
input.addEventListener("input", e => {
  cells = parseFloat(e.target.value, 10);
  setupValues();
});

piece.appendChild(input);

const input2 = document.createElement("input");
input2.type = "range";
input2.min = 1;
input2.max = 20;
input2.value = 21 - energyDivision;
input2.step = 0.01;
input2.addEventListener("input", e => {
  energyDivision = 21 - parseFloat(e.target.value, 10);
  setupValues();
});

piece.appendChild(input2);

              
            
!
999px

Console