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. You can use the CSS from another Pen by using it's URL and the proper URL extention.

+ 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

              
                <div class="canvas-wrapper">
  <canvas class="wave-bar" data-color1="#43c0e4" data-color2="#43c0e4" data-color3="#43c0e4"></canvas>
  <div class="inner" data-bgcolor="rgba(67, 193, 236, 0.72)" data-color="#fff">Waveform Canvas<br />Three waves</div>
</div>
<div class="canvas-wrapper">
  <canvas class="wave-bar" data-color1="#EB227E" data-color2="#EB0B29" data-color3="#FB00A4" data-bgcolor="rgba(67, 193, 236, 0.72)"></canvas>
  <div class="inner" data-bgcolor="rgba(239, 8, 100, 0.72)" data-color="#fff">Assimilate above color</div>
</div>
<div class="canvas-wrapper">
  <canvas class="wave-bar" data-color1="#96D601" data-color2="#00BA89"></canvas>
  <div class="inner" data-bgcolor="rgba(37, 189, 100, 0.72)" data-color="#fff">Both Waveform<br />Two waves</div>
  <canvas class="wave-bar flipped" data-color1="#96D601" data-color2="#00BA89"></canvas>
</div>
<div class="canvas-wrapper">
  <canvas class="wave-bar" data-color1="#C8D32F"></canvas>
  <div class="inner" data-bgcolor="#C8D32F" data-color="#fff">Single wave</div>
</div>
              
            
!

CSS

              
                body{
  background-image:url(https://images.unsplash.com/photo-1639592396001-5f5017b960a5?crop=entropy&cs=srgb&fm=jpg&ixid=MnwxNDU4OXwwfDF8cmFuZG9tfHx8fHx8fHx8MTY0MTk2MjczNg&ixlib=rb-1.2.1&q=85);
  background-position:center;
  background-size:cover;
  background-attachment:fixed;
}

.inner{
  padding:2vw 30px;
  font-family:Cinzel;
  font-size:38px;
  text-align:center;
}
canvas{
  display:block;
  width:100%;
  &.flipped{
    transform:rotate(180deg);
  }
}
              
            
!

JS

              
                (function () {
  var unit = 100,
      canvasList,
      drawInfo = {},
      colorList = [],
      innerFontColorList = [],
      innerBgColorList = [];
  /**
   * Init function.
   * 
   * Initialize variables and begin the animation.
   */
  function init() {
    drawInfo.seconds = 0;
    drawInfo.t = 0;

    // Get all canvas elements
    canvasList = document.querySelectorAll('.wave-bar');

    // Initialize each canvas
    Array.prototype.forEach.call(canvasList, 
      function(canvas, index){
        var canvasDataSet = canvas.dataset,
            inner = canvas.nextElementSibling,
            innerDataSet;
        // Set the canvas size (Retina ready)
        canvas.width = document.documentElement.clientWidth;
        canvas.width *= devicePixelRatio;
        canvas.height = canvas.width * .04;
        canvas.height *= devicePixelRatio;
        canvas.style.height = String(canvas.height / devicePixelRatio) + "px";

        canvas.contextCache = canvas.getContext("2d");
      
        // Add color
        colorList.push([canvasDataSet.color1, canvasDataSet.color2, canvasDataSet.color3]);
      
        // Wrapper background color
        if (canvasDataSet.bgcolor) {
          canvas.parentNode.style.backgroundColor = canvasDataSet.bgcolor;
        }
      
        // Inner content area
        if (inner){
          innerDataSet = inner.dataset;
          if (innerDataSet.color){
            inner.style.color = innerDataSet.color;
          }
          if (innerDataSet.bgcolor){
            inner.style.backgroundColor = innerDataSet.bgcolor;
          }
        }
      }
    );
    update();
  }

  function update() {
    // Each canvas
    Array.prototype.forEach.call(canvasList, 
      function(canvas, index){
        draw(canvas, colorList[index]);
      }
    );
    // 共通の描画情報の更新
    drawInfo.seconds = drawInfo.seconds + .014;
    drawInfo.t = drawInfo.seconds*Math.PI;
    // Re-call
    setTimeout(update, 35);
  }

  /**
   * Draw animation function.
   * 
   * This function draws one frame of the animation, waits 20ms, and then calls
   * itself again.
   */
  function draw(canvas, color) {
    // 対象のcanvasのコンテキストを取得
    var context = canvas.contextCache;
    // キャンバスの描画をクリア
    context.clearRect(0, 0, canvas.width, canvas.height);

    //波を描画
    if (color[0]) {
      if (color[1] && color[2]) {
        // 3 waves
        drawWave(canvas, color[0], 0.3, 3, 0);
        drawWave(canvas, color[1], 0.5, 2, 250);
        drawWave(canvas, color[2], 0.2, 1.6, 100);
      } else if (color[1] && !color[2]) {
        // 2 waves
        drawWave(canvas, color[0], 0.4, 3, 0);
        drawWave(canvas, color[1], 0.6, 2, 250);
      } else {
        // single wave only
        drawWave(canvas, color[0], 1, 3, 0);
      }
    }
  }
  /**
  * 波を描画
  * drawWave(キャンバス, 色, 不透明度, 波の幅のzoom, 波の開始位置の遅れ)
  */
  function drawWave(canvas, color, alpha, zoom, delay) {
    var context = canvas.contextCache;
    context.fillStyle = color;
    context.globalAlpha = alpha;

    context.beginPath(); //パスの開始
    drawSine(canvas, drawInfo.t / 0.5, zoom, delay);
    context.lineTo(canvas.width + 10, canvas.height); //パスをCanvasの右下へ
    context.lineTo(0, canvas.height); //パスをCanvasの左下へ
    context.closePath() //パスを閉じる
    context.fill(); //塗りつぶす
  }
  /**
   * Function to draw sine
   * 
   * The sine curve is drawn in 10px segments starting at the origin. 
   * drawSine(キャンバス, 時間, 波の幅のzoom, 波の開始位置の遅れ)
   */
  function drawSine(canvas, t, zoom, delay) {
    var xAxis = Math.floor(canvas.height/2);
    var yAxis = 0;
    var context = canvas.contextCache;
    // Set the initial x and y, starting at 0,0 and translating to the origin on
    // the canvas.
    var x = t; //時間を横の位置とする
    var y = Math.sin(x)/zoom;
    context.moveTo(yAxis, unit*y+xAxis); //スタート位置にパスを置く

    // Loop to draw segments (横幅の分、波を描画)
    for (i = yAxis; i <= canvas.width + 10; i += 10) {
      x = t+(-yAxis+i)/unit/zoom;
      y = Math.sin(x - delay)/2.4;
      context.lineTo(i, unit*y+xAxis);
    }
  }
  init();
})();
              
            
!
999px

Console