html5 canvas 一个漫天飞雪的效果


很棒的下雪效果


代码奉上

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
  5. <title>漫天飞雪</title>
  6. <style type="text/css">
  7. * {margin: 0; padding: 0;}

  8. body {
  9.     /*You can use any kind of background here.*/
  10.     background: #6b92b9;
  11. }
  12. canvas {
  13.     display: block;
  14. }
  15. </style>
  16. </head>

  17. <body>

  18. <div style=" background:#6b92b9; width:100%; height:2000px;" ></div>
  19. <canvas id="canvas" style="position:fixed; top:0px;left:0px;z-index:80;pointer-events:none;"></canvas>

  20. <script>
  21. window.onload = function(){
  22.     //canvas init
  23.     var canvas = document.getElementById("canvas");
  24.     var ctx = canvas.getContext("2d");
  25.     
  26.     //canvas dimensions
  27.     var W = window.innerWidth;
  28.     var H = window.innerHeight;
  29.     canvas.width = W;
  30.     canvas.height = H;
  31.     
  32.     //snowflake particles
  33.     var mp = 3000; //max particles
  34.     var particles = [];
  35.     for(var i = 0; i < mp; i++)
  36.     {
  37.         particles.push({
  38.             x: Math.random()*W, //x-coordinate
  39.             y: Math.random()*H, //y-coordinate
  40.             r: Math.random()*3+1, //radius
  41.             d: Math.random()*mp //density
  42.         })
  43.     }
  44.     
  45.     //Lets draw the flakes
  46.     function draw()
  47.     {
  48.         ctx.clearRect(0, 0, W, H);
  49.         
  50.        ctx.fillStyle = "rgba(255, 255, 255, 0.8)";
  51.        /* ctx.fillStyle = "#FF0000";*/
  52.         ctx.beginPath();
  53.         for(var i = 0; i < mp; i++)
  54.         {
  55.             var p = particles[i];
  56.             ctx.moveTo(p.x, p.y);
  57.             ctx.arc(p.x, p.y, p.r, 0, Math.PI*2, true);
  58.         }
  59.         ctx.fill();
  60.         update();
  61.     }
  62.     
  63.     //Function to move the snowflakes
  64.     //angle will be an ongoing incremental flag. Sin and Cos functions will be applied to it to create vertical and horizontal movements of the flakes
  65.     var angle = 0;
  66.     function update()
  67.     {
  68.         angle += 0.01;
  69.         for(var i = 0; i < mp; i++)
  70.         {
  71.             var p = particles[i];
  72.             //Updating X and Y coordinates
  73.             //We will add 1 to the cos function to prevent negative values which will lead flakes to move upwards
  74.             //Every particle has its own density which can be used to make the downward movement different for each flake
  75.             //Lets make it more random by adding in the radius
  76.             p.y += Math.cos(angle+p.d) + 1 + p.r/2;
  77.             p.x += Math.sin(angle) * 2;
  78.             
  79.             //Sending flakes back from the top when it exits
  80.             //Lets make it a bit more organic and let flakes enter from the left and right also.
  81.             if(p.x > W || p.x < 0 || p.y > H)
  82.             {
  83.                 if(i%3 > 0) //66.67% of the flakes
  84.                 {
  85.                     particles[i] = {x: Math.random()*W, y: -10, r: p.r, d: p.d};
  86.                 }
  87.                 else
  88.                 {
  89.                     //If the flake is exitting from the right
  90.                     if(Math.sin(angle) > 0)
  91.                     {
  92.                         //Enter fromth
  93.                         particles[i] = {x: -5, y: Math.random()*H, r: p.r, d: p.d};
  94.                     }
  95.                     else
  96.                     {
  97.                         //Enter from the right
  98.                         particles[i] = {x: W+5, y: Math.random()*H, r: p.r, d: p.d};
  99.                     }
  100.                 }
  101.             }
  102.         }
  103.     }
  104.     
  105.     //animation loop
  106.     setInterval(draw, 15);
  107. }
  108. </script>
  109. </body>
  110. </html>

r: Math.random()*3+1, //radius


这行代码改变雪花半径大小


setInterval(draw, 15);


这个改变雪花下落速度


   var mp = 3000; //max particles


这个值改变雪花密度


  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值