An AI editor that watches its own cut: talking-head / AI-avatar video → auto-edited short video. 网感口播精剪:数字人/真人口播 → AI 自动精剪成网感动效短视频 → 可选剪映分层草稿
/* ==== from HF registry: hw-callout-circle (helpers verbatim, self-preview removed) ==== */
(function(){
window.hwOnUpdate = function (tl, fn) {
if (!tl.__hwRenders) {
tl.__hwRenders = [];
tl.eventCallback("onUpdate", function () {
for (var i = 0; i < tl.__hwRenders.length; i++) tl.__hwRenders[i]();
});
}
tl.__hwRenders.push(fn);
fn();
};
window.hwHash = function (n, seed) {
var x = Math.sin(n * 127.1 + (seed || 1) * 311.7) * 43758.5453;
return (x - Math.floor(x)) * 2 - 1;
};
window.hwBoil = function (tl, target, opts) {
opts = opts || {};
var amp = opts.amp !== undefined ? opts.amp : 1.6;
var rot = opts.rot !== undefined ? opts.rot : 0.5;
var fps = opts.fps || 30;
var drop = opts.frameDrop || 3;
var seed = opts.seed || 1;
var els = gsap.utils.toArray(target);
window.hwOnUpdate(tl, function () {
var step = Math.floor((tl.time() * fps) / drop);
for (var i = 0; i < els.length; i++) {
gsap.set(els[i], {
x: window.hwHash(step * 3 + i * 97, seed) * amp,
y: window.hwHash(step * 3 + 1 + i * 97, seed) * amp,
rotation: (opts.baseRot || 0) + window.hwHash(step * 3 + 2 + i * 97, seed) * rot,
});
}
});
};
// boil control: authored poses over the family-locked frameDrop.
// Unknown pose → default + console error (bounded-controls law).
window.hwBoilPose = function (tl, target, pose, opts) {
opts = opts || {};
var poses = { calm: { amp: 1.6, rot: 0.5 }, lively: { amp: 2.6, rot: 0.9 } };
if (pose === "off") return;
var p = poses[pose];
if (!p) {
console.error('hw: unknown boil pose "' + pose + '" — falling back to "calm"');
p = poses.calm;
}
window.hwBoil(tl, target, { amp: p.amp, rot: p.rot, frameDrop: 3, seed: opts.seed || 1 });
};
/* ---- spring library (spring-lab, validated 2026-08-04 — verbatim) ---- */
var LN1000 = Math.log(1000);
var FEELS = {
snappy: { zeta: 0.9, response: 0.22 },
"heavy-settle": { zeta: 1.0, response: 0.8 },
bouncy: { zeta: 0.5, response: 0.4 },
wobbly: { zeta: 0.28, response: 0.5 },
};
window.springEase = function (opts) {
var f = opts.feel ? FEELS[opts.feel] : opts;
var zeta = f.zeta,
response = f.response;
var w0 = (2 * Math.PI) / response;
var T = LN1000 / (zeta * w0);
var x;
if (zeta < 1) {
var wd = w0 * Math.sqrt(1 - zeta * zeta);
x = function (t) {
return (
1 -
Math.exp(-zeta * w0 * t) *
(Math.cos(wd * t) + ((zeta * w0) / wd) * Math.sin(wd * t))
);
};
} else {
x = function (t) {
return 1 - Math.exp(-w0 * t) * (1 + w0 * t);
};
}
var xT = x(T);
return {
ease: function (p) {
return p >= 1 ? 1 : x(p * T) / xT;
},
duration: T,
zeta: zeta,
response: response,
};
};
window.hwWobbleEllipse = function (cx, cy, rx, ry, seed, wobblePct) {
wobblePct = wobblePct === undefined ? 3 : wobblePct;
var N = 14,
pts = [];
for (var i = 0; i < N; i++) {
var a = (i / N) * Math.PI * 2;
var wr = 1 + window.hwHash(i * 13 + 5, seed) * (wobblePct / 100);
pts.push([cx + Math.cos(a) * rx * wr, cy + Math.sin(a) * ry * wr]);
}
var mx = (pts[0][0] + pts[N - 1][0]) / 2,
my = (pts[0][1] + pts[N - 1][1]) / 2;
var d = "M" + mx.toFixed(1) + " " + my.toFixed(1);
for (var j = 0; j < N; j++) {
var p = pts[j],
q = pts[(j + 1) % N];
d +=
" Q" +
p[0].toFixed(1) +
" " +
p[1].toFixed(1) +
" " +
((p[0] + q[0]) / 2).toFixed(1) +
" " +
((p[1] + q[1]) / 2).toFixed(1);
}
return d;
};
/* ---- stroke matrix (family runtime): plain | soft | sharp | spray ----
strokeType is a g09 variant control. Authored constants per type;
unknown value → default ("plain") + console error (bounded-controls
law). sharp = micro stroke-dasharray gaps (dry marker) — draw-on
must therefore ride a MASK clone (the visible dasharray never
animates). spray = index-seeded deterministic dots along the path
via hwHash: position a pure function of path parameter + seed; NO
feTurbulence anywhere.
Returns { draw: pathToDashAnimate, group: maskedGroupOrNull } */
window.hwStrokeTypes = {
plain: {},
soft: { blur: 0.6 },
sharp: { dash: [3.0, 1.55] }, // × stroke-width
spray: {
coreOpacity: 0.5,
coreBlur: 0.4,
density: 0.16,
scatter: 2.6,
rMin: 1.1,
rMax: 2.6,
},
};
window.hwStrokeApply = function (pathEl, type, opts) {
opts = opts || {};
var cfg = window.hwStrokeTypes[type];
if (!cfg) {
console.error('hw: unknown strokeType "' + type + '" — falling back to "plain"');
type = "plain";
cfg = window.hwStrokeTypes.plain;
}
var svg = pathEl.ownerSVGElement;
var ns = "http://www.w3.org/2000/svg";
var w = parseFloat(getComputedStyle(pathEl).strokeWidth) || 6;
if (type === "plain") return { draw: pathEl, group: null, type: type };
if (type === "soft") {
pathEl.style.filter = "blur(" + cfg.blur + "px)";
return { draw: pathEl, group: null, type: type };
}
// sharp + spray: wrap in a masked group, draw-on animates the mask clone
var id =
opts.id ||
"hwsm-" + Math.abs(Math.round(window.hwHash((opts.seed || 1) * 13.7, 5) * 1e6));
var defs = svg.querySelector("defs");
if (!defs) {
defs = document.createElementNS(ns, "defs");
svg.insertBefore(defs, svg.firstChild);
}
var mask = document.createElementNS(ns, "mask");
mask.setAttribute("id", id);
mask.setAttribute("maskUnits", "userSpaceOnUse");
var clone = document.createElementNS(ns, "path");
clone.setAttribute("d", pathEl.getAttribute("d"));
clone.setAttribute("fill", "none");
clone.setAttribute("stroke", "#fff");
clone.setAttribute(
"stroke-width",
w * (type === "spray" ? 2 + (cfg.scatter * 2) / w : 1.7),
);
clone.setAttribute("stroke-linecap", "round");
clone.setAttribute("stroke-linejoin", "round");
mask.appendChild(clone);
defs.appendChild(mask);
var group = document.createElementNS(ns, "g");
group.setAttribute("mask", "url(#" + id + ")");
pathEl.parentNode.insertBefore(group, pathEl);
group.appendChild(pathEl);
if (type === "sharp") {
pathEl.setAttribute(
"stroke-dasharray",
(cfg.dash[0] * w).toFixed(1) + " " + (cfg.dash[1] * w).toFixed(1),
);
} else {
// spray: faint core + seeded dots
pathEl.setAttribute("stroke-opacity", cfg.coreOpacity);
pathEl.style.filter = "blur(" + cfg.coreBlur + "px)";
var len = pathEl.getTotalLength();
var n = Math.round(len * cfg.density * (opts.densityScale || 1));
var seed = opts.seed || 1;
var dots = document.createElementNS(ns, "g");
for (var i = 0; i < n; i++) {
var t = Math.min(
1,
Math.max(0, (i + 0.5) / n + (window.hwHash(i * 7, seed) * 0.35) / n),
);
var pt = pathEl.getPointAtLength(t * len);
var pt2 = pathEl.getPointAtLength(Math.min(len, t * len + 0.5));
var tx = pt2.x - pt.x,
ty = pt2.y - pt.y;
var tl2 = Math.hypot(tx, ty) || 1;
var nx = -ty / tl2,
nyv = tx / tl2;
var off = window.hwHash(i * 7 + 1, seed) * (cfg.scatter + w * 0.55);
var r = cfg.rMin + Math.abs(window.hwHash(i * 7 + 2, seed)) * (cfg.rMax - cfg.rMin);
var c = document.createElementNS(ns, "circle");
c.setAttribute("cx", (pt.x + nx * off).toFixed(1));
c.setAttribute("cy", (pt.y + nyv * off).toFixed(1));
c.setAttribute("r", r.toFixed(2));
c.setAttribute("fill", getComputedStyle(pathEl).stroke);
dots.appendChild(c);
}
group.appendChild(dots);
}
return { draw: clone, group: group, type: type };
};
/* Draw-on across the stroke matrix: dash-animate whatever hwStrokeApply
says. (The pen-velocity ease variant rides the write-on item, not
this one.) */
window.hwDrawOn = function (tl, applied, at, dur, opts) {
opts = opts || {};
var p = applied.draw;
var len = p.getTotalLength();
// the draw target is always solid (plain/soft: the path itself; sharp/spray:
// the mask clone — the visible path keeps its texture dasharray untouched)
p.setAttribute("stroke-dasharray", len + " " + len);
p.setAttribute("stroke-dashoffset", len);
gsap.set(p, { opacity: 0 }); // kill the round-cap start nub pre-draw
var ease = opts.ease || "power2.inOut";
tl.set(p, { opacity: 1 }, at);
tl.to(
p,
{ strokeDashoffset: 0, duration: dur === undefined ? 0.7 : dur, ease: ease },
at,
);
};
// ---- hw-callout helpers (copy these too) ----
// Builds outline + scribble + connector + label inside the wrapper and
// stores the draw records + control poses hwCalloutOn consumes.
// opts — content (uncounted): scribble (bool), seed, label, connector (bool)
// controls (g09 — see the CONFIG block, the published-parameter
// list): labelAt, strokeType, boil
window.hwCalloutBuild = function (target, opts) {
opts = opts || {};
var seed = opts.seed || 1;
var el = document.querySelector(target);
var svg = el.querySelector("svg");
var vb = svg.getAttribute("viewBox").split(" ");
var W = parseFloat(vb[2]),
H = parseFloat(vb[3]);
var cx = W / 2,
cy = H / 2,
rx = W * 0.42,
ry = H * 0.36;
var outline = el.querySelector(".hw-co-outline");
outline.setAttribute("d", window.hwWobbleEllipse(cx, cy, rx, ry, seed, 4));
var scr = el.querySelector(".hw-co-scribble");
if (opts.scribble === false) {
scr.style.display = "none";
} else {
/* Denser rows so the zigzag reads as a scribbled hatch. At 6 rows
the near-horizontal runs dominate and it renders as stacked bars. */
var d = "",
rows = 11;
for (var i = 0; i < rows; i++) {
var t = (i + 0.5) / rows;
var yy = cy - ry + t * ry * 2;
var half = Math.sqrt(Math.max(0.05, 1 - Math.pow((yy - cy) / ry, 2)));
var xw = rx * half * 0.86;
var xa = cx - xw + window.hwHash(i * 7 + 2, seed) * 8;
var xb = cx + xw + window.hwHash(i * 7 + 3, seed) * 8;
d += (i === 0 ? "M " : " L ") + (i % 2 ? xa : xb).toFixed(1) + " " + yy.toFixed(1);
d +=
" L " +
(i % 2 ? xb : xa).toFixed(1) +
" " +
(yy + window.hwHash(i * 7 + 4, seed) * 4).toFixed(1);
}
scr.setAttribute("d", d);
}
var conn = el.querySelector(".hw-co-connector");
var label = el.querySelector(".hw-co-label");
// labelAt is a declared variant control — bounded: unknown value
// falls back to the default WITH a console error. Validated BEFORE
// the connector branch so a malformed value always logs, even when
// connector: false leaves the control inert.
var at = opts.labelAt || "right";
if (at !== "left" && at !== "right" && at !== "below") {
console.error('hw: unknown labelAt "' + at + '" — falling back to "right"');
at = "right";
}
var ex = null,
ey = null;
if (opts.connector === false) {
conn.style.display = "none";
} else {
var sx = at === "left" ? cx - rx : at === "below" ? cx : cx + rx;
var sy = at === "below" ? cy + ry : cy + ry * 0.35;
ex = at === "left" ? sx - 70 : at === "below" ? sx + 40 : sx + 70;
ey = sy + 55;
conn.setAttribute(
"d",
"M " +
sx.toFixed(1) +
" " +
sy.toFixed(1) +
" Q " +
((sx + ex) / 2 + window.hwHash(21, seed) * 14).toFixed(1) +
" " +
(sy + 46).toFixed(1) +
" " +
ex.toFixed(1) +
" " +
ey.toFixed(1),
);
label.style.left = (at === "left" ? ex - 180 : ex + 12) + "px";
label.style.top = ey - 24 + "px";
}
label.textContent = opts.label || "";
// strokeType control: OUTLINE + CONNECTOR take the texture (the
// scribble stays plain). Bounding lives inside hwStrokeApply.
var strokeType = opts.strokeType || "plain";
var idBase = (el.id || "hw-co") + "-sm";
el.__hwCo = {
rx: rx,
seed: seed,
boil: opts.boil || "calm",
// the label pops out of the connector tip (falls back to center
// when the connector anatomy is off)
popOrigin: ex === null ? "50% 50%" : ex.toFixed(1) + "px " + ey.toFixed(1) + "px",
draw: {
outline: window.hwStrokeApply(outline, strokeType, {
seed: seed,
id: idBase + "-outline",
}),
scribble:
opts.scribble === false ? null : window.hwStrokeApply(scr, "plain", { seed: seed }),
connector:
opts.connector === false
? null
: window.hwStrokeApply(conn, strokeType, {
seed: seed,
id: idBase + "-connector",
}),
},
};
};
// Draw sequence: outline -> scribble -> connector -> label pop, with
// the g21 contact squash on the shapes group at pop arrival. The boil
// pose registers here too — the host wires a position only.
window.hwCalloutOn = function (tl, target, at) {
var el = document.querySelector(target);
var co = el.__hwCo;
var t = at;
["outline", "scribble", "connector"].forEach(function (k) {
var rec = co.draw[k];
if (!rec) return;
var dur = k === "scribble" ? 0.55 : k === "connector" ? 0.35 : 0.7;
window.hwDrawOn(tl, rec, t, dur);
t += dur * 0.85;
});
// label pop: the pop ARRIVES WITH MOMENTUM (accelerating into
// contact) — the squash absorbs it; a settling back.out crosses
// scale 1.0 at ~zero speed and the computed squash is invisible
// (measured, not hand-picked)
var pop = el.querySelector(".hw-co-pop");
var popEase = gsap.parseEase("power2.in");
gsap.set(pop, { scale: 0.001, opacity: 0, transformOrigin: co.popOrigin });
tl.to(pop, { opacity: 1, duration: 0.08, ease: "none" }, t);
tl.to(pop, { scale: 1, duration: 0.3, ease: popEase, immediateRender: false }, t);
// arrival velocity in px of radial (rx) travel, measured at the
// FIRST crossing of 1.0 — a settling ease's end-slope is ~0 and is
// NOT the arrival velocity
var eps = 1e-4;
var pc = 1;
for (var k2 = 1; k2 <= 1000; k2++) {
if (popEase(k2 / 1000) >= 1) {
pc = k2 / 1000;
break;
}
}
var vPop =
((popEase(Math.min(pc + eps, 1)) - popEase(pc - eps)) / (2 * eps)) * (co.rx / 0.3);
var S = Math.min(0.08 * (Math.abs(vPop) / 2000), 0.25); // 0.08 = light drawn line tier
var rec2 = window.springEase({ feel: "snappy" });
var st = { s: 0 };
var deform = el.querySelector(".hw-co-deform");
gsap.set(deform, { transformOrigin: "50% 100%" });
// volume-preserving squash on the SHAPES wrapper only — the label
// rides the uniform-scale pop OUTSIDE the deform, so non-uniform
// scale never reaches glyphs. Read-back assertion enforces the law.
window.hwOnUpdate(tl, function () {
var along = 1 - st.s;
gsap.set(deform, { scaleY: along, scaleX: 1 / along });
var ax = parseFloat(gsap.getProperty(deform, "scaleX"));
var ay = parseFloat(gsap.getProperty(deform, "scaleY"));
if (Math.abs(ax * ay - 1) > 0.01)
console.error("hw volume law violated (callout): " + (ax * ay).toFixed(4));
});
// contact: squash in 2 frames, 1-frame hold, spring home (factory
// duration consumed verbatim)
tl.to(st, { s: S, duration: 2 / 30, ease: "power3.out" }, t + 0.3);
tl.to(st, { s: 0, duration: rec2.duration, ease: rec2.ease }, t + 0.3 + 3 / 30);
// boil control (family base) — x/y/rotation on the boil wrapper only
window.hwBoilPose(tl, el.querySelector(".hw-co-boil"), co.boil, { seed: co.seed });
};
window.hwCalloutOff = function (tl, target, at) {
tl.to(target, { opacity: 0, duration: 0.35, ease: "power2.in" }, at);
};
})();
/* ==== from HF registry: hw-underline (helpers verbatim, self-preview removed) ==== */
(function(){
window.hwOnUpdate = function (tl, fn) {
if (!tl.__hwRenders) {
tl.__hwRenders = [];
tl.eventCallback("onUpdate", function () {
for (var i = 0; i < tl.__hwRenders.length; i++) tl.__hwRenders[i]();
});
}
tl.__hwRenders.push(fn);
fn();
};
window.hwHash = function (n, seed) {
var x = Math.sin(n * 127.1 + (seed || 1) * 311.7) * 43758.5453;
return (x - Math.floor(x)) * 2 - 1;
};
window.hwBoil = function (tl, target, opts) {
opts = opts || {};
var amp = opts.amp !== undefined ? opts.amp : 1.6;
var rot = opts.rot !== undefined ? opts.rot : 0.5;
var fps = opts.fps || 30;
var drop = opts.frameDrop || 3;
var seed = opts.seed || 1;
var els = gsap.utils.toArray(target);
window.hwOnUpdate(tl, function () {
var step = Math.floor((tl.time() * fps) / drop);
for (var i = 0; i < els.length; i++) {
gsap.set(els[i], {
x: window.hwHash(step * 3 + i * 97, seed) * amp,
y: window.hwHash(step * 3 + 1 + i * 97, seed) * amp,
rotation: (opts.baseRot || 0) + window.hwHash(step * 3 + 2 + i * 97, seed) * rot,
});
}
});
};
// ---- hw-mark helpers (copy these too) ----
// style: "underline" (squiggle) | "strike" (double pass) | "bracket"
window.hwMarkPath = function (w, h, style, seed) {
seed = seed || 1;
function wob(n, range) {
return window.hwHash(n, seed) * (range === undefined ? 5 : range);
}
var mid = h / 2;
if (style === "bracket") {
return (
"M " +
(w * 0.9 + wob(1, 3)) +
" " +
(2 + wob(2, 3)) +
" Q " +
wob(3, 4) +
" " +
(2 + wob(4)) +
" " +
(4 + wob(5, 3)) +
" " +
(h * 0.5 + wob(6)) +
" Q " +
wob(7, 4) +
" " +
(h - 2 + wob(8)) +
" " +
(w * 0.9 + wob(9, 3)) +
" " +
(h - 2 + wob(10, 3))
);
}
if (style === "strike") {
return (
"M " +
(2 + wob(1, 3)) +
" " +
(mid + wob(2)) +
" L " +
(w - 4 + wob(3, 3)) +
" " +
(mid - 3 + wob(4)) +
" M " +
(w - 10 + wob(5, 4)) +
" " +
(mid + 7 + wob(6)) +
" L " +
(8 + wob(7, 4)) +
" " +
(mid + 3 + wob(8))
);
}
/* underline squiggle */
var segs = Math.max(5, Math.round(w / 95));
var d = "M 2 " + (mid + wob(0, 4));
for (var i = 1; i <= segs; i++) {
var x = (w / segs) * i;
d +=
" Q " +
(x - w / segs / 2 + wob(i * 4 + 1, 9)) +
" " +
(mid + (i % 2 ? -1 : 1) * (9 + wob(i * 4 + 2, 4))) +
" " +
x.toFixed(1) +
" " +
(mid + wob(i * 4 + 3, 4));
}
return d;
};
window.hwMarkOn = function (tl, target, at, dur) {
var p = document.querySelector(target + " path");
var len = p.getTotalLength();
gsap.set(p, { strokeDasharray: len, strokeDashoffset: len });
tl.to(
p,
{ strokeDashoffset: 0, duration: dur === undefined ? 0.55 : dur, ease: "power2.inOut" },
at,
);
};
window.hwMarkOff = function (tl, target, at) {
tl.to(target, { opacity: 0, duration: 0.3, ease: "power2.in" }, at);
};
})();
/* ==== from HF registry: hw-arrow (helpers verbatim, self-preview removed) ==== */
(function(){
window.hwOnUpdate = function (tl, fn) {
if (!tl.__hwRenders) {
tl.__hwRenders = [];
tl.eventCallback("onUpdate", function () {
for (var i = 0; i < tl.__hwRenders.length; i++) tl.__hwRenders[i]();
});
}
tl.__hwRenders.push(fn);
fn();
};
window.hwHash = function (n, seed) {
var x = Math.sin(n * 127.1 + (seed || 1) * 311.7) * 43758.5453;
return (x - Math.floor(x)) * 2 - 1;
};
window.hwBoil = function (tl, target, opts) {
opts = opts || {};
var amp = opts.amp !== undefined ? opts.amp : 1.6;
var rot = opts.rot !== undefined ? opts.rot : 0.5;
var fps = opts.fps || 30;
var drop = opts.frameDrop || 3;
var seed = opts.seed || 1;
var els = gsap.utils.toArray(target);
window.hwOnUpdate(tl, function () {
var step = Math.floor((tl.time() * fps) / drop);
for (var i = 0; i < els.length; i++) {
gsap.set(els[i], {
x: window.hwHash(step * 3 + i * 97, seed) * amp,
y: window.hwHash(step * 3 + 1 + i * 97, seed) * amp,
rotation: (opts.baseRot || 0) + window.hwHash(step * 3 + 2 + i * 97, seed) * rot,
});
}
});
};
// boil control: authored poses over the family-locked frameDrop.
// Unknown pose → default + console error (bounded-controls law).
window.hwBoilPose = function (tl, target, pose, opts) {
opts = opts || {};
var poses = { calm: { amp: 1.6, rot: 0.5 }, lively: { amp: 2.6, rot: 0.9 } };
if (pose === "off") return;
var p = poses[pose];
if (!p) {
console.error('hw: unknown boil pose "' + pose + '" — falling back to "calm"');
p = poses.calm;
}
window.hwBoil(tl, target, { amp: p.amp, rot: p.rot, frameDrop: 3, seed: opts.seed || 1 });
};
/* ---- spring library (snappy register; factory duration consumed verbatim) ---- */
var LN1000 = Math.log(1000);
var FEELS = {
snappy: { zeta: 0.9, response: 0.22 },
};
window.springEase = function (opts) {
var f = opts.feel ? FEELS[opts.feel] : opts;
var zeta = f.zeta,
response = f.response;
var w0 = (2 * Math.PI) / response;
var T = LN1000 / (zeta * w0);
var x;
if (zeta < 1) {
var wd = w0 * Math.sqrt(1 - zeta * zeta);
x = function (t) {
return (
1 -
Math.exp(-zeta * w0 * t) *
(Math.cos(wd * t) + ((zeta * w0) / wd) * Math.sin(wd * t))
);
};
} else {
x = function (t) {
return 1 - Math.exp(-w0 * t) * (1 + w0 * t);
};
}
var xT = x(T);
return {
ease: function (p) {
return p >= 1 ? 1 : x(p * T) / xT;
},
duration: T,
zeta: zeta,
response: response,
};
};
/* ---- pen-velocity ease: pure function of the path geometry ----
Sample 25–75 curvature-adaptive points, speed ∝ 1/(1+k·curvature)
floored at 0.22 (a pen slows into curves, never stalls), integrate
ds/speed, invert to a normalized time→distance ease. Seek-safe:
no per-frame state. */
window.hwPenEase = function (pathEl, opts) {
opts = opts || {};
var kCurve = opts.kCurve !== undefined ? opts.kCurve : 10;
var L = pathEl.getTotalLength();
if (L <= 0)
return {
ease: function (t) {
return t;
},
samples: 2,
inkTime: 1,
len: 0,
};
var PRE = 48;
var pts = [];
for (var i = 0; i <= PRE; i++) pts.push(pathEl.getPointAtLength((L * i) / PRE));
var curv = [0];
for (i = 1; i < PRE; i++) {
var ax = pts[i].x - pts[i - 1].x,
ay = pts[i].y - pts[i - 1].y;
var bx = pts[i + 1].x - pts[i].x,
by = pts[i + 1].y - pts[i].y;
var la = Math.hypot(ax, ay) || 1e-6,
lb = Math.hypot(bx, by) || 1e-6;
var dot = Math.max(-1, Math.min(1, (ax * bx + ay * by) / (la * lb)));
curv.push(Math.acos(dot) / ((la + lb) / 2));
}
curv.push(0);
var mass = 0;
for (i = 0; i < curv.length; i++) mass += curv[i];
var N = Math.max(25, Math.min(75, Math.round(25 + mass * 18)));
var seg = L / N;
var times = [0];
var t = 0;
for (i = 1; i <= N; i++) {
var u = ((i - 0.5) / N) * PRE;
var i0 = Math.floor(u),
f = u - i0;
var c = curv[Math.min(i0, PRE)] * (1 - f) + curv[Math.min(i0 + 1, PRE)] * f;
var speed = Math.max(1 / (1 + kCurve * c * 14), 0.22);
t += seg / speed;
times.push(t);
}
var total = times[N];
for (i = 0; i <= N; i++) times[i] /= total;
var easeFn = function (p) {
if (p <= 0) return 0;
if (p >= 1) return 1;
var lo = 0,
hi = N;
while (hi - lo > 1) {
var mid = (lo + hi) >> 1;
if (times[mid] <= p) lo = mid;
else hi = mid;
}
var span = times[hi] - times[lo] || 1e-9;
return (lo + (p - times[lo]) / span) / N;
};
return { ease: easeFn, samples: N, inkTime: total, len: L };
};
/* ---- stroke matrix: plain | soft | sharp | spray -------------------
strokeType is a g09 variant control. Authored constants per type;
unknown value → default ("plain") + console error (bounded-controls
law). sharp = micro stroke-dasharray gaps (dry marker) — draw-on
therefore rides a MASK clone (the visible dasharray never animates).
spray = index-seeded deterministic dots along the path via hwHash;
NO feTurbulence anywhere.
Returns { draw: pathToDashAnimate, group: maskedGroupOrNull } */
window.hwStrokeTypes = {
plain: {},
soft: { blur: 0.6 },
sharp: { dash: [3.0, 1.55] }, // × stroke-width
spray: {
coreOpacity: 0.5,
coreBlur: 0.4,
density: 0.16,
scatter: 2.6,
rMin: 1.1,
rMax: 2.6,
},
};
window.hwStrokeApply = function (pathEl, type, opts) {
opts = opts || {};
var cfg = window.hwStrokeTypes[type];
if (!cfg) {
console.error('hw: unknown strokeType "' + type + '" — falling back to "plain"');
type = "plain";
cfg = window.hwStrokeTypes.plain;
}
var svg = pathEl.ownerSVGElement;
var ns = "http://www.w3.org/2000/svg";
var w = parseFloat(getComputedStyle(pathEl).strokeWidth) || 6;
if (type === "plain") return { draw: pathEl, group: null, type: type };
if (type === "soft") {
pathEl.style.filter = "blur(" + cfg.blur + "px)";
return { draw: pathEl, group: null, type: type };
}
// sharp + spray: wrap in a masked group, draw-on animates the mask clone
var id =
opts.id ||
"hwsm-" + Math.abs(Math.round(window.hwHash((opts.seed || 1) * 13.7, 5) * 1e6));
var defs = svg.querySelector("defs");
if (!defs) {
defs = document.createElementNS(ns, "defs");
svg.insertBefore(defs, svg.firstChild);
}
var mask = document.createElementNS(ns, "mask");
mask.setAttribute("id", id);
mask.setAttribute("maskUnits", "userSpaceOnUse");
var clone = document.createElementNS(ns, "path");
clone.setAttribute("d", pathEl.getAttribute("d"));
clone.setAttribute("fill", "none");
clone.setAttribute("stroke", "#fff");
clone.setAttribute(
"stroke-width",
w * (type === "spray" ? 2 + (cfg.scatter * 2) / w : 1.7),
);
clone.setAttribute("stroke-linecap", "round");
clone.setAttribute("stroke-linejoin", "round");
mask.appendChild(clone);
defs.appendChild(mask);
var group = document.createElementNS(ns, "g");
group.setAttribute("mask", "url(#" + id + ")");
pathEl.parentNode.insertBefore(group, pathEl);
group.appendChild(pathEl);
if (type === "sharp") {
pathEl.setAttribute(
"stroke-dasharray",
(cfg.dash[0] * w).toFixed(1) + " " + (cfg.dash[1] * w).toFixed(1),
);
} else {
// spray: faint core + seeded dots
pathEl.setAttribute("stroke-opacity", cfg.coreOpacity);
pathEl.style.filter = "blur(" + cfg.coreBlur + "px)";
var len = pathEl.getTotalLength();
var n = Math.round(len * cfg.density * (opts.densityScale || 1));
var seed = opts.seed || 1;
var dots = document.createElementNS(ns, "g");
for (var i = 0; i < n; i++) {
var t = Math.min(
1,
Math.max(0, (i + 0.5) / n + (window.hwHash(i * 7, seed) * 0.35) / n),
);
var pt = pathEl.getPointAtLength(t * len);
var pt2 = pathEl.getPointAtLength(Math.min(len, t * len + 0.5));
var tx = pt2.x - pt.x,
ty = pt2.y - pt.y;
var tl2 = Math.hypot(tx, ty) || 1;
var nx = -ty / tl2,
nyv = tx / tl2;
var off = window.hwHash(i * 7 + 1, seed) * (cfg.scatter + w * 0.55);
var r = cfg.rMin + Math.abs(window.hwHash(i * 7 + 2, seed)) * (cfg.rMax - cfg.rMin);
var c = document.createElementNS(ns, "circle");
c.setAttribute("cx", (pt.x + nx * off).toFixed(1));
c.setAttribute("cy", (pt.y + nyv * off).toFixed(1));
c.setAttribute("r", r.toFixed(2));
c.setAttribute("fill", getComputedStyle(pathEl).stroke);
dots.appendChild(c);
}
group.appendChild(dots);
}
return { draw: clone, group: group, type: type };
};
/* Draw-on across the stroke matrix: dash-animate whatever hwStrokeApply
says. The draw target is always solid (plain/soft: the path itself;
sharp/spray: the mask clone — the visible path keeps its texture
dasharray untouched). Optionally pen-velocity eased (opts.pen). */
window.hwDrawOn = function (tl, applied, at, dur, opts) {
opts = opts || {};
var p = applied.draw;
var len = p.getTotalLength();
// the draw target is always solid (plain/soft: the path itself; sharp/spray:
// the mask clone — the visible path keeps its texture dasharray untouched)
p.setAttribute("stroke-dasharray", len + " " + len);
p.setAttribute("stroke-dashoffset", len);
gsap.set(p, { opacity: 0 }); // kill the round-cap start nub pre-draw
var ease = opts.pen ? window.hwPenEase(p, opts).ease : opts.ease || "power2.inOut";
tl.set(p, { opacity: 1 }, at);
tl.to(
p,
{ strokeDashoffset: 0, duration: dur === undefined ? 0.7 : dur, ease: ease },
at,
);
};
// ---- hw-arrow helpers (copy these too) ----
// Wobbled arrow path in a w x h box, tail at left, head at right.
// curve: "straight" | "gentle" | "swoop"
window.hwArrowPath = function (w, h, curve, seed) {
seed = seed || 1;
var bend = curve === "swoop" ? 0.55 : curve === "gentle" ? 0.28 : 0.06;
function wob(v, n, range) {
return v + window.hwHash(n, seed) * range;
}
var x0 = wob(4, 1, 3),
y0 = wob(h * 0.72, 2, 4),
x1 = wob(w - 10, 3, 4),
y1 = wob(h * 0.3, 4, 4);
var cx1 = wob(w * 0.35, 5, 10),
cy1 = wob(h * (0.72 + bend * 0.5), 6, 8);
var cx2 = wob(w * 0.7, 7, 10),
cy2 = wob(h * (0.3 + bend), 8, 8);
var ang = Math.atan2(y1 - cy2, x1 - cx2);
function head(da, len) {
var a = ang + Math.PI + da;
return (
"M " +
x1.toFixed(1) +
" " +
y1.toFixed(1) +
" l " +
(Math.cos(a) * len).toFixed(1) +
" " +
(Math.sin(a) * len).toFixed(1)
);
}
return (
"M " +
x0.toFixed(1) +
" " +
y0.toFixed(1) +
" C " +
cx1.toFixed(1) +
" " +
cy1.toFixed(1) +
", " +
cx2.toFixed(1) +
" " +
cy2.toFixed(1) +
", " +
x1.toFixed(1) +
" " +
y1.toFixed(1) +
" " +
head(0.5, wob(34, 9, 6)) +
" " +
head(-0.5, wob(34, 10, 6))
);
};
// ---- hwArrowBuild: CONFIG-reading component builder (g09) ----
// Builds the arrow as TWO paths (curve + head group) inside the
// target's svg with the same wobble math and seed slots as
// hwArrowPath, applies CONFIG.strokeType to the curve (the head
// stays plain — texture on a ~30px head reads as noise), and rigs
// the travel-aligned arrival wrapper: an outer g rotated to the
// travel direction + an inner g counter-rotated. The pair cancels,
// so geometry stays put while scaleX on the outer acts along
// travel. NO transform-box styles on these g's — they fight GSAP's
// baked SVG origins (the outer's fill-box would derive from its
// rotated child and the cancellation breaks).
// `seed` is internal — defaults per target from the element id
// (a pure hash of the id string: deterministic across page loads);
// it is NOT a control. Targets must carry unique ids: id-less
// targets fall back to a shared "hw-arrow" slot (identical wobble;
// duplicate mask ids cross-wire sharp/spray reveals), so that case
// fails loudly (bounded-controls law).
window.hwArrowBuild = function (target, CONFIG, seed) {
CONFIG = CONFIG || {};
var el = typeof target === "string" ? document.querySelector(target) : target;
var svg = el.querySelector("svg");
if (!el.id)
console.error(
'hw: hwArrowBuild target has no id — falling back to the shared "hw-arrow" seed/mask slot; give each built target a unique id',
);
if (seed === undefined) {
var idStr = el.id || "hw-arrow";
seed = 0;
for (var si = 0; si < idStr.length; si++)
seed = (seed * 31 + idStr.charCodeAt(si)) % 997;
seed = (seed % 96) + 1;
}
var style = CONFIG.arrowStyle === undefined ? "gentle" : CONFIG.arrowStyle;
if (style !== "straight" && style !== "gentle" && style !== "swoop") {
console.error('hw: unknown arrowStyle "' + style + '" — falling back to "gentle"');
style = "gentle";
}
var vb = svg.viewBox.baseVal;
var w = vb && vb.width ? vb.width : el.clientWidth;
var h = vb && vb.height ? vb.height : el.clientHeight;
// same wobble math + hash slots as hwArrowPath (1–8 body, 9/10 head)
var bend = style === "swoop" ? 0.55 : style === "gentle" ? 0.28 : 0.06;
function wob(v, n, range) {
return v + window.hwHash(n, seed) * range;
}
var x0 = wob(4, 1, 3),
y0 = wob(h * 0.72, 2, 4),
x1 = wob(w - 10, 3, 4),
y1 = wob(h * 0.3, 4, 4);
var cx1 = wob(w * 0.35, 5, 10),
cy1 = wob(h * (0.72 + bend * 0.5), 6, 8);
var cx2 = wob(w * 0.7, 7, 10),
cy2 = wob(h * (0.3 + bend), 8, 8);
var ang = Math.atan2(y1 - cy2, x1 - cx2);
function headSeg(da, len) {
var a = ang + Math.PI + da;
return (
"M " +
x1.toFixed(1) +
" " +
y1.toFixed(1) +
" l " +
(Math.cos(a) * len).toFixed(1) +
" " +
(Math.sin(a) * len).toFixed(1)
);
}
var ns = "http://www.w3.org/2000/svg";
var curve = document.createElementNS(ns, "path");
curve.setAttribute(
"d",
"M " +
x0.toFixed(1) +
" " +
y0.toFixed(1) +
" C " +
cx1.toFixed(1) +
" " +
cy1.toFixed(1) +
", " +
cx2.toFixed(1) +
" " +
cy2.toFixed(1) +
", " +
x1.toFixed(1) +
" " +
y1.toFixed(1),
);
svg.appendChild(curve);
var headG = document.createElementNS(ns, "g");
var headInner = document.createElementNS(ns, "g");
var head = document.createElementNS(ns, "path");
head.setAttribute("d", headSeg(0.5, wob(34, 9, 6)) + " " + headSeg(-0.5, wob(34, 10, 6)));
headInner.appendChild(head);
headG.appendChild(headInner);
svg.appendChild(headG);
// curve texture; mask id derived from the element id — deterministic
// and collision-free across id-bearing instances (id-less targets
// errored above and share the fallback)
var applied = window.hwStrokeApply(
curve,
CONFIG.strokeType === undefined ? "plain" : CONFIG.strokeType,
{ seed: seed, id: (el.id || "hw-arrow") + "-sm" },
);
if (applied.group) {
// the item CSS (.hw-arrow path) outranks the mask clone's
// presentation attributes — pin them as inline styles so the
// reveal mask keeps its authored width and white stroke
applied.draw.style.stroke = "#fff";
applied.draw.style.strokeWidth = applied.draw.getAttribute("stroke-width");
}
// arrival rig: rotate the WRAPPER to travel, counter-rotate the
// content — wrapper scaleX then acts along travel
var angDeg = (ang * 180) / Math.PI;
gsap.set(headG, { transformOrigin: "50% 50%", rotation: angDeg, opacity: 0 });
gsap.set(headInner, { transformOrigin: "50% 50%", rotation: -angDeg });
el.__hwArrowBuild = {
el: el,
applied: applied,
headG: headG,
headInner: headInner,
seed: seed,
boil: CONFIG.boil === undefined ? "calm" : CONFIG.boil,
boiled: false,
};
return el.__hwArrowBuild;
};
// Draw-on. Built targets (hwArrowBuild): accelerating draw (causal
// arrival), then the head pops with the g21 travel-aligned stretch —
// v measured numerically from the draw ease end-slope, S = min(0.06
// × v/2000, 0.25), volume preserved (scaleX × scaleY ≈ 1, asserted),
// 2-frame apply + snappy spring recovery (factory duration verbatim).
// Legacy single-path targets keep the shipped dashoffset behavior.
window.hwArrowOn = function (tl, target, at, dur) {
var el = typeof target === "string" ? document.querySelector(target) : target;
var b = el && el.__hwArrowBuild;
if (!b) {
var p = document.querySelector(target + " path");
var len = p.getTotalLength();
gsap.set(p, { strokeDasharray: len, strokeDashoffset: len });
tl.to(
p,
{
strokeDashoffset: 0,
duration: dur === undefined ? 0.7 : dur,
ease: "power2.inOut",
},
at,
);
return;
}
// built targets (g09): durations are item-locked — a
// legacy-signature `dur` is ignored + console.error (edit the item
// source to change); the legacy branch above keeps honoring it
if (dur !== undefined)
console.error(
"hw: hwArrowOn draw duration is locked for built targets — ignoring dur=" + dur,
);
dur = 0.7;
// the pen ACCELERATES into the head (power2.in) so the measured
// arrival velocity is real — a settling ease ends at ~zero slope
// and would make the stretch invisible
var drawEase = gsap.parseEase("power2.in");
window.hwDrawOn(tl, b.applied, at, dur, { ease: drawEase });
var dlen = b.applied.draw.getTotalLength();
var eps = 1e-4;
var v = ((drawEase(1) - drawEase(1 - eps)) / eps) * (dlen / dur);
var S = Math.min(0.06 * (v / 2000), 0.25);
var rec = window.springEase({ feel: "snappy" });
var FPS = 30;
var st = { s: 0 };
var hg = b.headG;
window.hwOnUpdate(tl, function () {
// stretch ALONG travel (the wrapper's local x after rotation)
var along = 1 + st.s;
gsap.set(hg, { scaleX: along, scaleY: 1 / along });
var ax = parseFloat(gsap.getProperty(hg, "scaleX"));
var ay = parseFloat(gsap.getProperty(hg, "scaleY"));
if (Math.abs(ax * ay - 1) > 0.01)
console.error("hw volume law violated (arrowhead): " + (ax * ay).toFixed(4));
});
tl.set(hg, { opacity: 1 }, at + dur);
tl.to(st, { s: S, duration: 2 / FPS, ease: "power3.out" }, at + dur);
tl.to(st, { s: 0, duration: rec.duration, ease: rec.ease }, at + dur + 3 / FPS);
// boil pose (recorded from CONFIG at build; hwBoil needs the
// timeline, so it registers on first wiring). Boil owns x/y/rotation
// of the WRAPPER element — the arrival stretch lives on the nested
// head rig, never on the boiled element.
if (!b.boiled) {
b.boiled = true;
window.hwBoilPose(tl, el, b.boil, { seed: b.seed });
}
};
window.hwArrowOff = function (tl, target, at) {
tl.to(target, { opacity: 0, duration: 0.3, ease: "power2.in" }, at);
};
})();