DZone
Web Dev Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Web Dev Zone > Adding Video Element on the Fly and Drawing it to a Canvas Element

Adding Video Element on the Fly and Drawing it to a Canvas Element

Gil Fink user avatar by
Gil Fink
·
Nov. 01, 11 · Web Dev Zone · Interview
Like (0)
Save
Tweet
8.99K Views

Join the DZone community and get the full member experience.

Join For Free

one of the requirements in a project that i work on lately is to create on the fly video elements and to show them inside a canvas element. this post will show you how you can do it.

adding video element on the fly and drawing it to a canvas element

adding 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.

Element

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Is Your Code DRY or WET?
  • Java’s Encapsulation - When the Getter and Setter Became Your Enemy
  • Practice on Pushing Messages to Devices of Different Manufacturers
  • Applying Domain-Driven Design Principles to Microservice Architectures

Comments

Web Dev Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo