How it works

Edges are where neighbours disagree

An edge is a place where brightness changes quickly. A convolution kernel measures exactly that, one pixel neighbourhood at a time, and SVG has a primitive for it: feConvolveMatrix. A few more primitives turn the measurement into pencil lines, glowing outlines or a raised relief.

1. Convolution: feConvolveMatrix

<feConvolveMatrix> slides a small grid of weights, the kernel, over the image. Each output pixel is the weighted sum of the pixels under the grid, divided by divisor and shifted by bias. The kernels shown in the control panel all sum to 0, so a flat area gives 0 and only changes in brightness produce a response.

Sobel
two kernels: Gx compares the column right of a pixel with the one left of it, Gy the row below with the row above. The centre row/column counts double, which smooths a little across the edge. Each responds to edges in one direction; their magnitudes added together find edges in every direction.
Laplacian
one kernel: the pixel against all its neighbours (a second derivative). It finds every direction at once and catches finer detail, but draws a line on both sides of an edge and reacts more to noise. The 5×5 version is a Laplacian of Gaussian, which smooths first.
Emboss
one kernel weighted by each tap's offset along the light direction. It reads brightness as height and computes the slope towards the light, like Lambert shading: slopes that face the light come out bright, the others dark.

Everything runs on luminance: an <feColorMatrix> first writes the Rec. 709 brightness (the weights the duotone page explains) into all three channels and forces alpha to 1. Transparent areas then count as black, so a transparent icon's silhouette is an edge too. Pre-blur adds an <feGaussianBlur> before it, so texture and noise don't become lines.

Luminance misses edges between colors of the same brightness: a saturated red and a mid blue can be almost the same grey. Detect on: color skips the luminance step and lets the kernels run on R, G and B separately (they always do; on a grey image the three are just equal), then adds up the three magnitudes with half weight each in one more <feColorMatrix>. Emboss always works on luminance, since it reads brightness as height.

2. Keeping the negative half: bias and an absolute value

A gradient kernel is negative on one side of an edge and positive on the other, but filter results are clamped to 0–1, so the negative half would simply vanish. The fix is bias="0.5": flat areas come out mid-grey, dark-to-light edges above it, light-to-dark edges below. The divisor is chosen so that a full black-to-white step moves the result by ±0.5 × strength (for Sobel 3×3 the step response is 4, so the divisor is 8 / strength).

An <feComponentTransfer> table with the values 1 0 1 then folds that back into a magnitude: the table interpolates linearly from 1 at 0 to 0 at 0.5 and back to 1 at 1, which is exactly |2v − 1|. For Sobel, an <feComposite operator="arithmetic" k2="1" k3="1"> adds the two magnitudes, |Gx| + |Gy|, a cheap stand-in for the Euclidean length that filters can't compute.

3. From edge map to drawing

  • Threshold is a linear transfer function, (v − t) / (1 − t): weak edges drop to 0, the rest is stretched back to full range.
  • Line weight dilates the lines with <feMorphology> (the same primitive as the sticker outline).
  • Pencil grain multiplies the lines with stretched <feTurbulence> noise: lines · (1 − a + a · noise) as one arithmetic composite. Noise at low x and high y frequency gives long horizontal streaks, like graphite dragged across paper.
  • A two-value table maps the result onto two colors. Sketch maps no edge to paper and strong edges to ink; edges mode is the same drawing inverted, paper-colored lines on an ink ground. Emboss maps the relief from ink (facing away) to paper (facing the light).
  • Original color blends the drawing back onto the source with <feBlend>: multiply for the sketch (colored pencil), screen for the glowing edges, and hard-light with the raw grey relief for emboss, which brightens and darkens the original around mid-grey and leaves flat areas alone.
<svg width="0" height="0" aria-hidden="true">
  <filter id="edges" x="0" y="0" width="100%" height="100%"
          color-interpolation-filters="sRGB">
    <feGaussianBlur stdDeviation="0.5" />
    <!-- luminance into R, G and B, alpha forced to 1 -->
    <feColorMatrix type="matrix" result="luma" values="
      0.2126 0.7152 0.0722 0 0  0.2126 0.7152 0.0722 0 0
      0.2126 0.7152 0.0722 0 0  0 0 0 0 1" />
    <!-- Gx: signed response around 0.5, folded to |2v − 1| -->
    <feConvolveMatrix in="luma" order="3" divisor="3.2" bias="0.5"
                      edgeMode="duplicate" preserveAlpha="true"
                      kernelMatrix="1 0 -1  2 0 -2  1 0 -1" />
    <feComponentTransfer result="g0">
      <feFuncR type="table" tableValues="1 0 1" />
      <feFuncG type="table" tableValues="1 0 1" />
      <feFuncB type="table" tableValues="1 0 1" />
    </feComponentTransfer>
    <!-- Gy: signed response around 0.5, folded to |2v − 1| -->
    <feConvolveMatrix in="luma" order="3" divisor="3.2" bias="0.5"
                      edgeMode="duplicate" preserveAlpha="true"
                      kernelMatrix="1 2 1  0 0 0  -1 -2 -1" />
    <feComponentTransfer result="g1">
      <feFuncR type="table" tableValues="1 0 1" />
      <feFuncG type="table" tableValues="1 0 1" />
      <feFuncB type="table" tableValues="1 0 1" />
    </feComponentTransfer>
    <feComposite in="g0" in2="g1" operator="arithmetic" k2="1" k3="1" />
    <!-- threshold -->
    <feComponentTransfer result="clean">
      <feFuncR type="linear" slope="1.1111" intercept="-0.1111" />
      <feFuncG type="linear" slope="1.1111" intercept="-0.1111" />
      <feFuncB type="linear" slope="1.1111" intercept="-0.1111" />
    </feComponentTransfer>
    <!-- pencil grain: lines · (1 − a + a · noise) -->
    <feTurbulence type="fractalNoise" baseFrequency="0.04 0.7" numOctaves="2" seed="7" />
    <feColorMatrix type="matrix" result="grain" values="
      3 0 0 0 -1  3 0 0 0 -1  3 0 0 0 -1  0 0 0 0 1" />
    <feComposite in="clean" in2="grain" operator="arithmetic"
                 k1="0.6" k2="0.4" result="lines" />
    <!-- edge strength onto two colors -->
    <feComponentTransfer in="lines" result="mapped">
      <feFuncR type="table" tableValues="0.9529 0.1686" />
      <feFuncG type="table" tableValues="0.9294 0.149" />
      <feFuncB type="table" tableValues="0.8824 0.1333" />
    </feComponentTransfer>
  </filter>
</svg>
.drawing {
  filter: url(#edges);
}

color-interpolation-filters="sRGB" is set on purpose. Filters compute in linearRGB by default, and in linear light the same difference in brightness is a much smaller number in the shadows than in the highlights, so dark edges would come out faint. edgeMode="duplicate" repeats the border pixels, so the element's own frame isn't detected as an edge.

Browser support and performance

SVG filters on HTML, including feConvolveMatrix, have been Baseline widely available since July 2015 (web-features, svg-filters). Checked in Chromium and Firefox; Safari is untested.

  • A kernel costs order² multiply-adds per pixel and channel: 9 for 3×3, 25 for 5×5. Sobel runs two kernels. That is cheap for a static image, which is filtered once and cached, but a 5×5 Sobel over an animated full-screen element adds up quickly.
  • Without kernelUnitLength the kernel steps one pixel of the browser's offscreen image, which on a high-DPI screen is a device pixel: lines are as thin as the screen allows, and the same settings look finer at 2× than at 1×.
  • The output is opaque and replaces the content, so put the filter on images and illustrations, not on text that has to be read. filter: grayscale(1) contrast(2) is a rough fallback where SVG filters are unavailable.

Back to the demo ↑