SVG crispEdges: Rendering Crisp Straight Lines
The shape-rendering="crispEdges" SVG attribute alters
the rendering of vector graphics by disabling anti-aliasing and forcing
the rendering engine to align geometric edges with the display’s pixel
grid. When applied to straight lines, it eliminates the blurriness,
fuzziness, and color bleeding commonly caused by subpixel rendering,
ensuring that lines appear razor-sharp and distinctly defined at their
exact intended stroke width.
The Default Rendering Problem
By default, SVG renderers use anti-aliasing (often under
shape-rendering="auto" or
shape-rendering="geometricPrecision") to smooth out shapes.
When a straight horizontal or vertical 1-pixel line is placed on integer
coordinates (such as x="10" with a
stroke-width="1"), the stroke centers on the coordinate.
This means the stroke spans from 9.5 to 10.5 pixels.
Because a physical screen cannot illuminate a fraction of a pixel, the rendering engine splits the stroke across two adjacent pixel columns, coloring each at 50% opacity. The result is a 2-pixel wide, blurry line instead of a single crisp 1-pixel line.
How
crispEdges Changes Straight Line Rendering
Setting shape-rendering="crispEdges" directly modifies
how the rasterizer processes coordinates and colors:
- Disables Anti-Aliasing: The rasterizer stops blending colors across fractional pixel boundaries. A pixel is either fully filled with the stroke color or left unchanged, removing all intermediate semi-transparent shades.
- Forces Pixel Snapping: The browser adjusts line coordinates to align precisely with whole physical device pixels. Even if coordinates or stroke widths fall on fractional boundaries, the engine rounds or snaps the geometry to fit the pixel grid.
- Guarantees Exact Stroke Widths: A 1-pixel line is guaranteed to occupy exactly one physical pixel row or column, maintaining high contrast and true line thickness.
Effect on Different Line Orientations
Horizontal and Vertical Lines
Horizontal and vertical lines benefit the most from
crispEdges. Without anti-aliasing, these lines render with
clean, distinct boundaries. UI dividers, bar charts, and coordinate axes
appear sharp and consistent across different screen sizes and scaling
levels.
Diagonal Lines
For straight lines drawn at an angle, crispEdges removes
the smooth gradient transitions along the line’s path. As a consequence,
diagonal straight lines will exhibit a noticeable “staircase” or “jaggy”
appearance, as each step of the line snaps to rigid pixel blocks without
smoothing.
Practical Use Cases
- Data Visualizations: Chart axes, tick marks, and grid lines that require clean, non-blurry borders.
- UI Elements: Dividers, icons, and bounding boxes where precise, flat lines are preferred over smoothed edges.
- Pixel-Style Artwork: Vector art designed to emulate retro, grid-aligned, or pixelated aesthetics.