Canvas Confetti
Recently, I found myself needing to rebuild a confetti effect, the old one used css and had broken at some point during a stack upgrade. I decided it would be most efficient to use canvas over DOM elements so I fired up my trusty canvas2D template and got to work. I wanted implementation to be rapid so I decided to stick with square and rectangular confetti pieces. First we declare some variables, grabbing the canvas element and the 2D context. The confetti variable is an array of confetti objects which we will be mutating, the rest are constants to control the confetti count, global physics, and colors.
Next we populate the confetti array with objects. We choose a random color, dimension, and rotation. The rotation is a random number between 0 and 360 degrees but the canvas rotate method uses radians so I choose a random number between 0 and 2 * Math.PI. The position starts 1px above the bottom of the canvas since later we destroy confetti which hits the bottom. The scale is 1 in both axes because we multiply the dimensions by the scale. Last we set a random velocity which sends the confetti shooting upward when we render. You'll notice I use the randomRange method a lot, it's a helper I created to give me a random number from a negative or positive range.
Now we need to implement all the fun stuff in the render method. I want to make the confetti explode upward as if shot by a cannon, then float downward slowly. We start by clearing the canvas, then open a foreach to render each confetto. The canvas context has a translate and rotate method but the both work relative to the top left corner. In order to render each rectangles rotation we move the entire canvas to the confetto's position, then rotate the canvas.
Next we process the velocity of the confetto, horizontal momentum is dampened over time and randomized to simulate the breeze pushing the confetti around. Vertical momentum is increased by adding gravity until terminal velocity is reached. Now we set the new position of the piece by adding velocity to the position. If a piece is lower than the bottom of the screen, it is removed from the array to improve performance. If a confetto floats out of the screen horizontally it is looped to the other side.
At this point the confetti would float but it would never look like it was spinning in the wind. I simulate this by modifying the confetto's scale in the Y axis using Math.cos()
on the confetto's Y position. The end result is that the scale alternates between -1 and 1 as its Y position changes, making the spin relative to the piece's vertical movement speed. To add depth we darken the confetto's color when it's scale is negative.
Last, we draw the rectangle with fillRect()
then reset the context's transform matrix which resets the translation and rotation. The only thing left is to fire off another round of confetti when the count gets below 10 and call the animation frame again.
You can check out the codepen version here: Confetti on codepen.