# Confetti effect

**URL:** <https://phaser.discourse.group/t/confetti-effect/9943>\
**Category:** Phaser 3\
**Created:** [July 13, 2021, 2:05pm UTC](https://phaser.discourse.group/t/confetti-effect/9943 "2021-07-13T14:05:50Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![marcusx2](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/marcusx2/32/5259_2.png) [@marcusx2](https://phaser.discourse.group/u/marcusx2)\
**Post date:** [July 13, 2021, 2:05pm UTC](https://phaser.discourse.group/t/confetti-effect/9943/1 "2021-07-13T14:05:50Z")

</div>

Guys I need to do this confetti effect in Phaser 3 [https://codepen.io/cerpow/pen/RwNaeOb](https://codepen.io/cerpow/pen/RwNaeOb)

Can you guys give me some pointers on how I can do it?

If there’s a way to just use the exact same code but make it draw over the Phaser3’s scene, even better.

---

<div class="post-metadata">

**Author:** ![Milton](https://avatars.discourse-cdn.com/v4/letter/m/87869e/32.png) [@Milton](https://phaser.discourse.group/u/Milton)\
**Post date:** [July 13, 2021, 4:25pm UTC](https://phaser.discourse.group/t/confetti-effect/9943/2 "2021-07-13T16:25:32Z")

</div>

[A quick and dirty version.](http://placeholder.byethost4.com/confetti/?i=1)

Press ‘c’ to start confetti.

---

<div class="post-metadata">

**Author:** ![marcusx2](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/marcusx2/32/5259_2.png) [@marcusx2](https://phaser.discourse.group/u/marcusx2)\
**Post date:** [July 13, 2021, 6:11pm UTC](https://phaser.discourse.group/t/confetti-effect/9943/3 "2021-07-13T18:11:03Z")

</div>

Erm…nothing happens

---

<div class="post-metadata">

**Author:** ![Milton](https://avatars.discourse-cdn.com/v4/letter/m/87869e/32.png) [@Milton](https://phaser.discourse.group/u/Milton)\
**Post date:** [July 13, 2021, 6:17pm UTC](https://phaser.discourse.group/t/confetti-effect/9943/4 "2021-07-13T18:17:43Z")

</div>

Strange. Works for me with Chrome/FF on Linux/Windows.  
What platform do you use, and what does console say?

---

<div class="post-metadata">

**Author:** ![marcusx2](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/marcusx2/32/5259_2.png) [@marcusx2](https://phaser.discourse.group/u/marcusx2)\
**Post date:** [July 13, 2021, 6:19pm UTC](https://phaser.discourse.group/t/confetti-effect/9943/5 "2021-07-13T18:19:19Z")

</div>

Nothing happens. Nothing on the console. Just an anime pic.

---

<div class="post-metadata">

**Author:** ![Milton](https://avatars.discourse-cdn.com/v4/letter/m/87869e/32.png) [@Milton](https://phaser.discourse.group/u/Milton)\
**Post date:** [July 13, 2021, 6:20pm UTC](https://phaser.discourse.group/t/confetti-effect/9943/6 "2021-07-13T18:20:05Z")

</div>

And you pressed ‘c’ at least once (after making sure you have focus in the window)?

---

<div class="post-metadata">

**Author:** ![marcusx2](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/marcusx2/32/5259_2.png) [@marcusx2](https://phaser.discourse.group/u/marcusx2)\
**Post date:** [July 13, 2021, 6:25pm UTC](https://phaser.discourse.group/t/confetti-effect/9943/7 "2021-07-13T18:25:04Z")

</div>

Oh sorry I missed the part that I needed to press ‘c’. Yes, it worked, thanks. What did you do? It seems like it’s overlaying the phaser3 scene. You are not using “transparent: true” in the config either.

---

<div class="post-metadata">

**Author:** ![Milton](https://avatars.discourse-cdn.com/v4/letter/m/87869e/32.png) [@Milton](https://phaser.discourse.group/u/Milton)\
**Post date:** [July 13, 2021, 6:30pm UTC](https://phaser.discourse.group/t/confetti-effect/9943/8 "2021-07-13T18:30:56Z")

</div>

I did nothing really, just took out a bit of jQuery. You should be able to diff it with the original, hardly any changes. The confetti div container is added/removed every time you press ‘c’, so it goes on top (high z-index).

---

<div class="post-metadata">

**Author:** ![BdR](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/bdr/32/2621_2.png) [@BdR](https://phaser.discourse.group/u/BdR)\
**Post date:** [June 23, 2024, 2:17pm UTC](https://phaser.discourse.group/t/confetti-effect/9943/9 "2024-06-23T14:17:06Z")

</div>

For anyone looking for a falling confetti effect in Phaser3, you can use the particle emitter and rotate and scale the particles. You can do this by adding functions to the `scaleX` and `rotate`.properties. Btw you can either choose scaleX or scaleY doesn’t matter, the idea is that it makes the confetti’s look like they’re flipping over and over.

Copy&paste the code below into the [Phaser3 Sandbox](https://labs.phaser.io/edit.html) to see the effect

```javascript
class Example extends Phaser.Scene
{
    preload ()
    {
        this.load.spritesheet('raster', 'assets/demoscene/sunset-raster.png', { frameWidth: 16, frameHeight: 16 });
    }

    create ()
    {
        this.add.particles(0, 0, 'raster', {
            speed: 100,
            lifespan: 5000,
            gravityY: 100,
            frame: [0, 4, 8, 12, 16],
            x: { min: 0, max: 800 },
            scaleX: {
                onEmit: (particle) => {
                    return -1.0
                },
                onUpdate: (particle) => {
                    return (particle.scaleX > 1.0 ? -1.0 : particle.scaleX + 0.05)
                }
            },
            rotate: {
                onEmit: (particle) => {
                    return 0
                },
                onUpdate: (particle) => {
                    return particle.angle + 1
                }
            }
        });
    }
}

const config = {
    type: Phaser.AUTO,
    width: 800,
    height: 600,
    backgroundColor: '#000',
    parent: 'phaser-example',
    scene: Example
};

const game = new Phaser.Game(config);

```

The only down-side to this code is that the animatioin depends on the monitor refresh rate. Meaning, the particles will flip and rotate faster on a device with a 144hz monitor as compared to a device with a 60hz display.

---

<div class="post-metadata">

**Author:** ![samme](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/samme/32/5275_2.png) [@samme](https://phaser.discourse.group/u/samme)\
**Post date:** [June 23, 2024, 3:34pm UTC](https://phaser.discourse.group/t/confetti-effect/9943/10 "2024-06-23T15:34:10Z")

</div>

Here’s a way to time it, using [lifeT](https://newdocs.phaser.io/docs/3.80.0/focus/Phaser.GameObjects.Particles.Particle-lifeT).

https://codepen.io/samme/embed/preview/YzbLYXq?default-tabs=js%2Cresult&height=300&host=https%3A%2F%2Fcodepen.io&slug-hash=YzbLYXq

```js
const TAU = 2 * Math.PI;

this.add.particles(0, 0, 'raster', {
  speed: 100,
  lifespan: 5000,
  gravityY: 100,
  frame: [0, 4, 8, 12, 16],
  x: { min: 0, max: 800 },
  scaleX: {
    onEmit: (particle) => {
      return 1;
    },
    onUpdate: (particle) => {
      // 5 cycles per lifespan
      return Math.cos(TAU * 5 * particle.lifeT);
    }
  },
  rotate: {
    onEmit: (particle) => {
      return 0;
    },
    onUpdate: (particle) => {
      // 1 cycle per lifespan
      return 1 * 360 * particle.lifeT;
    }
  }
});

```

---

<div class="post-metadata">

**Author:** ![BdR](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/bdr/32/2621_2.png) [@BdR](https://phaser.discourse.group/u/BdR)\
**Post date:** [June 23, 2024, 9:26pm UTC](https://phaser.discourse.group/t/confetti-effect/9943/11 "2024-06-23T21:26:55Z")

</div>

@samme You are right that fixes the rotate and flip animations 👍 very cool code update, thank you. Btw the `onEmit` function can be removed with the new `onUpdate` code, because the properties don’t need to be initialised.

But I just noticed something else besides the animation. I can switch my laptop monitor between 144hz and 60hz, and when I run the test code in Phaser3 Sandbox with 144hz there are noticably more particles onscreen when compared to 60hz 🤔 Must be something with the particle emitter…

---

<div class="post-metadata">

**Author:** ![samme](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/samme/32/5275_2.png) [@samme](https://phaser.discourse.group/u/samme)\
**Post date:** [June 23, 2024, 9:38pm UTC](https://phaser.discourse.group/t/confetti-effect/9943/12 "2024-06-23T21:38:20Z")

</div>

The default particle emitter frequency is `0` (1 burst per frame).

You can change the frequency to `1000 / 60` (60 bursts per second).

---

<div class="post-metadata">

**Author:** ![samme](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/samme/32/5275_2.png) [@samme](https://phaser.discourse.group/u/samme)\
**Post date:** [July 26, 2024, 4:14pm UTC](https://phaser.discourse.group/t/confetti-effect/9943/13 "2024-07-26T16:14:04Z")

</div>

#### Windy leaves

[https://codepen.io/samme/pen/MWMjdVR](https://codepen.io/samme/pen/MWMjdVR)
