cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

Pen Settings

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.

Quick-add: + add another resource

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.

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" preload="auto">
  </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;
}
            
          
!
            
              // 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
Loading ..................

Console