一款html5 canvas实现的图片玻璃碎片特效

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



一款html5 canvas实现的图片玻璃碎片特效 三联 



html代码:

代码如下:
<img src="city_copy.jpg" id="src_img" class="hidden">
<div id="container" style="-webkit-perspective: 500px;">
<div>
<script src="delaunay.js"></script>
<script src="TweenMax.min.js"></script>
 


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 
本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Js正则表达式过滤特殊字符、表情的实例代码: let ret = "12312ffds#¥@¥#%^***(()))*)).`@%@¥@¥", val = this.customDeviceName;//特殊字符过滤let pattern = new RegExp("[`~!@#$^*()=|{}':;',\\[\\]./?~!@#¥……*()——|{}【】‘;:”“'。,、?]
1、htmlshiv.js Remy开发的HTML5shiv工具能利用JavaScript在老式IE里创建main,header,footer等HTML5元素。也就是说使用JavaScript能创建这些本来不存在的HTML5新元素。这是什么原理?你可能花几天也想不明白,但谁在意呢!这个脚本几乎是所有正式网站必用
HTML5 火的正热,最近有个想法也是要用到HTML的相关功能,所以也要好好学习一把。 好好看了一下Canvas的功能,感觉HTML5在客户端交互的功能性越来越强了,今天看了一下Canvas绘图,下边是几个实例,记下以备后用。 1、使用Canvas绘制直线: !doctype html ht
响应式布局,理想状态是,对PC/移动各种终端进行响应。媒体查询的支持程度是IE9+以及其他现代的浏览器,但是IE8在市场当中仍然占据了比较大量的市场份额,使我们不得不进行IE低端浏览器的考虑。那么如何在IE6~8浏览器中兼容响应式布局呢?这里我们需要借助这
近日,微软宣布开源Chakra的核心组件。Chakra是微软新一代浏览器 Microsoft Edge的JavaScript引擎。相关代码将于2016年1月上传到微软的GitHub账号,项目名称为 ChakraCore,遵循MIT许可协议。 ...,HTML5中国,中国最大的HTML5中文门户。
这篇文章报道的不是“新闻”,因为W3C早已开始着手CSS变量方面的工作。至于Google,则要追溯到11月初,当时该公司主要开发人员之一的Addy Osmani宣布了率先在Chrome Canary中引入对CSS变量的支持。 ...,HTML5中国,中国最大的HTML5中文门户。