Skip to content

armgit / hypit

Public Imported from GitHub
0

Clone any viral video with AI agents. Not just a script, the whole workflow: swap the face, the words, the B-roll, ship 100 variants in one command, and get your 100M views.

fix: stop the boxes that clip from being measured without the ink they hold (#74)

rponeawa66860eae7076
diff --git a/packages/media-track/src/lower.ts b/packages/media-track/src/lower.ts
index ea9a4b6d..8f58b619 100644
--- a/packages/media-track/src/lower.ts
+++ b/packages/media-track/src/lower.ts
@@ -51,19 +51,27 @@ function paint(value: MediaPaint): string {
return `radial-gradient(circle at ${value.center.x * 100}% ${value.center.y * 100}%,${value.stops.map(stop).join(",")})`;
}
-function pathData(path: SpatialPath): string {
+/**
+ * A Path is drawn in Canvas pixels, and the element it clips is the Frame's own box — positioned at
+ * the Frame's origin, so its coordinates start there. Handed the Canvas numbers unchanged, the clip
+ * region lands one Frame origin down and to the right of where it was drawn, which for any Frame away
+ * from the corner is entirely outside the box: the Item renders blank.
+ */
+function pathData(path: SpatialPath, origin: SpatialFrame): string {
+ const x = (value: number): number => value - origin.xPx;
+ const y = (value: number): number => value - origin.yPx;
return path.commands.map((command) => {
switch (command.kind) {
- case "move": return `M ${command.xPx} ${command.yPx}`;
- case "line": return `L ${command.xPx} ${command.yPx}`;
- case "quadratic": return `Q ${command.controlX} ${command.controlY} ${command.xPx} ${command.yPx}`;
- case "cubic": return `C ${command.control1X} ${command.control1Y} ${command.control2X} ${command.control2Y} ${command.xPx} ${command.yPx}`;
+ case "move": return `M ${x(command.xPx)} ${y(command.yPx)}`;
+ case "line": return `L ${x(command.xPx)} ${y(command.yPx)}`;
+ case "quadratic": return `Q ${x(command.controlX)} ${y(command.controlY)} ${x(command.xPx)} ${y(command.yPx)}`;
+ case "cubic": return `C ${x(command.control1X)} ${y(command.control1Y)} ${x(command.control2X)} ${y(command.control2Y)} ${x(command.xPx)} ${y(command.yPx)}`;
case "close": return "Z";
}
}).join(" ");
}
-function frameStyles(value: MediaFramePresentation): VisualStyleDeclaration[] {
+function frameStyles(value: MediaFramePresentation, frame: SpatialFrame): VisualStyleDeclaration[] {
const styles: VisualStyleDeclaration[] = [
{ name: "box-sizing", value: "border-box" },
{ name: "height", value: "100%" },
@@ -76,7 +84,7 @@ function frameStyles(value: MediaFramePresentation): VisualStyleDeclaration[] {
else {
styles.push({ name: "overflow", value: "hidden" });
if (value.clip.kind === "rounded") styles.push({ name: "border-radius", value: px(value.clip.radiusPx) });
- if (value.clip.kind === "path") styles.push({ name: "clip-path", value: `path("${pathData(value.clip.path)}")` });
+ if (value.clip.kind === "path") styles.push({ name: "clip-path", value: `path("${pathData(value.clip.path, frame)}")` });
}
if (value.border !== undefined) {
styles.push({ name: "border", value: `${value.border.widthPx}px ${value.border.style} ${value.border.color}` });
@@ -91,13 +99,22 @@ function frameStyles(value: MediaFramePresentation): VisualStyleDeclaration[] {
return styles;
}
+/**
+ * The box a layer is fitted into: the Frame less its border and its padding.
+ *
+ * The border was left out, and it is drawn inward — `box-sizing: border-box` puts it inside the
+ * Frame's own rectangle — so a fitted picture was sized against a box wider and taller than the one
+ * it had to sit in. What that looks like is the picture offset by the border width down and to the
+ * right, with the same amount cut off its far edges, and a `contain` fit that no longer contains.
+ */
function innerFrame(item: MediaItemProgram): SpatialFrame {
- const { padding } = item.presentation;
+ const { border, padding } = item.presentation;
+ const edge = border?.widthPx ?? 0;
return {
- xPx: item.frame.xPx + padding.leftPx,
- yPx: item.frame.yPx + padding.topPx,
- widthPx: item.frame.widthPx - padding.leftPx - padding.rightPx,
- heightPx: item.frame.heightPx - padding.topPx - padding.bottomPx,
+ xPx: item.frame.xPx + edge + padding.leftPx,
+ yPx: item.frame.yPx + edge + padding.topPx,
+ widthPx: item.frame.widthPx - edge * 2 - padding.leftPx - padding.rightPx,
+ heightPx: item.frame.heightPx - edge * 2 - padding.topPx - padding.bottomPx,
};
}
@@ -131,6 +148,11 @@ function sampleElements(
{ name: "transform-origin", value: "center center" },
{ name: "width", value: px(content.widthPx) },
];
+ // A blur reads across the box and paints past it — roughly twice its radius — so a `contain` fit
+ // that was letterboxed on purpose had its blurred copy spilling into the letterbox it was fitted
+ // away from. Clipping the wrapper ends the overscan at the fitted rectangle; the Gaussian still
+ // reads real content across the whole box, so only the spill goes.
+ if (layer.appearance.filter.blurPx > 0) wrapperStyle.push({ name: "overflow", value: "hidden" });
const wrapperElement: VisualElement = {
id: wrapper,
parent,
@@ -297,7 +319,7 @@ export function lowerMediaItemElements(
parent = handoff;
}
const frame = `${item.id}:frame`;
- elements.push({ id: frame, parent, order: order.value++, kind: "box", style: frameStyles(item.presentation) });
+ elements.push({ id: frame, parent, order: order.value++, kind: "box", style: frameStyles(item.presentation, item.frame) });
for (const layer of item.layers) {
elements.push(...layerElements(
layer,
diff --git a/packages/media-track/src/motion.ts b/packages/media-track/src/motion.ts
index a3557478..48ddf6f8 100644
--- a/packages/media-track/src/motion.ts
+++ b/packages/media-track/src/motion.ts
@@ -111,9 +111,21 @@ function translate(directionValue: MediaMotionDirection, amount: number): string
return `translateY(${amount}px)`;
}
-function style(state: MotionState): VisualStyleDeclaration[] {
+/**
+ * `clipping` is decided once per animation, not per keyframe.
+ *
+ * `inset(0% 0% 0% 0%)` reads as a no-op and is not one: it clips the element and its descendants to
+ * the border box, which removes the Frame's own drop shadows for the whole window. So an Item given
+ * any lifecycle at all — a plain fade among them — lost its shadows, while the same Item with no
+ * enter or exit kept them.
+ *
+ * Every keyframe of one animation has to declare the same properties, so a wipe cannot drop the
+ * property on the frames where it happens to be neutral: it would hold its last declared inset for
+ * ever. The question is whether this animation clips anywhere.
+ */
+function style(state: MotionState, clipping: boolean): VisualStyleDeclaration[] {
return [
- { name: "clip-path", value: state.clipPath },
+ ...(clipping ? [{ name: "clip-path", value: state.clipPath }] : []),
{ name: "filter", value: state.filter },
{ name: "opacity", value: state.opacity },
{ name: "transform", value: state.transform },
@@ -130,11 +142,13 @@ export function lifecycleAnimation(value: MediaLifecycleMotion, durationFrames:
if (value.exit !== undefined) {
for (let frame = Math.max(0, durationFrames - value.exit.durationFrames); frame <= durationFrames; frame += 1) frames.add(frame);
}
+ const ordered = [...frames].sort((left, right) => left - right);
+ const clipping = ordered.some((atFrame) => lifecycleStateAt(value, durationFrames, atFrame).clipPath !== neutral.clipPath);
return {
- keyframes: [...frames].sort((left, right) => left - right).map((atFrame) => ({
+ keyframes: ordered.map((atFrame) => ({
atFrame,
easing: "linear",
- style: style(lifecycleStateAt(value, durationFrames, atFrame)),
+ style: style(lifecycleStateAt(value, durationFrames, atFrame), clipping),
})),
};
}
@@ -339,11 +353,12 @@ export function lifecycleAnimationWindow(
const next = lifecycleStateAt(value, outerDurationFrames, ordered[index + 1]!);
return JSON.stringify(previous) !== JSON.stringify(current) || JSON.stringify(current) !== JSON.stringify(next);
});
+ const clipping = compact.some((frame) => lifecycleStateAt(value, outerDurationFrames, frame).clipPath !== neutral.clipPath);
return {
keyframes: compact.map((frame) => ({
atFrame: frame - startOffset,
easing: "linear",
- style: style(lifecycleStateAt(value, outerDurationFrames, frame)),
+ style: style(lifecycleStateAt(value, outerDurationFrames, frame), clipping),
})),
};
}
diff --git a/packages/media-track/test/media-track.test.ts b/packages/media-track/test/media-track.test.ts
index 58cd2e3a..13cbf5fb 100644
--- a/packages/media-track/test/media-track.test.ts
+++ b/packages/media-track/test/media-track.test.ts
@@ -324,23 +324,28 @@ test("an Item keeps ordered Paint/sample layers, two-frame fit and frame present
const media = elements.find((element) => element.id === "content");
assert.equal(media?.kind, "image");
const wrapper = elements.find((element) => element.id === "content:sampling");
+ // The fitting box is the Frame less its 2px border and its 10px padding. `box-sizing: border-box`
+ // draws the border inside the Frame, so the content starts 12px in and is 24px smaller each way.
assert.deepEqual(wrapper?.style, [
- { name: "height", value: "280px" },
- { name: "left", value: "60px" },
+ { name: "height", value: "276px" },
+ { name: "left", value: "62px" },
{ name: "position", value: "absolute" },
- { name: "top", value: "10px" },
+ { name: "top", value: "12px" },
{ name: "transform-origin", value: "center center" },
- { name: "width", value: "280px" },
+ { name: "width", value: "276px" },
]);
});
test("an owned clip path is an explicit Media input and lowers only over the Item's pixels", () => {
+ // A Path is drawn in Canvas pixels, which is what `spatial` declares three times over. The Frame
+ // sits at 100,200, so a trapezoid filling it is written from there and arrives at the Frame's own
+ // box translated back to 0,0.
const clipped = bindMediaItemClipPath(itemSpec(), {
commands: [
- { kind: "move", xPx: 0, yPx: 0 },
- { kind: "line", xPx: 400, yPx: 0 },
- { kind: "line", xPx: 360, yPx: 300 },
- { kind: "line", xPx: 40, yPx: 300 },
+ { kind: "move", xPx: 100, yPx: 200 },
+ { kind: "line", xPx: 500, yPx: 200 },
+ { kind: "line", xPx: 460, yPx: 500 },
+ { kind: "line", xPx: 140, yPx: 500 },
{ kind: "close" },
],
});
diff --git a/packages/ranking/src/render.ts b/packages/ranking/src/render.ts
index a2eff832..1e4e74af 100644
--- a/packages/ranking/src/render.ts
+++ b/packages/ranking/src/render.ts
@@ -598,9 +598,15 @@ export function renderTopThree(space: ProgramSpace, program: TopThreeProgram): V
else elements.push(simpleText({ id: "rank", parent: root, order: 2, text: String(index + 1),
typography: { ...style.text, color: rankColor(style.slotColors, index), sizePx: style.iconSizePx * 0.45 },
x: 0, y: 0, width: style.iconSizePx, height: style.iconSizePx }));
+ // The label's box is one line box, and a line box is not the ink: accents reach above it and
+ // descenders below. It clips, so at a tight `line-height` the tops and tails were shaved. The
+ // box grows by one line box's worth of leading at each end and its top moves up by the same,
+ // so the text stays where it was painted and only the clip rectangle moves.
+ const labelSlack = style.text.sizePx * 0.25;
elements.push(simpleText({ id: "label", parent: root, order: 3, text: item.label, typography: style.text,
- x: -style.slotGapPx / 2, y: style.iconSizePx + style.labelGapPx,
- width: style.iconSizePx + style.slotGapPx, height: style.text.sizePx * style.text.lineHeight }));
+ x: -style.slotGapPx / 2, y: style.iconSizePx + style.labelGapPx - labelSlack,
+ width: style.iconSizePx + style.slotGapPx,
+ height: style.text.sizePx * style.text.lineHeight + labelSlack * 2 }));
return elements;
};
presents.push(present({