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 class="container">
  <div class="video-container">
    <video id="SourceVideo" src="https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4" loop crossorigin="Anonymous">
  </div>

  <div class="video-container">
    <canvas id="Canvas" class="video"></canvas>
    <button id="Button">Invert Colors</button>
  </div>
</div> -->
  
  
<div class="container">
  <div class="column">
    <video id="SourceVideo" src="https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4" loop crossorigin="Anonymous"></video>
  </div>
  <div class="column">
    <div class="canvas">
       <canvas id="Canvas" class="video"></canvas>
    </div>
  </div>
</div>

<button id="StartButton">Start</button>
<button id="ToggleButton">Toggle Effect</button>
    
              
            
!

CSS

              
                .container {
  display: flex;
  width: 400px;
  
  .column {
    max-width: 100%;
    flex: 0 0 400px;
  }
  
  video, canvas {
    max-width: 100%;
  }
  
  canvas {
    border: 1px solid #ccc;
  }
}

.video-container {
  width: 400px;
}
              
            
!

JS

              
                var startButton;
var canvas;
var context;
var video;
var canPlay;
var applyEffect;

function init() {
  startButton = document.getElementById('StartButton');
  toggleButton = document.getElementById('ToggleButton');
  canvas = document.getElementById('Canvas');
  context = canvas.getContext('2d');
  video = document.getElementById('SourceVideo');
  
  video.muted = true;

  if (video.readyState >= 3) {
    readyToPlay();
  } else {
    video.addEventListener('canplay', readyToPlay);
  }
  
  startButton.addEventListener('click', function () {
    console.log(canPlay);
    if (!canPlay) return;
    // Play video
    video.play();
    
    drawFrame(video);
  });
  
  toggleButton.addEventListener('click', function () {
    applyEffect = !applyEffect;
  });
}

function readyToPlay() {
  // Set the canvas the same width and height of the video
  canvas.width = video.videoWidth;
  canvas.height = video.videoHeight;

  canPlay = true;
}

function drawFrame(video) {
  context.drawImage(video, 0, 0);
  
  if (applyEffect) {
    var imageData = context.getImageData(0, 0, canvas.width, canvas.height);
    invertColors(imageData.data);
    context.putImageData(imageData, 0, 0);
  }
  
  setTimeout(function () {
    drawFrame(video);
  }, 10);
}


function invertColors(data) {
  for (var i = 0; i < data.length; i+= 4) {
    data[i] = data[i] ^ 255; // Invert Red
    data[i+1] = data[i+1] ^ 255; // Invert Green
    data[i+2] = data[i+2] ^ 255; // Invert Blue
  }
}

window.addEventListener('load', init);
              
            
!
999px

Console