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

零下一度
Release: 2017-05-04 15:21:32
Original
1618 people have browsed it

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>
Copy after login

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);           
}());
Copy after login

【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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template