ES6写了一个水纹效果。还有点计算的问题。直接上代码Copy一下吧。

export  default class Vertex {static BASE_Y = 150;static BASE_R = 10;static  FRICTION = 0.1;//波形抖动后回复到正常状态的速率指数static  DECELERATION = 0.95;static  SPEED_OF_BASE_WAVE = 3;theta = 0;goalY = 0;amp = 0;x;y;constructor(prmID, parent) {this.theta = 360 * prmID / ( parent.NUM - 1);//角度的弧度值。根据NUM值将舞台上分为NUM块,然后将2π的弧度分配给各块,这样舞台上平静的时候正好是一段完整的波形。this.x = prmID * parent.STAGE_W / (parent.NUM - 1);this.y = Vertex.BASE_Y + Vertex.BASE_R * Math.sin(this.theta * Math.PI / 180);}//让波形不断波动的函数,不断更新各点的y坐标updatePos(diffVal) {this.theta += Vertex.SPEED_OF_BASE_WAVE;if (this.theta >= 360) {this.theta -= 360;}this.goalY = Vertex.BASE_Y + Vertex.BASE_R * Math.sin(this.theta * Math.PI / 180);this.goalY += diffVal;this.amp += this.goalY - this.y;this.y += this.amp * Vertex.FRICTION;//y坐标以FRICTION的缓冲速率缓冲到正常状态this.amp *= Vertex.DECELERATION;}
}/*** Created by EricXie on 2019/6/19.*/
import Vertex from "./Vertex.js";
export default class Wave{STAGE_W=800;STAGE_H=300;NUM=800;MOUSE_DIFF_RATIO=1;AUTO_INTERVAL=3000;vertexes=[];mdlPt=[];diffPt=[[],[]];startIndex=[0,0];mouseOldY;mouseNewY;mouseDiff=0;//mouseDiffGoal的缓冲mouseDiffGoal=0;//鼠标拖动后产生的位相差autoTimer;autoDiff=0;//计时器自动生成的位相差mouseY=0;mouseX=0;constructor(){this.canvas=this.createCanvas();this.ctx=this.canvas.getContext("2d");this.init();this.animation();}createCanvas(){if(this.canvas) return this.canvas;let canvas=document.createElement("canvas");Object.assign(canvas.style,{width:"800px",height:"300px",backgroundColor:"#FFFFFF",margin:"auto"});canvas.addEventListener("mousemove",this.mouseHandler.bind(this));return canvas;}mouseHandler(e){this.mouseX=e.clientX;this.mouseY=e.clientY;}appendTo(parent){parent.appendChild(this.canvas);}animation(){requestAnimationFrame(this.animation.bind(this));this.updateMouseDiff();this.updateWave();}init(){for (let i=0; i<this.NUM; i++) {let vertex=new Vertex(i,this);this.vertexes.push(vertex);//中点作成if (i>1) {this.mdlPt.push( {x:(this.vertexes[i-1].x+this.vertexes[i].x)*0.5,y:(this.vertexes[i-1].y+this.vertexes[i].y)*0.5});}//差分this.diffPt[0].push( 0 );this.diffPt[1].push( 0 );}this.mouseNewY=this.mouseY;if (this.mouseNewY<0) {this.mouseNewY=0;} else if (this.mouseNewY > this.STAGE_H) {this.mouseNewY=this.STAGE_H;}this.mouseOldY=this.mouseNewY;setInterval(this.generateAutoWave.bind(this),this.AUTO_INTERVAL);}updateMouseDiff(){this.mouseOldY=this.mouseNewY;this.mouseNewY=this.mouseY;if (this.mouseNewY<0) {this.mouseNewY=0;} else if (this.mouseNewY > this.STAGE_H) {this.mouseNewY=this.STAGE_H;}this.mouseDiffGoal = (this.mouseNewY - this.mouseOldY) * this.MOUSE_DIFF_RATIO;}updateWave(){this.ctx.clearRect(0,0,this.STAGE_W,this.STAGE_H);this.mouseDiff -= (this.mouseDiff - this.mouseDiffGoal)*0.3;this.autoDiff-=this.autoDiff*0.9;//波形自动波动时的速率let mX=this.mouseX;if (mX<0) {mX=0;} else if (mX > this.STAGE_W-2) {mX=this.STAGE_W-2;}this.startIndex[0] = 1+Math.floor( (this.NUM-2) * mX / this.STAGE_W );//startIndex[0]表示波形图上,鼠标拖动的那个点,用Math.floor是//可以取到NUM个点里面x坐标小于当前鼠标x坐标的最大值this.diffPt[0][this.startIndex[0]] -= ( this.diffPt[0][this.startIndex[0]] - this.mouseDiff )*0.99;//自动波this.diffPt[1][this.startIndex[1]] -= ( this.diffPt[1][this.startIndex[1]] - this.autoDiff )*0.99;let d;let i;for ( i=this.startIndex[0]-1; i >=0; i--) {d=this.startIndex[0]-i;if (d>15) {d=15;}this.diffPt[0][i] -= ( this.diffPt[0][i] - this.diffPt[0][i+1] )*(1-0.01*d);}for ( i=this.startIndex[0]+1; i < this.NUM; i++) {d=i-this.startIndex[0];if (d>15) {d=15;}this.diffPt[0][i] -= ( this.diffPt[0][i] - this.diffPt[0][i-1] )*(1-0.01*d);}for ( i=this.startIndex[1]-1; i >=0; i--) {d=this.startIndex[1]-i;if (d>15) {d=15;}this.diffPt[1][i] -= ( this.diffPt[1][i] - this.diffPt[1][i+1] )*(1-0.01*d);}for ( i=this.startIndex[1]+1; i < this.NUM; i++) {d=i-this.startIndex[1];if (d>15) {d=15;}this.diffPt[1][i] -= ( this.diffPt[1][i] - this.diffPt[1][i-1] )*(1-0.01*d);}for ( i=0; i < this.NUM; i++) {this.vertexes[i].updatePos( this.diffPt[0][i]+this.diffPt[1][i]);//更新波形上各点的位相,位相差等于鼠标抖动的和自动产生的,即为diffPt[0][i]+diffPt[1][i]}for ( i=0; i < this.NUM-2; i++) {this.mdlPt[i].y = (this.vertexes[i+1].y + this.vertexes[i+2].y)*0.5;//更新波形图上两点中点的位相,使波形图看起来更流畅}this.drawWave();}drawWave(){this.ctx.fillStyle="#666666";this.ctx.beginPath();this.ctx.moveTo(this.STAGE_W, this.STAGE_H);this.ctx.lineTo(0, this.STAGE_H);this.ctx.lineTo(this.vertexes[0].x, this.vertexes[0].y-50);this.ctx.quadraticCurveTo(this.vertexes[1].x, this.vertexes[1].y-50, this.mdlPt[0].x, this.mdlPt[0].y-50);for (let i=2; i<this.NUM-2; i++) {this.ctx.quadraticCurveTo(this.vertexes[i].x, this.vertexes[i].y-50, this.mdlPt[i-1].x, this.mdlPt[i-1].y-50);}this.ctx.quadraticCurveTo( this.vertexes[this.NUM-2].x, this.vertexes[this.NUM-2].y-50, this.vertexes[this.NUM-1].x, this.vertexes[this.NUM-1].y-50);this.ctx.closePath();this.ctx.fill();}generateAutoWave(){this.autoDiff=200;//自动生成100的位相差this.startIndex[1] = Math.round( Math.random()*(this.NUM-1) );}}
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>Title</title>
</head>
<body><script type="module">import Wave from "./js/wave.js";let wave=new Wave();wave.appendTo(document.body);
</script>
</body>
</html>

javascript ES6鼠标划入产生水纹波动效果相关推荐

  1. regl 水纹波动效果

    原文链接: regl 水纹波动效果 上一篇: vscode中 直接使用es6 import, 可以右键直接运行js文件 下一篇: regl fake-3d 静态图变动态图, 使用深度信息图片 翻墙出去 ...

  2. Android 贝塞尔曲线实现水纹波动效果

    前言 最近工作上比较忙碌,很久没有更新文章了,难得国庆小长假,现在是2019年10月2日凌晨00:49,写一篇简单且实用的贝塞尔曲线应用,许多技术点的文章很多前辈都已经写的很好了,所以 如有纰漏之处, ...

  3. android 贝塞尔曲线 波浪线,Android 贝塞尔曲线实现水纹波动效果

    贝塞尔曲线简介 千篇一律,很多类似的文章都会介绍一下什么是贝塞尔曲线,但是这里就不做介绍了,我们在这里只需要知道在Android API为我们提供了绘制二阶贝塞尔曲线和三阶贝塞尔曲线的方法即可. 效果 ...

  4. css实现圆形水纹波动效果

    <!doctype html> <html><head><meta charset="utf-8"><title>CSS ...

  5. 修改elementUI轮播图鼠标划入轮播暂停的原生效果

    修改elementUI轮播图鼠标划入轮播暂停的原生效果_Boriska1996的博客-CSDN博客_iview carousel 暂停

  6. JavaScript实现鼠标拖拽效果

    JavaScript实现鼠标拖拽效果 前几篇博客一直在讲鼠标的移动事件相关的效果,今天再来讲一讲如何利用鼠标事件实现拖拽div块移动的效果.效果如图: HTML代码: <div id=" ...

  7. swiper移入暂停_react中swiper注意事项及鼠标划入停止轮播

    首先是实例化swiper 这里有一个注意点,就是实例化的时机 如果你的swiper内容是写死的,可以在componentDidMount中实例化,但是如果你的内容是通过接口异步请求过来的,就必须在co ...

  8. es6字符串添加html标签,JavaScript_详解JavaScript ES6中的模板字符串,在 ES6 中引入了一种新的字符 - phpStudy...

    详解JavaScript ES6中的模板字符串 在 ES6 中引入了一种新的字符串字面量 - 模板字符串,除了使用反引号 (`) 表示,它们看上去和普通的字符串没有什么区别.在最简单的情况下,他们就是 ...

  9. 前端面试+学习笔记(HTML+CSS+JavaScript+ES6+Vue+NodeJs)

    前端面试+学习笔记(HTML+CSS+JavaScript+ES6+Vue+NodeJs) 一. HTML 1. 盒子模型 是什么:每个元素被表示为一个矩形的盒子,有四个部分组成:内容(content ...

最新文章

  1. Github配置(git+vscode+python+jupyter)
  2. 使用Formik轻松开发更高质量的React表单(一)入门
  3. Extjs4.2+webAPI+EF实现分页以及webapi的数据传值
  4. 如何解读「量子计算应对大数据挑战:中国科大首次实现量子机器学习算法」?——是KNN算法吗?...
  5. web api接口开发实例_小程序开发如何调用 API 接口,以豆瓣电影为例
  6. 从零开始实现ASP.NET Core MVC的插件式开发(五) - 插件的删除和升级
  7. boost解析info文件
  8. pip不是内部或外部命令,也不是可运行的程序 或批处理文件--解决办法
  9. 批处理批量创建域用户
  10. SPOJ Can you answer the Queries系列
  11. 计算机网络电子邮件的格式,电子邮件的格式是什么
  12. python转txt到xml并编译为exe
  13. 阿里巴巴FastJson整理(20分钟阅读)
  14. 椭圆型变分问题理论及数值方法
  15. SEO系列三:周珍谈新手如何快速入门
  16. 一是数据库系统备份,二是数据本身的备份
  17. python json接口数据提取_返回数据中提取数据的方法(JSON数据取其中某一个值的方法)...
  18. oracle11g ins208022,解决重装 Oracle 出现的 INS-32025 问题,完全卸载 Oracle11g
  19. Windows Azure Storage 论文阅读
  20. Latex排版 Chapter2格式调整(长度单位、字体、段落、页面、目录)

热门文章

  1. android-ndk-r17c,Cannot use old NDK (android-ndk-r17c) after Catalina upgrade due to new security
  2. Vue引入第三方js库
  3. VLAN是什么?VLAN基础知识精讲简单易懂
  4. Linux 忘记密码怎么办,CentOS和Ubuntu重置密码方法
  5. apk反编译教程[素材提取,源码查看]
  6. JS toFixed(银行家舍入法)及其缺陷和解决方法
  7. 外贸型企业网站的设计思路
  8. element ui登录界面_vue vue-router vuex element-ui axios 写一个代理平台的学习笔记(四)美化头部和登录界面...
  9. python3长整型怎么用_python3有长整型吗
  10. java cas原理