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:

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