Patterns
Patterns draw a repeated motif over the background, such as dots, grid lines, stars or rays. This page documents every pattern, its options and how patterns stack with other layers.
Overview
xeerpe ships four patterns: dots, grid, stars and rays. The first three are drawn as lightweight SVG tiles, while rays is a repeating conic gradient that radiates from a point. Every option is optional.
import { Builder } from 'xeerpe'
const style = new Builder()
.linearGradient({
from: '#0f172a',
to: '#1e293b',
})
.dots({
color: '#e2e8f0',
opacity: 0.25,
})
.toStyle()Patterns are drawn above gradients and below effects. When you add several patterns, the one you add first is drawn on top.
Lengths such as size, spacing and strokeWidth must be a number followed by a unit, for example '24px' or '2rem'. Use the same unit for all the lengths of a pattern, because they are measured against each other.
Note: unlike colors, an invalid length is not replaced by a fallback. A value that cannot be parsed throws an
Invalid CSS lengtherror.
Shared options
These options work the same way in every pattern.
colorstring'#000000'The color of the dots, lines, stars or rays. The pattern opacity is applied to hex, rgb() and hsl() colors. Any other format, such as a named color or rgba(), is used as it is and ignores opacity. For 8-digit hex colors, the alpha channel of the color wins over opacity.
backgroundstring'transparent'The color painted behind the pattern in every tile. Leave it transparent to see the gradient below. An opaque color covers every layer under the pattern.
backgroundSizestring'auto'A CSS background-size value for the pattern layer. With auto, the tile keeps the size it has by default.
Dots
A regular grid of round dots. The distance between dots comes from spacing, and the dot radius comes from size.
Options
sizestring'20px'The base size of the dots. The dot radius is 15% of this value, so '20px' gives a radius of 3px. When spacing is not set, size is also the distance between the centers of the dots.
spacingstringThe distance between the centers of neighboring dots, which is the size of the repeating tile. It defaults to the value of size.
strokeWidthstringThe dot radius, which overrides the radius calculated from size. Only the number is used, measured in the same unit as spacing.
opacitynumber0.2Opacity of the dots, from 0 to 1. See color for the color formats it applies to.
Examples
Polka dots
With the defaults, size controls both the dots and their spacing.
new Builder()
.linearGradient({
from: '#0f172a',
to: '#1e293b',
})
.dots({
color: '#e2e8f0',
opacity: 0.25,
})
.toStyle()Spacing
Set spacing larger than size to get small dots far apart, like a field of stars.
new Builder()
.linearGradient({
from: '#020617',
to: '#1e1b4b',
})
.dots({
color: '#ffffff',
size: '14px',
spacing: '30px',
opacity: 0.45,
})
.toStyle()Dot radius
Use strokeWidth to set the radius directly. Here the dots are bigger than the 15% that size would give.
new Builder()
.linearGradient({
from: '#fb7185',
to: '#f59e0b',
})
.dots({
color: '#ffffff',
size: '40px',
strokeWidth: '5px',
opacity: 0.35,
})
.toStyle()Tile background
Set background to an opaque color to make the pattern a standalone surface. It replaces everything under the pattern.
new Builder()
.dots({
color: '#38bdf8',
background: '#0b1220',
size: '24px',
opacity: 0.6,
})
.toStyle()Grid
A grid of thin lines, useful for technical and blueprint looks.
Options
sizestring'40px'The size of each grid cell.
strokeWidthstring'1px'The thickness of the lines. Only the number is used, measured in the same unit as size.
opacitynumber0.1Opacity of the lines, from 0 to 1. See color for the color formats it applies to.
Examples
Basic grid
A fine grid with a light color on a dark gradient.
new Builder()
.linearGradient({
from: '#04140d',
to: '#0a2a1c',
})
.grid({
color: '#34d399',
size: '32px',
opacity: 0.25,
})
.toStyle()Stroke width
Increase strokeWidth for bolder lines, and use a smaller size for a denser grid.
new Builder()
.linearGradient({
from: '#312e81',
to: '#1e1b4b',
})
.grid({
color: '#a5b4fc',
size: '24px',
strokeWidth: '2px',
opacity: 0.3,
})
.toStyle()Stars
A field of small stars scattered across a square tile. The layout is pseudo-random but deterministic: the same seed always draws the same sky, so the background never changes between renders.
Options
colorstring'#ffffff'The color of the stars. Unlike the other patterns, the default is white.
sizestring'200px'The size of the repeating tile. Larger tiles make the repetition harder to notice.
countnumber24How many stars are drawn in each tile.
strokeWidthstring'1.4px'The radius of the largest star. Each star gets a random radius between 0.4 and this value, and a random brightness, so the field has depth.
seednumber1The starting point of the random layout. Change it to get a different arrangement with the same options.
opacitynumber0.9Opacity of the stars, from 0 to 1. See color for the color formats it applies to.
Examples
Night sky
With the defaults, stars() adds a white star field. It works best over a dark gradient.
new Builder()
.radialGradient({
from: '#1b2559',
to: '#050816',
position: '30% 20%',
colorToPosition: '90%',
})
.stars()
.toStyle()Layered stars
Two layers with different tile sizes and seeds hide the repetition of the tile. Here many small stars sit under a few large, warm ones.
new Builder()
.linearGradient({
from: '#0b1026',
to: '#2a1f5c',
direction: 'to bottom',
})
.stars({
size: '160px',
count: 30,
strokeWidth: '1px',
seed: 4,
})
.stars({
color: '#ffe8b0',
size: '310px',
count: 8,
strokeWidth: '2px',
seed: 9,
})
.toStyle()Rays
Alternating rays that radiate from a point, like a sunburst. The rays fill the whole element and do not repeat as a tile, so size and strokeWidth are ignored.
Options
countnumber12How many rays are drawn around the full circle. Each ray is followed by a gap of the same width.
positionstring'center'The point the rays start from, using CSS position syntax. Values outside the element, such as '50% 115%', put the center below the edge, so only a fan of rays is visible.
anglestring'0deg'Rotates the rays, as a CSS angle such as '15deg' or '0.25turn'.
opacitynumber0.15Opacity of the rays, from 0 to 1. The gaps use background, which is transparent by default.
Examples
Sunburst
Translucent white rays over a warm radial gradient.
new Builder()
.radialGradient({
from: '#fde68a',
to: '#f97316',
})
.rays({
color: '#ffffff',
opacity: 0.25,
})
.toStyle()Art Deco fan
Move the center below the bottom edge to show a fan of rays, then darken the corners with a vignette.
new Builder()
.radialGradient({
from: '#e6c35c',
to: '#15130e',
position: '50% 115%',
colorToPosition: '70%',
})
.rays({
color: '#e6c35c',
count: 18,
position: '50% 115%',
angle: '270deg',
opacity: 0.12,
})
.vignette({ intensity: 0.5, spread: 0.5 })
.toStyle()Combining patterns
A single chain can contain several patterns. Add the one that should be on top first. A large, faint grid mixed with small dots adds depth without competing for attention.
Examples
Grid and dots
A large grid with a lighter layer of dots over a deep blue gradient.
new Builder()
.linearGradient({
from: '#0c4a6e',
to: '#020617',
angle: '160deg',
})
.grid({
color: '#7dd3fc',
size: '80px',
opacity: 0.18,
})
.dots({
color: '#e0f2fe',
size: '16px',
spacing: '20px',
opacity: 0.12,
})
.toStyle()Types reference
type PatternType = 'dots' | 'grid' | 'stars' | 'rays'
interface PatterBaseOptions {
color?: string
background?: string
size?: string
opacity?: number
strokeWidth?: string
backgroundSize?: string
}
interface DotsOptions extends PatterBaseOptions {
color?: string
background?: string
size?: string
spacing?: string
style?: string
opacity?: number
}
interface GridOptions extends PatterBaseOptions {}
interface StarsOptions extends PatterBaseOptions {
count?: number
seed?: number
}
interface RaysOptions extends PatterBaseOptions {
count?: number
position?: string
angle?: string
}
type PatternOptions =
| DotsOptions
| GridOptions
| StarsOptions
| RaysOptions