Files
hyperframes/packages/core/src/inline-scripts/parityContract.ts
T
miga-heygenandMiguel Ángel e23b3a3e31 fix(engine): copy border-width/style/color onto the replacement video frame (#3993)
* fix(engine): copy border-width/style/color onto the replacement video frame

injectVideoFramesBatch substitutes each <video> with a sibling <img> holding
an extracted still frame, copying an allow-listed set of CSS properties from
the video's computed style onto the img. border-width/border-style/
border-color were absent from that allow-list, so any border authored
directly on a <video> never reached the replacement image and disappeared
from render/snapshot output. border-radius and clip-path were already on the
list and already clip a replaced element's content correctly without
needing overflow:hidden, confirmed by a real-Chromium test that exercises
the actual capture path and reads real screenshot pixels.

Also corrected a stale comment in frameCapture.ts claiming
detectCssEffectRisk can return "clip-path" as a risk value -- it can't; that
string only ever comes from a separate, animation-only at-risk-props gate
further down the same file.

Co-Authored-By: Miguel Ángel <miguel.sierra@heygen.com>

* test(producer): regenerate style-9-prod golden for the border fix

style-9-prod's video has an authored 8px border + 16px border-radius
(design_review.md calls out the "floating" look) that never rendered before
this fix, so its committed golden baked in the pre-fix (borderless) output.
Regenerated via `tsx src/regression-harness.ts style-9-prod --update`;
verified the diff is exactly the border/radius becoming visible (extracted
frames before/after) and the fixture passes its own visual/audio checks
again (100/100 checkpoints, correlation 1.000).

Co-Authored-By: Miguel Ángel <miguel.sierra@heygen.com>

* fix(engine): measure the video's box before styling its replacement frame

injectVideoFramesBatch measured the video's used box (offsetLeft/Top/
Width/Height) after creating, inserting, and styling the replacement
<img> sibling -- once that copy loop started carrying border-width
(added for the border-visibility fix), the freshly-inserted, still
in-flow, now-bordered <img> competed for space in a flex row before
the video's own box was ever read. In a flex-centered layout (video
width:100%, sharing a row with the img sibling), that shrank the
measured video box by the exact width of the border, so the img ended
up sized from the video's post-shrink box instead of its authored one.

Moved the measurement to before the <img> is created/inserted/styled
at all, and set img.style.boxSizing = "border-box" explicitly after
the style-copy loop, since offsetWidth/offsetHeight (and the
getBoundingClientRect fallback) are always a border-box measurement
regardless of what box-sizing value that loop copies from the video's
own computed style.

Added a flex-centered regression test asserting the replacement img's
box stays identical to the video's box; confirmed it fails with the
old measurement order (a 16px border shrinks a 500px box to 484px) and
passes with the fix. Regenerated the one fixture (style-9-prod) whose
committed golden had baked in the shrunk-frame geometry.

Co-Authored-By: Miguel Ángel <miguel.sierra@heygen.com>

* refactor(engine): consolidate video-box measurement into one object

Follow-up to c88890357: group the four measurement locals into a
single videoBox object instead of four loose consts, and trim the
surrounding comments down to their load-bearing content. No behavior
change -- same measurement order, same fallback conditions, same
applied style values; re-verified red (reverting the measurement order
still reproduces the 500px -> 484px shrink) and green.

Co-Authored-By: Miguel Ángel <miguel.sierra@heygen.com>

* refactor(engine): trim the border-fix comments to the invariant

* test(producer): keep main's style-9-prod compiled.html; timings are unchanged

* test(producer): repin the classification source hash for test reachability

* fix(core): a color-graded bordered source keeps its canvas inside its border box

* fix(core): a graded source with an empty content box still shows its border

* fix(core): a border-only graded canvas stays hidden until the source has drawn

* fix(core): border-only graded canvas shows only while grading hides the source

* test(core): pin the border-only canvas hide after a context loss

* fix(core): grading canvas carries no border, so a graded source never paints it twice

* fix(core): graded injected frame drops its border once grading draws over it

* fix(core): a released grading entry gives the injected frame its border back

* fix(core): a released injected frame takes the video's current border style

---------

Co-authored-by: Miguel Ángel <miguel.sierra@heygen.com>
2026-10-10 18:17:23 +00:00

112 lines
3.2 KiB
TypeScript

export const MEDIA_VISUAL_STYLE_PROPERTIES = [
"width",
"height",
"top",
"left",
"right",
"bottom",
"inset",
"object-fit",
"object-position",
"z-index",
"opacity",
"visibility",
"filter",
"mix-blend-mode",
"backdrop-filter",
"border-width",
"border-style",
"border-color",
"border-radius",
"overflow",
"clip-path",
"mask",
"mask-image",
"mask-size",
"mask-position",
"mask-repeat",
"transform",
"transform-origin",
"translate",
"rotate",
"scale",
"box-sizing",
] as const;
export type MediaVisualStyleProperty = (typeof MEDIA_VISUAL_STYLE_PROPERTIES)[number];
const FRAME_BOUNDARY_EPSILON = 1e-3;
function safeFps(fps: number): number {
return Number.isFinite(fps) && fps > 0 ? fps : 30;
}
function safeTime(timeSeconds: number): number {
return Number.isFinite(timeSeconds) && timeSeconds > 0 ? timeSeconds : 0;
}
export function quantizeTimeToFrame(timeSeconds: number, fps: number): number {
const grid = safeFps(fps);
const frameIndex = Math.floor(safeTime(timeSeconds) * grid + 1e-9);
return frameIndex / grid;
}
/**
* The grid a deterministic render seek lands on.
*
* With no `subFrameDivisions` this is the output frame grid, unchanged. Sub-frame
* sampling (motion blur) passes an integer subdivision count, which can only refine
* the output grid, never move a frame-time instant off it.
*
* The finer grid rounds where the frame grid floors. A sub-frame time makes a float
* round trip (the caller builds `ticks / (fps * divisions)`, this recomputes
* `time * fps * divisions`), and past a few million ticks that error exceeds the
* floor epsilon, which would silently drop a sample one tick early.
*/
export function quantizeSeekTime(
timeSeconds: number,
fps: number,
subFrameDivisions?: number,
): number {
const divisions = Number.isInteger(subFrameDivisions) ? (subFrameDivisions as number) : 1;
if (divisions <= 1) return quantizeTimeToFrame(timeSeconds, fps);
const grid = safeFps(fps) * divisions;
return Math.round(safeTime(timeSeconds) * grid) / grid;
}
/** Snap decimal noise near a frame boundary without moving genuinely fractional timing. */
export function snapTimeToFrameBoundary(timeSeconds: number, fps: number): number {
const grid = safeFps(fps);
const time = safeTime(timeSeconds);
const framePosition = time * grid;
const nearestFrame = Math.round(framePosition);
return Math.abs(framePosition - nearestFrame) <= FRAME_BOUNDARY_EPSILON
? nearestFrame / grid
: time;
}
/** The window export shows a clip in: both ends snapped like its seeks, so a hair off a frame counts as on it. */
export function exportClipWindow(
start: number,
end: number,
fps: number,
): { start: number; end: number } {
return {
start: snapTimeToFrameBoundary(start, fps),
end: Number.isFinite(end) ? snapTimeToFrameBoundary(end, fps) : end,
};
}
export function copyMediaVisualStyles(
targetStyle: CSSStyleDeclaration,
sourceStyle: CSSStyleDeclaration,
properties: readonly string[] = MEDIA_VISUAL_STYLE_PROPERTIES,
): void {
for (const property of properties) {
const value = sourceStyle.getPropertyValue(property);
if (value) {
targetStyle.setProperty(property, value);
}
}
}