js点击放烟花烟花绽放动画效果代码

代码语言:html

所属分类:粒子

代码描述:js点击放烟花烟花绽放动画效果代码

代码标签: 烟花 烟花 绽放 动画 效果

下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta content="width=device-width,height=device-height,inital-scale=1.0,maximum-scale=1.0,user-scalable=no" name="viewport">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">

    <style type="text/css">



        html,body {
            height: 100%;
            margin: 0;
            padding: 0
        }



        ul,li {
            text-indent: 0;
            text-decoration: none;
            margin: 0;
            padding: 0
        }



        img {
            border: 0
        }



        body {
            background-color: #000;
            color: #999;
            font: 100%/18px helvetica, arial, sans-serif
        }



        canvas {
            cursor: crosshair;
            display: block;
            left: 0;
            position: absolute;
            top: 0;
            z-index: 20
        }



        #header img {
            width: 100%;
            height: 20%;
        }



        #bg img {
            width: 100%;
            height: 80%;
        }



        #header,#bg {
            position: fixed;
            left: 0;
            right: 0;
            z-index: 10
        }



        #header {
            top: 0
        }



        #bg {
            position: fixed;
            z-index: 1;
        }



        audio {
            position: fixed;
            display: none;
            bottom: 0;
            left: 0;
            right: 0;
            width: 100%;
            z-index: 5
        }


        body {
            background: url(//repo.bfw.wiki/bfwrepo/image/604a26e01dd36.png) no-repeat;
            background-size: cover;
        }


    </style>

</head>



<body>



    <script type="text/javascript" src="//repo.bfw.wiki/bfwrepo/js/jquery-3.2.1.min.js"></script>

    <script type="text/javascript">

        $(function() {

            var Fireworks = function() {

                var self = this;

                var rand = function(rMi, rMa) {
                    return ~~((Math.random()*(rMa-rMi+1))+rMi);
                }

                var hitTest = function(x1, y1, w1, h1, x2, y2, w2, h2) {
                    return !(x1 + w1 < x2 || x2 + w2 < x1 || y1 + h1 < y2 || y2 + h2 < y1);
                };

                window.requestAnimFrame = function() {
                    return window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || window.oRequestAnimationFrame || window.msRequestAnimationFrame || function(a) {
                        window.setTimeout(a, 1E3/60)}}();



                self.init = function() {

                    self.canvas = document.createElement('canvas');

                    self.canvas.width = self.cw = $(window).innerWidth();

                    self.canvas.height = self.ch = $(window).innerHeight();

                    self.particles = [];

                    self.partCount = 150;

                    self.fireworks = [];

                    self.mx = self.cw/2;

                    self.my = self.ch/2;

                    self.currentHue = 30;

                    self.partSpeed = 5;

                    self.partSpeedVariance = 10;

                    self.partWind = 50;

                    self.partFriction = 5;

                    self.partGravity = 1;

                    self.hueMin = 0;

                    self.hueMax = 360;

                    self.fworkSpeed = 4;

                    self.fworkAccel = 10;

                    self.hueVariance = 30;

                    self.flickerDensity = 25;

                    self.showShockwave = true;

                    self.showTarget = false;

                    self.clearAlpha = 25;



                    $(document.body).append(self.canvas);

                    self.ctx = self.canvas.getContext('2d');

                    self.ctx.lineCap = 'round';

                    self.ctx.lineJoin = 'round';

                    self.lineWidth = 1;

                    self.bindEvents();

                    self.canvasLoop();



                    self.canvas.onselectstart = function() {

                        return false;

                    };

                };



                self.createParticles = function(x, y, hue) {

                    var countdown = self.partCount;

                    while (countdown--) {

                        var newParticle = {

                            x: x,

                            y: y,

                            coordLast: [{
                                x: x,
                                y: y
                            },

                                {
                                    x: x,
                                    y: y
                                },

                                {
                                    x: x,
                                    y: y
                                }],

                            angle: rand(0, 360),

                            speed: rand(((self.partSpeed - self.partSpeedVariance) <= 0) ? 1: self.partSpeed - self.partSpeedVariance, (self.partSpeed + self.partSpeedVariance)),

                            friction: 1 - self.partFriction/100,

                            gravity: self.partGravity/2,

                            hue: rand(hue-self.hueVariance, hue+self.hueVariance),

                            brightness: rand(50, 80),

                            alpha: rand(40, 100)/100,

                            decay: rand(10, 50)/1000,

                            wind: (rand(0, self.partWind) - (self.partWind/2))/25,

                            lineWidth: self.lineWidth

                        };

                        self.particles.push(newParticle);

                    }

                };





                self.updateParticles = function() {

                    var i = self.particles.length;

                    while (i--) {

                        var p = self.particles[i];

                        var radians = p.angle * Math.PI / 180;

                        var vx = Math.cos(radians) * p.speed;

                        var vy = Math.sin(radians) * p.speed;

                        p.speed *= p.friction;



                        p.coordLast[2].x = p.coordLast[1].x;

                        p.coordLast[2].y = p.coordLast[1].y;

                        p.coordLast[1].x = p.coordLast[0].x;

                        p.coordLast[1].y = p.coordLast[0].y;

                        p.coordLast[0].x = p.x;

                        p.coordLast[0].y = p.y;



                        p.x += vx;

                        p.y += vy;

                        p.y += p.gravity;



                        p.angle += p.wind;

                        p.alpha -= p.decay;



                        if (!hitTest(0, 0, self.cw, self.ch, p.x-p.radius, p.y-p.radius, p.radius*2, p.radius*2) || p.alpha < .05) {

                            self.particles.splice(i, 1);

                        }

                    };

                };



                self.drawParticles = function() {

                    var i = self.particles.length;

                    while (i--) {

                        var p = self.particles[i];



                        var coordRand = (rand(1, 3)-1);

                        self.ctx.beginPath();

                        self.ctx.moveTo(Math.round(p.coordLast[coordRand].x), Math.round(p.coordLast[coordRand].y));

                        self.ctx.lineTo(Math.round(p.x), Math.round(p.y));

                        self.ctx.closePath();

                        self.ctx.strokeStyle = 'hsla('+p.hue+', 100%, '+p.brightness+'%, '+p.alpha+')';

                        self.ctx.stroke();



                        if (self.flickerDensity > 0) {

                            var inverseDensity = 50 - self.flickerDensity;

                            if (rand(0, inverseDensity) === inverseDensity) {

                                self.ctx.beginPath();

                                self.ctx.arc(Math.round(p.x), Math.round(p.y), rand(p.lineWidth, p.lineWidth+3)/2, 0, Math.PI*2, false)

                                self.ctx.closePath();

                                var randAlpha = rand(50, 100)/100;

                                self.ctx.fillStyle = 'hsla('+p.hue+', 100%, '+p.brightness+'%, '+randAlpha+')';

                                self.ctx.fill();

 .........完整代码请登录后点击上方下载按钮下载查看

网友评论0