asimryu

canvas play video

Jul 10th, 2020
244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 1.38 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html lang="ko">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <link rel="stylesheet" href="http://code.jquery.com/ui/1.11.2/themes/smoothness/jquery-ui.css">
  6. <script src="http://code.jquery.com/jquery-latest.js"></script>
  7. <script src="http://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
  8. <title>캔버스에 비디오 재생하기</title>
  9. <style type="text/css">
  10. canvas {border:1px solid #000;}
  11. </style>
  12. </head>
  13. <body>
  14. <form>
  15. <input type="file" id="video">
  16. </form>
  17. <canvas id="mycanvas" width="500" height="500"></canvas>
  18. <script type="text/javascript">
  19. var canvas = document.getElementById("mycanvas");
  20. var ctx = canvas.getContext("2d");
  21. var video = document.createElement("video");
  22. video.addEventListener('loadeddata', function() {
  23.   video.play();
  24.   refresh();
  25. });
  26.  
  27. $("#video").change(function(){
  28.     videoLoad(this);
  29. });
  30.  
  31. function videoLoad(videoSelector){
  32.     if( ! videoSelector.files ) return;
  33.     if( ! videoSelector.files[0] ) return;
  34.     console.log("v");
  35.     var reader = new FileReader();
  36.     reader.onload = function(event){
  37.         var src = event.target.result;
  38.         video.src = src;
  39.     };
  40.     reader.readAsDataURL(videoSelector.files[0]);
  41. }
  42.  
  43. function refresh(){
  44.     ctx.drawImage(video,0,0,canvas.width,canvas.height);  
  45.     requestAnimationFrame(refresh);  
  46. }
  47. </script>
  48. </body>
  49. </html>
Add Comment
Please, Sign In to add comment