canvas水波纹效果

Water ripple effect with HTML5. Today we continue JavaScript examples, and our article will about using javascript in modeling of water effects. This will emulation of water drops at images. We should click at image in desired place to see this effect. Sometimes we can create very interesting solutions using ordinary Javascript (of course for HTML) :)

HTML5的水波纹效果。 今天,我们继续JavaScript示例,并且我们的文章将涉及在水效果建模中使用javascript。 这将模拟图像上的水滴。 我们应该在所需位置单击图像以查看此效果。 有时我们可以使用普通Javascript(当然是HTML)创建非常有趣的解决方案:)

Here are sample and downloadable package:

以下是示例和可下载的软件包:

现场演示

[sociallocker]

[社交储物柜]

打包下载

[/sociallocker]

[/ sociallocker]

Ok, download the example files and lets start coding !

好的,下载示例文件并开始编码!

步骤1. HTML (Step 1. HTML)

As usual, we start with the HTML.

和往常一样,我们从HTML开始。

This is our main page code with all samples.

这是我们所有示例的主页代码。

index.html (index.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>

<!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>

步骤2. CSS (Step 2. CSS)

Here are used CSS styles.

这是使用CSS样式。

css / main.css (css/main.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;
}

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

步骤3. JS (Step 3. JS)

Here are our main control JS file.

这是我们的主要控制JS文件。

js / main.js (js/main.js)


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 = {// variablestimeStep : 20,refractions : 2,shading : 3,damping : 0.99,screenWidth : 500,screenHeight : 400,pond : null,textureImg : null,interval : null,backgroundURL : 'data_images/underwater1.jpg',// initializationinit : function() {var canvas = document.getElementById('water');if (canvas.getContext){// fps countrtfps = 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 funcchangePicture : function(url){this.backgroundURL = url;this.init();},// get mouse position funcgetMousePosition : 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 drawingrun : function(){var ctx = document.getElementById('water').getContext('2d');watereff.pond.update();watereff.pond.draw(ctx);fps++;}
}
window.onload = function(){watereff.init();
}

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 = {// variablestimeStep : 20,refractions : 2,shading : 3,damping : 0.99,screenWidth : 500,screenHeight : 400,pond : null,textureImg : null,interval : null,backgroundURL : 'data_images/underwater1.jpg',// initializationinit : function() {var canvas = document.getElementById('water');if (canvas.getContext){// fps countrtfps = 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 funcchangePicture : function(url){this.backgroundURL = url;this.init();},// get mouse position funcgetMousePosition : 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 drawingrun : function(){var ctx = document.getElementById('water').getContext('2d');watereff.pond.update();watereff.pond.draw(ctx);fps++;}
}
window.onload = function(){watereff.init();
}

As you can see- I using vector2d function here. This function available in ‘vector2d.js’ (in our package). Another code – pretty difficult – pure mathematics. But you are welcome to make experiments here.

如您所见-我在这里使用vector2d函数。 此功能可在“ vector2d.js”(在我们的软件包中)中找到。 另一个代码-很难-纯数学。 但是,欢迎您在这里进行实验。

现场演示

结论 (Conclusion)

Hope that you was happy to play with it. I hope that water drops looks fine :) If is you were wondering – do not forget to thank. I would be grateful for your interesting comments. Good luck!

希望您对此玩得开心。 我希望水滴看起来不错:)如果您想知道–别忘了感谢。 谢谢您提出的宝贵意见。 祝好运!

翻译自: https://www.script-tutorials.com/how-to-create-water-drops-effect-using-html5-canvas/

canvas水波纹效果

canvas水波纹效果_如何使用HTML5 canvas创建水波纹效果相关推荐

  1. java canvas 动画效果_八大疯狂HTML5 Canvas及WebGL动画效果

    [IT168应用]HTML5.WebGL和JavaScript改变了长久以来的动画制作行业.在过去的几年中,我们想要制作卓越的网页动画只能使用Flash和Java Applet.而现在,使用脚本语言和 ...

  2. html5 canvas实训报告,初级篇关于HTML5 canvas调研报告样本.doc

    初级篇关于HTML5 canvas调研报告样本 初级篇关于HTML5 canvas调研报告样本 初级篇关于HTML5 canvas调研报告样本 第一章 绘制步骤 一.取得canvas元素 所有的绘图功 ...

  3. 毛边效果 html,详解Html5 Canvas画线有毛边解决方法

    Html5 Canvas 所有的画线指令画出来的线条都有毛边(比如 lineTo, arcTo,strokeRect),这是因为在Canvas中整数坐标值对应的位置恰巧是屏幕象素点中间的夹缝,那么当按 ...

  4. java canvas 画图片_[Java教程][HTML5] Canvas绘制简单图片

    [Java教程][HTML5] Canvas绘制简单图片 0 2016-05-13 13:00:04 获取Image对象,new出来 定义Image对象的src属性,参数:图片路径 定义Image对象 ...

  5. canvas中文显示乱码 html5_如何使用HTML5 canvas绘制文字

    如果要使用HTML5 Canvas绘制文字,那么需要使用到画布上下文的fillText()方法.下面我们来看具体的内容. 我们先来看具体的示例 function draw() { var canvas ...

  6. html5闪光字效果,如何使用css3+html5来制作文字霓虹灯效果(付完整代码)

    在科技日益发展的今天,人们对于审美越来越挑剔,这就要求我们对于前端开发的态度更加的严谨的同时,需要加入新鲜的元素,以达到吸引目光的目的.那么今天本文带大家了解一下如何使用css3+html5来制作文字 ...

  7. H5画布 canvas 入门到精通 _ 第三部分(canvas 库 Konva.js 的使用)

    目录 一.Konva 基本概念 二.Konva 的使用 1. 引入 Konva 2. Konva 基本绘制步骤 三.Konva 动画 1. tween 对象 2. 动画 to 方法的使用 3. 循环播 ...

  8. 乙腈和水共沸_一种从乙腈水混合物中回收乙腈的方法与流程

    本发明涉及一种从乙腈水混合物中回收乙腈的方法. 背景技术: 乙腈是制药.化工生产中常用的大极性非质子溶剂.在许多场合乙腈是不可替代的优秀溶剂.但是很多使用乙腈进行化学反应的场合,都需要使用无水乙腈,使 ...

  9. html canvas图片上绘制文字,如何用HTML5 CANVAS绘制文字

    您可能感兴趣的话题: 绘制文字 核心提示:如何在HTML5 canvas上绘制绘制文字,并且可以设置文字的字体,大小和颜色. 我们可以在HTML5 canvas上绘制绘制文字,并且可以设置文字的字体, ...

最新文章

  1. C# Redis写入程序
  2. 【原创】Python 源文件编码解读
  3. 投资100亿美元,谷歌计划在2020年向美国办事处和数据中心
  4. nothing comes free
  5. That assembly does not allow partially trusted callers.
  6. CH - 6801 棋盘覆盖(二分图最大匹配+奇偶拆点)
  7. async await实例
  8. 北京大学:“巍巍上庠 国运所系”北大为时代发展而歌(附历年高考各省投档线)...
  9. 定义整型数组_C++数组的定义与初始化(学习笔记:第6章 01)
  10. python 多功能下载网页
  11. html滚动条怎么置顶,js控制滚动条到最底端(置底)和最顶端(置顶)
  12. 图像分割并存储 matlab,matlab图像分割算法源码.pdf
  13. wheeltech惯导模块使用
  14. 在阿里云建网站体验123
  15. docker-compose的nginx重启失败: Error starting userland proxy: listen tcp 0.0.0.0:80: bind: address alread
  16. UE4 相机对焦学习笔记
  17. android 蓝牙BluetoothAdapter的介绍
  18. 【历史上的今天】8 月 20 日:两位传奇程序员的诞生日!
  19. Spartan6系列之SelectIO---IOB深入详解
  20. Android和C#实现实时视频传输Demo

热门文章

  1. html给文字链接图片,(精)HTML第二讲(文字段落超级链接图片).ppt
  2. 细节决定成败,浅析《合金弹头》的成功之道
  3. java前台计算date差,js计算时间差代码【包括计算,天,时,分,秒】_javascri
  4. 20去向(仅供参考)
  5. MDM(EMM)前期基本调研
  6. IBM摆出“Linux组合拳”的架势
  7. python 储蓄计划_个人储蓄计划
  8. h1z1服务器未响应,h1z1未响应各种闪退 | 手游网游页游攻略大全
  9. python列表某项与数字乘_如何将列表中的每个元素乘以一个数字?
  10. python中乘号可以省略吗_字母之间的乘号为什么可以省略不写?