H5小动画,分享一下!!

<!DOCTYPE html>
 <html>
 <head>
 <title> new document </title>
 <meta charset="utf-8"/>
  
 </head>
  
 <body >
 <script>
 var bd=document.getElementsByTagName('body')[0];
 var canvas=document.createElement('canvas');
 bd.appendChild(canvas);
 var context=canvas.getContext('2d');
 canvas.width=window.innerWidth;
 canvas.height=window.innerHeight;
 //定义彩球的构造器
 function circle(){
 this.x=Math.random()*canvas.width;
 this.y=canvas.height;
 this.r1=Math.random()*10+1;
 this.r2=parseInt(Math.random()*256);
 this.g=parseInt(Math.random()*256);
 this.b=parseInt(Math.random()*256);
 this.paint=function(){
 if(this.y<canvas.height/2-50){
  
  
 context.strokeStyle='#69b6b1';
 context.lineWidth=.2;
 context.beginPath();
 context.arc(this.x,this.y,this.r1,0,Math.PI*2);
 context.stroke()
 }else if(this.y<canvas.height/2){
 context.fillStyle='#69b6b1';
 context.lineWidth=.2;
 context.beginPath();
 context.arc(this.x,this.y,this.r1,0,Math.PI*2);
 context.fill()
  
  
  
  
 }else{
 context.fillStyle='rgb('+this.r2+','+this.g+','+this.b+')';
 context.beginPath();
 context.arc(this.x,this.y,this.r1,0,Math.PI*2);
 context.fill()}
 };
 this.step=function(){
  
 this.y--;
  
  
  
 }
 }
 var circles=[];
 var time=0;//控制数量;
 //定义所有彩球的绘制方法和移动
 function createCircles(){
 time++;
 if(time%10==0){
 circles.push(new circle())
 }
 for(var i=0,l=circles.length;i<l;i++){
 circles[i].paint();
 circles[i].step();
 }
 }
 //优化性能删除画布外面的圈圈
 function deleteCircles(){
 for(var i=0,l=circles.length;i<l;i++){
 if(this.y<0){
 circles.splice(i,1)
 }
  
 }
  
 }
 //核心定时器
 var img=new Image();
 img.src='bg.jpg'//这里图片路径
 var timer=setInterval(function(){
 context.drawImage(img,0,0,canvas.width,canvas.height)
 createCircles()
 deleteCircles()
  
  
 },10)
  
  
  
 </script>
 </body>
 

</html>

//气泡在背景图中 上升  到消失 代码中不插入图片看不到效果

 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值