html5 canvas粒子形成下雪背景的效果

來源:互聯網
上載者:User
本篇文章給大家帶來的內容是關於html5 canvas粒子形成下雪背景的效果,有一定的參考價值,有需要的朋友可以參考一下,希望對你有所協助。

<!doctype html><html lang="en"><head><meta charset="UTF-8" /><title>canvas粒子形成下雪背景</title>    body{        margin: 0px;        padding: 0px;    }    #canvas{        display: block;        background: #222;    }</style></head><body>    <canvas id="canvas"></canvas></body><script>    var canvas = document.getElementById("canvas");//擷取canvas    var ctx = canvas.getContext("2d");//建立畫筆    var w = canvas.width = window.innerWidth;//瀏覽器寬度    var h = canvas.height = window.innerHeight;//瀏覽器高度    window.onresize = function(){        w = canvas.width = window.innerWidth;        h = canvas.height = window.innerHeight;    };//當瀏覽器重新整理的時候重新整理    var num = 1000;//數量    var shuju = [];//空數組    for(i = 0;i<num;i++){        shuju.push({//數組末項添加            x : Math.random()*w,             y : Math.random()*h,            r : Math.random()*2        });        draw(shuju[i].x,shuju[i].y,shuju[i].r)//for迴圈迴圈darw function    };    console.log(shuju[0]);    function draw(x1,y1,r1){        ctx.beginPath();//開始繪畫        ctx.fillStyle = "#fff";//顏色        ctx.arc(x1,y1,r1,0,2*Math.PI,false)//arc園        ctx.fill();//填充    }    function chageY(){        for(var i = 0;i<num;i++){//for迴圈            shuju[i].y += Math.random()*5;            if(shuju[i].y > h){//大於高度                shuju[i].y = 0;//等於0            }            draw(shuju[i].x,shuju[i].y,shuju[i].r);//調取        }    }    setInterval(function(){        ctx.clearRect(0,0,w,h);//清楚畫布 0 0 高度 寬度        chageY();     },10);</script>
相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.