2013-03-19 99 views
1

如何在我的畫布動畫下在畫布上播放視頻?如何在我的畫布動畫下的畫布上播放視頻?

例如這個片段: http://trailers.apple.com/movies/marvel/ironman3/ironman3-tlr2_h480p.mov

所以沿着視頻底部這部動畫動作。

代碼:

<html> 
     <head> 
<script type='text/javascript' src='http://code.jquery.com/jquery-1.8.3.js'></script> 
     <script type="text/javascript"> 
      var context; 
      var text = ""; 
      var textDirection =""; 

      $(function() 
      { 
       context = document.getElementById("cvs").getContext("2d");    
       setInterval("animate()", 360); 

       textDirection ="right"; 
       textXpos = 5; 
       text = "This is my video..";  
      }); 

      function animate() {    
       // Clear screen 
       context.clearRect(0, 0, 500, 500); 
       context.globalAlpha = 1; 
       context.fillStyle = '#fff'; 
       context.fillRect(0, 0, 500, 500);  

       var metrics = context.measureText(text); 
       var textWidth = metrics.width; 

       if (textDirection == "right") { 
        textXpos += 10; 

        if (textXpos > 500 - textWidth) { 
         textDirection = "left"; 
        } 
       } 
       else { 
        textXpos -= 10; 

        if (textXpos < 10) { 
         textDirection = "right"; 
        }      
       } 

       context.font = '20px _sans'; 
       context.fillStyle = 'black'; 
       context.textBaseline = 'top'; 
       context.fillText (text, textXpos, 180);  
       }  
       </script> 
      </head> 
      <body> 
      <div id="page"> 
       <canvas id="cvs" width="500" height="500"> 
        Your browser does not support the HTML 5 Canvas. 
       </canvas> 
      </div> 
      </body> 
     </html> 

回答

1

我不認爲你需要在畫布上播放視頻。只需在您的HTML中創建一個視頻元素,並使用CSS將您的canvas元素放在它上面。

HTML:

<div id="page"> 
    <video id="video" autoplay loop> 
    <source id='mp4' 
    src="http://media.w3.org/2010/05/sintel/trailer.mp4" 
    type='video/mp4'> 
    <source id='webm' 
    src="http://media.w3.org/2010/05/sintel/trailer.webm" 
    type='video/webm'> 
    <source id='ogv' 
    src="http://media.w3.org/2010/05/sintel/trailer.ogv" 
    type='video/ogg'> 
    <p>Your user agent does not support the HTML5 Video element.</p> 
    </video> 
    <canvas id="cvs" width="500" height="500"> 
    Your browser does not support the HTML 5 Canvas. 
    </canvas> 
</div> 

CSS:

#cvs { 
    position: absolute; 
    top: 0px; 
    left: 0px; 
} 

Fiddle

+0

這不會顯示視頻頂部的文字。它將視頻向右移動並在創建的空間中執行動畫。 http://jsfiddle.net/bS79G/195/ – user2186669 2013-03-19 14:27:32

+0

對不起,忘了點擊保存。立即試用:http://jsfiddle.net/jBHCN/1/ – 2013-03-19 14:28:31