Javascript五彩纸屑爆炸效果

时间:2015-11-10 12:04:21

标签: javascript animation

我能够找到这个代码来创建一个javascript五彩纸屑爆炸。 但是,我需要三种主要颜色的五彩纸屑,而对于五彩纸屑,我需要是正方形。任何帮助将不胜感激。

以下是代码

// shim layer with setTimeout fallback
window.requestAnimFrame = (function(){
  return  window.requestAnimationFrame       || 
		  window.webkitRequestAnimationFrame || 
		  window.mozRequestAnimationFrame    || 
		  window.oRequestAnimationFrame      || 
		  window.msRequestAnimationFrame     || 
		  function( callback ){
			window.setTimeout(callback, 1000 / 60);
		  };
})();

var canvas = document.getElementById("canvas"),
	ctx = canvas.getContext("2d"),
	W = window.innerWidth,
	H = window.innerHeight,
	circles = [];

canvas.width = W;
canvas.height = H; 

//Random Circles creator
function create() {
	
	//Place the circles at the center
	
	this.x = W/2;
	this.y = H/2;

	
	//Random radius between 2 and 6
	this.radius = 2 + Math.random()*3; 
	
	//Random velocities
	this.vx = -5 + Math.random()*10;
	this.vy = -5 + Math.random()*10;
	
	//Random colors
	this.r = Math.round(Math.random())*255;
	this.g = Math.round(Math.random())*255;
	this.b = Math.round(Math.random())*255;
}

for (var i = 0; i < 500; i++) {
	circles.push(new create());
}

function draw() {
	
	//Fill canvas with black color
    ctx.globalCompositeOperation = "source-over";
    ctx.fillStyle = "rgba(0,0,0,0.15)";
    ctx.fillRect(0, 0, W, H);
	
	//Fill the canvas with circles
	for(var j = 0; j < circles.length; j++){
		var c = circles[j];
		
		//Create the circles
		ctx.beginPath();
		ctx.arc(c.x, c.y, c.radius, 0, Math.PI*2, true);
        ctx.fillStyle = "rgba("+c.r+", "+c.g+", "+c.b+", 1)";
		ctx.fill();
		
		c.x += c.vx;
		c.y += c.vy;
		c.radius -= .02;
		
		if(c.radius > 3)
			circles[j] = new create();
	}
}

function animate() {
	requestAnimFrame(animate);
	draw();
}

animate();
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Untitled Document</title>
<style type="text/css">
	
	body{
	padding: 0; margin: 0;
	min-height: 400px; height: 100%;
	width: 100%;
	overflow: hidden;
	background-color: #000000;
}
</style>
</head>

<body>
<canvas id="canvas"></canvas>
<script src="explosion.js"></script>
</body>
</html>

4 个答案:

答案 0 :(得分:1)

我写了一个替代解决方案。我注意到您当前的解决方案存在一些运行时错误,也有很多不必要的代码(IMO-可能更干净或更容易阅读?)并绘制了整个矩形画布,您可以通过CSS将其应用为背景色,或保留为透明您可能想让五彩纸屑展示一些东西?

https://codepen.io/BenMoses/pen/MWyEePJ

ctx.globalCompositeOperation = "source-over"; //not needed as is default.
// shim layer with setTimeout fallback ? This is globally support so probably unnecessary?
//this.radius = 2 + Math.random()*3;  //this is only for circles so can be removed
// all circle references should be updated to rect or square or confetti and confetto (a single confetti :o )

答案 1 :(得分:0)

只有几个地方可以修改(下面评论)。

<!DOCTYPE html>
<html>
<head>
    <style type="text/css">
    body{
        padding: 0; margin: 0;
        min-height: 400px; height: 100%;
        width: 100%;
        overflow: hidden;
        background-color: #000000;
    }
    </style>
</head>

<body>
    <canvas id="canvas"></canvas>

    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.4/jquery.js"></script>
    <script>
        // shim layer with setTimeout fallback
        window.requestAnimFrame = (function(){
          return  window.requestAnimationFrame       || 
                  window.webkitRequestAnimationFrame || 
                  window.mozRequestAnimationFrame    || 
                  window.oRequestAnimationFrame      || 
                  window.msRequestAnimationFrame     || 
                  function( callback ){
                    window.setTimeout(callback, 1000 / 60);
                  };
        })();

        var canvas = document.getElementById("canvas"),
            ctx = canvas.getContext("2d"),
            W = window.innerWidth,
            H = window.innerHeight,
            circles = [];

        canvas.width = W;
        canvas.height = H; 

        //Random Circles creator
        function create() {

            //Place the circles at the center

            this.x = W/2;
            this.y = H/2;


            //Random radius between 2 and 6
            this.radius = 2 + Math.random()*3; 

            //Random velocities
            this.vx = -5 + Math.random()*10;
            this.vy = -5 + Math.random()*10;
        }

        for (var i = 0; i < 500; i++) {
            circles.push(new create());
        }

        function draw() {

            //Fill canvas with black color
            ctx.globalCompositeOperation = "source-over";
            ctx.fillStyle = "rgba(0,0,0,0.15)";
            ctx.fillRect(0, 0, W, H);

            var myColors = ["e8e8e8", "a98547", "ccac7b"];
            var which = 0;

            //Fill the canvas with circles
            for(var j = 0; j < circles.length; j++){
                var c = circles[j];

                //Create the circles
                ctx.beginPath();
                //ctx.arc(c.x, c.y, c.radius, 0, Math.PI*2, true);
                ctx.fillRect(c.x , c.y, 20, 20); //change to box

                //ctx.fillStyle = "rgba("+c.r+", "+c.g+", "+c.b+", 1)";
                ctx.fillStyle = "#" + myColors[which].toString();
                ctx.fill();

                c.x += c.vx;
                c.y += c.vy;
                c.radius -= .02;

                if(c.radius > 3)
                    circles[j] = new create();

                if(which < myColors.length - 1) {
                    which++;
                }
                else {
                    which = 0;
                }
            }
        }

        function animate() {
            requestAnimFrame(animate);
            draw();
        }

        animate();
    </script>
</body>

</html>

答案 2 :(得分:0)

传入索引并使用modulus operator确定要设置的颜色。基本理念:

function create(ind) {

    /* other code */    

    if(ind%3===0) { /* set color one */ }
    else if (ind%2===0) { /* set color two */ }
    else { /* set color three */ }     
}

for (var i = 0; i < 500; i++) {
    circles.push(new create(i));
}

希望您知道需要更改this.rthis.gthis.b值以匹配您需要的颜色。

答案 3 :(得分:0)

这是最终的代码,谢谢Stephen Brickner的所有帮助。我已经学到了很多东西

    // shim layer with setTimeout fallback
    window.requestAnimFrame = (function(){
      return  window.requestAnimationFrame       || 
              window.webkitRequestAnimationFrame || 
              window.mozRequestAnimationFrame    || 
              window.oRequestAnimationFrame      || 
              window.msRequestAnimationFrame     || 
              function( callback ){
                window.setTimeout(callback, 1000 / 60);
              };
    })();

    var canvas = document.getElementById("canvas"),
        ctx = canvas.getContext("2d"),
        W = window.innerWidth,
        H = window.innerHeight,
        circles = [];

    canvas.width = W;
    canvas.height = H; 

    //Random Circles creator
    function create() {

        //Place the circles at the center

        this.x = W/2;
        this.y = H/2;


        //Random radius between 2 and 6
        this.radius = 2 + Math.random()*3; 

        //Random velocities
        this.vx = -10 + Math.random()*30;
        this.vy = -10 + Math.random()*30;
    }

    for (var i = 0; i < 100; i++) {
        circles.push(new create());
    }

    function draw() {

        //Fill canvas with black color
        ctx.globalCompositeOperation = "source-over";
        ctx.fillStyle = "rgba(175,38,28,255)";
        ctx.fillRect(0, 0, W, H);

        var myColors = ["e8e8e8", "a98547", "ccac7b"];
        var which = 0;

        //Fill the canvas with circles
        for(var j = 0; j < circles.length; j++){
            var c = circles[j];

            //Create the circles
            ctx.beginPath();
            //ctx.arc(c.x, c.y, c.radius, 0, Math.PI*2, true);
            ctx.fillRect(c.x , c.y, 8, 8); //change to box

            //ctx.fillStyle = "rgba("+c.r+", "+c.g+", "+c.b+", 1)";
            ctx.fillStyle = "#" + myColors[which].toString();
            ctx.fill();

            c.x += c.vx;
            c.y += c.vy;
            c.radius -= .02;

            if(c.radius > 3)
                circles[j] = new create();

            if(which < myColors.length - 1) {
                which++;
            }
            else {
                which = 0;
            }
        }
    }

    function animate() {
        requestAnimFrame(animate);
        draw();
    }

    animate();