Link to home
Start Free TrialLog in
Avatar of RadhaKrishnaKiJaya
RadhaKrishnaKiJaya

asked on

Add confetti animation to a jquery dialog box

Hi Experts,

I have a pop up dialog in jquery. Is it possible to add some confetti animation effect when the pop up is displayed?

Thanks in advance!

<div id="WelcomeGiftpageModalMessage" style="background-color: #e8e8e8;">
    @Display(Model.WelcomeGiftPage)

</div>

Open in new window


<script type="text/javascript">
        jQuery(document).ready(function () {
            var ShowPopUp = jQuery('#ShowPopUp').val();
            if (ShowPopUp == 'TRUE') {
                jQuery("#WelcomeGiftpageModalMessage").dialog({
                    autoOpen: true,
                    resizable: false,
                    modal: true,
                    width: 600,
                    position: 'center',
                });
            }
           
        });

</script>

Open in new window

Avatar of David H.H.Lee
David H.H.Lee
Flag of Malaysia image

Is it possible to add some confetti animation effect when the pop up is displayed?
Possible. Try this confetti effect applied in your page.

//Add these JS and CSS
- Refer JS: https://github.com/CoderZ90/confetti/blob/main/confetti.js 
- Refer CSS: https://github.com/CoderZ90/confetti/blob/76ac0a6e72b8963b2cdd8e73a6960dca7d36fd48/style.css

Open in new window

<script src="confetti.js"></script>
<link rel="stylesheet" href="style.css">

Open in new window


Include following script after your WelcomeGiftpageModalMessage <div> tag.
<div id="WelcomeGiftpageModalMessage" style="background-color: #e8e8e8;">
    @Display(Model.WelcomeGiftPage)
</div>

Open in new window

<script>
// for starting the confetti 

const start = () => {
setTimeout(function() {confetti.start()}, 1000);};

//  for stopping the confetti 
const stop = () => {
setTimeout(function() {confetti.stop()}, 5000);};

start();
stop();

</script>

Open in new window


The complete code below:
<style>
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    font-family: 'Product Sans';
}

html,
body {
    width: 100%;
    height: 100%;
    background: #f1f1f1;
    display: flex;
    align-items: center;
    justify-content: center;
}

p {
    font-size: 4rem;
    text-shadow: 0 0 10px;
}
</style>

<script>
var confetti = {
   maxCount: 150,      //set max confetti count
   speed: 2,         //set the particle animation speed
   frameInterval: 15,   //the confetti animation frame interval in milliseconds
   alpha: 1.0,         //the alpha opacity of the confetti (between 0 and 1, where 1 is opaque and 0 is invisible)
   gradient: false,   //whether to use gradients for the confetti particles
   start: null,      //call to start confetti animation (with optional timeout in milliseconds, and optional min and max random confetti count)
   stop: null,         //call to stop adding confetti
   toggle: null,      //call to start or stop the confetti animation depending on whether it's already running
   pause: null,      //call to freeze confetti animation
   resume: null,      //call to unfreeze confetti animation
   togglePause: null,   //call to toggle whether the confetti animation is paused
   remove: null,      //call to stop the confetti animation and remove all confetti immediately
   isPaused: null,      //call and returns true or false depending on whether the confetti animation is paused
   isRunning: null      //call and returns true or false depending on whether the animation is running
};

(function() {
   confetti.start = startConfetti;
   confetti.stop = stopConfetti;
   confetti.toggle = toggleConfetti;
   confetti.pause = pauseConfetti;
   confetti.resume = resumeConfetti;
   confetti.togglePause = toggleConfettiPause;
   confetti.isPaused = isConfettiPaused;
   confetti.remove = removeConfetti;
   confetti.isRunning = isConfettiRunning;
   var supportsAnimationFrame = window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || window.oRequestAnimationFrame || window.msRequestAnimationFrame;
   var colors = ["rgba(30,144,255,", "rgba(107,142,35,", "rgba(255,215,0,", "rgba(255,192,203,", "rgba(106,90,205,", "rgba(173,216,230,", "rgba(238,130,238,", "rgba(152,251,152,", "rgba(70,130,180,", "rgba(244,164,96,", "rgba(210,105,30,", "rgba(220,20,60,"];
   var streamingConfetti = false;
   var animationTimer = null;
   var pause = false;
   var lastFrameTime = Date.now();
   var particles = [];
   var waveAngle = 0;
   var context = null;

   function resetParticle(particle, width, height) {
      particle.color = colors[(Math.random() * colors.length) | 0] + (confetti.alpha + ")");
      particle.color2 = colors[(Math.random() * colors.length) | 0] + (confetti.alpha + ")");
      particle.x = Math.random() * width;
      particle.y = Math.random() * height - height;
      particle.diameter = Math.random() * 10 + 5;
      particle.tilt = Math.random() * 10 - 10;
      particle.tiltAngleIncrement = Math.random() * 0.07 + 0.05;
      particle.tiltAngle = Math.random() * Math.PI;
      return particle;
   }

   function toggleConfettiPause() {
      if (pause)
         resumeConfetti();
      else
         pauseConfetti();
   }

   function isConfettiPaused() {
      return pause;
   }

   function pauseConfetti() {
      pause = true;
   }

   function resumeConfetti() {
      pause = false;
      runAnimation();
   }

   function runAnimation() {
      if (pause)
         return;
      else if (particles.length === 0) {
         context.clearRect(0, 0, window.innerWidth, window.innerHeight);
         animationTimer = null;
      } else {
         var now = Date.now();
         var delta = now - lastFrameTime;
         if (!supportsAnimationFrame || delta > confetti.frameInterval) {
            context.clearRect(0, 0, window.innerWidth, window.innerHeight);
            updateParticles();
            drawParticles(context);
            lastFrameTime = now - (delta % confetti.frameInterval);
         }
         animationTimer = requestAnimationFrame(runAnimation);
      }
   }

   function startConfetti(timeout, min, max) {
      var width = window.innerWidth;
      var height = window.innerHeight;
      window.requestAnimationFrame = (function() {
         return window.requestAnimationFrame ||
            window.webkitRequestAnimationFrame ||
            window.mozRequestAnimationFrame ||
            window.oRequestAnimationFrame ||
            window.msRequestAnimationFrame ||
            function (callback) {
               return window.setTimeout(callback, confetti.frameInterval);
            };
      })();
      var canvas = document.getElementById("confetti-canvas");
      if (canvas === null) {
         canvas = document.createElement("canvas");
         canvas.setAttribute("id", "confetti-canvas");
         canvas.setAttribute("style", "display:block;z-index:999999;pointer-events:none;position:fixed;top:0");
         document.body.prepend(canvas);
         canvas.width = width;
         canvas.height = height;
         window.addEventListener("resize", function() {
            canvas.width = window.innerWidth;
            canvas.height = window.innerHeight;
         }, true);
         context = canvas.getContext("2d");
      } else if (context === null)
         context = canvas.getContext("2d");
      var count = confetti.maxCount;
      if (min) {
         if (max) {
            if (min == max)
               count = particles.length + max;
            else {
               if (min > max) {
                  var temp = min;
                  min = max;
                  max = temp;
               }
               count = particles.length + ((Math.random() * (max - min) + min) | 0);
            }
         } else
            count = particles.length + min;
      } else if (max)
         count = particles.length + max;
      while (particles.length < count)
         particles.push(resetParticle({}, width, height));
      streamingConfetti = true;
      pause = false;
      runAnimation();
      if (timeout) {
         window.setTimeout(stopConfetti, timeout);
      }
   }

   function stopConfetti() {
      streamingConfetti = false;
   }

   function removeConfetti() {
      stop();
      pause = false;
      particles = [];
   }

   function toggleConfetti() {
      if (streamingConfetti)
         stopConfetti();
      else
         startConfetti();
   }
   
   function isConfettiRunning() {
      return streamingConfetti;
   }

   function drawParticles(context) {
      var particle;
      var x, y, x2, y2;
      for (var i = 0; i < particles.length; i++) {
         particle = particles[i];
         context.beginPath();
         context.lineWidth = particle.diameter;
         x2 = particle.x + particle.tilt;
         x = x2 + particle.diameter / 2;
         y2 = particle.y + particle.tilt + particle.diameter / 2;
         if (confetti.gradient) {
            var gradient = context.createLinearGradient(x, particle.y, x2, y2);
            gradient.addColorStop("0", particle.color);
            gradient.addColorStop("1.0", particle.color2);
            context.strokeStyle = gradient;
         } else
            context.strokeStyle = particle.color;
         context.moveTo(x, particle.y);
         context.lineTo(x2, y2);
         context.stroke();
      }
   }

   function updateParticles() {
      var width = window.innerWidth;
      var height = window.innerHeight;
      var particle;
      waveAngle += 0.01;
      for (var i = 0; i < particles.length; i++) {
         particle = particles[i];
         if (!streamingConfetti && particle.y < -15)
            particle.y = height + 100;
         else {
            particle.tiltAngle += particle.tiltAngleIncrement;
            particle.x += Math.sin(waveAngle) - 0.5;
            particle.y += (Math.cos(waveAngle) + particle.diameter + confetti.speed) * 0.5;
            particle.tilt = Math.sin(particle.tiltAngle) * 15;
         }
         if (particle.x > width + 20 || particle.x < -20 || particle.y > height) {
            if (streamingConfetti && particles.length <= confetti.maxCount)
               resetParticle(particle, width, height);
            else {
               particles.splice(i, 1);
               i--;
            }
         }
      }
   }
})();
</script>


<script>
// for starting the confetti 

const start = () => {
setTimeout(function() {confetti.start()}, 1000);};

//  for stopping the confetti 
const stop = () => {
setTimeout(function() {confetti.stop()}, 5000);};

start();
stop();

</script>

Open in new window

Avatar of RadhaKrishnaKiJaya
RadhaKrishnaKiJaya

ASKER

Thank you so much for trying to help me. The code is working. But the confetti is falling all over the page. Instead how can I just add to the popup window? Thank you again!
User generated image
Noted. In this case, tweak your code to call out this confetti effect via dialog window.
<script type="text/javascript">

 jQuery(document).ready(function () {
   var ShowPopUp = jQuery('#ShowPopUp').val();
   if (ShowPopUp == 'TRUE') {

   //call confetti effect
   ConfettiPopup();

   jQuery("#WelcomeGiftpageModalMessage").dialog({
                    autoOpen: true,
                    resizable: false,
                    modal: true,
                    width: 600,
                    position: 'center',
       });
    }
           
});

function ConfettiPopup()
{
  // for starting the confetti 

  const start = () => {
  setTimeout(function() {confetti.start()}, 1000);};

  //  for stopping the confetti 
  const stop = () => {
  setTimeout(function() {confetti.stop()}, 5000);};

  start();
  stop();
}

</script>




Open in new window

Still it is happening in the whole page instead of only in the pop up. Please suggest. I really appriciate your time and help!
User generated image

ASKER CERTIFIED SOLUTION
Avatar of David H.H.Lee
David H.H.Lee
Flag of Malaysia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I am sorry, but it is still the same.

Thank you!
Can I know where's current this <div> reside in your current environment?
<div id="WelcomeGiftpageModalMessage" style="background-color: #e8e8e8;">
@Display(Model.WelcomeGiftPage)</div>

Open in new window

It is in the index.cshtml file. Thank you!
@using Draftfcb.PurchasEdge.Member.ViewModels
@model MemberWelcomeGiftPageViewModel
@{
    Script.Require("jQueryUI_Dialog").AtFoot();
    Style.Require("jQueryUI_Orchard").AtFoot();
    Script.Require("Confetti").AtFoot();
    
}

<div id="WelcomeGiftpageModalMessage" style="background-color: #e8e8e8;">
    @Display(Model.WelcomeGiftPage)
</div>

Open in new window


Noted. Set dialog's autoOpen: false

Furthermore, can I know in what condition the value will be changed to TRUE in ShowPopUp control and call out the jQuery dialog?

var ShowPopUp = jQuery('#ShowPopUp').val();

Open in new window

   if (ShowPopUp == 'TRUE') {

Open in new window

jQuery("#WelcomeGiftpageModalMessage").dialog({
        autoOpen: false,

Open in new window

Unfortunately, when I make autoOpen: false, it does not pop up the dialog at all. Also for testing purpose I removed all the conditions. So It displays the dialog on document ready.

Thank you so much for your time!
Can i know how you assign the "TRUE" value for this control?
 var ShowPopUp = jQuery('#ShowPopUp').val(); 

Open in new window


Furthermore, can you share all the related code for further inspection anything keep trigger the dialog popup?
Actually the code is very simple.
if (Model.WelcomeGiftPage != null)  
        {
            
            <div id="WelcomeGiftpageModalMessage" style="background-color: #e8e8e8;">
                @Display(Model.WelcomeGiftPage)
            </div>

            @Html.Hidden("ShowPopUp", "TRUE")

Open in new window

Also, it is not possible for me to share all the code because of policy. Is it possible for you to make a sample program in fiddle and share with me? That would be helpful.

Thank you so much!

Hi RadhaKrishnaKiJaya,

No need to share all the code here... Now only left out the challenge where the confetti effect as given only need to show at dialog box call out as mentioned in your previous post, but now somehow show in all the page: https://www.experts-exchange.com/questions/29240892/Add-confetti-animation-to-a-jquery-dialog-box.html#a43430920 

So, the focus now is find out where you call out the confetti effect code will resolve the entire hassle, right?

Based on your one of the past comment at: https://www.experts-exchange.com/questions/29240892/Add-confetti-animation-to-a-jquery-dialog-box.html#a43431030 , can i get your confirmation where your dialog box will appear at footer, that could be the reason it will keep trigger and show in all pages?

@using Draftfcb.PurchasEdge.Member.ViewModels
@model MemberWelcomeGiftPageViewModel
@{
    Script.Require("jQueryUI_Dialog").AtFoot();
    Style.Require("jQueryUI_Orchard").AtFoot();
    Script.Require("Confetti").AtFoot();//call confetti function at footer?
    
}

<div id="WelcomeGiftpageModalMessage" style="background-color: #e8e8e8;">
    @Display(Model.WelcomeGiftPage)
</div>

Open in new window





May be you are right. But I am not sure where to add the Confetti .js? Any suggestion?

Thanks a lot!
You can place the confetti script tag at end of the <body> section in the page that you want to show the confetti effect only.
Hi,

by default the css of the script you are using will display the confetti to the body so you need to adjust the CSS to display to a specific div in your popup / specific container
*To my opinion it is ok to display on the whole page body.

I would use another confetti script which has a css container, different confetti design and also have promise so you have more control on it.
Code https://github.com/loonywizard/js-confetti/tree/main/site_dist
Options https://github.com/loonywizard/js-confetti
Demo https://loonywizard.github.io/js-confetti/



Thank you so much Mr. David for your help! Right now I think it is ok if it shows on the whole page!
Thank you lenamtl  for your opinion!