cssAudio - ActiveCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - Activehtmlicon-personicon-teamoctocatspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External CSS

These stylesheets will be added in this order and before the code you write in the CSS editor. You can also add another Pen here, and it will pull the CSS from it. Try typing "font" or "ribbon" below.

Quick-add: + add another resource

Add External JavaScript

These scripts will run in this order and before the code in the JavaScript editor. You can also link to another Pen here, and it will run the JavaScript from it. Also try typing the name of any popular library.

Quick-add: + add another resource

Code Indentation

     

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.

            
              <div class="container">
  <!-- video要素 -->
  <video id="js-video" src="https://dl.dropboxusercontent.com/u/98366439/blog/sample.mp4">
  </video>
  
  <!-- canvas要素 -->
  <canvas id="js-canvas"></canvas>
</div>

<div class="container">
  <!-- playButton要素 -->
  <button id="js-playButton">play</button>
</div>

            
          
!
            
              .container {
  width: 100%;
}

#js-playButton {
  width: 100%;
  height: 80px;
}

#js-video {
  display: none;
}
            
          
!
            
              // video要素
var elemVideo = document.getElementById('js-video');

// canvas要素
var elemCanvas = document.getElementById('js-canvas');

// playButton要素
var elemPlayButton = document.getElementById('js-playButton');

// canvasDrawのスキップ
var canvasDrawSkip = false;

/**
 * videoの再生を実行
 */
function videoPlay() {
  // fpsを設定
  var fps = 24;
  var frameTime = 1000 / fps;
  
  // currentTimeを初期化
  var currentTime = 0;
    
  var timer = setInterval(function () {
    if (canvasDrawSkip === true) {
      return;
    }
  // canvasDrawのスキップを設定
    canvasDrawSkip = true;
    // currentTimeを加算
    currentTime += frameTime / 1000;


    // videoのcurrentTimeを設定
    if (elemVideo.duration < currentTime) {
      // currentTimeがvideoのdurationを超過した場合
      elemVideo.currentTime = elemVideo.duration;
      clearInterval(timer);
    } else {
      elemVideo.currentTime = currentTime;
    }
  }, frameTime);
}

/**
 * canvasの描画
 */
function canvasDraw() {
  // canvasの大きさを設定
  // video要素の大きさに合わせる
  elemCanvas.setAttribute('width', elemVideo.videoWidth);
  elemCanvas.setAttribute('height', elemVideo.videoHeight);
  
  // canvasのcontextを取得
  var ctx = elemCanvas.getContext('2d');
  
  // video要素をcanvasに描画
  ctx.drawImage(elemVideo, 0, 0, elemVideo.videoWidth, elemVideo.videoHeight);
  
  // canvasDrawのスキップを解除
  canvasDrawSkip = false;
}

// イベントを設定
window.addEventListener('load', function () {  
  // playButton要素のonclickでvideoの再生を実行
  elemPlayButton.addEventListener('click', videoPlay);  
  
  // video要素のonloadeddataでcanvasの描画を実行
  elemVideo.addEventListener('loadeddata', canvasDraw);
  
  // video要素ontimeupdateでcanvasの描画を実行
  elemVideo.addEventListener('timeupdate', canvasDraw);
  
  // videoのloadを実行
  elemVideo.load();
});
            
          
!
999px
Close

Asset uploading is a PRO feature.

As a PRO member, you can drag-and-drop upload files here to use as resources. Images, Libraries, JSON data... anything you want. You can even edit them anytime, like any other code on CodePen.

Go PRO

Loading ..................

Console