101 lines
3.9 KiB
TypeScript
101 lines
3.9 KiB
TypeScript
// Small repeating patterns, generated from a seed when the game starts, so surfaces
|
|
// read as parchment, woven cloth and wood without any image files. Every tile uses flat,
|
|
// opaque colours a shade off its surface and leaves the rest transparent, so the surface
|
|
// colour shows through: no gradients, no opacity.
|
|
|
|
/** Deterministic pseudo-random numbers in [0, 1). */
|
|
function random(seed: number) {
|
|
let s = seed >>> 0
|
|
return () => {
|
|
s = (s + 0x6d2b79f5) >>> 0
|
|
let t = s
|
|
t = Math.imul(t ^ (t >>> 15), t | 1)
|
|
t ^= t + Math.imul(t ^ (t >>> 7), t | 61)
|
|
return ((t ^ (t >>> 14)) >>> 0) / 4294967296
|
|
}
|
|
}
|
|
|
|
/** `a` moved towards `b` by `t` (0..1), both #rrggbb. */
|
|
function mix(a: string, b: string, t: number) {
|
|
const ch = (hex: string, i: number) => parseInt(hex.slice(1 + i * 2, 3 + i * 2), 16)
|
|
const out = [0, 1, 2].map((i) => Math.round(ch(a, i) + (ch(b, i) - ch(a, i)) * t).toString(16).padStart(2, '0'))
|
|
return `#${out.join('')}`
|
|
}
|
|
|
|
const svg = (w: number, h: number, body: string) =>
|
|
`url("data:image/svg+xml,${encodeURIComponent(`<svg xmlns="http://www.w3.org/2000/svg" width="${w}" height="${h}" shape-rendering="crispEdges">${body}</svg>`)}")`
|
|
|
|
const INK = '#2b1d10'
|
|
const LIGHT = '#f3e7c9'
|
|
|
|
/** Parchment: scattered flecks and short fibres a shade darker than the page. */
|
|
function parchment(base: string, seed: number) {
|
|
const r = random(seed)
|
|
const dark = mix(base, INK, 0.07)
|
|
const light = mix(base, LIGHT, 0.25)
|
|
let body = ''
|
|
for (let i = 0; i < 26; i++) {
|
|
const x = Math.floor(r() * 96)
|
|
const y = Math.floor(r() * 96)
|
|
body += r() < 0.7 ? `<rect x="${x}" y="${y}" width="1" height="1" fill="${r() < 0.75 ? dark : light}"/>` : `<rect x="${x}" y="${y}" width="${2 + Math.floor(r() * 3)}" height="1" fill="${dark}"/>`
|
|
}
|
|
return svg(96, 96, body)
|
|
}
|
|
|
|
/** Woven cloth for header bands: a fine diagonal twill. */
|
|
function twill(base: string) {
|
|
const line = mix(base, INK, 0.06)
|
|
let body = ''
|
|
for (let i = 0; i < 6; i++) body += `<rect x="${i}" y="${i}" width="1" height="1" fill="${line}"/><rect x="${(i + 3) % 6}" y="${i}" width="1" height="1" fill="${line}"/>`
|
|
return svg(6, 6, body)
|
|
}
|
|
|
|
/** Wood: long grain lines, lighter streaks in a darker board, with the odd knot. */
|
|
function wood(base: string, seed: number) {
|
|
const r = random(seed)
|
|
const grain = mix(base, LIGHT, 0.07)
|
|
const dark = mix(base, '#000000', 0.18)
|
|
let body = ''
|
|
for (let y = 1; y < 24; y += 3 + Math.floor(r() * 3)) {
|
|
let x = 0
|
|
while (x < 160) {
|
|
const len = 12 + Math.floor(r() * 40)
|
|
body += `<rect x="${x}" y="${y + (r() < 0.3 ? 1 : 0)}" width="${len}" height="1" fill="${r() < 0.8 ? grain : dark}"/>`
|
|
x += len + 4 + Math.floor(r() * 18)
|
|
}
|
|
}
|
|
const kx = Math.floor(r() * 150)
|
|
body += `<rect x="${kx}" y="10" width="3" height="2" fill="${dark}"/>`
|
|
return svg(160, 24, body)
|
|
}
|
|
|
|
/** A running stitch along the bottom of a band, like a sewn hem. */
|
|
function stitch(color: string) {
|
|
return svg(10, 4, `<rect x="1" y="1" width="5" height="1" fill="${color}"/>`)
|
|
}
|
|
|
|
/** Notches across a scrollbar's handle, like a carved grip. */
|
|
function notches(base: string) {
|
|
return svg(8, 6, `<rect x="0" y="2" width="8" height="1" fill="${mix(base, LIGHT, 0.16)}"/>`)
|
|
}
|
|
|
|
/**
|
|
* The CSS custom properties main.css paints its surfaces with: `--pat-<name>`.
|
|
* Seeds are fixed, so every visit gets the same patterns.
|
|
*/
|
|
export function patternVars(): Record<string, string> {
|
|
return {
|
|
'--pat-parchment-50': parchment('#f3e7c9', 11),
|
|
'--pat-parchment-100': parchment('#e8d6ad', 12),
|
|
'--pat-parchment-200': parchment('#dcc595', 13),
|
|
'--pat-parchment-300': twill('#c4a676'),
|
|
'--pat-wood-950': wood('#1f150c', 21),
|
|
'--pat-wood-900': wood('#2f2013', 22),
|
|
'--pat-wood-800': wood('#3e2b1a', 23),
|
|
'--pat-wood-700': wood('#57391f', 24),
|
|
'--pat-stitch': stitch('#75502e'),
|
|
'--pat-notches': notches('#57391f'),
|
|
'--pat-backdrop': parchment('#cdb488', 31),
|
|
}
|
|
}
|