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
注:本文爱编程原创文章,转载请注明原文地址:http://www.w2bc.com/Article/6399
html5 canvas实现图片玻璃碎片特效的更多相关文章
- HTML5 Canvas彩色小球碰撞运动特效
脚本简介 HTML5 Canvas彩色小球碰撞运动特效是一款基于canvas加面向对象制作的运动小球动画特效. 效果展示 http://hovertree.com/texiao/html5/39/ ...
- Html5 Canvas 实现图片合成
多个图片合成一张 <!doctype html> <html> <head> <meta charset="utf-8"> < ...
- 【HTML】html5 canvas全屏烟花动画特效
<!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF-8&quo ...
- 用HTML5 Canvas为Web图形创建特效
HTML5 Canvas 将使用像素在屏幕上绘制图形图像. 本节演示了五种用于操作像素以创建摄影特效的 Canvas 技术. 您可使用这些技术来生成独具特色的图像,为您的网站.博客.视频游戏画面.广告 ...
- HTML5 CANVAS 实现图片压缩和裁切
原文地址:http://leonshi.com/2015/10/31/html5-canvas-image-compress-crop/?utm_source=tuicool&utm_medi ...
- 使用html5 canvas绘制图片
注意:本文属于<html5 Canvas绘制图形入门详解>系列文章中的一部分.如果你是html5初学者,仅仅阅读本文,可能无法较深入的理解canvas,甚至无法顺畅地通读本文.请点击上述链 ...
- 基于HTML5 Canvas粒子效果文字动画特效
之前我们分享过很多超酷的文字特效,其中也有利用HTML5和CSS3的.今天我们要来分享一款基于HTML5 Canvas的文字特效,输入框中输入想要展示的文字,回车后即可在canvas上绘制出粒子效果的 ...
- 基于 HTML5 Canvas 实现的文字动画特效
前言 文字是网页中最基本的元素,一般我们在网页上都是展示的静态文字,但是就效果来说,还是比较枯燥的.文字淡入淡出的动画效果在项目中非常实用,如果有某些关键的文字,可以通过这种动态的效果来提醒用户阅读. ...
- HTML5 Canvas 绘制图片不显示的问题
问题: 慕名赶来,却一脚踩空,低头一看,地上一个大坑. 事情是这样的,在我看完w3c的介绍和很有说服力和教学力的demo后,本着实践出真知的思想决定上手一试,这一试不要紧~ 我按照流水线工程铺设以下几 ...
随机推荐
- 腾讯云-搭建 WordPress 个人博客
搭建 WordPress 个人博客 准备 LNMP 环境 任务时间:30min ~ 60min LNMP 是 Linux.Nginx.MySQL 和 PHP 的缩写,是 WordPress 博客系统依 ...
- HDUOJ ---悼念512汶川大地震遇难同胞——来生一起走
悼念512汶川大地震遇难同胞——来生一起走 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...
- nyoj-----前缀式计算
前缀式计算 时间限制:1000 ms | 内存限制:65535 KB 难度:3 描述 先说明一下什么是中缀式: 如2+(3+4)*5这种我们最常见的式子就是中缀式. 而把中缀 ...
- 给定随机数列求第k大的数字
原来STL我还是有很多不知道的地方 STL 采用的算法是: 当数组长度 <= 3时, 采用插入排序. 当长度 > 3时, 采用快排 Partition 的思想,也就是说类似快速排序(这里不 ...
- android程序监听home键与电源键
01 private final BroadcastReceiver homePressReceiver = new BroadcastReceiver() { 02 final String SYS ...
- Web 前端性能优化相关内容解析[转]
Web 前端性能优化相关内容,来源于<Google官方网页载入速度检测工具PageSpeed Insights 使用教程>一文中PageSpeed Insights 的相关说明.大家可以对 ...
- 《JAVA与模式》之有感
陆陆续续看了gof,大话设计模式等books,对于设计模式仍然是徘徊于门外,随偶有感悟,然久必忘记,是实则悟性太低. 因此作此文,结合博客中一系列关于设计模式的博文,加深对设计模式的理解,同时备自己随 ...
- Linux时钟
一.前言 时钟或者钟表(clock)是一种计时工具,每个人都至少有一块,可能在你的手机里,也可能佩戴在你的手腕上.如果Linux也是一个普通人的话,那么她的手腕上应该有十几块手表,包括:CLOCK_R ...
- explore your hadoop data and get real-time results
deep api integration makes getting value from your big data easy 深度api集成使你大数据訪问更加easy Elasticsearch ...
- 在ubuntu16.4中为pycharm创建桌面快捷启动方式
在ubuntu环境中每次使用pycharm需要到它的安装目录中执行./pycharm.sh来启动pycharm.比较麻烦,既然ubuntu提供了桌面环境我们应该从分利用.哈哈哈... 上干货 我的py ...