appcropolois
  •  Templates
  • Builder
    •  Product Tour
    •  Sign up
    •  Dashboard
  •  Learn
  •  Discover
  •  Upgrade
  •  Sign in
  •  My Profile
    • View Orders
    • Edit Profile
    • Sign Out
  •  Cart
    ➜ 
  1. Home
  2. Learn
  3. Using HTML5 Canvas to capture ...
Share

Using HTML5 Canvas to capture frames from a video

HTML5 has some interesting capability that allows extracting individual frames from a video source and draw it on a canvas element. The following example quickly shows how to capture a frame from a video and attach it to the DOM as an HTML5 canvas. To run this example please use FireFox 3.5 or a WebKit based browser.

Once you have captured an image click on it to see the base64 encoded version on a separate window.

Javascript code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
var videoId = 'video';
var scaleFactor = 0.25;
var snapshots = [];
 
/**
 * Captures a image frame from the provided video element.
 *
 * @param {Video} video HTML5 video element from where the image frame will be captured.
 * @param {Number} scaleFactor Factor to scale the canvas element that will be return. This is an optional parameter.
 *
 * @return {Canvas}
 */
function capture(video, scaleFactor) {
    if(scaleFactor == null){
        scaleFactor = 1;
    }
    var w = video.videoWidth * scaleFactor;
    var h = video.videoHeight * scaleFactor;
    var canvas = document.createElement('canvas');
        canvas.width  = w;
        canvas.height = h;
    var ctx = canvas.getContext('2d');
        ctx.drawImage(video, 0, 0, w, h);
    return canvas;
} 
 
/**
 * Invokes the <code>capture</code> function and attaches the canvas element to the DOM.
 */
function shoot(){
    var video  = document.getElementById(videoId);
    var output = document.getElementById('output');
    var canvas = capture(video, scaleFactor);
        canvas.onclick = function(){
            window.open(this.toDataURL());
        };
    snapshots.unshift(canvas);
    output.innerHTML = '';
    for(var i=0; i<4; i++){
        output.appendChild(snapshots[i]);
    }
}
var videoId = 'video';
var scaleFactor = 0.25;
var snapshots = [];

/**
 * Captures a image frame from the provided video element.
 *
 * @param {Video} video HTML5 video element from where the image frame will be captured.
 * @param {Number} scaleFactor Factor to scale the canvas element that will be return. This is an optional parameter.
 *
 * @return {Canvas}
 */
function capture(video, scaleFactor) {
	if(scaleFactor == null){
		scaleFactor = 1;
	}
	var w = video.videoWidth * scaleFactor;
	var h = video.videoHeight * scaleFactor;
	var canvas = document.createElement('canvas');
		canvas.width  = w;
	    canvas.height = h;
	var ctx = canvas.getContext('2d');
		ctx.drawImage(video, 0, 0, w, h);
    return canvas;
} 

/**
 * Invokes the <code>capture</code> function and attaches the canvas element to the DOM.
 */
function shoot(){
	var video  = document.getElementById(videoId);
	var output = document.getElementById('output');
	var canvas = capture(video, scaleFactor);
		canvas.onclick = function(){
			window.open(this.toDataURL());
		};
	snapshots.unshift(canvas);
	output.innerHTML = '';
	for(var i=0; i<4; i++){
		output.appendChild(snapshots[i]);
	}
}

HTML code:

1
2
3
4
5
6
7
8
9
<div style="border: solid 1px #ccc; padding: 10px; text-align: center;">
    <video id="video" width="320" controls="true">
        <source src="video.ogv"><!-- FireFox 3.5 -->
        <source src="movie.mp4"><!-- WebKit -->
        Your browser does not support HTML5 video tag. Please download FireFox 3.5 or higher.
    </video><br/>
    <button onclick="shoot()" style="width: 64px;border: solid 2px #ccc;">Capture</button><br/>
    <div id="output" style="display: inline-block; top: 4px; position: relative ;border: dotted 1px #ccc; padding: 2px;"></div>
</div>
<div style="border: solid 1px #ccc; padding: 10px; text-align: center;">
	<video id="video" width="320" controls="true">
		<source src="video.ogv"><!-- FireFox 3.5 -->
		<source src="movie.mp4"><!-- WebKit -->
		Your browser does not support HTML5 video tag. Please download FireFox 3.5 or higher.
	</video><br/>
	<button onclick="shoot()" style="width: 64px;border: solid 2px #ccc;">Capture</button><br/>
	<div id="output" style="display: inline-block; top: 4px; position: relative ;border: dotted 1px #ccc; padding: 2px;"></div>
</div>

 

By: Raul Sanchez
Category: Web Technology
Posted on: Thursday, February 2, 2012

Sign in to post your comment


Not registered? Create an Account.

Lost your password? Click here to recover.

8 comment


Using HTML5 Canvas to capture frames from a video | Sharp as a Tack
Using HTML5 Canvas to capture frames from a video | Sharp as a Tack Thursday 23rd, February 2012 - 05:05:06 PM

[...] could see this becoming a popular plugin for people to screen capture video.  The code is provided here demonstrating how to do it. This entry was posted in Code, HTML5 by TheCaddy. Bookmark the [...]

Matijs
Matijs Tuesday 5th, March 2013 - 05:36:56 PM

Can you show us some code on how to save the image to a folder on the website ? Thx

sanraul
sanraul Monday 11th, March 2013 - 09:03:23 PM

Mathjs, Saving the image in the server sounds like a good idea for another article. Thanks

Matijs
Matijs Wednesday 13th, March 2013 - 04:55:12 PM

Maybe when you make that new article, could you reply it here ?

sanraul
sanraul Wednesday 13th, March 2013 - 08:45:03 PM

Deal!

Matijs
Matijs Thursday 14th, March 2013 - 08:21:32 PM

Ok thx

Miguel
Miguel Thursday 9th, May 2013 - 10:03:02 AM

This is exactly what i needed.. I just wish i could save the image on a file in the server :\

Amit
Amit Wednesday 15th, April 2015 - 12:18:20 PM

can i create same application for screen recorder using phonegap code

Building Apps?

Create, customize, and publish mobile web applications using the Appcropolis Mobile Builder.

Get Started
Browse Categories
  • Books (0)
  • CSS3 (2)
  • Design (0)
  • Frameworks (10)
  • How-To (6)
  • HTML5 (2)
  • jQuery Mobile (7)
  • Miscelaneous (1862)
  • Mobile Builder (1)
  • News (9)
  • Opinion (1)
  • User Experience (0)
  • Web Technology (11)
  • Webclips (4)

Ready-to-use Templates

Check out Appcropolis Mobile Templates

We offer 1,000's of mobile templates that are fully designed, coded, and ready to use. Learn more.

Download Free Templates

Contact Us

Your feedback means the world to us.
Contact us if you have any questions or
any feature requests:

Saint Louis, MO 63101
911 Washington Ave
Email: info@appcropolis.com

Appcropolis

  •   About Us
  •   Blog
  •   Discover
  •   Templates
  •   Contact Us

Get Free Templates

Subscribe to our newsletter to receive
notification about new templates, prodcut updates,
and technical articles.


© 2025 Appcropolis, LLC · All Rights Reserved
Sitemap  ·  Terms Of Service  ·  Privacy