Getting started
Felt in SVG
Sew your own SVG drawings — gauges, rings, chart bands — with the thread and felt of the components, taken from the same files.
View page source on GitHubOn this page
Example#
A gauge in a card. In the felt look its value is a felt patch sewn on with a seam down the middle and its track is pressed into the card; in the clean look both are plain strokes.
812 ppm
<div class="card" style="max-width: 18rem">
<div class="card-header">CO₂</div>
<div class="card-body">
<div class="position-relative mx-auto" style="width: 200px">
<svg class="d-block" width="200" height="110" viewBox="0 0 200 110" aria-hidden="true">
<defs>
<pattern id="felt-gauge" width="256" height="256" patternUnits="userSpaceOnUse">
<image href="../../../img/felt.webp" width="256" height="256"/>
</pattern>
<filter id="gauge-blur" x="-10%" y="-10%" width="120%" height="120%"><feGaussianBlur stdDeviation="1.5"/></filter>
</defs>
<g fill="none" stroke-width="16">
<path d="M24 96A76 76 0 0 1 176 96" style="stroke: var(--felt-track)"/>
<path d="M24 96A76 76 0 0 1 138 30.2" class="felt-shadow" filter="url(#gauge-blur)"/>
<path d="M24 96A76 76 0 0 1 138 30.2" class="felt-edge" style="color: var(--felt-primary)"/>
<path d="M24 96A76 76 0 0 1 138 30.2" style="stroke: var(--felt-primary)"/>
<path d="M24 96A76 76 0 0 1 138 30.2" stroke="url(#felt-gauge)" class="felt-texture"/>
</g>
</svg>
<svg class="stitches stitches-patch" viewBox="0 0 200 110" aria-hidden="true">
<g transform="translate(100 96)">
<use href="../../../img/stitch.svg#stitch" transform="rotate(-86.25) translate(0 -76)"/>
<use href="../../../img/stitch.svg#stitch" transform="rotate(-78.75) translate(0 -76)"/>
<use href="../../../img/stitch.svg#stitch" transform="rotate(-71.25) translate(0 -76)"/>
<use href="../../../img/stitch.svg#stitch" transform="rotate(-63.75) translate(0 -76)"/>
<use href="../../../img/stitch.svg#stitch" transform="rotate(-56.25) translate(0 -76)"/>
<use href="../../../img/stitch.svg#stitch" transform="rotate(-48.75) translate(0 -76)"/>
<use href="../../../img/stitch.svg#stitch" transform="rotate(-41.25) translate(0 -76)"/>
<use href="../../../img/stitch.svg#stitch" transform="rotate(-33.75) translate(0 -76)"/>
<use href="../../../img/stitch.svg#stitch" transform="rotate(-26.25) translate(0 -76)"/>
<use href="../../../img/stitch.svg#stitch" transform="rotate(-18.75) translate(0 -76)"/>
<use href="../../../img/stitch.svg#stitch" transform="rotate(-11.25) translate(0 -76)"/>
<use href="../../../img/stitch.svg#stitch" transform="rotate(-3.75) translate(0 -76)"/>
<use href="../../../img/stitch.svg#stitch" transform="rotate(3.75) translate(0 -76)"/>
<use href="../../../img/stitch.svg#stitch" transform="rotate(11.25) translate(0 -76)"/>
<use href="../../../img/stitch.svg#stitch" transform="rotate(18.75) translate(0 -76)"/>
<use href="../../../img/stitch.svg#stitch" transform="rotate(26.25) translate(0 -76)"/>
</g>
</svg>
</div>
<p class="text-center mb-0">812 ppm</p>
</div>
</div>Thread#
img/stitch.svg#stitch is one stitch of the seams, drawn along the x axis and centred on the origin. Place it with <use> and a transform; a seam is a row of them.
| Size | Value |
|---|---|
| Length | 7px, --felt-stitch-length |
| Pitch (stitch and gap) | 10px, --felt-stitch-pitch |
| Thickness | 1.6px |
Sizes are in user units: keep one unit at one CSS pixel (width equal to the viewBox width) and the stitches match the seams around them. On an arc of radius r the pitch is an angle of 10 / r radians; the gauge above has r = 76 and a stitch every 7.5°. Along any other path, measure it in script:
const seam = document.querySelector('#seam') // a path in the overlay: the line to sew along
const pitch = 10 // --felt-stitch-pitch
const length = seam.getTotalLength()
const count = Math.round(length / pitch)
for (let i = 0; i < count; i++) {
const at = (i + .5) * length / count
const p = seam.getPointAtLength(at), a = seam.getPointAtLength(at - .5), b = seam.getPointAtLength(at + .5)
const use = document.createElementNS('http://www.w3.org/2000/svg', 'use')
use.setAttribute('href', 'img/stitch.svg#stitch')
use.setAttribute('transform', `translate(${p.x} ${p.y}) rotate(${Math.atan2(b.y - a.y, b.x - a.x) * 180 / Math.PI})`)
seam.before(use)
}The stitch is near-white thread, like the seam images. Put the stitches in an <svg class="stitches"> of their own, laid over the drawing with the same viewBox inside a .position-relative wrapper. In the felt look the overlay casts the seams' shadow down the page (--felt-stitch-shadow) and is tinted with their --felt-thread-filter and --felt-thread-strength, so the thread is a tone of the felt underneath and follows the theme; add .stitches-patch when the stitches lie on a coloured patch, for the patches' lighter thread. In the clean look the overlay is hidden.
Felt#
Coloured shapes are patches: the grey felt img/felt.webp, tiled at --felt-texture-size (256px) and blended soft-light onto the colour. In SVG, define it as a pattern and lay a copy of the shape over the coloured one with .felt-texture, which blends it in the felt look and hides it in the clean look:
<pattern id="felt" width="256" height="256" patternUnits="userSpaceOnUse">
<image href="img/felt.webp" width="256" height="256"/>
</pattern>
<path d="…" class="felt-shadow" filter="url(#blur)"/>
<path d="…" class="felt-edge" style="color: var(--felt-primary)"/>
<path d="…" style="stroke: var(--felt-primary)"/>
<path d="…" stroke="url(#felt)" class="felt-texture"/>A patch lies on the felt with a cut edge and a soft shadow, as buttons and badges do: put copies of the shape under it, one with .felt-shadow (blurred by an SVG <filter> with feGaussianBlur) and one with .felt-edge, coloured with color. In the felt look they are drawn a little lower, the edge in a darker tone of that colour; in the clean look they are hidden.
The cream and charcoal sheet that the page and cards are cut from is a photo per theme (--felt-texture-sheet, --felt-texture-sheet-blend). Rather than painting it, leave such shapes transparent or shade them with --felt-track, like the gauge's track (the same groove as the empty part of progress bars and ranges), and the sheet underneath shows through. As a fill it is img/felt-light.webp blended multiply in light mode and img/felt-dark.webp blended soft-light in dark mode, both tiled at 256px.
Browsers#
- Keep the pattern inline. Safari ignores paint servers from another file (
fill="url(file.svg#id)"), and Chrome draws no images inside one. - Thread in its own
<svg>. Safari ignores CSSfilteron elements inside an SVG, so the tint sits on the overlay's<svg>element. - Same origin.
<use>loadsstitch.svgfrom the page's own origin only. The file has no internal references, which Safari doesn't resolve in a used file.