大橙子网站建设,新征程启航

为企业提供网站建设、域名注册、服务器等服务

怎么用html5canvas实现图片玻璃碎片特效-创新互联

本篇内容主要讲解“怎么用html5 canvas实现图片玻璃碎片特效”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“怎么用html5 canvas实现图片玻璃碎片特效”吧!

成都创新互联公司服务项目包括南召网站建设、南召网站制作、南召网页制作以及南召网络营销策划等。多年来,我们专注于互联网行业,利用自身积累的技术优势、行业经验、深度合作伙伴关系等,向广大中小型企业、政府机构等提供互联网行业的解决方案,南召网站推广取得了明显的社会效益与经济效益。目前,我们服务的客户以成都为中心已经辐射到南召省份的部分城市,未来相信会继续扩大服务区域并继续获得客户的支持与信任!

今天要为大家带来一款html5 canvas实现的图片玻璃碎片特效。图片以玻璃碎片的形式出现到界面中,然后似玻璃被打碎的效果渐消息。效果图如下:

怎么用html5 canvas实现图片玻璃碎片特效 

源码下载 

html代码:




代码如下:









js代码:



代码如下:


// canvas settings
var imageWidth = 768,
imageHeight = 485;
var vertices = [],
indices,
boxes = [];
var image,
fragments = [],
container = document.getElementById('container');
window.onload = function () {
image = document.getElementById('src_img');
triangulate();
makeBoxes();
makeFragments();
};
function triangulate() {
var x,
y,
dx = imageWidth / 8,
dy = imageHeight / 8,
offset = 0.5;
for (var i = 0; i <= imageWidth; i += dx) {
for (var j = 0; j <= imageHeight; j += dy) {
if (i && (i !== imageWidth)) x = i + randomRange(-dx * offset, dx * offset);
else x = i;
if (j && (j !== imageHeight)) y = j + randomRange(-dy * offset, dy * offset);
else y = j;
vertices.push([x, y]);
}
}
indices = Delaunay.triangulate(vertices);
}
function makeBoxes() {
var p0, p1, p2,
xMin, xMax,
yMin, yMax;
for (var i = 0; i < indices.length; i += 3) {
p0 = vertices[indices[i + 0]];
p1 = vertices[indices[i + 1]];
p2 = vertices[indices[i + 2]];
xMin = Math.min(p0[0], p1[0], p2[0]);
xMax = Math.max(p0[0], p1[0], p2[0]);
yMin = Math.min(p0[1], p1[1], p2[1]);
yMax = Math.max(p0[1], p1[1], p2[1]);
boxes.push({
x: xMin,
y: yMin,
w: xMax - xMin,
h: yMax - yMin
});
}
}
function makeFragments() {
var p0, p1, p2,
box,
fragment;
TweenMax.set(container, { perspective: 500 });
var tl0 = new TimelineMax({ repeat: -1 });
for (var i = 0; i < indices.length; i += 3) {
p0 = vertices[indices[i + 0]];
p1 = vertices[indices[i + 1]];
p2 = vertices[indices[i + 2]];
box = boxes[i / 3];
fragment = new Fragment(p0, p1, p2, box);
var rx = randomRange(30, 60) * ((i % 2) ? 1 : -1);
var ry = randomRange(30, 60) * ((i % 2) ? -1 : 1);
var tl1 = new TimelineMax();
TweenMax.set(fragment.canvas, {
y: box.y - 1000
});
tl1.to(fragment.canvas, randomRange(0.9, 1.1), {
y: box.y,
ease: Back.easeOut
});
tl1.to(fragment.canvas, 0.5, {
z: -100,
ease: Cubic.easeIn,
delay: 0.4
});
tl1.to(fragment.canvas, randomRange(1, 1.2), {
rotationX: rx,
rotationY: ry,
z: 250,
alpha: 0,
ease: Cubic.easeOut
});
tl0.insert(tl1);
fragments.push(fragment);
container.appendChild(fragment.canvas);
}
}
function randomRange(min, max) {
return min + (max - min) * Math.random();
}
Fragment = function (v0, v1, v2, box) {
this.v0 = v0;
this.v1 = v1;
this.v2 = v2;
this.box = box;
this.canvas = document.createElement('canvas');
this.canvas.width = this.box.w;
this.canvas.height = this.box.h;
this.canvas.style.width = this.box.w + 'px';
this.canvas.style.height = this.box.h + 'px';
this.ctx = this.canvas.getContext('2d');
TweenMax.set(this.canvas, {
x: this.box.x,
y: this.box.y
});
this.ctx.translate(-this.box.x, -this.box.y);
this.ctx.beginPath();
this.ctx.moveTo(this.v0[0], this.v0[1]);
this.ctx.lineTo(this.v1[0], this.v1[1]);
this.ctx.lineTo(this.v2[0], this.v2[1]);
this.ctx.closePath();
this.ctx.clip();
this.ctx.drawImage(image, 0, 0);
}; //@ sourceURL=pen.js


到此,相信大家对“怎么用html5 canvas实现图片玻璃碎片特效”有了更深的了解,不妨来实际操作一番吧!这里是创新互联建站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!


分享题目:怎么用html5canvas实现图片玻璃碎片特效-创新互联
当前URL:http://dzwzjz.com/article/ceecgc.html