Skip to content

Seamless Texture — TypeScript source

Make images tile seamlessly or generate tileable noise textures — entirely in your browser. Nothing uploads.

This is the TypeScript implementation — the same logic the interactive tool runs, in a shareable, citable form.

// seamless — pure tileable-texture math (FEAT-036). No canvas, no DOM:
// weights, hashes and noise fields in, numbers out, so every tiling
// guarantee is unit-testable and the island owns all drawing.

/**
 * Cross-fade weight for wrap-blending a texture's seams.
 *
 * Returns 1 in the interior and falls linearly to 0 across `feather`
 * pixels approaching each wrapped edge (distance to the nearest edge on
 * each axis is `min(coord, size - coord)`; the two axis weights are
 * multiplied, then clamped to 0..1). `feather <= 0` disables the ramp —
 * constant 1 everywhere.
 */
export function blendWeight(x: number, y: number, w: number, h: number, feather: number): number {
  if (!(feather > 0)) return 1;
  const fx = Math.min(x, w - x) / feather;
  const fy = Math.min(y, h - y) / feather;
  return Math.min(1, Math.max(0, fx)) * Math.min(1, Math.max(0, fy));
}

/**
 * Deterministic 0..1 hash of an integer lattice point.
 *
 * Formula: 32-bit integer avalanche — h = x·0x9E3779B1 XOR y·0x85EBCA77
 * XOR seed·0xC2B2AE35 (all multiplied with Math.imul, truncated to int32),
 * then three xor-shift/multiply rounds (>>>15 ·0x2C1B3C6D, >>>12
 * ·0x297A2D39, >>>15). The unsigned result is scaled by 1/2³² to 0..1.
 * Same (x, y, seed) → same value on every platform and run.
 */
export function hash2(x: number, y: number, seed: number): number {
  let h = (Math.imul(x | 0, 0x9e3779b1) ^ Math.imul(y | 0, 0x85ebca77) ^ Math.imul(seed | 0, 0xc2b2ae35)) | 0;
  h ^= h >>> 15;
  h = Math.imul(h, 0x2c1b3c6d);
  h ^= h >>> 12;
  h = Math.imul(h, 0x297a2d39);
  h ^= h >>> 15;
  return (h >>> 0) / 4294967296;
}

export interface NoiseOptions {
  seed: number;
  /** Tile period in pixels — noise(x + scale, y) === noise(x, y) exactly. */
  scale: number;
  /** ≥1. Each extra octave doubles the frequency and halves the amplitude. */
  octaves?: number;
}

/**
 * Wrapping value noise over a unit lattice (lattice spacing 1 px, tile
 * period `scale` px). Lattice indices wrap modulo `scale`, so the field
 * tiles exactly: valueNoise(x + scale, y) === valueNoise(x, y). Octaves ≥2
 * sum frequency-doubled, amplitude-halved layers (period stays `scale`)
 * and the result is normalized by the total amplitude to 0..1; a single
 * octave is the raw layer. scale < 1 snaps to 1; octaves < 1 snaps to 1.
 */
export function valueNoise(x: number, y: number, opts: NoiseOptions): number {
  const scale = Math.max(1, Math.round(opts.scale));
  const octaves = Math.max(1, Math.floor(opts.octaves ?? 1));
  let sum = 0;
  let total = 0;
  let amp = 1;
  let freq = 1;
  for (let o = 0; o < octaves; o++) {
    sum += amp * layer(x * freq, y * freq, scale * freq, opts.seed + o * 101);
    total += amp;
    amp /= 2;
    freq *= 2;
  }
  return sum / total;
}

/** One octave: bilinear (quintic-faded) interpolation of wrapped lattice hashes. */
function layer(x: number, y: number, period: number, seed: number): number {
  const x0 = Math.floor(x);
  const y0 = Math.floor(y);
  const u = fade(x - x0);
  const v = fade(y - y0);
  const xa = wrap(x0, period);
  const xb = wrap(x0 + 1, period);
  const ya = wrap(y0, period);
  const yb = wrap(y0 + 1, period);
  const top = lerp(hash2(xa, ya, seed), hash2(xb, ya, seed), u);
  const bottom = lerp(hash2(xa, yb, seed), hash2(xb, yb, seed), u);
  return lerp(top, bottom, v);
}

/** Positive modulo — lattice indices wrap onto the torus of `period` cells. */
function wrap(i: number, period: number): number {
  return ((i % period) + period) % period;
}

/** Quintic smoothstep fade — C2-continuous interpolation curve. */
function fade(t: number): number {
  return t * t * t * (t * (t * 6 - 15) + 10);
}

function lerp(a: number, b: number, t: number): number {
  return a + (b - a) * t;
}

export interface WorleyOptions {
  seed: number;
  /** Number of hashed feature points scattered in the unit tile (≥1). */
  points: number;
}

export interface WorleyResult {
  /** Mapped 0..1 distance to the nearest feature point. */
  d1: number;
  /** Mapped 0..1 distance to the second-nearest feature point. */
  d2: number;
}

/**
 * Wrapping (torus-metric) Worley/F1-F2 field. `points` feature points are
 * hashed into the unit tile (point i sits at (hash2(i,0,seed),
 * hash2(i,1,seed))); each axis distance wraps via min(d, 1-d), so the field
 * tiles exactly with period 1 in x and y. Raw torus distances top out at
 * the half-diagonal √½, which maps to 1. Fewer than one point returns the
 * all-max { d1: 1, d2: 1 }; exactly one point leaves d2 at the maximum.
 */
export function worley(x: number, y: number, opts: WorleyOptions): WorleyResult {
  // Wrap the query onto the unit tile first so shifted queries are exact.
  const wx = x - Math.floor(x);
  const wy = y - Math.floor(y);
  const n = Math.floor(opts.points);
  let d1 = 1;
  let d2 = 1;
  if (n >= 1) {
    for (let i = 0; i < n; i++) {
      const px = hash2(i, 0, opts.seed);
      const py = hash2(i, 1, opts.seed);
      let dx = Math.abs(wx - px);
      dx = Math.min(dx, 1 - dx);
      let dy = Math.abs(wy - py);
      dy = Math.min(dy, 1 - dy);
      const d = Math.sqrt(dx * dx + dy * dy) * Math.SQRT2;
      if (d < d1) {
        d2 = d1;
        d1 = d;
      } else if (d < d2) {
        d2 = d;
      }
    }
  }
  return { d1, d2 };
}

Also available in 13 other languages

Every CosmoDev tool ships its pure logic in TypeScript (web) and Go (CLI), with authored implementations in a dozen-plus languages — the same contract, ported. Compare all languages side by side →