How to Animate SVG Liquid and Wave Effects
Simulating liquid and wave dynamics with Scalable Vector Graphics (SVG) relies on manipulating vector points and utilizing visual distortion filters. By altering path data dynamically or applying mathematical functions to control points, developers can create realistic fluid movement, surface tension, and continuous wave undulations. This guide breaks down the core techniques used to achieve liquid animations in SVG, ranging from purely mathematical approaches to CSS animations and specialized SVG filter effects.
1. Trigonometric Path Manipulation with JavaScript
Liquid movement often follows harmonic motion, which can be modeled
mathematically using sine and cosine functions. By generating the
d attribute of an SVG <path> dynamically
on each animation frame (requestAnimationFrame), you can
create natural waves.
- Sine Wave Generation: Calculate vertical offsets
for a series of cubic Bézier anchor points along an X-axis using
y = amplitude * Math.sin(frequency * x + phase). - Dynamic Control Points: Adjust the control handles
of cubic (
C) or quadratic (Q) Bézier commands in proportion to the slope to keep curves smooth and organic. - Continuous Flow: Increment the
phasevariable over time to simulate a rolling, infinite wave.
2. SVG Path Morphing
Path morphing transitions one vector shape into another by interpolating between sets of coordinates. This method is ideal for predetermined liquid behaviors, such as water sloshing in a container or a droplet changing shape as it falls.
- Point Count Matching: For smooth interpolation, both the starting and ending paths should ideally have the same number and sequence of coordinate commands.
- JavaScript Libraries: Libraries like GSAP with the MorphSVGPlugin or Flubber handle complex morphing between paths with mismatched point counts, ensuring the liquid does not twist unnaturally during transitions.
- CSS
d: path()Transition: Modern browsers support interpolating thedproperty directly via CSS transitions or@keyframes, provided the path segment types match exactly.
3. The SVG “Gooey” Filter Effect
To simulate surface tension and the merging or separation of liquid droplets, SVG filter primitives can be combined to blur and contrast elements together.
- Gaussian Blur
(
<feGaussianBlur>): Applied to group elements (<g>) to create a soft, overlapping halo between adjacent shapes. - Color Matrix (
<feColorMatrix>): Used to increase the alpha contrast drastically, sharpening the blurred edges. Where two blurred shapes meet, their overlapping alpha values exceed the threshold, fusing them into a single fluid shape. - Blend (
<feBlend>or<feComposite>): Layering the original sharp content over the filtered liquid ensures clean rendering while preserving the fluid connection between moving elements.
4. Seamless Repeating Wave Translation
For continuous background waves, you can combine a static SVG wave shape with linear CSS translation.
- Seamless Wave Design: Draw a wave pattern in an SVG path where the ending Y-coordinate and tangent match the starting points precisely.
- Duplicate Path: Place two identical wave shapes side-by-side within a single SVG view or container.
- CSS Infinite Loop: Apply a CSS animation that
translates the wave horizontally by 50% (the width of one wave cycle)
using
transform: translateX(-50%)and loops infinitely with a linear timing function. - Layering: Stack multiple wave paths with varying opacity, scales, and animation durations to add visual depth and complexity.
5. Spring and Particle Physics Integration
For interactive fluids—such as water reacting to mouse movement or simulated buoyancy—vector paths can be connected to physics-driven spring networks.
- Node Columns: Treat the liquid surface as an array of vertical springs (nodes) connected to adjacent springs via Hooke’s law.
- Tension and Damping: When an external force disturbs a node, the velocity propagates to neighboring nodes, creating realistic splashes and ripples.
- Real-Time Path Construction: Read the heights of all spring nodes on every frame and generate a smooth curve using cardinal splines or cubic Bézier interpolation to render the final SVG path.