html5 video canvas,html - html5: display video inside canvas - Stack Overflow

Using canvas to display Videos

Displaying a video is much the same as displaying an image. The minor differences are to do with onload events and the fact that you need to render the video every frame or you will only see one frame not the animated frames.

The demo below has some minor differences to the example. A mute function (under the video click mute/sound on to toggle sound) and some error checking to catch IE9+ and Edge if they don't have the correct drivers.

Keeping answers current.

The previous answers by user372551 is out of date (December 2010) and has a flaw in the rendering technique used. It uses the setTimeout and a rate of 33.333..ms which setTimeout will round down to 33ms this will cause the frames to be dropped every two seconds and may drop many more if the video frame rate is any higher than 30. Using setTimeout will also introduce video shearing created because setTimeout can not be synced to the display hardware.

There is currently no reliable method that can determine a videos frame rate unless you know the video frame rate in advance you should display it at the maximum display refresh rate possible on browsers. 60fps

The given top answer was for the time (6 years ago) the best solution as requestAnimationFrame was not widely supported (if at all) but requestAnimationFrame is now standard across the Major browsers and should be used instead of setTimeout to reduce or remove dropped frames, and to prevent shearing.

The example demo.

Loads a video and set it to loop. The video will not play until the you click on it. Clicking again will pause. There is a mute/sound on button under the video. The video is muted by default.

Note users of IE9+ and Edge. You may not be able to play the video format WebM as it needs additional drivers to play the videos. They can be found at tools.google.com Download IE9+ WebM support

// This code is from the example document on stackoverflow documentation. See HTML for link to the example.

// This code is almost identical to the example. Mute has been added and a media source. Also added some error handling in case the media load fails and a link to fix IE9+ and Edge support.

// Code by Blindman67.

// Original source has returns 404

// var mediaSource = "http://video.webmfiles.org/big-buck-bunny_trailer.webm";

// New source from wiki commons. Attribution in the leading credits.

var mediaSource = "http://upload.wikimedia.org/wikipedia/commons/7/79/Big_Buck_Bunny_small.ogv"

var muted = true;

var canvas = document.getElementById("myCanvas"); // get the canvas from the page

var ctx = canvas.getContext("2d");

var videoContainer; // object to hold video and associated info

var video = document.createElement("video"); // create a video element

video.src = mediaSource;

// the video will now begin to load.

// As some additional info is needed we will place the video in a

// containing object for convenience

video.autoPlay = false; // ensure that the video does not auto play

video.loop = true; // set the video to loop.

video.muted = muted;

videoContainer = { // we will add properties as needed

video : video,

ready : false,

};

// To handle errors. This is not part of the example at the moment. Just fixing for Edge that did not like the ogv format video

video.onerror = function(e){

document.body.removeChild(canvas);

document.body.innerHTML += "

There is a problem loading the video


";

document.body.innerHTML += "Users of IE9+ , the browser does not support WebM videos used by this demo";

document.body.innerHTML += " Download IE9+ WebM support from tools.google.com
this includes Edge and Windows 10";

}

video.oncanplay = readyToPlayVideo; // set the event to the play function that

// can be found below

function readyToPlayVideo(event){ // this is a referance to the video

// the video may not match the canvas size so find a scale to fit

videoContainer.scale = Math.min(

canvas.width / this.videoWidth,

canvas.height / this.videoHeight);

videoContainer.ready = true;

// the video can be played so hand it off to the display function

requestAnimationFrame(updateCanvas);

// add instruction

document.getElementById("playPause").textContent = "Click video to play/pause.";

document.querySelector(".mute").textContent = "Mute";

}

function updateCanvas(){

ctx.clearRect(0,0,canvas.width,canvas.height);

// only draw if loaded and ready

if(videoContainer !== undefined && videoContainer.ready){

// find the top left of the video on the canvas

video.muted = muted;

var scale = videoContainer.scale;

var vidH = videoContainer.video.videoHeight;

var vidW = videoContainer.video.videoWidth;

var top = canvas.height / 2 - (vidH /2 ) * scale;

var left = canvas.width / 2 - (vidW /2 ) * scale;

// now just draw the video the correct size

ctx.drawImage(videoContainer.video, left, top, vidW * scale, vidH * scale);

if(videoContainer.video.paused){ // if not playing show the paused screen

drawPayIcon();

}

}

// all done for display

// request the next frame in 1/60th of a second

requestAnimationFrame(updateCanvas);

}

function drawPayIcon(){

ctx.fillStyle = "black"; // darken display

ctx.globalAlpha = 0.5;

ctx.fillRect(0,0,canvas.width,canvas.height);

ctx.fillStyle = "#DDD"; // colour of play icon

ctx.globalAlpha = 0.75; // partly transparent

ctx.beginPath(); // create the path for the icon

var size = (canvas.height / 2) * 0.5; // the size of the icon

ctx.moveTo(canvas.width/2 + size/2, canvas.height / 2); // start at the pointy end

ctx.lineTo(canvas.width/2 - size/2, canvas.height / 2 + size);

ctx.lineTo(canvas.width/2 - size/2, canvas.height / 2 - size);

ctx.closePath();

ctx.fill();

ctx.globalAlpha = 1; // restore alpha

}

function playPauseClick(){

if(videoContainer !== undefined && videoContainer.ready){

if(videoContainer.video.paused){

videoContainer.video.play();

}else{

videoContainer.video.pause();

}

}

}

function videoMute(){

muted = !muted;

if(muted){

document.querySelector(".mute").textContent = "Mute";

}else{

document.querySelector(".mute").textContent= "Sound on";

}

}

// register the event

canvas.addEventListener("click",playPauseClick);

document.querySelector(".mute").addEventListener("click",videoMute)

body {

font :14px arial;

text-align : center;

background : #36A;

}

h2 {

color : white;

}

canvas {

border : 10px white solid;

cursor : pointer;

}

a {

color : #F93;

}

.mute {

cursor : pointer;

display: initial;

}

Basic Video & canvas example

Code example from Stackoverflow Documentation HTML5-Canvas

Basic loading and playing a video on the canvas

Loading content.
Attribution in the leading credits.

Canvas extras

Using the canvas to render video gives you additional options in regard to displaying and mixing in fx. The following image shows some of the FX you can get using the canvas. Using the 2D API gives a huge range of creative possibilities.

See video title in above demo for attribution of content in above inmage.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值