Canvas Dynamic Background

HTML5 canvas game dynamic background design effect

A dynamic background effect generated using the HTML5 canvas basic method arc~~

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Canvas Background</title>
    <style type="text/css">
        body {
            margin:0px;
            padding:0px;
            text-align:center;
        }

        canvas{
            outline:0;
            margin-left: auto;
            margin-right: auto;
        }
    </style>
</head>
<body>
<canvas id='c'></canvas>
<script>
    /*Javascript code snippet*/

    var width = 1000,
            height = 1000,
            c = document.getElementById('c'),
            ctx = c.getContext('2d');

    c.width = width;
    c.height = height;

    var clear = function(){
        ctx.fillStyle = '#d0e7f9';
        ctx.beginPath();
        ctx.rect(0, 0, width, height);
        ctx.closePath();
        ctx.fill();
    }

    var howManyCircles = 8, circles = [];

    for (var i = 0; i < howManyCircles; i++)
        circles.push([Math.random() * width, Math.random() * height, Math.random() * 100, Math.random() / 2]);

    var DrawCircles = function(){
        for (var i = 0; i < howManyCircles; i++) {
            ctx.fillStyle = 'rgba(255, 255, 255, ' + circles[i][3] + ')';
            ctx.beginPath();
            ctx.arc(circles[i][0], circles[i][1], circles[i][2], 0, Math.PI * 2, true);
            ctx.closePath();
            ctx.fill();
        }
    };

    var MoveCircles = function(deltaY){
        for (var i = 0; i < howManyCircles; i++) {
            if (circles[i][1] - circles[i][2] > height) {
                circles[i][0] = Math.random() * width;
                circles[i][2] = Math.random() * 100;
                circles[i][1] = 0 - circles[i][2];
                circles[i][3] = Math.random() / 2;
            } else {
                circles[i][1] += deltaY;
            }
        }
    };

    var width = 1000,
            height = 1000,
            gLoop;

    var GameLoop = function(){
        clear();
        MoveCircles(3);
        DrawCircles();
        gLoop = setTimeout(GameLoop, 1000 / 50);
    }

    GameLoop();
</script>
</body>
</html>

 

 

 

 

.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326151672&siteId=291194637