使用 HTML5 Canvas 制作水波纹效果点击图片就会触发


Posted in HTML / CSS onSeptember 15, 2014

今天,我们继续分享 JavaScript 实现的效果例子,这篇文章会介绍使用 JavaScript 实现水波纹效果。水波效果以图片为背景,点击图片任意位置都会触发。有时候,我们使用普通的 Javascript 就可以创建一个很有趣的解决功能。
使用 HTML5 Canvas 制作水波纹效果点击图片就会触发 

源码下载

Step 1. HTML

和以前一样,首先是 HTML 代码:

复制代码
代码如下:

<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title>Water drops effect</title>
<link rel="stylesheet" href="css/main.css" type="text/css" />
<script src="js/vector2d.js" type="text/javascript" charset="utf-8"></script>
<script src="js/waterfall.js" type="text/javascript" charset="utf-8"></script>
</head>
<body>
<div class="example">
<h3><a href="#">Water drops effect</a></h3>
<canvas id="water">HTML5 compliant browser required</canvas>
<div id="switcher">
<img onclick='watereff.changePicture(this.src);' src="data_images/underwater1.jpg" />
<img onclick='watereff.changePicture(this.src);' src="data_images/underwater2.jpg" />
</div>
<div id="fps"></div>
</div>
</body>
</html> 

Step 2. CSS

这是用到的 CSS 代码:

复制代码
代码如下:

body{background:#eee;margin:0;padding:0}
.example{background:#FFF;width:600px;border:1px #000 solid;margin:20px auto;padding:15px;-moz-border-radius: 3px;-webkit-border-radius: 3px}
#water {
width:500px;
height:400px;
display: block;
margin:0px auto;
cursor:pointer;
}
#switcher {
text-align:center;
overflow:hidden;
margin:15px;
}
#switcher img {
width:160px;
height:120px;
}

Step 3. JS

下面是主要的 JavaScript 代码:

复制代码
代码如下:

function drop(x, y, damping, shading, refraction, ctx, screenWidth, screenHeight){
this.x = x;
this.y = y;
this.shading = shading;
this.refraction = refraction;
this.bufferSize = this.x * this.y;
this.damping = damping;
this.background = ctx.getImageData(0, 0, screenWidth, screenHeight).data;
this.imageData = ctx.getImageData(0, 0, screenWidth, screenHeight);
this.buffer1 = [];
this.buffer2 = [];
for (var i = 0; i < this.bufferSize; i++){
this.buffer1.push(0);
this.buffer2.push(0);
}
this.update = function(){
for (var i = this.x + 1, x = 1; i < this.bufferSize - this.x; i++, x++){
if ((x < this.x)){
this.buffer2[i] = ((this.buffer1[i - 1] + this.buffer1[i + 1] + this.buffer1[i - this.x] + this.buffer1[i + this.x]) / 2) - this.buffer2[i];
this.buffer2[i] *= this.damping;
} else x = 0;
}
var temp = this.buffer1;
this.buffer1 = this.buffer2;
this.buffer2 = temp;
}
this.draw = function(ctx){
var imageDataArray = this.imageData.data;
for (var i = this.x + 1, index = (this.x + 1) * 4; i < this.bufferSize - (1 + this.x); i++, index += 4){
var xOffset = ~~(this.buffer1[i - 1] - this.buffer1[i + 1]);
var yOffset = ~~(this.buffer1[i - this.x] - this.buffer1[i + this.x]);
var shade = xOffset * this.shading;
var texture = index + (xOffset * this.refraction + yOffset * this.refraction * this.x) * 4;
imageDataArray[index] = this.background[texture] + shade;
imageDataArray[index + 1] = this.background[texture + 1] + shade;
imageDataArray[index + 2] = 50 + this.background[texture + 2] + shade;
}
ctx.putImageData(this.imageData, 0, 0);
}
}
var fps = 0;
var watereff = {
// variables
timeStep : 20,
refractions : 2,
shading : 3,
damping : 0.99,
screenWidth : 500,
screenHeight : 400,
pond : null,
textureImg : null,
interval : null,
backgroundURL : 'data_images/underwater1.jpg',
// initialization
init : function() {
var canvas = document.getElementById('water');
if (canvas.getContext){
// fps countrt
fps = 0;
setInterval(function() {
document.getElementById('fps').innerHTML = fps / 2 + ' FPS';
fps = 0;
}, 2000);
canvas.onmousedown = function(e) {
var mouse = watereff.getMousePosition(e).sub(new vector2d(canvas.offsetLeft, canvas.offsetTop));
watereff.pond.buffer1[mouse.y * watereff.pond.x + mouse.x ] += 200;
}
canvas.onmouseup = function(e) {
canvas.onmousemove = null;
}
canvas.width = this.screenWidth;
canvas.height = this.screenHeight;
this.textureImg = new Image(256, 256);
this.textureImg.src = this.backgroundURL;
canvas.getContext('2d').drawImage(this.textureImg, 0, 0);
this.pond = new drop(
this.screenWidth,
this.screenHeight,
this.damping,
this.shading,
this.refractions,
canvas.getContext('2d'),
this.screenWidth, this.screenHeight
);
if (this.interval != null){
clearInterval(this.interval);
}
this.interval = setInterval(watereff.run, this.timeStep);
}
},
// change image func
changePicture : function(url){
this.backgroundURL = url;
this.init();
},
// get mouse position func
getMousePosition : function(e){
if (!e){
var e = window.event;
}
if (e.pageX || e.pageY){
return new vector2d(e.pageX, e.pageY);
} else if (e.clientX || e.clientY){
return new vector2d(e.clientX, e.clientY);
}
},
// loop drawing
run : function(){
var ctx = document.getElementById('water').getContext('2d');
watereff.pond.update();
watereff.pond.draw(ctx);
fps++;
}
}
window.onload = function(){
watereff.init();
}

正如你所看到的,这里使用 Vector2D 函数,这个函数在 vector2d.js 里提供了。另一个很难的方法是使用纯数学实现,感兴趣的可以自己实验一下。
HTML / CSS 相关文章推荐
纯CSS实现设置半个字符的样式
Jul 03 HTML / CSS
CSS3绘制圆角矩形的简单示例
Sep 28 HTML / CSS
CSS3点击按钮实现背景渐变动画效果
Oct 19 HTML / CSS
Html5 localStorage入门教程
Apr 26 HTML / CSS
HTML5中的autofocus(自动聚焦)属性介绍
Apr 23 HTML / CSS
HTML5几个设计和修改的页面范例分享
Sep 29 HTML / CSS
iphoneX 适配客户端H5页面的方法教程
Dec 08 HTML / CSS
canvas绘制树形结构可视图形的实现
Apr 03 HTML / CSS
HTML5逐步分析实现拖放功能的方法
Sep 30 HTML / CSS
HTML速写之Emmet语法规则的实现
Apr 07 HTML / CSS
详解CSS玩转图片Base64编码
May 25 HTML / CSS
bootstrapv4轮播图去除两侧阴影及线框的方法
Feb 15 HTML / CSS
HTML5 video 事件应用示例
Sep 11 #HTML / CSS
一款html5 canvas实现的图片玻璃碎片特效
Sep 11 #HTML / CSS
基于html5 canvas实现漫天飞雪效果实例
Sep 10 #HTML / CSS
html5中的input新属性range使用记录
Sep 05 #HTML / CSS
让IE下支持Html5的placeholder属性的插件
Sep 02 #HTML / CSS
html5摇一摇代码优化包括DeviceMotionEvent等等
Sep 01 #HTML / CSS
Html5 FileReader实现即时上传图片功能实例代码
Sep 01 #HTML / CSS
You might like
追忆往昔!浅谈收音机的百年发展历史
2021/03/01 无线电
用PHP调用Oracle存储过程的方法
2008/09/12 PHP
十幅图告诉你什么是PHP引用
2015/02/22 PHP
PHP基于ICU扩展intl快速实现汉字转拼音及按拼音首字母分组排序的方法
2017/05/03 PHP
Laravel 5.5 实现禁用用户注册示例
2019/10/24 PHP
json的前台操作和后台操作实现代码
2012/01/20 Javascript
可在线编辑网页文字效果代码(单击)
2013/03/02 Javascript
上传的js验证(图片/文件的扩展名)
2013/04/25 Javascript
jquery 定位input元素的几种方法小结
2013/07/28 Javascript
JavaScript实现的简单烟花特效代码
2015/10/20 Javascript
AngularJS Ajax详解及示例代码
2016/08/17 Javascript
完美解决jQuery的hover事件在IE中不停闪动的问题
2017/02/10 Javascript
canvas实现爱心和彩虹雨效果
2017/03/09 Javascript
详解vue服务端渲染(SSR)初探
2017/06/19 Javascript
underscore之Collections_动力节点Java学院整理
2017/07/10 Javascript
使用Vue中 v-for循环列表控制按钮隐藏显示功能
2019/04/23 Javascript
SSM+layUI 根据登录信息显示不同的页面方法
2019/09/20 Javascript
Vue程序化的事件监听器(实例方案详解)
2020/01/07 Javascript
解决vue项目获取dom元素宽高总是不准确问题
2020/07/29 Javascript
JavaScript中的执行环境和作用域链
2020/09/04 Javascript
JavaScript实现随机点名小程序
2020/10/29 Javascript
Python实现的数据结构与算法之基本搜索详解
2015/04/22 Python
python实现读取并显示图片的两种方法
2017/01/13 Python
利用python写个下载teahour音频的小脚本
2017/05/08 Python
python 通过 socket 发送文件的实例代码
2018/08/14 Python
Python完成哈夫曼树编码过程及原理详解
2019/07/29 Python
使用python将excel数据导入数据库过程详解
2019/08/27 Python
Python3 操作 MySQL 插入一条数据并返回主键 id的实例
2020/03/02 Python
Python 找出出现次数超过数组长度一半的元素实例
2020/05/11 Python
Python wordcloud库安装方法总结
2020/12/31 Python
HTML5本地存储之Web Storage应用介绍
2013/01/06 HTML / CSS
质检部部长职责
2013/12/16 职场文书
纪检干部对照检查材料
2014/08/22 职场文书
迎七一演讲稿
2014/09/12 职场文书
人工作失职检讨书
2015/05/05 职场文书
2016年五一促销广告语
2016/01/28 职场文书