html5 canvas 特效(JavaScript canvas实现流星特效)
类别:编程学习 浏览量:1309
时间:2022-03-29 18:45:18 html5 canvas 特效
JavaScript canvas实现流星特效本文实例为大家分享了JavaScript canvas实现流星特效展示的具体代码,供大家参考,具体内容如下
1、控制透明度变化函数
function easeInQuad(curtime,begin,end,duration){ let x = curtime/duration; //x值 let y = x*x; //y值 return begin+(end-begin)*y; //套入最初的公式 } //用平方根构建的缓慢减速运动 核心函数:x*x + 2*x function easeOutQuad(curtime,begin,end,duration){ let x = curtime/duration; //x值 let y = -x*x + 2*x; //y值 return begin+(end-begin)*y; //套入最初的公式 } function easeInoutQuad(curtime,begin,end,duration){ if(curtime<duration/2){ //前半段时间 return easeInQuad(curtime,begin,(begin+end)/2,duration/2);//改变量和时间都除以2 }else{ let curtime1 = curtime-duration/2; //注意时间要减去前半段时间 let begin1 = (begin+end)/2;//初始量要加上前半段已经完成的 return easeOutQuad(curtime1,begin1,end,duration/2);//改变量和时间都除以2 } }
2、流星星体光环的闪烁特效
function intervalOpcity (cur=0,start=0.3,end=0.8,dur=3) { if(fadeOpcity(cur,start,end,dur)===end){ cur = 0 let temp = start start = end end = temp } let opcity = fadeOpcity(cur,start,end,dur) let color = `rgba(0,0,0,${opcity})` clearBgcolor(wrap) newParticle (wrap,[x,y],r,color) cur=parseFloat(cur+0.1) setTimeout(()=>{intervalOpcity (cur,start,end,dur)},33) }
3、流星尾巴
function intervalMove (speed,g=0) { if(g===720){ g = 360 } g = g+speed let nextPosition = movePosition (a,g,startPosition) x = nextPosition[0] y = nextPosition[1] clearBgcolor(wrap1) for(let i =1;i<=360;i++){ let g1 = g-i/2 if(g1<0&&g<=360){ break } let g1Position = movePosition (a,g1,startPosition) newParticle (wrap1,[g1Position[0],g1Position[1]],r/(1+i/5),`rgba(255,200,200,0.5)`) } setTimeout(()=>{intervalMove (speed,g)},33) }
4、完整代码
function easeInQuad(curtime,begin,end,duration){ let x = curtime/duration; //x值 let y = x*x; //y值 return begin+(end-begin)*y; //套入最初的公式 } //用平方根构建的缓慢减速运动 核心函数:x*x + 2*x function easeOutQuad(curtime,begin,end,duration){ let x = curtime/duration; //x值 let y = -x*x + 2*x; //y值 return begin+(end-begin)*y; //套入最初的公式 } function easeInoutQuad(curtime,begin,end,duration){ if(curtime<duration/2){ //前半段时间 return easeInQuad(curtime,begin,(begin+end)/2,duration/2);//改变量和时间都除以2 }else{ let curtime1 = curtime-duration/2; //注意时间要减去前半段时间 let begin1 = (begin+end)/2;//初始量要加上前半段已经完成的 return easeOutQuad(curtime1,begin1,end,duration/2);//改变量和时间都除以2 } } function newCanvas (width,height) { let bodyEl = document.body let canvasEl = document.createElement("canvas") canvasEl.width = width canvasEl.height = height canvasEl.style.position = "absolute" bodyEl.append(canvasEl) let wrap = canvasEl.getContext("2d") return wrap } function setBgcolor (wrap,color) { wrap.fillStyle=color; wrap.fillRect(0,0,wrap.canvas.width,wrap.canvas.height); } function clearBgcolor(wrap){ wrap.clearRect(0,0,wrap.canvas.width,wrap.canvas.height) } function newParticle (wrap,position,r,color) { let x = position[0] let y = position[1] wrap.fillStyle=color; wrap.beginPath(); wrap.arc(x,y,r,0,2*Math.PI); wrap.shadowBlur=20; wrap.shadowColor=`rgba(255,255,255,0.8)`; wrap.fill(); } function fadeOpcity(cur,start,end,dur){ let opcity = parseFloat(easeInoutQuad(cur,start,end,dur).toFixed(2)) return opcity } let wrap0 = newCanvas (1000,800) let wrap2 = newCanvas (1000,800) let wrap = newCanvas (1000,800) let wrap1 = newCanvas (1000,800) setBgcolor (wrap0,"black") setBgcolor (wrap,"rgba(0,0,0,0)") setBgcolor (wrap1,"rgba(0,0,0,0)") setBgcolor (wrap2,"rgba(0,0,0,0)") let startPosition = [500,200] let r = 10 let a = -200 let x = a*(1-Math.cos(0))*Math.sin(0)+startPosition[0] let y = a*(1-Math.cos(0))*Math.cos(0)+startPosition[1] function movePosition (a,g,startPosition) { let t = Math.PI*2/360 let x = a*(1-Math.cos(g*t))*Math.sin(g*t)+startPosition[0] let y = a*(1-Math.cos(g*t))*Math.cos(g*t)+startPosition[1] return [x,y] } function intervalMove (speed,g=0) { if(g===720){ g = 360 } g = g+speed let nextPosition = movePosition (a,g,startPosition) x = nextPosition[0] y = nextPosition[1] clearBgcolor(wrap1) for(let i =1;i<=360;i++){ let g1 = g-i/2 if(g1<0&&g<=360){ break } let g1Position = movePosition (a,g1,startPosition) newParticle (wrap1,[g1Position[0],g1Position[1]],r/(1+i/5),`rgba(255,200,200,0.5)`) } setTimeout(()=>{intervalMove (speed,g)},33) } function intervalOpcity (cur=0,start=0.3,end=0.8,dur=3) { if(fadeOpcity(cur,start,end,dur)===end){ cur = 0 let temp = start start = end end = temp } let opcity = fadeOpcity(cur,start,end,dur) let color = `rgba(0,0,0,${opcity})` clearBgcolor(wrap) newParticle (wrap,[x,y],r,color) cur=parseFloat(cur+0.1) setTimeout(()=>{intervalOpcity (cur,start,end,dur)},33) } intervalOpcity () intervalMove (1)
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持开心学习网。
您可能感兴趣
- canvas 绘图解决方案(高清屏下canvas重置尺寸引发的问题的解决)
- canvas宽高技巧(canvas画图被放大且模糊的解决方法)
- jscanvas画折线图(js+canvas实现代码雨效果)
- canvas怎么画特效(canvas中普通动效与粒子动效的实现代码示例)
- html5canvas动画(html5 canvas 实现光线沿不规则路径运动)
- canvas绘制图像文件方法(Canvas多边形绘制的实现方法)
- 前端ui 滑动条(AmazeUI 手机版页面的顶部导航条Header与侧边导航栏offCanvas的示例代码)
- canvas跟随鼠标绘制(如何在Canvas上的图形/图像绑定事件监听的实现)
- canvas图片裁剪插件(浅析图片上传及canvas压缩的流程)
- html5canvas功能介绍(Html5 Canvas实现图片标记、缩放、移动和保存历史状态功能 附转换公式)
- canvas绘图画圆基本步骤(利用 Canvas实现绘画一个未闭合的带进度条的圆环)
- canvas怎么压缩图片(使用canvas压缩图片大小的方法示例)
- html5 canvas 性能(基于html5 canvas做批改作业的小插件)
- js使用canvas(JavaScript canvas实现七彩时钟效果)
- canvas如何在网页上画图形(canvas绘制图片drawImage使用方法)
- canvas俄罗斯方块(JavaScript canvas实现俄罗斯方块游戏)
- 菲律宾旅游攻略(菲律宾旅游攻略地图)
- 清华大学难考吗(清华大学考研录取分数线)
- 观花盆栽佛肚竹盆景制作及养护(观花盆栽佛肚竹盆景制作及养护)
- 春天养佛肚竹,做好这几件事,叶绿根壮寓意好 越养越旺家(春天养佛肚竹做好这几件事)
- 律界衣品最好,时尚圈学识数高,41岁的Amal堪称现实版的傲骨贤妻(时尚圈学识数高)
- 刘智泰确认出演《Good Wife》 担纲男一号(刘智泰确认出演Good)
热门推荐
- docker网络通信(Google和Facebook不使用Docker的原理解析)
- js网页截图(JS如何实现页面截屏功能实例代码)
- mysql批量生成uuid(一种简单的ID生成策略: Mysql表生成全局唯一ID的实现)
- sqlserver新建连接服务器卡住(SQL Server成功与服务器建立连接但是在登录过程中发生错误的快速解决方案)
- 真实的codeigniter错误(Codeigniter里的无刷新上传的实现代码)
- nginx过滤http请求协议(nginx对http请求处理的各个阶段详析)
- html5+css3动画效果图(纯DOM+CSS3实现简单的小风车动画)
- php使用yield处理并发(Yii框架中使用PHPExcel的方法分析)
- phpcurl详解(解决PHP curl或file_get_contents下载图片损坏或无法打开的问题)
- mysql数据类型
排行榜
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9