Draw video on canvas HTML5

后端 未结 3 919
感情败类
感情败类 2021-02-06 03:30

I\'m trying to draw a video on a canvas. To achieve this I capture the onMouseDown and onMouseUp events in Javascript to get the x and y coordinates of each event (that I need t

相关标签:
3条回答
  • 2021-02-06 03:31

    1) The drawImage() method was only be called once. It needs to be called once per frame.

    2) Call pause() method to stop video.

    For example, the follow code starts a timer loop (for drawing the frames) on mouse up and stops any previous video on mouse down.

    var canvas, context, video, xStart, yStart, xEnd, yEnd;
    
    canvas = document.getElementById("canvas");
    context = canvas.getContext("2d");
    canvas.addEventListener("mousedown", mouseDown);
    canvas.addEventListener("mouseup", mouseUp);
    
    function mouseDown(e) {
        if (video) {
            video.pause();
            video = null;
            context.clearRect(0, 0, canvas.width, canvas.height);
        }
        xStart = e.offsetX;
        yStart = e.offsetY;
    }
    
    function mouseUp(e) {
        xEnd = e.offsetX;
        yEnd = e.offsetY;
        if (xStart != xEnd && yStart != yEnd) {
            video = document.createElement("video");
            video.src = "http://techslides.com/demos/sample-videos/small.mp4";
            video.addEventListener('loadeddata', function() {
                console.log("loadeddata");
                video.play();
                setTimeout(videoLoop, 1000 / 30);
            });
        }
    }
    
    function videoLoop() {
        if (video && !video.paused && !video.ended) {
            context.drawImage(video, xStart, yStart, xEnd - xStart, yEnd - yStart);
            setTimeout(videoLoop, 1000 / 30);
        }
    }
    
    0 讨论(0)
  • 2021-02-06 03:32

    You need to set up a continuous rendering. You are only rendering the first frame of the video. Canvas is dumb and does not know about videos. You have just dumped pixels from the video onto the canvas. You need to update the canvas continuously.

    The best way is to use requestAnimationFrame this makes sure everything is synced up with the browsers own rendering.

    In the example below the rendering is started when the video is loaded. Be sure to end the previous updates if you load a second video.

    var canvas = document.getElementById("canV");
    var ctx = canvas.getContext("2d");
    
    var video = document.createElement("video");
    video.src = "http://techslides.com/demos/sample-videos/small.mp4";
    video.addEventListener('loadeddata', function() {
      video.play();  // start playing
      update(); //Start rendering
    });
    
    
    
    
    function update(){
      ctx.drawImage(video,0,0,256,256);   
      requestAnimationFrame(update); // wait for the browser to be ready to present another animation fram.       
    }
    #canV {
      width:256px;
      height:256px;
    }
    <canvas id="canV" width=256 height=256></canvas>

    0 讨论(0)
  • 2021-02-06 03:50

    From last few days i am working on same project called as chroma keying effect which consist f mixing of two videos.so after searching a long on internet i found an api called RecordRTC this API can give you a solution.

    0 讨论(0)
提交回复
热议问题