跳到主内容
UISpell
Experiment 02 布料模拟

晾衣绳 Hero

把指针移过布面。它跟着凹陷,再慢慢恢复。按住向上拖:布角朝你掀起、翻过晾衣绳,松手后落回原位。没有视频、没有精灵图——357 个粒子、三类约束、一个固定步长。

实时实验 · 02

先悬停,再向上拖动布料,直到它翻过晾衣绳。「Physics & Poster」里可以调风力、挺括度、海报预设,并切到静态降级。

01

你能感到什么

把效果拆成几个部分,每一部分都对应一个可以改的数字。

凹陷
悬停时,附近粒子沿远离观察者的方向被推开,衰减是高斯曲线,所以棉布看起来是软的,而不是画上去的。快速移动时,凹陷会在指针后面拖出一小段尾迹。
翻面
拖拽时,被抓取的局部同时朝镜头方向抬起。越过绳子后,一层很薄的碰撞面会把布料留在另一侧,所以可以把一角掀过晾衣绳,而不是穿模过去。
回摆
松手后由重力和阻尼接管。结构、剪切与弯曲约束在每个 120 Hz 步长里迭代七次,所以布身保持挺括,自由角则会摆动、收敛。
02

Prompt 本体

写来复现行为本身,而不只是外观。可以直接粘进 Claude Code、Cursor 或 v0。

You are a front-end engineer with a feel for cloth. Build a single HTML page whose whole job is one sensation: a cotton poster hanging from a washing line that can be touched, lifted and flipped. Every number below exists to make the cloth read as fabric, not as a deformed rectangle.

[The scene]
- Full-viewport hero, no scroll. Sky is a three-stop vertical gradient. Morning: #9ec9ee → #cde5f7 → #f2f6f9. Golden hour: #d99b78 → #f3cdb1 → #faf1e4. Blue hour: #0f1c38 → #1f3a63 → #4a668a. Four soft radial cloud blooms drift across it at different speeds, drawn as ellipses with a transparent outer stop; no blur filter.
- A single rope crosses the viewport as a quadratic curve: left end at y = lineY − 26, right end at y = lineY − 18, control point at (52vw, lineY + 19). Draw a 3.5px shadow 3px lower, a 2.6px braided line, and a 0.9px sunlit highlight 0.8px above.
- Two wooden clothespins clamp the top edge at 15% and 85% of the cloth width: a rounded body 12×38 with a three-stop wood gradient, a top notch, and a 4px metal spring band.
- Left editorial column, max 36rem: mono eyebrow, serif headline clamp(2.25rem, 5.2vw, 3.5rem) at 1.06 line-height and −0.02em tracking, one paragraph, three pill buttons (Copy prompt, Gust, Reset), and a mono telemetry row that reports the fixed step, grid size and current gesture.

[The cloth]
- A 17 × 21 particle grid (357 particles, 16 × 20 segments) forms a 3:4 poster. Size it as min(max(27vw, 270px), 380px) wide on desktop and 66vw capped at 260px on mobile. On desktop centre it at 69vw; on mobile centre it horizontally at 48vh or lower.
- Pins are row 0, columns 2 and 15. Give the top edge a catenary droop of up to 10px, and each particle an initial z of 4px × sin(c × 0.5 + r × 0.3) so the first frame already catches light.
- Constraint families: structural (horizontal and vertical) at stiffness 1.0; shear (both diagonals) at 0.72; bending (two steps away) at 0.36. Solve all of them seven times per step by default, adjustable 3–12 from a slider. With these numbers the total is 1,954 constraints.

[Physics]
- Verlet integration at a fixed 120 Hz (dt = 1/120). Damping 0.992, gravity 980 px/s². Use an accumulator; clamp the incoming frame delta to 50 ms.
- Wind is a time-varying vector: base 0.45, modulated by sin(t × 1.45) × 0.5 + sin(t × 3.1) × 0.25 + 0.65, with x = 260 × wind, y = sin(t × 2.2) × 45 × wind, z = (cos(t × 1.9) × 0.6 + 0.45) × 240 × wind.
- Apply wind per triangle: normalise the triangle normal, multiply by the dot product of normal and wind, scale by 0.33, and add the same force to all three vertices. Add a per-triangle flutter of 0.75 + 0.35 × sin(t × 4.2 + u × 6 + v × 4) so the surface never moves as one sheet.
- Clamp z to −260…280.

[Touch]
- Hover: for every unpinned particle within 74px of the pointer, add +Z force 180 × exp(−d² / (2 × 30²)), plus pointer velocity × 36 on x and y. This is the dent.
- Pointer down: pick the nearest unpinned particle within 78px. Grab every unpinned particle within 56px of it, weighting each by cos((d / 56) × π/2)². Store each particle's offset from the pointer.
- Drag: move grabbed particles 42% of the way toward the pointer plus 0.72 × their stored offset. Lift z toward −135 × lift − 35, where lift = clamp((centreY + 0.55 × clothH − pointerY) / (0.75 × clothH), 0, 1.3). This is what lets a corner flip over the rope.
- Collision: after each solver iteration, any particle within 10px of the line and within 12px of z = 0 is snapped to z = ±12, staying on the side it came from.
- On release, clear the grab and let gravity and damping settle the cloth. The rope itself is a damped spring: stiffness 90, damping 7.5, driven by the vertical pull of the hand.

[The poster]
- Draw the poster once into a 540 × 720 offscreen canvas, then map it onto the cloth triangle by triangle with an affine transform. Expand each clip triangle 0.65px from its centroid to hide seams.
- Presets: an editorial Swiss poster (cobalt-to-sky block, orange sun, cream wave, serif “Touchable Physics.”); a Mediterranean linen (terracotta field, amber sun, navy arch, “Atelier Vento”); and a DeepSeek craft edition (dark panel, cyan orb, “Zero Slop. Real Feel.”). Add a cotton weave: 1px lines every 3px in both directions at 4.5–7% alpha, plus a dashed stitched hem 14px in from the edge.
- When a triangle faces away from the camera, draw the mirrored back texture at 22% alpha so the poster reads as ink bleeding through cotton.
- Light the cloth with a Lambert term against a per-sky light direction. Below 0.55, darken toward the sky's shadow colour by up to 42%; above, lift toward warm white by up to 20%.
- Painter-sort triangles by average z. Draw the rope first, then cloth, then the two pegs.

[Config, fallback and output]
- Put eyebrow, headline, subtitle, sky theme, poster preset, wind, stiffness, gravity and damping into one hero.json object. The visible controls and the JSON editor both edit that object; applying redraws the poster and re-reads physics on the next step.
- Static fallback: render the poster into an overlay and stop the animation loop. Enter it automatically under prefers-reduced-motion, and expose a visible Static / Live toggle so it can be inspected.
- Cap devicePixelRatio at 2. Clamp frame delta to 50 ms. Never allocate per frame: reuse the particle, constraint and force arrays.
- One complete HTML5 file. No libraries, no external images, no analytics. Write noindex, nofollow in head. Tailwind may be compiled ahead of time; do not ship the CDN build.
03

我们给自己定的规则

  1. 01 只做一个手势:悬停与拖拽。没有捏合、没有旋转、不劫持滚动。
  2. 02 海报是唯一内容。没有视频、没有精灵图、没有 shader、没有外部图片。
  3. 03 物理跑在 120 Hz 固定累加器上。渲染可以掉帧,布料不会变慢。
  4. 04 棉布、油墨和背面的透印全部画进离屏 canvas,页面不加载任何图片。
  5. 05 一个 hero.json 对象同时驱动文案、天空、海报与物理;控件改的就是它,不是隐藏状态。
  6. 06 降级是一个设计过的海报,不是空白帧,也不是一句道歉。
04

性能降级

效果是有意会退化的。只能在快电脑上成立的材质叫 Demo,不叫设计。

  • prefers-reduced-motion 直接进入静态模式:海报照常生成,模拟循环不启动。
  • 「Static」开关随时停止 rAF 循环并显示海报;「Live」再把它接回去。
  • 设备像素比封顶为 2,帧间隔钳制在 50 ms,后台标签页不会把累加器灌爆。
  • 开销从构造上就是有界的:357 个粒子、1,954 条约束,求解迭代 3–12 次由挺括度滑块决定。

prefers-reduced-motion → 静态海报 · Static 开关停掉 rAF 循环 · DPR ≤ 2 · 帧间隔 ≤ 50 ms · 357 粒子 / 1,954 约束

05

它不是什么

  • 不是服装模拟器。没有自碰撞、摩擦、撕裂,也没有布料之间的接触;这张网格是首屏玩具。
  • 不是布料扫描。海报和织纹都是 canvas 程序化绘制的,不是实拍材质。
  • 没有做设备实验室跑分。性能靠设计封顶,而不是逐机型实测。