美文网首页程序员
canvas制作随机移动粒子背景

canvas制作随机移动粒子背景

作者: infi_ | 来源:发表于2017-12-25 16:25 被阅读0次

效果如图


<!DOCTYPE html>
<html lang="en" style='height:100%;position:relative'>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<link rel="stylesheet" href="./css/main.css">

<style>
html,body {
            margin:0;
            overflow:hidden;
            width:100%;
            height:100%;
    
            background:black;
        }
</style>
</head>
<body>
  <canvas id="canvas"></canvas>
</body>
</html>

<script>
    var ctx=document.getElementById("canvas")
    context=ctx.getContext("2d");
    var WIDTH=document.documentElement.clientWidth;
    var HEIGHT=document.documentElement.clientHeight;
    var initRoundPopulation=180; 
    var round = [];
    ctx.width=WIDTH;
    ctx.height=HEIGHT;


    function Round_item(index,x,y){
    this.index=index;
    this.x=x;
    this.y=y;
    this.r=Math.random()*2+1;
    var alpha = (Math.floor(Math.random() * 10) + 1) / 10 / 2;
    this.color = "rgba(255,255,255," + alpha + ")";
    }
    
    Round_item.prototype.draw = function () {
        context.fillStyle = this.color;
        context.shadowBlur = this.r * 2;
        context.beginPath();
        context.arc(this.x, this.y, this.r, 0, 2 * Math.PI, false);
        context.closePath();
        context.fill();
    };
    Round_item.prototype.move_top = function () {
        this.y -= 0.15;
        if (this.y <= -10) {
            this.y = HEIGHT + 10;
        }
        this.x-=0.15;
        if(this.x<=-10){
            this.x=WIDTH+10
        }
        this.draw();
    }
    Round_item.prototype.move_down=function(){
        this.y += 0.15;
        if (this.y >HEIGHT) {
            this.y = 0 
        }


        this.x+=0.15
        if(this.x>WIDTH){
            this.x=0
        }
        if(this.x<0){
            this.x=WIDTH
        }
        this.draw();

    }

    Round_item.prototype.move_2=function(){
        this.y += 0.15;
        if (this.y >HEIGHT) {
            this.y = 0 
        }


        this.x-=0.15
        if(this.x>WIDTH){
            this.x=0
        }
        if(this.x<0){
            this.x=WIDTH
        }
        this.draw();

    }

    Round_item.prototype.move_4=function(){
        this.y -= 0.15;
        if (this.y<0) {
            this.y = HEIGHT 
        }


        this.x+=0.15
        if(this.x>WIDTH){
            this.x=0
        }
        if(this.x<0){
            this.x=WIDTH
        }
        this.draw();

    }

    function init() {
        for(var i = 0; i < initRoundPopulation; i++ ){
            round[i] = new Round_item(i,Math.random() * WIDTH,Math.random() * HEIGHT);
            round[i].draw();
        }
        animate();
    }
    init()

    function animate() {
        context.clearRect(0, 0, WIDTH, HEIGHT);
        var num=initRoundPopulation/2;
        for(var i in round){
            if(i<=num){
                if(round[i].index%2==0){round[i].move_top()}else{
                    round[i].move_down()
                }
            }else{
                if(round[i].index%2==0){round[i].move_2()}else{
                    round[i].move_4()
                }
            }
        }

        
        requestAnimationFrame(animate)
    }





</script>

相关文章

网友评论

    本文标题:canvas制作随机移动粒子背景

    本文链接:https://www.haomeiwen.com/subject/quvlgxtx.html