SVG radialGradient: Defining Color Spreads

The SVG <radialGradient> element defines circular and elliptical color transitions by radiating color stops outward from a central point to an outer perimeter. It allows developers to create depth, lighting effects, and textured backgrounds in vector graphics by controlling geometric boundaries, focal origins, coordinate mapping systems, and color distribution stops.

Core Geometry Attributes

A standard radial gradient is defined by an outer circle using three primary attributes:

The gradient spreads colors smoothly from the starting point outward until it reaches the edge defined by the radius r.

Setting the Focal Point

While cx and cy define the outer boundary, the SVG specification allows the origin of the color spread to be shifted independently using focal coordinates:

Moving fx and fy away from cx and cy creates an asymmetrical lighting or 3D spherical effect, directing the perceived light source toward the focal coordinate.

Defining Circular vs. Elliptical Spreads

The <radialGradient> element inherently defines a circular transition, but its rendered shape depends on the coordinate system:

  1. Object Bounding Box (gradientUnits="objectBoundingBox"): This is the default setting. The percentages are mapped relative to the bounding box of the target shape. If applied to a non-square rectangle, the gradient naturally stretches to match the aspect ratio, forming an ellipse.
  2. User Space Coordinates (gradientUnits="userSpaceOnUse"): Coordinates are mapped to the absolute SVG viewport coordinate system. In this mode, the gradient remains a true geometric circle regardless of the target shape’s dimensions.
  3. Using gradientTransform: To create a precise elliptical spread in any coordinate system, the gradientTransform attribute can apply scale, skew, or rotate transformations directly to the gradient definition.

Defining Color Transitions with Stops

Color distribution along the gradient radius is controlled by child <stop> elements:

The rendering engine interpolates colors linearly along the distance vector from the focal circle (fx, fy, fr) to the outer boundary circle (cx, cy, r).

Handling Overflow with Spread Methods

When the gradient does not fill the entire target geometry, the spreadMethod attribute determines how the area outside the boundary circle is rendered: