html5 canvas气泡动画
来源:互联网 发布:中国软件杯影响力 编辑:程序博客网 时间:2024/04/30 06:03
写了一个简单的demo,用canvas实现一个气泡的动画,点击画布的时候增加气泡。当气泡碰撞到画布边缘的时候会反弹回来,能同时改变颜色。
思路:
画布的宽度为w,高度为y,球的半径为为r
每个新生成的气泡有2个随机值,一个表示x轴方向的移动比例movex,一个表示y轴方向的移动比例movey。
当气泡移动到左上角、左下角、右下角、右上角的时候movex,movey反向
当气泡移动到左边缘和右边缘的时候,movex反向
当气泡移动到上边缘和下边缘的时候,movey反向
代码:
<!DOCTYPE html><html> <head> <meta charset="utf-8" /> <title></title> <style type="text/css"> #bubble { border:1px solid black; } </style> </head> <body> <div> <canvas id="bubble" width="400px" height="400px"></canvas> </div> <script> var canvas=document.getElementById('bubble'); var ctx=canvas.getContext('2d'); var w=ctx.canvas.width; var h=ctx.canvas.height; var color=['#ffc0cb','#dda0dd','#9370db','#6495ed','#87cefa','#ffffff','#fa8072','#a0522d','#ff8c00','#008000']; var value=[]; //气泡半径 var r=20; //画气泡 function drawBubble(x,y,r,n) { ctx.save(); var i=n%(color.length); ctx.globalAlpha=0.3; ctx.strokeStyle='rgba(0,0,0,0.5)'; ctx.lineWidth=2; ctx.fillStyle=color[i]; ctx.beginPath(); ctx.moveTo(x,y); ctx.arc(x,y,r,0,2*Math.PI,true); ctx.stroke(); ctx.closePath(); //设置阴影 ctx.shadowBlur=7; ctx.shadowColor='#fff'; ctx.shadowOffsetX=10; ctx.shadowOffsetY=-6; ctx.fill(); //画光圈 ctx.beginPath(); ctx.strokeStyle='rgba(255,255,255,0.5)'; ctx.lineCap='round'; ctx.lineWidth=3; ctx.arc(x,y,r/2,(Math.PI*2)/0.9,(Math.PI*2),true); ctx.stroke(); ctx.restore(); } //放数据 function putValue(x,y,mx,my) { var temp={}; temp.color=0; temp.x=x; temp.y=y; temp.movex=mx; temp.movey=my; value.push(temp); } //移动 function move() { //ctx.restore(); value.forEach(function(item,index) { var newx=item.x+item.movex*15; var newy=item.y+item.movey*15; //判断是否到达边界 if(newx>r&&newx<(w-r)&&newy>r&&newy<(h-r)) { drawBubble(newx,newy,r,item.color); item.x=newx; item.y=newy; } else if((newx>=(w-r)&&newy>=(h-r))||(newx>=(w-r)&&newy<r)||(newx<r&&newy<r)||(newx<r&&newy>(h-r)))//角 { //换颜色 item.color++; item.color=item.color%color.length; drawBubble(newx,newy,item.color); item.x=newx; item.y=newy; item.movex=0-item.movex; item.movex=0-item.movex; } else if(newx>=(w-r)||newx<r)//左右边 { item.color++; item.color=item.color%color.length; drawBubble(newx,newy,item.color); item.x=newx; item.y=newy; item.movex=0-item.movex; } else if(newy>=(h-r)||newy<r)//上下边 { item.color++; drawBubble(newx,newy,item.color); item.color=item.color%color.length; item.x=newx; item.y=newy; item.movey=0-item.movey; } }); } function draw(x,y) { drawBubble(x,y,r,0); var mx=Math.random()-0.5; var my=Math.random()-0.5; putValue(x,y,mx,my); } ctx.fillStyle='#000'; ctx.fillRect(0,0,w,h); draw(w/2,h/2); //设置间隔性定时器 setInterval(function(){ ctx.clearRect(0,0,w,h); ctx.fillStyle='#000'; ctx.fillRect(0,0,w,h); move(); console.log(value); },100); canvas.onclick=function(evt) { draw(evt.pageX,evt.pageY); } </script> </body></html>
最终效果:
不过气泡和气泡之间的碰撞反弹,还没有想到比较好的方法来处理。
阅读全文
0 0
- html5 canvas气泡动画
- html5之canvas动画
- HTML5 Canvas 视差滚动动画
- HTML5 Canvas动画效果演示
- HTML5 Canvas超炫酷烟花动画!
- HTML5 canvas 制作动画原理
- 动画裁剪程序-HTML5 Canvas
- html5的canvas旋转动画
- Html5 Canvas 3D 动画
- HTML5 中的 canvas 标签 实现动画效果
- 一个JS动画框架-基于HTML5::Canvas
- HTML5 Canvas 基于OO的动画实例
- HTML5 Canvas 逐帧动画的实现
- HTML5 Canvas 逐帧动画的实现
- 使用html5 canvas 画一台小单车动画
- html5 canvas创建弹性碰撞动画
- 7.HTML5 高级Canvas技术-基本动画
- HTML5----kinetic,CSS3(Canvas)--小丑动画
- WEB基础:JavaScript
- 【快搭】不懂使用vim可以用EditPlus连接Linux编辑文本
- 2018腾讯实习生招聘笔试题
- Eclipse中使用SVN和Git版本管理工具
- 安卓基础8:服务和AIDL
- html5 canvas气泡动画
- 2017华为笔试题3
- sql server 表分区
- 实现中文字符串截取无乱码的方法
- [贪心]BZOJ 3410: [Usaco2009 Dec]Selfish Grazing 自私的食草者
- Node.js配置服务端与客户端交互
- 读书笔记:SQL必知必会【第7课-第9课】
- IO-压缩流
- hdu 1248 完全背包