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

              
                <canvas id="canvas" width="500" height="500"></canvas>
              
            
!

CSS

              
                body, html {
  height: 100%;
}

body {
  display: flex;
  justify-content: center;
  align-items: center;
  background-color: black;
}

canvas {
  border-radius: 50%;
}
              
            
!

JS

              
                const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');
const rowLength = canvas.width * 4;
const startTime = performance.now();
const INTERVAL = 100;

setInterval(drawRipple, INTERVAL);

// FUNCTION DEFINITIONS

function drawRipple() {
  let elapsedTimeSeconds = (performance.now() - startTime) / 1000;
  const nextPixelData = ctx.createImageData(canvas.width, canvas.height);
  // 200x200 means 40,000 pixels, x4 values per pixel = 160,000 elements to loop over.
  // If a single row has 200 pixels, x4 values per pixel, every 800 values is a new row.
  for (let i = 0; i < nextPixelData.data.length; i += 4) {
    // calculate the current x and y (canvas cooridnates)
    let x = Math.floor(i / 4) % canvas.width;
    let y = Math.floor(i / rowLength);

    // calculate the alternative x and y, if the origin were in the center
    let reIndexedX = -((canvas.width - x) - (canvas.width / 2));
    let reIndexedY = (canvas.height - y) - (canvas.height / 2);
    
    let radialX = hypotenuseLength(reIndexedX, reIndexedY);
    let waveHeight = sineFunction(radialX, elapsedTimeSeconds);
    let scaledHeight = (waveHeight * 60) + (255/2);
    
    // FOR LOGGING. PRINTS ONCE PER "FRAME".
    if (reIndexedX === 0 && reIndexedY == 0) {
      console.log(`elapsed time: ${elapsedTimeSeconds}`);
      console.log(`wave height: ${scaledHeight}`);
    }

    // Write new pixel values
    nextPixelData.data[i]     = 0; // red
    nextPixelData.data[i + 1] = 0; // green
    nextPixelData.data[i + 2] = scaledHeight; // blue
    nextPixelData.data[i + 3] = 255; // opacity
  }
  
  ctx.putImageData(nextPixelData, 0, 0);
}

function hypotenuseLength(x, y) {
  return Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2));
}

function sineFunction(x, t) {
  let frequencyConstant = 8;
  let scaledTime = t * 20;

  // For reference, see https://www.desmos.com/calculator/bp9t79pfa0
  return Math.sin((x - scaledTime) / frequencyConstant);
}




              
            
!
999px

Console