Google's animated fireworks, like those you sometimes see on special occasions (e.g., New Year's Eve, anniversaries, or celebrations), are created using web technologies such as JavaScript, HTML5 Canvas, and sometimes SVG along with CSS animations.
User
give me an example with complete code
Assistant
Here’s a high-level overview of how Google’s fireworks animation typically works:
HTML5 Canvas Element:
The animation is usually rendered inside an HTML5 <canvas> element, which provides a drawable region on the web page where JavaScript can dynamically render graphics pixel by pixel.
JavaScript Animation Loop:
A JavaScript animation loop (often using requestAnimationFrame) continuously updates the canvas by clearing the old frame and drawing the next frame to produce smooth animations.
Particle System:
Fireworks animations are implemented as a particle system:
Launcher particles simulate the firework shooting up.
Explosion particles simulate the burst of sparks when the firework explodes.
Each particle has properties like position, velocity, color, size, opacity, and lifespan.
Physics Simulation:
Basic physics, such as gravity, velocity decay, friction, and sometimes wind, are applied to particles to make their motion appear realistic.
Color and Light Effects:
Colors are defined per particle or burst, often with gradients or glow effects. Opacity changes over the particle’s lifespan to simulate fading sparks.
Timing and Randomization:
Fireworks are triggered at random intervals and positions on the screen for variety. The lifespan and explosion size might also vary.
Sound (sometimes):
On some occasions, sounds of fireworks may be played in sync with the visuals using the Web Audio API.
Example Simplified Flow:
Create a canvas and get its drawing context.
Define a Firework class with methods to launch and explode.
Create multiple Firework instances randomly over time.
In each animation frame:
Clear the canvas.
Update the position and state of each firework particle.
Draw particles to the canvas.
Remove particles after their lifespan ends.
Summary
Google’s fireworks use a combination of canvas drawing, JavaScript animation loops, and particle physics to simulate explosions and sparkles. The effect results in a visually engaging, smooth animation that celebrates events.
If you want, I can help provide an example code snippet for a simple fireworks animation on canvas!
Certainly! Below is a complete example of a simple fireworks animation using HTML5 Canvas and JavaScript. You can copy this into an .html file and open it in your browser to see fireworks in action.
Fireworks are launched from random horizontal positions near the bottom of the screen.
Each firework moves upward, slowing down, and then explodes into many colorful particles.
Particles simulate sparks by moving outward in all directions, affected by gravity and fading away.
The background is painted every frame with a semi-transparent black fill (rgba(0,0,0,0.15)), creating a trailing effect.
New fireworks are launched every ~500ms.
Feel free to tweak parameters like particle count, speed, gravity, decay, and colors to customize the effect! Let me know if you want explanations of any parts or want to add sound or interactivity.