Skip to content

Seamless Texture — JavaScript source

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

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

// seamless-texture — tileable-texture math: wrap-blend weights, lattice
// hash, wrapping value noise, torus Worley, plain JS.
// Plain-JavaScript twin of src/lib/seamless.ts: same hash avalanche, same
// noise and Worley fields, no types and no dependencies — the browser
// island owns all drawing.
/** Cross-fade weight for wrap-blending a texture's seams (0..1). */
export function blendWeight(x, y, w, h, feather) {
  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 (32-bit integer
 * avalanche; same (x, y, seed) → same value on every platform and run).
 */
export function hash2(x, y, seed) {
  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;
}

/** Wrapping value noise over a unit lattice; the field tiles exactly. */
export function valueNoise(x, y, opts) {
  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, y, period, seed) {
  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, period) {
  return ((i % period) + period) % period;
}

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

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

/** Wrapping (torus-metric) Worley/F1-F2 field; tiles exactly with period 1. */
export function worley(x, y, opts) {
  // 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 →