canvas制作随机粒子移动特效

注意要根据你电脑的实际情况量力而行
要是随机渲染2000个的话,就会是这钟情况


你的浏览器就会几秒钟之后失去响应,GG。

一般渲染个200个就很炫酷了
效果如下


代码如下

<!DOCTYPE html>
<html>
<head>
    <title>背景滚动</title>
   <meta charset="utf-8">
        <style> 
           body {
                background: #dddddd;
            }

            #canvas {
            position: absolute;
            top: 30px;
            left: 10px;
            background: #ffffff;
            cursor: crosshair;
            margin-left: 10px;
            margin-top: 10px;
            -webkit-box-shadow: 4px 4px 8px rgba(0,0,0,0.5);
            -moz-box-shadow: 4px 4px 8px rgba(0,0,0,0.5);
            box-shadow: 4px 4px 8px rgba(0,0,0,0.5);
            }

         input {
            margin-left: 15px;
            }

        </style>

</head>
<body>
    <canvas id='canvas' width='1024' height='512'>
      Canvas not supported
    </canvas>

    <input id='animateButton' type='button' value='Animate'/>

    <script src='./js/requestNextAnimationFrame.js'></script>
   
</body>
</html>


<script type="text/javascript">
var canvas=document.getElementById("canvas")
var context=canvas.getContext("2d")

var Sprite=function(name,painter,behaviors){
  if(name!==undefined){this.name=name}
    if(painter!==undefined){this.painter=painter}
      this.top=0
      this.left=0
      this.width=0
      this.height=0
      this.velocityX=0
      this.velocityY=0
      this.visible=true
      this.animating=false
      this.behaviors=behaviors||[]
      this.RADIUS=0
      return this


}
Sprite.prototype={
   paint:function(context){
    if(this.painter!==undefined&&this.visible){this.painter.paint(this,context)}
   },
   update:function(context,time){
    for(var i=0;i<this.behaviors.length;i++){
      this.behaviors[i].excute(this,context,time)
    }
   }
}

// var RADIUS=5
var pai={
        paint:function(sprite,context){

             context.save()

             context.beginPath()
             sprite.left=sprite.left+sprite.velocityX
             sprite.top=sprite.top+sprite.velocityY

             if(sprite.left+sprite.RADIUS>canvas.width||sprite.left-sprite.RADIUS<0){
                sprite.velocityX=-sprite.velocityX
             }
             if(sprite.top+sprite.RADIUS>canvas.height||sprite.top-sprite.RADIUS<0){
                sprite.velocityY=-sprite.velocityY
             }


             
             context.arc(sprite.left+sprite.width/2,sprite.top+sprite.height/2,sprite.RADIUS,0,Math.PI*2,false)
             context.clip()

             context.shadowBlur=8
             context.lineWidth=2
             context.strokeStyle="rgb(100,100,195)"
             context.fillStyle="rgba(30,144,255,0.15)"
             context.fill()
             context.stroke()
             context.restore()


        }

}
var star_list=[]
for(var i=0;i<100;i++){
  var star=new Sprite("star"+i,pai)
   star.left=parseInt(19+(Math.random()*800))
   star.top=parseInt(19+(Math.random()*450))

  if(i%2==0){
    star.velocityY=parseInt(Math.random()*15+1)
  star.velocityX=parseInt(Math.random()*15+1)

  }else{

       star.velocityY=-(parseInt(Math.random()*15+1))
  star.velocityX=-(parseInt(Math.random()*15+1))
  }

  


  star.RADIUS=parseInt(Math.random()*20+1)
  star_list.push(star)
}
function animate(){
       context.clearRect(0,0,canvas.width,canvas.height)
       context.fillStyle="rgb(0,0,0)"
       context.fillRect(0,0,canvas.width,canvas.height)
       
       for(var i=0;i<star_list.length;i++){

           star_list[i].paint(context)
       }

       window.requestAnimationFrame(animate)
}

window.requestAnimationFrame(animate)


</script>
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 今天从徐州开到沧州,高速上的车明显变多了,堵车足足耽误了快三个小时,感觉整个人的脑子到现在都是晃荡的…脑子太晃太累...
    c2cfbbac795f阅读 178评论 0 0
  • 总教练分享267 『崭新的旅游政策 ! 』2017 年 6 月29 日 亲爱的家人们 : 我们的旅游已经正式决...
    egaoTina阅读 128评论 0 0
  • 【七月未央】20171211学习力践行D212 2017年12月11日 七月+妈妈 小小书语者 D42 ❶手指谣_...
    七妈_haiyan阅读 505评论 0 50