用HTML5制作视频拼图的教程_html5教程技巧

几天前同事给我看了一个特效,是一个拼图游戏,不同的是,拼图里的是动画。他让我看下做个demo,于是就自己整了一会,也确实不难。用canvas很容易做。所以这篇博文不适合高手看。。。。就是随便写来玩玩的。
效果图:
用HTML5制作视频拼图的教程_html5教程技巧

至少我刚看到这个的时候觉得挺新颖的,所以才会想到做出来玩玩,觉得楼主out的哥们请轻喷

  不多说,先上 body{margin:0;padding:0;}
.allCanvas{
position: relative;
margin:50px auto;
width:600px;
}
.vcanvas{
position: absolute;
display: block;
border: 1px solid;
}

视频拼图

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 = [];

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);
}
}
}

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;
}
}
}
}

Array.prototype.forEach = function(callback){
for(var i=0;i<this.length;i++){
callback.call(this[i]);
}
}

var lastTime = 0;
function initAnimate(){
lastTime = new Date();
createCanvas();
animate();
}

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);
}
}

video.play();
initAnimate();

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至253000106@qq.com举报,一经查实,本站将立刻删除。

发布者:PHP中文网,转转请注明出处:https://www.chuangxiangniao.com/p/2913674.html

(0)
上一篇 2025年3月11日 09:10:31
下一篇 2025年3月11日 09:10:35

AD推荐 黄金广告位招租... 更多推荐

相关推荐

发表回复

登录后才能评论