Home >Web Front-end >H5 Tutorial >Example code for capturing still images from video using HTML5

Example code for capturing still images from video using HTML5

零下一度
零下一度Original
2017-05-04 15:21:321634browse

A simple example showing how to capture a still image from a video. Let's take a look

Suppose you have the following HTML code:

<video id="video" controls="controls">
    <source src=".mp4" />
</video>
 
<button id="capture">Capture</button>
 
<p id="output"></p>

Then, when the user clicks the "Capture" button, we write the current video content to a canvas and use Display the image on the canvas.

(function() {
    "using strict";
 
    var $video, $output;
    var scale = 0.25;
 
    var initialize = function() {
        $output = $("#output");
        $video = $("#video").get(0);
        $("#capture").click(captureImage);               
    };
 
    var captureImage = function() {
        var canvas = document.createElement("canvas");
        canvas.width = $video.videoWidth * scale;
        canvas.height = $video.videoHeight * scale;
        canvas.getContext(&#39;2d&#39;)
              .drawImage($video, 0, 0, canvas.width, canvas.height);
 
        var img = document.createElement("img");
        img.src = canvas.toDataURL();
        $output.prepend(img);
    };
    $(initialize);           
}());

【Related recommendations】

1. Free h5 online video tutorial

2.HTML5 full version manual

3. php.cn original html5 video tutorial

The above is the detailed content of Example code for capturing still images from video using HTML5. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn