Javascript 五彩纸屑效应

Javascript 五彩纸屑效应,javascript,jquery,html,css,html5-canvas,Javascript,Jquery,Html,Css,Html5 Canvas,我正在努力复制下面的屏幕截图。为此,我正在编写以下代码。目前,我一直在寻找一种方法,以实现所需大小的混合薄片形状的预期结果。任何人都可以分享他们的经验来解决这个问题。如有任何建议,如使用SVG等,也将不胜感激。提前谢谢 (function () { // globals var canvas; var ctx; var W; var H; var mp = 150; //max particles var particles = [];

我正在努力复制下面的屏幕截图。为此,我正在编写以下代码。目前,我一直在寻找一种方法,以实现所需大小的混合薄片形状的预期结果。任何人都可以分享他们的经验来解决这个问题。如有任何建议,如使用SVG等,也将不胜感激。提前谢谢

(function () {
    // globals
    var canvas;
    var ctx;
    var W;
    var H;
    var mp = 150; //max particles
    var particles = [];
    var angle = 0;
    var tiltAngle = 0;
    var confettiActive = true;
    var animationComplete = true;
    var deactivationTimerHandler;
    var reactivationTimerHandler;
    var animationHandler;

    // objects

    var particleColors = {
        colorOptions: ["DodgerBlue", "OliveDrab", "Gold", "pink", "SlateBlue", "lightblue", "Violet", "PaleGreen", "SteelBlue", "SandyBrown", "Chocolate", "Crimson"],
        colorIndex: 0,
        colorIncrementer: 0,
        colorThreshold: 10,
        getColor: function () {
            if (this.colorIncrementer >= 10) {
                this.colorIncrementer = 0;
                this.colorIndex++;
                if (this.colorIndex >= this.colorOptions.length) {
                    this.colorIndex = 0;
                }
            }
            this.colorIncrementer++;
            return this.colorOptions[this.colorIndex];
        }
    }

    function confettiParticle(color) {
        this.x = Math.random() * W; // x-coordinate
        this.y = (Math.random() * H) - H; //y-coordinate
        this.r = RandomFromTo(10, 30); //radius;
        this.d = (Math.random() * mp) + 10; //density;
        this.color = color;
        this.tilt = Math.floor(Math.random() * 10) - 10;
        this.tiltAngleIncremental = (Math.random() * 0.07) + .05;
        this.tiltAngle = 0;

        this.draw = function () {
            ctx.beginPath();
            ctx.lineWidth = this.r / 2;
            ctx.strokeStyle = this.color;
            ctx.moveTo(this.x + this.tilt + (this.r / 4), this.y);
            ctx.lineTo(this.x + this.tilt, this.y + this.tilt + (this.r / 4));
            return ctx.stroke();
        }
    }

    $(document).ready(function () {
        SetGlobals();
        InitializeButton();
        InitializeConfetti();

        $(window).resize(function () {
            W = window.innerWidth;
            H = window.innerHeight;
            canvas.width = W;
            canvas.height = H;
        });

    });

    function InitializeButton() {
        $('#stopButton').click(DeactivateConfetti);
        $('#startButton').click(RestartConfetti);
    }

    function SetGlobals() {
        canvas = document.getElementById("canvas");
        ctx = canvas.getContext("2d");
        W = window.innerWidth;
        H = window.innerHeight;
        canvas.width = W;
        canvas.height = H;
    }

    function InitializeConfetti() {
        particles = [];
        animationComplete = false;
        for (var i = 0; i < mp; i++) {
            var particleColor = particleColors.getColor();
            particles.push(new confettiParticle(particleColor));
        }
        StartConfetti();
    }

    function Draw() {
        ctx.clearRect(0, 0, W, H);
        var results = [];
        for (var i = 0; i < mp; i++) {
            (function (j) {
                results.push(particles[j].draw());
            })(i);
        }
        Update();

        return results;
    }

    function RandomFromTo(from, to) {
        return Math.floor(Math.random() * (to - from + 1) + from);
    }


    function Update() {
        var remainingFlakes = 0;
        var particle;
        angle += 0.01;
        tiltAngle += 0.1;

        for (var i = 0; i < mp; i++) {
            particle = particles[i];
            if (animationComplete) return;

            if (!confettiActive && particle.y < -15) {
                particle.y = H + 100;
                continue;
            }

            stepParticle(particle, i);

            if (particle.y <= H) {
                remainingFlakes++;
            }
            CheckForReposition(particle, i);
        }

        if (remainingFlakes === 0) {
            StopConfetti();
        }
    }

    function CheckForReposition(particle, index) {
        if ((particle.x > W + 20 || particle.x < -20 || particle.y > H) && confettiActive) {
            if (index % 5 > 0 || index % 2 == 0) //66.67% of the flakes
            {
                repositionParticle(particle, Math.random() * W, -10, Math.floor(Math.random() * 10) - 10);
            } else {
                if (Math.sin(angle) > 0) {
                    //Enter from the left
                    repositionParticle(particle, -5, Math.random() * H, Math.floor(Math.random() * 10) - 10);
                } else {
                    //Enter from the right
                    repositionParticle(particle, W + 5, Math.random() * H, Math.floor(Math.random() * 10) - 10);
                }
            }
        }
    }
    function stepParticle(particle, particleIndex) {
        particle.tiltAngle += particle.tiltAngleIncremental;
        particle.y += (Math.cos(angle + particle.d) + 3 + particle.r / 2) / 2;
        particle.x += Math.sin(angle);
        particle.tilt = (Math.sin(particle.tiltAngle - (particleIndex / 3))) * 15;
    }

    function repositionParticle(particle, xCoordinate, yCoordinate, tilt) {
        particle.x = xCoordinate;
        particle.y = yCoordinate;
        particle.tilt = tilt;
    }

    function StartConfetti() {
        W = window.innerWidth;
        H = window.innerHeight;
        canvas.width = W;
        canvas.height = H;
        (function animloop() {
            if (animationComplete) return null;
            animationHandler = requestAnimFrame(animloop);
            return Draw();
        })();
    }

    function ClearTimers() {
        clearTimeout(reactivationTimerHandler);
        clearTimeout(animationHandler);
    }

    function DeactivateConfetti() {
        confettiActive = false;
        ClearTimers();
    }

    function StopConfetti() {
        animationComplete = true;
        if (ctx == undefined) return;
        ctx.clearRect(0, 0, W, H);
    }

    function RestartConfetti() {
        ClearTimers();
        StopConfetti();
        reactivationTimerHandler = setTimeout(function () {
            confettiActive = true;
            animationComplete = false;
            InitializeConfetti();
        }, 100);

    }

    window.requestAnimFrame = (function () {
        return window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || window.oRequestAnimationFrame || window.msRequestAnimationFrame || function (callback) {
            return window.setTimeout(callback, 1000 / 60);
        };
    })();
})();
(函数(){
//全球的
var帆布;
var-ctx;
var-W;
var H;
var mp=150;//最大粒子数
var粒子=[];
var角=0;
var倾斜角=0;
var=active=true;
var animationComplete=true;
无功失活定时器;
var重新激活timerhandler;
var animationHandler;
//物体
var粒子颜色={
颜色选项:[“道奇蓝”、“橄榄蓝”、“金色”、“粉色”、“石板蓝”、“浅蓝色”、“紫罗兰色”、“淡绿色”、“钢蓝”、“山黛蓝”、“巧克力色”、“深红色”],
颜色索引:0,
颜色递增器:0,
颜色阈值:10,
getColor:函数(){
如果(this.colorIncrementer>=10){
this.colorIncrementer=0;
这个.colorIndex++;
如果(this.colorIndex>=this.colorOptions.length){
该指数为0;
}
}
这个.colorIncrementer++;
返回此.colorOptions[this.colorIndex];
}
}
功能粒子(颜色){
this.x=Math.random()*W;//x坐标
this.y=(Math.random()*H)-H;//y坐标
this.r=RandomFromTo(10,30);//半径;
this.d=(Math.random()*mp)+10;//密度;
这个颜色=颜色;
this.tilt=Math.floor(Math.random()*10)-10;
this.tiltAngleIncremental=(Math.random()*0.07)+.05;
这个倾斜角=0;
this.draw=函数(){
ctx.beginPath();
ctx.lineWidth=此.r/2;
ctx.strokeStyle=this.color;
ctx.moveTo(this.x+this.tilt+(this.r/4),this.y);
ctx.lineTo(this.x+this.tilt,this.y+this.tilt+(this.r/4));
返回ctx.stroke();
}
}
$(文档).ready(函数(){
SetGlobals();
初始化按钮();
初始化Econfetti();
$(窗口)。调整大小(函数(){
W=窗宽;
H=窗内高度;
画布宽度=W;
canvas.height=H;
});
});
函数初始化按钮(){
$(“#停止按钮”)。单击(停用ECONFETTI);
$(“#开始按钮”)。单击(restarconfetti);
}
函数SetGlobals(){
canvas=document.getElementById(“canvas”);
ctx=canvas.getContext(“2d”);
W=窗宽;
H=窗内高度;
画布宽度=W;
canvas.height=H;
}
函数初始化EconFetti(){
粒子=[];
animationComplete=false;
对于(变量i=0;iH)和{
如果(指数%5>0 | |指数%2==0)//66.67%的薄片
{
重新定位粒子(粒子,Math.random()*W,-10,Math.floor(Math.random()*10)-10);
}否则{
如果(数学sin(角度)>0){
//从左边进入
重新定位粒子(粒子,-5,Math.random()*H,Math.floor(Math.random()*10)-10);
}否则{
//从右边进入
重新定位粒子(粒子,W+5,Math.random()*H,Math.floor(Math.random()*10)-10);
}
}
}
}
函数步骤粒子(粒子,粒子索引){
particle.tiltAngle+=particle.tiltAngle增量;
粒子.y+=(数学cos(角度+粒子.d)+3+粒子.r/2)/2;
粒子x+=数学sin(角度);
particle.tilt=(数学sin(particle.tiltAngle-(particleIndex/3))*15;
}
功能粒子(粒子、X坐标、Y坐标、倾斜){
粒子x=x坐标;
粒子y=y坐标;
粒子倾斜=倾斜;
}
函数StartConfetti(){
W=窗宽;
H=窗内高度;
画布宽度=W;
canvas.height=H;
(函数animloop(){
如果(animationComplete)返回null;
animationHandler=requestAnimFrame(animloop);
返回Draw();
})();
}
函数ClearTimers(){
clearTimeout(重新激活TimerHandler);
clearTimeout(animationHandler);
}
函数DeactivateConfetti(){
坦白=虚假;
清除计时器();
}
函数StopConfetti(){
animationComplete=true;
如果(ctx==未定义)返回;
ctx.clearRect(0,0,W,H);
}
函数RestartConfetti(){
清除计时器();
彩色纸屑