| Start |
|---|
| Basic loading and playing a video on the canvas. The canvas can be used to display video from a variety of sources. This example shows how to load a video as a file resource, display it and add a simple click on screen play/pause toggle. This stackoverflow self answered question How do I display a video using HTML5 canvas tag (opens new window) shows the following example code in action. Just an imageA video is just an image as far as the canvas is concerned. You can draw it like any image. The difference being the video can play and has sound. Get canvas and basic setupCreating and loading the videoUnlike images elements videos don't have to be fully loaded to be displayed on the canvas. Videos also provide a host of extra events that can be used to monitor status of the video. In this case we wish to know when the video is ready to play. Alternatively you can use Only use one of the canPlay events not both. The can play event (equivalent to image onload)DisplayingThe video will not play itself on the canvas. You need to draw it for every new frame. As it is difficult to know the exact frame rate and when they occur the best approch is to display the video as if running at 60fps. If the frame rate is lower then w just render the same frame twice. If the frame rate is higher then there is nothing that can be don to see the extra frames so we just ignore them. The video element is just a image element and can be draw like any image, you can scale, rotate, pan the video, mirror it, fade it, clip it and display only parts, draw it twice the second time with a global composite mode to add FX like lighten, screen, etc.. Basic play pause controlNow we have the video loaded and displayed all we need is the play control. We will make it as a click toggle play on the screen. When the video is playing and the user clicks the video is paused. When paused the click resumes play. We will add a function to darken the video and draw an play icon (triangle) Now the play pause eventSummaryPlaying a video is very easy using the canvas, adding effect in real time is also easy. There are however some limitations on formats, how you can play and seek. MDN HTMLMediaElement is the place to get the full referance to the video object. Once the image has been drawn on the canvas you can use Note if the video has sound then playing it will also play the sound. Happy videoing. |
| Loading and displaying an Image To load an image and place it on the canvas Creating an image There are several ways to create an image
The image is a Image.src property The image
*The dataURL is a 1 by 1 pixel gif image containing black Remarks on loading and errors The image will begin loading when its src property is set. The loading is syncriouse but the If you get an image from the page (for example
If the image is from an unreliable source and may not be accessible for a variety of reasons it will generate an error event. When this happens the image will be in a broken state. If you then attempt to draw it onto the canvas it will throw the following error By supplying the |
| Drawing an svg image To draw a vector SVG image, the operation is not different from a raster image : SVG images have some advantages over raster ones, since you won't loose quality, whatever the scale you'll draw it on your canvas. But beware, it may also be a bit slower than drawing a raster image. However, SVG images come with more restrictions than raster images. **For security purpose, no external content can be loaded from an SVG image referenced in an HTMLImageElement(` **The root element (` **Some browsers will [taint the canvas](http://stackoverflow.com/documentation/html5-canvas/1892/introduction-to-html5-canvas/10512/the-tainted-canvas#t=201607270037419008111) when an SVG image has been drawn to it.** |
| Capture canvas and Save as webM video Creating a WebM video from canvas frames and playing in canvas, or upload, or downloading. Example capture and play canvasRather than put in a huge effort only to be rejected, this is a quick insert to see if acceptable. Will Give full details if accepted. Also include additional capture options for better HD capture rates (removed from this version, Can capture HD 1080 at 50fps on good machines.) This was inspired by Wammy (opens new window) but is a complete rewrite with encode as you go methodology, greatly reducing the memory required during capture. Can capture more than 30 seconds better data, handling algorithms. Note frames are encoded into webP images. Only Chrome supports webP canvas encoding. For other browsers (Firefox and Edge) you will need to use a 3rd party webP encoder such as Libwebp Javascript (opens new window) Encoding WebP images via Javascript is slow. (will include addition of raw webp images support if accepted). The webM encoder inspired by Whammy: A Real Time Javascript WebM (opens new window) |