NikolayBezay

Confety example

Mar 23rd, 2021 (edited)
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 16.92 KB | None | 0 0
  1. <canvas id="CONFETTI_CANVAS" style="position:fixed;top:0;left:0;display:block;z-index:2147483646;"></canvas>
  2.  
  3. <script>
  4. function CUSTOM_CONFETTI_CANVAS(e, t, a, i) {
  5.     this.canvas = document.getElementById(e),
  6.     this.W = this.canvas.width = parseInt(t),
  7.     this.H = this.canvas.height = parseInt(a),
  8.     this.ctx = this.canvas.getContext("2d"),
  9.     this.ctx.width = t,
  10.     this.ctx.height = a,
  11.     this.particles = [],
  12.     this.angle = 0,
  13.     this.effectActive = !0,
  14.     this.animationComplete = !0,
  15.     this.animationHandler,
  16.     this.config = i,
  17.     this.init = function() {
  18.         switch (this.config.effect_type) {
  19.         case "Confetti":
  20.             void 0 === this.config.mp && (this.config.mp = Math.floor(this.W / 7)),
  21.            this.Particle = this.confettiParticle;
  22.             break;
  23.         case "Hearts":
  24.             void 0 === this.config.mp && (this.config.mp = Math.floor(this.W / 24)),
  25.            this.Particle = this.HeartParticle;
  26.             break;
  27.         case "Snow":
  28.             void 0 === this.config.mp && (this.config.mp = Math.floor(this.W / 10)),
  29.            this.Particle = this.snowflakesParticle;
  30.             break;
  31.         case "Balloons":
  32.             void 0 === this.config.mp && (this.config.mp = Math.floor(this.W / 24)),
  33.            this.Particle = this.BalloonParticle;
  34.             break;
  35.         case "Stars":
  36.             void 0 === this.config.mp && (this.config.mp = 200),
  37.            this.Particle = this.StarParticle;
  38.             break;
  39.         case "Custom Image":
  40.             void 0 === this.config.mp && (this.config.mp = 50),
  41.            this.Particle = this.ImageParticle
  42.        }
  43.    }
  44.    ;
  45.     var n = {
  46.         colorRGB: ["rgb(30, 144, 255)", "rgb(152,251,152)", "rgb(255, 215, 0)", "rgb(255, 192, 203)", "Thistle", "rgb(173, 216, 230)", "rgb(238, 130, 238)", "rgb(152, 251, 152)", "rgb(70, 130, 180)", "rgb(244, 164, 96)", "rgb(210, 105, 30)", "rgb(220, 20, 60)"],
  47.         secondColor: ["DeepSkyBlue", "OliveDrab", "PaleGoldenRod", "HotPink", "MediumSlateBlue", "LightCyan", "MediumVioletRed", "PaleTurquoise", "Teal", "Salmon", "Coral", "DeepPink"],
  48.         colorIndex: 0,
  49.         getColor: function(e) {
  50.             return "Snow" == e ? "rgb(255, 255, 255)" : "Hearts" == e ? "rgb(255,100,100)" : "Stars" == e ? "rgb(255, 255, 200)" : (this.colorIndex++,
  51.             this.colorIndex >= this.colorRGB.length && (this.colorIndex = 0),
  52.            this.colorRGB[this.colorIndex])
  53.        },
  54.        getSecondColor: function(e) {
  55.            var t = this.colorRGB.indexOf(e);
  56.             return this.secondColor[t]
  57.         }
  58.     };
  59.     function r(e, t) {
  60.         return Math.floor(Math.random() * (t - e + 1) + e)
  61.     }
  62.     this.start = function() {
  63.         this.particles = [],
  64.         this.animationComplete = !1;
  65.         for (var e = 0; e < this.config.mp; e++) {
  66.            this.config.color = n.getColor(this.config.effect_type);
  67.            var t = Object.assign(!0, {}, this.config);
  68.            this.particles.push(new this.Particle(this.W,this.H,this.ctx,t))
  69.        }
  70.        this.StartEffect()
  71.    }
  72.    ,
  73.    this.confettiParticle = function(e, t, a, i) {
  74.        this.x = Math.random() * e,
  75.        this.y = Math.random() * t - t,
  76.        this.r = r(10, 30),
  77.        this.d = Math.random() * i.mp + 10,
  78.        this.tilt = Math.floor(10 * Math.random()) - 10,
  79.        this.tiltAngleIncremental = .07 * Math.random() + .05,
  80.        this.tiltAngle = 0,
  81.        this.config = i,
  82.        this.ctx = a,
  83.        this.draw = function() {
  84.            this.ctx.beginPath(),
  85.            this.ctx.lineWidth = this.r / 2,
  86.            this.ctx.strokeStyle = this.config.color,
  87.            this.ctx.moveTo(this.x + this.tilt + this.r / 4, this.y),
  88.            this.ctx.lineTo(this.x + this.tilt, this.y + this.tilt + this.r / 4),
  89.            this.ctx.stroke()
  90.        }
  91.    }
  92.    ,
  93.    this.snowflakesParticle = function(e, t, a, i) {
  94.        this.x = Math.random() * e,
  95.        this.y = Math.random() * t - t,
  96.        this.r = 4 * Math.random() + 1,
  97.        this.d = Math.random() * i.mp,
  98.        this.config = i,
  99.        this.ctx = a,
  100.        this.draw = function() {
  101.            this.ctx.beginPath(),
  102.            this.ctx.fillStyle = this.config.color,
  103.            this.ctx.moveTo(this.x, this.y),
  104.            this.ctx.arc(this.x, this.y, this.r, 0, 2 * Math.PI, !0),
  105.            this.ctx.fill()
  106.        }
  107.    }
  108.    ,
  109.    this.HeartParticle = function(e, t, a, i) {
  110.        this.x = Math.random() * e,
  111.        this.y = Math.random() * t - t,
  112.        this.ys = Math.random() + 2,
  113.        this.minScale = .4,
  114.        this.opacity = Math.random() * (1 - this.minScale) + this.minScale,
  115.        this.config = i,
  116.        this.ctx = a,
  117.        this.draw = function() {
  118.            this.ctx.save(),
  119.            this.ctx.translate(this.x, this.y),
  120.            this.ctx.rotate(4),
  121.            this.ctx.fillStyle = this.config.color.replace("rgb", "rgba").replace(")", "," + this.opacity + ")"),
  122.            this.ctx.beginPath(),
  123.            this.ctx.moveTo(-8, 0),
  124.            this.ctx.arc(0, 0, 8, 0, Math.PI, !1),
  125.            this.ctx.lineTo(8, 0),
  126.            this.ctx.arc(8, -8, 8, 90 * Math.PI / 180, 270 * Math.PI / 180, !0),
  127.            this.ctx.lineTo(8, -16),
  128.            this.ctx.lineTo(-8, -16),
  129.            this.ctx.lineTo(-8, 0),
  130.            this.ctx.fill(),
  131.            this.ctx.closePath(),
  132.            this.ctx.restore()
  133.        }
  134.    }
  135.    ,
  136.    this.StarParticle = function(e, t, a, i) {
  137.        this.x = Math.random() * e,
  138.        this.y = Math.random() * t,
  139.        this.length = r(5, 9),
  140.        this.opacity = Math.random(),
  141.        this.factor = 1,
  142.        this.increment = .03 * Math.random(),
  143.        this.config = i,
  144.        this.ctx = a,
  145.        this.W = e,
  146.        this.H = t,
  147.        this.draw = function() {
  148.            this.ctx.save(),
  149.            this.ctx.translate(this.x, this.y),
  150.            this.ctx.beginPath();
  151.            for (var e = 5; e--; )
  152.                this.ctx.lineTo(0, this.length),
  153.                this.ctx.translate(0, this.length),
  154.                this.ctx.rotate(2 * Math.PI / 10),
  155.                this.ctx.lineTo(0, -this.length),
  156.                this.ctx.translate(0, -this.length),
  157.                this.ctx.rotate(-6 * Math.PI / 10);
  158.            this.ctx.lineTo(0, this.length),
  159.            this.ctx.closePath(),
  160.            this.ctx.fillStyle = "rgba(255, 255, 200, " + this.opacity + ")",
  161.            this.ctx.shadowBlur = 5,
  162.            this.ctx.shadowColor = "#ffff33",
  163.            this.ctx.fill(),
  164.            this.ctx.restore()
  165.        }
  166.    }
  167.    ,
  168.    this.ImageParticle = function(e, t, a, i) {
  169.        this.x = Math.random() * e,
  170.        this.y = Math.random() * t,
  171.        this.ys = Math.random() + 1;
  172.        var n = .6 * Math.random() + .4;
  173.        this.img_height = n * i.img_h,
  174.        this.img_width = n * i.img_w,
  175.        this.opacity = n,
  176.        this.image = new Image,
  177.        this.image.style.height = i.img_h,
  178.        this.image.src = i.img_url,
  179.        this.ctx = a,
  180.        this.config = i,
  181.        this.draw = function() {
  182.            this.ctx.globalAlpha = this.opacity,
  183.            this.ctx.drawImage(this.image, this.x, this.y, this.img_width, this.img_height)
  184.        }
  185.    }
  186.    ,
  187.    this.BalloonParticle = function(e, t, a, i) {
  188.        var k = {};
  189.        k.KAPPA = 4 * (Math.sqrt(2) - 1) / 3,
  190.        k.WIDTH_FACTOR = .0333,
  191.        k.HEIGHT_FACTOR = .4,
  192.        k.TIE_WIDTH_FACTOR = .12,
  193.        k.TIE_HEIGHT_FACTOR = .1,
  194.        k.TIE_CURVE_FACTOR = .13,
  195.        k.GRADIENT_FACTOR = .3,
  196.        k.GRADIENT_CIRCLE_RADIUS = 3,
  197.        this.centerX = Math.random() * e,
  198.        this.centerY = Math.random() * t,
  199.        this.radius = r(20, 30),
  200.        this.secondColor = n.getSecondColor(i.color),
  201.        this.config = i,
  202.        this.ctx = a,
  203.        this.draw = function() {
  204.            var e = this.centerX
  205.              , t = this.centerY
  206.              , a = this.radius
  207.              , i = k.KAPPA * a
  208.              , n = a * k.WIDTH_FACTOR
  209.              , r = a * k.HEIGHT_FACTOR
  210.              , o = t + a + r;
  211.            this.ctx.beginPath();
  212.            var s = e - a
  213.              , _ = t
  214.              , p = e
  215.              , l = t - a;
  216.            this.ctx.moveTo(s, _),
  217.            this.ctx.bezierCurveTo(s, _ - i - n, p - i, l, p, l);
  218.            var c = e
  219.              , u = t - a
  220.              , d = e + a
  221.              , m = t;
  222.            this.ctx.bezierCurveTo(c + i + n, u, d, m - i, d, m);
  223.            var g = e + a
  224.              , f = t
  225.              , h = e
  226.              , y = o;
  227.            this.ctx.bezierCurveTo(g, f + i, h + i, y, h, y);
  228.            var v = e
  229.              , A = o
  230.              , b = e - a
  231.              , C = t;
  232.            this.ctx.bezierCurveTo(v - i, A, b, C + i, b, C);
  233.            var Z = a / 3
  234.              , z = this.ctx.createRadialGradient(e + Z, t - Z, k.GRADIENT_CIRCLE_RADIUS, e, t, a + r);
  235.            z.addColorStop(0, this.config.color),
  236.            z.addColorStop(.7, this.secondColor),
  237.            this.ctx.fillStyle = z,
  238.            this.ctx.fill();
  239.            var w = a * k.TIE_WIDTH_FACTOR / 2
  240.              , x = a * k.TIE_HEIGHT_FACTOR
  241.              , S = a * k.TIE_CURVE_FACTOR;
  242.            this.ctx.beginPath(),
  243.            this.ctx.moveTo(e - 1, o),
  244.            this.ctx.lineTo(e - w, o + x),
  245.            this.ctx.quadraticCurveTo(e, o + S, e + w, o + x),
  246.            this.ctx.lineTo(e + 1, o),
  247.            this.ctx.fill()
  248.        }
  249.    }
  250.    ,
  251.    this.Draw = function() {
  252.        this.ctx.clearRect(0, 0, this.W, this.H);
  253.        for (var e = 0; e < this.config.mp; e++)
  254.            this.particles[e].draw();
  255.        switch (this.config.effect_type) {
  256.        case "Confetti":
  257.            this.UpdateConfetti();
  258.            break;
  259.        case "Snow":
  260.            this.UpdateSnow();
  261.            break;
  262.        case "Balloons":
  263.            this.UpdateBalloons();
  264.            break;
  265.        case "Hearts":
  266.            this.UpdateHearts();
  267.            break;
  268.        case "Stars":
  269.            this.UpdateStars();
  270.            break;
  271.        case "Custom Image":
  272.            this.UpdateImage()
  273.        }
  274.    }
  275.    ,
  276.    this.UpdateSnow = function() {
  277.        var e = 0;
  278.        this.angle += .01;
  279.        for (var t = 0; t < this.config.mp; t++) {
  280.            var a = this.particles[t];
  281.            if (this.animationComplete)
  282.                return;
  283.            a.y += Math.cos(this.angle + a.d) + 1 + a.r / 2,
  284.            a.x += 2 * Math.sin(this.angle),
  285.            (a.x > this.W + 5 || a.x < -5 || a.y > this.H) && this.effectActive && (this.particles[t].d = (this.particles[t].r = (this.particles[t].y = 0 < t % 3 ? (this.particles[t].x = Math.random() * this.W,
  286.            -10) : (0 < Math.sin(this.angle) ? this.particles[t].x = -5 : this.particles[t].x = this.W + 5,
  287.            Math.random() * this.H),
  288.            a.r),
  289.            a.d)),
  290.            this.effectActive || (this.particles[t].y += 5),
  291.            this.particles[t].y <= this.H && e++
  292.        }
  293.        0 === e && this.StopEffect()
  294.    }
  295.    ,
  296.    this.UpdateBalloons = function() {
  297.        for (var e, t = 0, a = 0; a < this.config.mp; a++) {
  298.            if (e = this.particles[a],
  299.            this.animationComplete)
  300.                return;
  301.            e.centerX += r(-1, 1),
  302.            this.effectActive ? e.centerY -= r(2, 3) : e.centerY -= r(7, 8),
  303.            e.centerY < -40 && this.effectActive && (e.centerY = this.H + 30),
  304.            0 <= e.centerY && t++
  305.        }
  306.        0 === t && this.StopEffect()
  307.    }
  308.    ,
  309.    this.UpdateImage = function() {
  310.        for (var e, t = 0, a = 0; a < this.config.mp; a++) {
  311.            if (e = this.particles[a],
  312.            this.animationComplete)
  313.                return;
  314.            "Top - Down" == this.config.effect_dir ? (e.y += e.ys,
  315.            e.y > this.H && this.effectActive && (e.x = Math.random() * this.W,
  316.            e.y = -1 * e.img_height),
  317.            this.effectActive || (e.y += 7),
  318.            e.y <= this.H && t++) : "Down - Top" == this.config.effect_dir ? (e.y -= e.ys,
  319.            e.y < 0 && this.effectActive && (e.x = Math.random() * this.W,
  320.            e.y = this.H + e.img_height),
  321.            this.effectActive || (e.y -= 7),
  322.            0 <= e.y && t++) : "Right - Left" == this.config.effect_dir ? (e.x -= e.ys,
  323.            e.x < 0 && this.effectActive && (e.y = Math.random() * this.H,
  324.            e.x = this.W + e.img_width),
  325.            this.effectActive || (e.x -= 14),
  326.            0 <= e.x && t++) : "Left - Right" == this.config.effect_dir && (e.x += e.ys,
  327.            e.x > this.W && this.effectActive && (e.y = Math.random() * this.H,
  328.            e.x = -1 * e.img_width),
  329.            this.effectActive || (e.x += 14),
  330.            e.x <= this.W && t++)
  331.        }
  332.        0 === t && this.StopEffect()
  333.    }
  334.    ,
  335.    this.UpdateStars = function() {
  336.        for (var e, t = 0; t < this.config.mp; t++) {
  337.            if (e = this.particles[t],
  338.            !this.effectActive)
  339.                return void this.StopEffect();
  340.            1 < e.opacity ? e.factor = -1 : e.opacity <= .1 && (e.factor = 1,
  341.            e.x = Math.random() * this.W,
  342.            e.y = Math.random() * this.H),
  343.            e.opacity += e.increment * e.factor
  344.        }
  345.    }
  346.    ,
  347.    this.UpdateHearts = function() {
  348.        for (var e, t = 0, a = 0; a < this.config.mp; a++) {
  349.            if (e = this.particles[a],
  350.            this.animationComplete)
  351.                return;
  352.            e.y += e.ys,
  353.            e.y > this.H && this.effectActive && (e.x = Math.random() * this.W,
  354.            e.y = -10),
  355.            this.effectActive || (e.y += 7),
  356.            e.y <= this.H && t++
  357.        }
  358.        0 === t && this.StopEffect()
  359.    }
  360.    ,
  361.    this.UpdateConfetti = function() {
  362.        var e, t = 0;
  363.         this.angle += .01,
  364.         this.tiltAngle += .1;
  365.         for (var a = 0; a < this.config.mp; a++) {
  366.            if (e = this.particles[a],
  367.            this.animationComplete)
  368.                return;
  369.            !this.effectActive && e.y < -15 ? e.y = this.H + 100 : (this.stepParticle(e, a),
  370.            this.effectActive || (e.y += 10),
  371.            e.y <= this.H && t++,
  372.            this.CheckForReposition(e, a))
  373.        }
  374.        0 === t && this.StopEffect()
  375.    }
  376.    ,
  377.    this.CheckForReposition = function(e, t) {
  378.        (e.x > this.W + 20 || e.x < -20 || e.y > this.H) && this.effectActive && (0 < t % 5 || t % 2 == 0 ? this.repositionParticle(e, Math.random() * this.W, -10, Math.floor(10 * Math.random()) - 10) : 0 < Math.sin(this.angle) ? this.repositionParticle(e, -5, Math.random() * this.H, Math.floor(10 * Math.random()) - 10) : this.repositionParticle(e, this.W + 5, Math.random() * this.H, Math.floor(10 * Math.random()) - 10))
  379.    }
  380.    ,
  381.    this.stepParticle = function(e, t) {
  382.        e.tiltAngle += e.tiltAngleIncremental,
  383.        e.y += (Math.cos(this.angle + e.d) + 3 + e.r / 2) / 2,
  384.        e.x += Math.sin(this.angle),
  385.        e.tilt = 15 * Math.sin(e.tiltAngle - t / 3)
  386.    }
  387.    ,
  388.    this.repositionParticle = function(e, t, a, i) {
  389.        e.x = t,
  390.        e.y = a,
  391.        e.tilt = i
  392.    }
  393.    ,
  394.    this.StartEffect = function() {
  395.        if (this.animationComplete)
  396.            return null;
  397.         this.Draw();
  398.         var t, e = this;
  399.         try {
  400.             switch (this.config.speed) {
  401.             case "1":
  402.                 t = 90;
  403.                 break;
  404.             case "2":
  405.                 t = 70;
  406.                 break;
  407.             case "3":
  408.                 t = 50;
  409.                 break;
  410.             case "4":
  411.                 t = 30;
  412.                 break;
  413.             case "5":
  414.                 t = 10;
  415.                 break;
  416.             default:
  417.                 t = 33
  418.             }
  419.         } catch (e) {
  420.             t = 33
  421.         }
  422.         setTimeout(function() {
  423.             e.StartEffect()
  424.         }, t)
  425.     }
  426.     ,
  427.     this.ClearTimers = function() {
  428.         clearTimeout(this.animationHandler)
  429.     }
  430.     ,
  431.     this.stop = function(e) {
  432.         "true" == this.config.effect_close_remove && this.StopEffect(),
  433.        e && (this.animationComplete = !0),
  434.        this.effectActive = !1,
  435.        this.ClearTimers()
  436.    }
  437.    ,
  438.    this.StopEffect = function() {
  439.        this.animationComplete = !0,
  440.        void 0 !== this.ctx && (this.ctx.clearRect(0, 0, this.W, this.H),
  441.        $(this.canvas).remove())
  442.    }
  443.    ,
  444.    window.requestAnimFrame = function() {
  445.        return window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || window.oRequestAnimationFrame || window.msRequestAnimationFrame || function(e) {
  446.            return window.setTimeout(e, 1e3 / 60)
  447.        }
  448.    }
  449. }
  450.  
  451. var f = window.innerWidth;
  452. var h = window.innerHeight;
  453.  
  454. var y = {
  455.     effect_type: 'Confetti',
  456.     mp: 50,
  457.     speed: '1'
  458. };
  459.  
  460. var v = new CUSTOM_CONFETTI_CANVAS("CONFETTI_CANVAS",f,h,y);
  461. v.init();
  462. v.start();
  463. </script>
Add Comment
Please, Sign In to add comment