diff --git a/sdk/cloud/image/Framed.tsx b/sdk/cloud/image/Framed.tsx index 9e33b133..be450861 100644 --- a/sdk/cloud/image/Framed.tsx +++ b/sdk/cloud/image/Framed.tsx @@ -39,9 +39,11 @@ const median = (xs: number[]) => { /** the face's room from the crop's edge: a third of its own width either side */ const ROOM = 1 / 3; -/** the take's face at each sample from `a` to `b` seconds: [t, cx, ey, fw] */ -function path(take: Take, a: number, b: number, near?: number): [number, number, number, number][] { +/** the take's face at each sample from `a` to `b` seconds: [t, cx, ey, fw], and every other face's span across the + * take ([t, left, right]) */ +function path(take: Take, a: number, b: number, near?: number): { p: [number, number, number, number][]; others: [number, number, number][] } { const out: [number, number, number, number][] = []; + const others: [number, number, number][] = []; let last: number | undefined = near; for (let i = 0; i < take.t.length; i++) { const t = take.t[i]; @@ -52,8 +54,23 @@ function path(take: Take, a: number, b: number, near?: number): [number, number, const f = last === undefined ? fs[0] : fs.reduce((p, q) => (Math.abs(q[0] - last!) < Math.abs(p[0] - last!) ? q : p)); if (near === undefined) last = f[0]; out.push([t, f[0], f[1], f[2]]); + for (const o of fs) if (o !== f) others.push([t, o[0] - o[2] / 2, o[0] + o[2] / 2]); } - return out; + return { p: out, others }; +} + +/** the crop's left edge nearest `ideal` that keeps the framed face (lo..hi across the take) inside it and cuts no other + * face: each other face either whole in the crop or wholly out of it (12-05, Body Shop: the crop on the host cut the + * man beside her in half at the right edge). `ideal` when no such edge exists */ +function cleanLeft(ideal: number, vw: number, lo: number, hi: number, others: [number, number][]): number { + const most = Math.max(0, 1 - vw); + const a = clamp(hi - vw, 0, most); + const b = clamp(lo, 0, most); + if (a > b || !others.length) return ideal; + const cuts = (l: number) => others.some(([x1, x2]) => (l > x1 && l < x2) || (l + vw > x1 && l + vw < x2)); + const tries = [ideal, ...others.flatMap(([x1, x2]) => [x2 + 0.005, x1 - 0.005, x1 - vw - 0.005, x2 - vw + 0.005])]; + const ok = tries.filter((l) => l >= a && l <= b && l >= 0 && l <= most && !cuts(l)); + return ok.length ? ok.reduce((x, y) => (Math.abs(y - ideal) < Math.abs(x - ideal) ? y : x)) : ideal; } export default function Framed({ src, startFrom = 0, endAt, at, punchIn = 1, volume, playbackRate, muted, style }: Props) { @@ -74,7 +91,7 @@ export default function Framed({ src, startFrom = 0, endAt, at, punchIn = 1, vol const src_ = take.w / take.h; const vw = Math.min(1, box / src_) / k; const vh = Math.min(1, src_ / box) / k; - const p = path(take, a, b, at); + const { p, others } = path(take, a, b, at); if (p.length) { const xs = p.map((s) => s[1]); const fw = Math.max(...p.map((s) => s[3])); @@ -84,7 +101,13 @@ export default function Framed({ src, startFrom = 0, endAt, at, punchIn = 1, vol const cx = fits ? median(xs) : median((near.length ? near : p).map((s) => s[1])); const ey = median((fits || !near.length ? p : near).map((s) => s[2])); // the crop's left and top edges, in shares of the take: the face across its middle, the eyes a third down - const left = clamp(cx - vw / 2, 0, 1 - vw); + // the framed face's span the crop must hold (every sample's when still, the nearest second's when following), + // and the other faces there, which it mustn't cut + const held = fits || !near.length ? p : near; + const lo = Math.min(...held.map((s) => s[1] - s[3] / 2)); + const hi = Math.max(...held.map((s) => s[1] + s[3] / 2)); + const span = (fits ? others : others.filter((o) => Math.abs(o[0] - now) <= 0.5)).map((o) => [o[1], o[2]] as [number, number]); + const left = cleanLeft(clamp(cx - vw / 2, 0, 1 - vw), vw, lo, hi, span); const top = clamp(ey - vh / 3, 0, 1 - vh); if (k === 1) { pos = `${vw >= 1 ? 50 : (left / (1 - vw)) * 100}% ${vh >= 1 ? 50 : (top / (1 - vh)) * 100}%`; @@ -92,7 +115,7 @@ export default function Framed({ src, startFrom = 0, endAt, at, punchIn = 1, vol // cover at 1, then scaled about the point that puts the chosen window in the box const vw1 = vw * k; const vh1 = vh * k; - const l1 = clamp(cx - vw1 / 2, 0, 1 - vw1); + const l1 = cleanLeft(clamp(cx - vw1 / 2, 0, 1 - vw1), vw1, lo, hi, span); const t1 = clamp(ey - vh1 / 3, 0, 1 - vh1); pos = `${vw1 >= 1 ? 50 : (l1 / (1 - vw1)) * 100}% ${vh1 >= 1 ? 50 : (t1 / (1 - vh1)) * 100}%`; // the window's left edge at the box's left: scale about the point o where (o - l1)/vw1 = (o - left)/vw