What is a mesh gradient?
Published
A mesh gradient blends color across a grid. Each corner of the grid has a color, and inside each cell the color blends smoothly between its corners. Because every corner can be different, a mesh can describe color that changes in several directions at once: the glow on a cheek, a sky that is warm near the sun and cool overhead, the shading of a folded cloth.
Linear, radial and mesh
- A linear gradient blends along a straight line: every point on a line across it has the same color.
- A radial gradient blends outward from a point: every point on a circle around it has the same color.
- A mesh gradient has no such rule. Its color at any point comes from the corners of the cell it falls in, so it can follow any shape of shading.
To get a similar result with linear and radial gradients alone, an illustrator stacks many shapes, each with its own gradient, and blurs or blends them together. A mesh does it in one.
Drafted for SVG 2, drawn by no browser
Mesh gradients were written into the drafts of SVG 2: a mesh made of patches with four curved edges, known as Coons patches, and a color at each corner. By the SVG 2 Candidate Recommendation of September 2016, they had a section of their own, under the element name <mesh>.
Browsers never implemented them. In May 2018 the SVG Working Group removed mesh gradients from SVG 2, together with hatches, as features at risk of having no implementations, and the change deferred them to a future version of SVG. The current SVG 2 specification has no mesh gradients; its list of changes records both their arrival and their deferral.
Inkscape implemented the drafts and has created mesh gradients since version 0.92, writing them as <meshgradient>. Its own documentation warns that browsers may not support them, and suggests replacing a mesh with a bitmap for the web: open such a file in a browser and the meshed shapes show up empty.
Adobe Illustrator has its own gradient mesh, but since SVG has no equivalent, it saves the mesh into SVG as an embedded bitmap image, not as a mesh: zoom in and it pixelates.
XSVG: mesh gradients that work everywhere
XSVG, short for eXtensible SVG, is a format from Vision Cortex, the team behind VTracer. It adds elements SVG does not have, mesh gradients among them, in a namespace of their own, so an ordinary viewer simply skips them and the file never breaks. Compiling an XSVG file turns every extension into plain SVG.
XSVG implements the SVG 2 mesh: it reads the <meshgradient> that Inkscape writes, curved Coons patches and all, as well as its own simpler x:mesh, a grid of corner colors. See the XSVG mesh gradient documentation.
A mesh compiles to a tiny image, often just 2 × 2 pixels, sized and placed so that the viewer's own smooth image scaling recreates the blend, and clipped to the shape's outline, so edges stay sharp at any zoom. The result draws in every browser and design app.
Mesh gradients in VTracer 2
Turn on Mesh Gradient in the tracing options, and VTracer 2 can describe a smoothly shaded area as one mesh instead of many bands or gradient shapes. That suits exactly the artwork VTracer 1 struggled with: painted illustration, AI-generated art, anything with soft shading.
Take the blush on a cheek. In this trace of an anime illustration, the whole face, blush included, is one mesh: a grid of 16 × 16 patches with a color at each corner. The blush is just a few warmer corners on each cheek, and the viewer blends them into a soft glow. Compiled to plain SVG, the entire face mesh is one PNG of 17 × 17 pixels, one pixel per corner: 952 bytes.
Mesh gradients matter most on shading that turns in every direction, such as a soft 3D character. Here is one traced at the same gradient step, first without and then with mesh gradients:
- Save as SVG writes plain SVG, with each mesh compiled, so the file opens anywhere.
- Save as XSVG, from the menu below Save as SVG, keeps the meshes as XSVG elements, with the background and foreground as separate layers of an artboard.
In a design app, a compiled mesh is an image clipped to a shape: it looks right at any size, but its colors are edited as an image, not as points.