用HTML5制作視頻拼圖的教程

幾天前同事給我看了一個(gè)特效,是一個(gè)拼圖游戲,不同的是,拼圖里的是動(dòng)畫(huà)。他讓我看下做個(gè)DEMO,于是就自己整了一會(huì),也確實(shí)不難。用canvas很容易做。所以這篇博文不適合高手看。。。。就是隨便寫(xiě)來(lái)玩玩的。
效果圖:
至少我剛看到這個(gè)的時(shí)候覺(jué)得挺新穎的,所以才會(huì)想到做出來(lái)玩玩,覺(jué)得樓主out的哥們請(qǐng)輕噴
不多說(shuō),先上DEMO:視頻拼圖 (或許要等一會(huì)才能看到效果,我是直接在w3school那里搞了個(gè)視頻鏈接過(guò)來(lái)的,拖動(dòng)什么的都做的很簡(jiǎn)單,或許還有些bug,畢竟就只是做一個(gè)DEMO玩玩而已,說(shuō)說(shuō)原理就行了),還有一點(diǎn),直接把視頻的當(dāng)前幀畫(huà)到canvas中在移動(dòng)設(shè)備上好像還不支持。。。至少我用ipad看了一下,發(fā)現(xiàn)畫(huà)不上去,如果有知道腫么解決這問(wèn)題的大牛請(qǐng)為小弟解答一下,不甚感激
原理:每一塊拼圖就是一個(gè)canvas,同時(shí)還需要一個(gè)離屏canvas。先整一個(gè)video標(biāo)簽
并且把video隱藏掉,然后播放視頻的時(shí)候把每一幀都畫(huà)到離屏canvas中(離屏canvas就是隱藏了的canvas,用于保存數(shù)據(jù)),寫(xiě)法很簡(jiǎn)單:
,直接用drawImage方法畫(huà)上去就行了。為何要先用離屏canvas呢,因?yàn)槿绻苯影衙恳粠瑪?shù)據(jù)同時(shí)畫(huà)到所有拼圖塊的canvas中,瀏覽器會(huì)瞬間崩掉。所以用一個(gè)離屏canvas作為緩沖。先把當(dāng)前幀的數(shù)據(jù)保存到canvas,然后再將canvas畫(huà)到作為拼圖塊的canvas中。將canvas畫(huà)到canvas中也很簡(jiǎn)單,也是用drawImage就可以搞定:
ctx2.drawImage(cs , -this.cols*this.w , -this.rows*this.h , vw , vh);
然后。。。。原理就這么簡(jiǎn)單,之后提醒一點(diǎn),用requestAnimationFrame循環(huán)取幀時(shí),要限一下速,例如下面所寫(xiě)的,我是每30毫秒取一次,推薦30~50毫秒,太低瀏覽器容易崩潰,太高的話視頻出現(xiàn)卡幀現(xiàn)象了:
function animate(){
var newTime = new Date();
if(newTime - lastTime > 30){
lastTime = newTime;
ctx.drawImage(video , 0 , 0 , vw , vh);
canvases.forEach(function(){
var ctx2 = this.cas.getContext('2d');
ctx2.drawImage(cs , -this.cols*this.w , -this.rows*this.h , vw , vh);
});
}
if("requestAnimationFrame" in window){
requestAnimationFrame(animate);
}
else if("webkitRequestAnimationFrame" in window){
webkitRequestAnimationFrame(animate);
}
else if("msRequestAnimationFrame" in window){
msRequestAnimationFrame(animate);
}
else if("mozRequestAnimationFrame" in window){
mozRequestAnimationFrame(animate);
}
}
最后貼出所有代碼:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<style>
body{margin:0;padding:0;}
.allCanvas{
position: relative;
margin:50px auto;
width:600px;
}
.vcanvas{
position: absolute;
display: block;
border: 1px solid;
}
</style>
<title>視頻拼圖</title>
</head>
<body>
<div class="allCanvas">
<canvas id="liping" width="600" height="400" style="display:none"></canvas>
</div>
<video id="video" src="http://www.w3school.com.cn/example/html5/mov_bbb.mp4" width="600px" height="400px" controls="control" loop="loop" style="display:block;position:absolute;top:-6000px;"></video>
<script>
var video = document.getElementById("video");
var cs = document.getElementById("liping");
var ctx = cs.getContext('2d')
var rows = 3,
cols = 3,
cb = document.querySelector(".allCanvas"),
vw = 600,
vh = 400,
canvases = [];</p> <p> function createCanvas(){
var num = rows*cols;
for(var i=0;i<cols;i++){
for(var j=0;j<rows;j++){
var canvas = new vCanvas(Math.random()*600, Math.random()*600 , vw/rows , vh/cols , j , i);
canvases.push(canvas);
}
}
}</p> <p> var vCanvas = function(x,y,w,h,cols,rows){
this.x = x;
this.y = y;
this.w = w;
this.h = h;
this.cols = cols;
this.rows = rows;
this.creat();
this.behavior();
}
vCanvas.prototype = {
creat:function(){
this.cas = document.createElement("canvas");
cb.appendChild(this.cas);
this.cas.className = "vcanvas";
this.cas.id = "vc_"+(this.cols+1)*(this.rows+1);
this.cas.style.left = this.x+"px";
this.cas.style.top = this.y+"px";
this.cas.width = this.w;
this.cas.height = this.h;
},
behavior:function(){
this.cas.onmousedown = function(e){
e = e || window.event;
var that = this;
var om = {
x:e.clientX,
y:e.clientY
}
window.onmousemove = function(e){
e = e || window.event;
var nm = {
x:e.clientX,
y:e.clientY
}
that.style.left = parseInt(that.style.left.replace("px","")) + (nm.x-om.x) + "px";
that.style.top = parseInt(that.style.top.replace("px","")) + (nm.y-om.y) + "px";
om = nm;
}
window.onmouseup = function(){
this.onmousemove = null;
}
}
}
}</p> <p> Array.prototype.forEach = function(callback){
for(var i=0;i<this.length;i++){
callback.call(this[i]);
}
}</p> <p> var lastTime = 0;
function initAnimate(){
lastTime = new Date();
createCanvas();
animate();
}</p> <p> function animate(){
var newTime = new Date();
if(newTime - lastTime > 30){
lastTime = newTime;
ctx.drawImage(video , 0 , 0 , vw , vh);
canvases.forEach(function(){
var ctx2 = this.cas.getContext('2d');
ctx2.drawImage(cs , -this.cols*this.w , -this.rows*this.h , vw , vh);
});
}
if("requestAnimationFrame" in window){
requestAnimationFrame(animate);
}
else if("webkitRequestAnimationFrame" in window){
webkitRequestAnimationFrame(animate);
}
else if("msRequestAnimationFrame" in window){
msRequestAnimationFrame(animate);
}
else if("mozRequestAnimationFrame" in window){
mozRequestAnimationFrame(animate);
}
}</p> <p> video.play();
initAnimate();
</script>
</body>
</html>
相關(guān)文章
- 拖拽API是HTML5的新特性,相對(duì)于其他新特性來(lái)說(shuō),重要程度占到6成。這篇文章通過(guò)經(jīng)典案例給大家介紹了HTML5拖拽API的知識(shí)要點(diǎn),需要的朋友參考下吧2018-04-20
- 這篇文章主要介紹了HTML5 拖拽批量上傳文件的示例代碼的相關(guān)資料,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-03-28
- 這是一款基于html5 canvas繪制可拖拽的3D立體文字效果源碼。畫(huà)面上立體的文字可通過(guò)鼠標(biāo)拖拽改變觀察角度,也可通過(guò)鼠標(biāo)滾輪進(jìn)行文字的縮放。文字帶有極其強(qiáng)烈的明暗陰影視2017-12-09
html5 canvas繪制的可拖拽全屏3D地球旋轉(zhuǎn)動(dòng)畫(huà)特效源碼
這是一款基于html5 canvas繪制的可拖拽全屏3D地球旋轉(zhuǎn)動(dòng)畫(huà)特效源碼。頁(yè)面的星空背景上呈現(xiàn)出3D地球旋轉(zhuǎn)的效果,通過(guò)鼠標(biāo)拖動(dòng)可改變觀察視角。點(diǎn)擊右下方的按鈕可切換到全屏2017-12-08HTML5+Three.js WebGL實(shí)現(xiàn)的月球發(fā)光著色與拖拽動(dòng)畫(huà)效果源碼
這是一款基于HTML5+Three.js WebGL實(shí)現(xiàn)的月球發(fā)光著色與拖拽動(dòng)畫(huà)效果源碼。畫(huà)面中的星空背景中央是帶有大氣層發(fā)光效果的月球,通過(guò)鼠標(biāo)拖拽可改變?cè)虑虻挠^察角度。右上方的2017-10-26- 下面小編就為大家?guī)?lái)一篇HTML5拖拽的簡(jiǎn)單實(shí)例。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-05-30
html5實(shí)現(xiàn)的網(wǎng)格圖片鼠標(biāo)拖動(dòng)拼圖游戲源碼
這是一款基于html5實(shí)現(xiàn)的網(wǎng)格圖片鼠標(biāo)拖動(dòng)拼圖游戲源碼,通過(guò)鼠標(biāo)拖動(dòng)圖片碎片來(lái)將其組合成一個(gè)完整圖片,拼圖完成后會(huì)彈出提示信息2016-08-03- 這篇文章主要為大家介紹了html5版canvas自由拼圖實(shí)例,較為詳細(xì)的講述了該功能的完整實(shí)現(xiàn)過(guò)程,具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2014-10-15
- 本文通過(guò)實(shí)例代碼給大家介紹了HTML5拖拽功能實(shí)現(xiàn)的拼圖游戲,代碼簡(jiǎn)單易懂,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2018-07-31