Understanding SVG filterUnits and Bounding Boxes
The filterUnits attribute in SVG defines the coordinate
system used to calculate the filter region—the bounding box where visual
effects are rendered. By setting filterUnits to either
objectBoundingBox or userSpaceOnUse,
developers control whether the filter’s boundaries scale dynamically
with the referenced element or remain fixed to the overall SVG
coordinate space. Choosing the correct mode ensures that effects such as
blurs, shadows, and glows render fully without unexpected clipping or
performance overhead.
The Filter Region
Every SVG <filter> element defines a filter region
using the x, y, width, and
height attributes. This rectangular area acts as the canvas
and hard clipping boundary for the filter operations. The
filterUnits attribute determines how the numerical values
assigned to these boundaries are interpreted during rendering.
objectBoundingBox
(Default)
When filterUnits="objectBoundingBox" is set (or omitted,
as it is the default), the filter region is calculated relative to the
target element’s bounding box.
- Value Interpretation: Values are expressed as
fractions (from
0.0to1.0) or percentages (0%to100%). - Default Dimensions: The standard default values are
x="-10%",y="-10%",width="120%", andheight="120%". This 10% overflow margin provides extra room around the element for spreading effects like drop shadows or blurs. - Behavior: The filter bounding box automatically resizes and moves whenever the target element changes dimensions or position.
This mode is ideal for reusable filters designed to fit shapes of various sizes without manual recalculation.
userSpaceOnUse
When filterUnits="userSpaceOnUse" is specified, the
filter region is calculated within the current SVG user coordinate
system (the coordinate system established by the
<svg> or <g> element).
- Value Interpretation: Values represent absolute units (such as pixels) mapped directly to the SVG viewport.
- Behavior: The filter boundaries remain fixed in the SVG canvas space, completely independent of the dimensions or position of the element applying the filter.
This mode is useful when applying consistent, coordinate-aligned effects across multiple elements simultaneously, or when working with fixed-size canvas regions.
Impact on Rendering and Clipping
The bounding box calculated by filterUnits acts as a
strict rendering boundary:
- Clipping: If a filter operation (like a high-radius
blur) spreads beyond the bounds calculated via
filterUnits, the excess pixels are hard-clipped at the boundary edge. - Performance: A filter region that is unnecessarily large increases memory usage and rendering computation, as the graphics engine must calculate pixels for the entire area.
Difference
Between filterUnits and primitiveUnits
While filterUnits defines the outer boundary for the
overall <filter> container, the
primitiveUnits attribute controls the coordinate system for
the individual filter primitive subregions inside it (such as
<feOffset> or <feGaussianBlur>).
Both work together to manage the spatial boundaries and detail levels of
SVG graphical effects.