Use of video tags and canvas drawings in HTML5

Source: Internet
Author: User

Use of video tags

The video tag defines the movie, which is a new label in HTML5, and its properties are as follows (refer to the documentation):

  

Domo01

<! DOCTYPE html>"en">"UTF-8"> <title>demo01</title>"Madashuai.mp4"loop="Loop"autoplay="AutoPlay"controls="Controls"Width=" -"height=" -"></video></body>
 
 
    • SRC is the path to the video
    • Loop is looping, that is, playback continues after the play is finished
    • AutoPlay is ready to play
    • Controls appear as control controls, and no start-pause buttons if not
    • Width and height to control the video widths and heights

Use of DrawImage in canvas

The first parameter that is accepted can be an IMG, or video, or a canvas, and the following parameters are used for clipping and controlling the width of the height.

DOMO02:

<! DOCTYPE html>"en">"UTF-8"> <title>demo01</title>"Myvideo"Src="Madashuai.mp4"loop="Loop"autoplay="AutoPlay"controls="Controls"></video> <br> <button id="Snapscreen"> screenshot </button> <canvas id="MyCanvas"Width=" -"height=" -"></canvas> <script>varMyCanvas = document.getElementById ('MyCanvas'). GetContext ('2d'), Snapscreen= document.getElementById ('Snapscreen'), Video= document.getElementById ('Myvideo'); Snapscreen.onclick=function () {mycanvas.drawimage (video,0,0, -, -); } </script></body>

    • We get to the canvas environment first and then click the button to take the current video screenshot.

Navigator.mediaDevices.getUserMedia ()

By this method, we can get the right to use the local webcam, official documentation.

When used, it is usually combined with the video tag, that is, the camera is displayed on video.

DEMO03:

<! DOCTYPE html>"en">"UTF-8"> <title>demo01</title>"Myvideo"Src=""></video> <script>Navigator.mediaDevices.getUserMedia ({//Audio:true,//You can also open the sound here .Videotrue}). Then (function (mediastream) {varMyvideo = document.getElementById ('Myvideo'); Myvideo.srcobject=MediaStream; Myvideo.onloadedmetadata=function () {Myvideo.controls="Controls";    Myvideo.play (); }  }); </script></body>

    • Note: The use of MediaStream here is similar to the usage of promise in ES6, which is passed to then as a parameter after the result is obtained.
    • The srcobject tag that assigns the video source to the visual tag can be displayed in video.
    • The Onloadedmetadata event is triggered after the video file has been loaded.

Now we can combine the camera with video and canvas so we can take a screenshot.

demo04

<! DOCTYPE html>"en">"UTF-8"> <title>demo01</title>"Myvideo"Width=" -"height=" -"Src=""></video> <button id="Snapscreen"> screenshot </button> <canvas id="MyCanvas"Width=" -"height=" -"></canvas> <script>varMyvideo = document.getElementById ('Myvideo'); Navigator.mediaDevices.getUserMedia ({video:true}). Then (function (mediastream) {Myvideo.srcobject=MediaStream; Myvideo.onloadedmetadata=function () {Myvideo.controls="Controls";    Myvideo.play ();  }  }); varSnapscreen = document.getElementById ('Snapscreen'), Canvas= document.getElementById ('MyCanvas'). GetContext ('2d'); Snapscreen.onclick=function () {canvas.drawimage (Myvideo,0,0); } </script></body>

The following is quite interesting:

Demo05

<! DOCTYPE html>"en">"UTF-8"> <title>demo01</title>class="Myvideos"Src=""></video> <videoclass="Myvideos"Src=""></video> <videoclass="Myvideos"Src=""></video> <videoclass="Myvideos"Src=""></video> <videoclass="Myvideos"Src=""></video> <videoclass="Myvideos"Src=""></video> <videoclass="Myvideos"Src=""></video> <videoclass="Myvideos"Src=""></video> <videoclass="Myvideos"Src=""></video> <videoclass="Myvideos"Src=""></video> <videoclass="Myvideos"Src=""></video> <videoclass="Myvideos"Src=""></video> <videoclass="Myvideos"Src=""></video> <videoclass="Myvideos"Src=""></video> <videoclass="Myvideos"Src=""></video> <script>varMyvideos = Document.getelementsbyclassname ('Myvideos'); Navigator.mediaDevices.getUserMedia ({video:true}). Then (function (mediastream) { for(vari =0; i < myvideos.length; i++) {Myvideos[i].srcobject=MediaStream; Myvideos[i].width= -;    Myvideos[i].play (); }  }); </script></body>

Use of video tags and canvas drawings in HTML5

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.