Adding Video Element on the Fly and Drawing it to a Canvas Element
Join the DZone community and get the full member experience.
Join For FreeAdding Video Element on the Fly
The HTML5 Video element is a DOM element. As any other DOM element you can create it using the document.createElement function and then to supply its relevant properties. After the creation of the element you will have to append it into the DOM tree. One place that you might append the element to is at the end of the body element (but you might append it to other elements as well). The following code snippet shows how to create a video element on the fly and append it to the body element:
var video = document.createElement('video'); video.src = 'your source goes here'; video.controls = true; document.body.appendChild(video);
Drawing The Video to a Canvas Element
After you have created the video element and append it to the DOM tree the video won’t start up until somebody will play it (or you use the autoplay attribute). If you need to display that video on a canvas you will have to wire up an event handler to the video’s onplay function and to draw the video on the canvas in the handler. Pay attention that you will have to draw the video’s frames or the canvas will only show the first frame that you draw. This is way you will need to call the draw in an interval. Here is a simple example of how to do exactly that:
var canvas = document.getElementById('canvas') var ctx = canvas.getContext('2d'); video.onplay = function() { canvas.width = video.videoWidth; canvas.height = video.videoHeight; draw(); }; function draw() { if(video.paused || video.ended) return false; ctx.drawImage(video, 0, 0); setTimeout(draw, 20); }
The Full Example
Here is the full example:
<!DOCTYPE html> <html> <head> <title>On the Fly Video inside a Canvas</title> </head> <body> <canvas id='canvas'></canvas> <script type="text/javascript"> var video = document.createElement('video'); video.src = 'trailer.mp4'; video.controls = true; document.body.appendChild(video); var canvas = document.getElementById('canvas') var ctx = canvas.getContext('2d'); video.onplay = function() { canvas.width = video.videoWidth; canvas.height = video.videoHeight; draw(); }; function draw() { if(video.paused || video.ended) return false; ctx.drawImage(video, 0, 0); setTimeout(draw, 20); } </script> </body> </html>
Summary
When you need to create on the fly videos in your web pages you can just create them as a any other DOM element. If you need to show these elements inside a canvas then you will have to draw the video inside the canvas element while it is playing.
Published at DZone with permission of Gil Fink, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Trending
-
Which Is Better for IoT: Azure RTOS or FreeRTOS?
-
Building and Deploying Microservices With Spring Boot and Docker
-
Best Practices for Securing Infrastructure as Code (Iac) In the DevOps SDLC
-
Top Six React Development Tools
Comments