ComponentsMedia

Unwoven Slider

Ready

An infinite image strip that frays into threads at the edges — canvas-drawn, drag to scrub, auto-drifts forever. Falls back to a plain CSS card row if images fail or JS is off.

Preview

Code

unwoven-slider-example.html
<style>
  .tt-unwoven {
    --uw-height: 452px;
    --uw-radius: 20px;
    --uw-aspect: 0.75;
    --uw-gap: 0.11;
    position: relative;
    width: 100%;
    height: var(--uw-height);
    overflow: hidden;
    user-select: none;
    touch-action: pan-y;
    cursor: grab;
    background: #000;
  }
  .tt-unwoven.is-dragging { cursor: grabbing; }

  .tt-unwoven__stage {
    position: absolute;
    inset: 0;
    opacity: 0;
    transition: opacity .8s ease;
  }
  .tt-unwoven__stage.is-ready { opacity: 1; }
  .tt-unwoven__stage canvas { display: block; width: 100%; height: 100%; }

  .tt-unwoven__sources {
    position: absolute;
    inset: 0;
    display: flex;
    align-items: center;
    gap: calc(var(--uw-height) * .74 * var(--uw-aspect) * var(--uw-gap));
    padding-inline: 2vw;
  }
  .tt-unwoven__sources img {
    flex: none;
    height: calc(var(--uw-height) * .74);
    aspect-ratio: var(--uw-aspect);
    object-fit: cover;
    border-radius: var(--uw-radius);
  }
  .tt-unwoven.is-error::after {
    content: "Images couldn't load. Check the image URLs, or host the images yourself.";
    position: absolute; inset: 0; display: grid; place-items: center;
    color: #888; font-size: 14px;
  }
  .tt-unwoven.is-error .tt-unwoven__sources,
  .tt-unwoven.is-live .tt-unwoven__sources {
    visibility: hidden;
    pointer-events: none;
  }
</style>
<div class="tt-unwoven"
     data-tt-uw-threads="26"
     data-tt-uw-height="452"
     data-tt-uw-aspect="0.75"
     data-tt-uw-gap="0.11"
     data-tt-uw-radius="20"
     data-tt-uw-speed="45"
     data-tt-uw-tear="0.26"
     role="region"
     aria-label="Image slider"
     aria-roledescription="carousel">
  <div class="tt-unwoven__stage" aria-hidden="true"></div>
  <div class="tt-unwoven__sources">
    <img src="image-1.jpg" alt="" decoding="async">
    <img src="image-2.jpg" alt="" decoding="async">
    <img src="image-3.jpg" alt="" decoding="async">
    <img src="image-4.jpg" alt="" decoding="async">
    <img src="image-5.jpg" alt="" decoding="async">
    <img src="image-6.jpg" alt="" decoding="async">
    <img src="image-7.jpg" alt="" decoding="async">
    <img src="image-8.jpg" alt="" decoding="async">
    <img src="image-9.jpg" alt="" decoding="async">
  </div>
</div>
<script>
(() => {
  const clamp = (v, a, b) => Math.min(b, Math.max(a, v));
  const rng = seed => () => ((seed = (seed * 16807) % 2147483647) - 1) / 2147483646;

  function Unwoven(root) {
    const d = root.dataset;
    const cfg = {
      threads: +d.ttUwThreads || 26,
      height:  +d.ttUwHeight  || 452,
      aspect:  +d.ttUwAspect  || 0.75,
      gap:     +d.ttUwGap     || 0.11,
      radius:  +d.ttUwRadius  || 20,
      speed:   +d.ttUwSpeed   || 45,
      tear:    +d.ttUwTear    || 0.26
    };
    root.style.setProperty('--uw-height', cfg.height + 'px');
    root.style.setProperty('--uw-radius', cfg.radius + 'px');
    root.style.setProperty('--uw-aspect', cfg.aspect);
    root.style.setProperty('--uw-gap', cfg.gap);

    const stage = root.querySelector('.tt-unwoven__stage');
    let imgs = [...root.querySelectorAll('.tt-unwoven__sources img')];
    const canvas = document.createElement('canvas');
    const ctx = canvas.getContext('2d');
    stage.appendChild(canvas);

    const reduced = matchMedia('(prefers-reduced-motion: reduce)').matches;

    let cards = [];
    const makeCards = list => list.map((img, c) => {
      const r = rng(1234 + c * 977);
      return {
        img,
        threads: Array.from({ length: cfg.threads }, () => ({
          zone:    1 + cfg.tear * (r() * 2 - 1),
          stretch: 1 + cfg.tear * 2.2 * (r() * 2 - 1),
          bend:    (r() * 2 - 1),
          phase:   r() * Math.PI * 2,
          thin:    0.55 + r() * 0.35
        }))
      };
    });

    let W = 0, H = 0, dpr = 1, cw = 0, ch = 0, step = 0, cy = 0;
    function resize() {
      const rect = root.getBoundingClientRect();
      dpr = Math.min(window.devicePixelRatio || 1, 2);
      W = rect.width; H = rect.height;
      canvas.width = Math.round(W * dpr);
      canvas.height = Math.round(H * dpr);
      ch = H * 0.74;
      cw = ch * cfg.aspect;
      step = cw * (1 + cfg.gap);
      cy = (H - ch) / 2;
    }

    function cover(img) {
      const iw = img.naturalWidth, ih = img.naturalHeight, ir = iw / ih;
      if (ir > cfg.aspect) { const sw = ih * cfg.aspect; return [(iw - sw) / 2, 0, sw, ih]; }
      const sh = iw / cfg.aspect; return [0, (ih - sh) / 2, iw, sh];
    }

    function field(x, th, t) {
      const zone = Math.max(150, W * 0.2);
      const zl = zone * th.zone, zr = zone * th.zone;
      const l = clamp((zl - x) / zl, 0, 1);
      const r = clamp((x - (W - zr)) / zr, 0, 1);
      const L = Math.max(l, r);
      const e = Math.pow(L, 1.7);
      const S = zone * 1.9 * th.stretch;
      const dx = l > r ? -e * S : e * S;
      const dy = e * (th.bend * ch * 0.18 + Math.sin(x * 0.009 + t * 1.2 + th.phase) * ch * 0.06);
      return { L, dx, dy };
    }

    function drawIntact(card, x, src) {
      ctx.save();
      ctx.beginPath();
      ctx.roundRect(x, cy, cw, ch, cfg.radius);
      ctx.clip();
      ctx.drawImage(card.img, src[0], src[1], src[2], src[3], x, cy, cw, ch);
      ctx.restore();
    }

    function drawFrayed(card, x, src, t) {
      const n = cfg.threads, rowH = ch / n, r = cfg.radius;
      const [sx, sy, sw, sh] = src;
      const SEG = 64;
      for (let i = 0; i < n; i++) {
        const th = card.threads[i];
        const top = i * rowH;
        const nearCorner = top < r || top + rowH > ch - r;
        const sub = nearCorner ? Math.max(4, Math.ceil(rowH / 2)) : 1;

        const pts = [];
        for (let s = 0; s <= SEG; s++) {
          const u = s / SEG, f = field(x + u * cw, th, t);
          pts.push({ u, x: x + u * cw + f.dx, y: f.dy, L: f.L });
        }

        for (let k = 0; k < sub; k++) {
          const a0 = k / sub, a1 = (k + 1) / sub;
          const ym = top + (a0 + a1) / 2 * rowH;
          const edge = Math.min(ym, ch - ym);
          const inset = edge < r ? r - Math.sqrt(r * r - (r - edge) ** 2) : 0;
          const uMin = inset / cw, uMax = 1 - inset / cw;
          const srcY = sy + ((top + a0 * rowH) / ch) * sh;
          const srcH = (rowH / sub / ch) * sh;

          for (let s = 1; s <= SEG; s++) {
            const p0 = pts[s - 1], p1 = pts[s];
            if (p1.u <= uMin || p0.u >= uMax) continue;
            const ua = Math.max(p0.u, uMin), ub = Math.min(p1.u, uMax);
            const ka = (ua - p0.u) / (p1.u - p0.u), kb = (ub - p0.u) / (p1.u - p0.u);
            const xa = p0.x + (p1.x - p0.x) * ka, xb = p0.x + (p1.x - p0.x) * kb;
            const w = xb - xa;
            if (xa > W || xb < 0 || w <= 0) continue;
            const L = (p0.L + p1.L) / 2;
            const sep = Math.min(1, L * 4);
            const thick = rowH * (1 - 0.3 * sep) * (1 - th.thin * Math.pow(L, 1.5));
            const y = cy + top + rowH / 2 + (p0.y + p1.y) / 2 - thick / 2 + thick * a0;
            ctx.globalAlpha = L > 0.85 ? 1 - (L - 0.85) / 0.15 : 1;
            ctx.drawImage(card.img,
              sx + ua * sw, srcY, (ub - ua) * sw, srcH,
              xa, y, w + 0.75, thick / sub + (sub > 1 ? 0.5 : 0));
          }
        }
      }
      ctx.globalAlpha = 1;
    }

    let offset = 0, vel = 0, dragging = false, lastX = 0, lastT = 0, visible = true;
    root.addEventListener('pointerdown', e => {
      dragging = true; lastX = e.clientX; lastT = performance.now(); vel = 0;
      root.setPointerCapture(e.pointerId); root.classList.add('is-dragging');
    });
    root.addEventListener('pointermove', e => {
      if (!dragging) return;
      const now = performance.now(), dx = e.clientX - lastX;
      offset -= dx;
      vel = -dx / Math.max(1, now - lastT) * 1000;
      lastX = e.clientX; lastT = now;
    });
    const endDrag = () => { dragging = false; root.classList.remove('is-dragging'); };
    root.addEventListener('pointerup', endDrag);
    root.addEventListener('pointercancel', endDrag);

    new IntersectionObserver(([en]) => { visible = en.isIntersecting; }).observe(root);
    new ResizeObserver(resize).observe(root);
    resize();

    let prevT = performance.now();
    function frame(now) {
      const dt = Math.min(0.05, (now - prevT) / 1000);
      prevT = now;
      if (visible) {
        const t = now / 1000;
        if (!dragging) {
          vel *= Math.pow(0.04, dt);
          offset += ((reduced ? 0 : cfg.speed) + vel) * dt;
        }
        ctx.setTransform(dpr, 0, 0, dpr, 0, 0);
        ctx.clearRect(0, 0, W, H);

        const total = step * cards.length;
        const start = -(((offset % total) + total) % total);
        const pad = Math.max(150, W * 0.2) * 2.5;
        let k = 0;
        for (let x = start - total; x < W + pad; x += step, k++) {
          if (x + cw < -pad) continue;
          const card = cards[k % cards.length];
          if (!card.img.complete || !card.img.naturalWidth) continue;
          const src = card.src || (card.src = cover(card.img));
          const zone = Math.max(150, W * 0.2) * (1 + cfg.tear);
          if (x > zone && x + cw < W - zone) drawIntact(card, x, src);
          else drawFrayed(card, x, src, t);
        }
      }
      requestAnimationFrame(frame);
    }

    const settle = img => new Promise(res => {
      if (img.complete) return res();
      img.addEventListener('load', res, { once: true });
      img.addEventListener('error', res, { once: true });
    });
    const timeout = new Promise(res => setTimeout(res, 6000));
    Promise.race([Promise.all(imgs.map(settle)), timeout]).then(() => {
      const ok = imgs.filter(i => i.complete && i.naturalWidth > 0);
      if (!ok.length) {
        console.warn('[unwoven] No images could be loaded - check the image URLs.');
        root.classList.add('is-error');
        return;
      }
      cards = makeCards(ok);
      stage.classList.add('is-ready');
      root.classList.add('is-live');
      requestAnimationFrame(frame);
    });
  }

  document.querySelectorAll('.tt-unwoven').forEach(Unwoven);
})();
</script>