chore(studio): trace what moves a dragged group and when

A drag that jumps is a position that changed without the pointer asking for it,
and nothing on that path says anything today, so the frame it diverges can only
be guessed at. `hf-drag-debug` (localStorage, off by default) records the whole
gesture: the mapping and start position each member got, the pointer delta
against the delta actually applied on every eighth move, what each member was
told to commit, and where they all sit at the drop, once the commit resolves, and
120/400/900ms later.

That last group is the point of it. The source write, the preview reload and the
timeline resume all land within a few frames of the drop, and any of them can put
the elements back where they started before the new position arrives — a
snap-back shows up as a settle sample reverting to the gesture-start reading.
A gap between `pointer` and `applied` instead means snapping pulled the group off
the cursor, which is a different fault with a different fix.
This commit is contained in:
Miguel Angel Simon Sierra
2026-08-09 19:10:54 -04:00
parent bccc22245d
commit fbc892c071
4 changed files with 152 additions and 2 deletions
@@ -33,6 +33,7 @@ import {
} from "./domEditOverlayGestures";
import { collectSnapContext, buildExcludeElements } from "./snapTargetCollection";
import { logResize, resetResizeMoveLog } from "../../utils/resizeDebug";
import { logDrag, readDragPositions, resetDragMoveLog } from "../../utils/dragDebug";
export function startGroupDrag(
e: React.PointerEvent<HTMLElement>,
@@ -70,6 +71,22 @@ export function startGroupDrag(
}
members.push(result.member);
}
resetDragMoveLog();
logDrag("group-start", {
// A member whose mapping differs from its neighbours travels a different
// distance for the same pointer delta, which is the group coming apart.
members: Object.fromEntries(
members.map((member) => [
member.key,
{
map: `${member.screenToOffset.a.toFixed(3)},${member.screenToOffset.d.toFixed(3)}`,
base: `${Math.round(member.baseGsap.x)},${Math.round(member.baseGsap.y)}`,
offset: `${Math.round(member.initialOffset.x)},${Math.round(member.initialOffset.y)}`,
},
]),
),
at: readDragPositions(members),
});
const overlayEl = opts.overlayRef.current;
const iframe = opts.iframeRef.current;
@@ -197,6 +197,56 @@ describe("measureManualOffsetDragScreenToOffsetMatrix", () => {
});
});
/**
* A group drag is rigid: every member is handed the SAME pointer delta and must
* travel the same distance on screen, or the group visibly comes apart mid-drag.
* Members do not share a mapping though — each measures its own, because each can
* sit under different ancestor transforms. A member whose movement cannot be
* measured falls back to a guess, and this pins what that guess costs the group.
*/
describe("group drag stays rigid", () => {
function member(key: string, response: number, measurable: boolean) {
const window = new Window();
const element = window.document.createElement("div");
window.document.body.append(element);
element.getBoundingClientRect = () => {
const ox = Number.parseFloat(element.style.getPropertyValue(STUDIO_OFFSET_X_PROP)) || 0;
const oy = Number.parseFloat(element.style.getPropertyValue(STUDIO_OFFSET_Y_PROP)) || 0;
const move = measurable ? response : 0;
return new window.DOMRect(100 + move * ox, 200 + move * oy, 40, 20);
};
const result = createManualOffsetDragMember({
key,
selection: { element } as never,
element,
rect: { left: 100, top: 200, width: 40, height: 20, editScaleX: 1, editScaleY: 1 },
});
if (!result.ok) throw new Error(result.reason);
return { member: result.member, response };
}
/** Screen distance this member travels for a pointer delta of `d`. */
function screenTravel(entry: ReturnType<typeof member>, d: number): number {
const offset = resolveManualOffsetForPointerDelta({
initialOffset: entry.member.initialOffset,
screenToOffset: entry.member.screenToOffset,
dx: d,
dy: 0,
});
return offset.x * entry.response;
}
it("moves every measurable member the same distance for one pointer delta", () => {
// Two members under different ancestor scales: one 1:1, one inside a half-scale
// parent. Different offsets, identical screen travel — that is what rigid means.
const a = member("a", 1, true);
const b = member("b", 0.5, true);
expect(screenTravel(a, 60)).toBeCloseTo(60, 6);
expect(screenTravel(b, 60)).toBeCloseTo(60, 6);
});
});
describe("createManualOffsetDragMember uses raw CSS var offset", () => {
it("ignores GSAP transform — initialOffset comes from CSS vars only", () => {
const window = new Window();
@@ -52,6 +52,7 @@ import {
import { hugRectForElement } from "./domEditOverlayCrop";
import { resolveSnapAdjustment, resolveEquidistanceGuides, SNAP_THRESHOLD_PX } from "./snapEngine";
import { logResize, logResizeMove, logResizeSettle } from "../../utils/resizeDebug";
import { logDrag, logDragMove, logDragSettle, readDragPositions } from "../../utils/dragDebug";
export function createDomEditOverlayGestureHandlers(opts: UseDomEditOverlayGesturesOptions) {
const setDraftOverlayRect = (next: OverlayRect) => {
@@ -162,7 +163,18 @@ export function createDomEditOverlayGestureHandlers(opts: UseDomEditOverlayGestu
rect: { ...item.rect, left: item.rect.left + dx, top: item.rect.top + dy },
})),
);
for (const member of groupG.members) applyManualOffsetDragDraft(member, dx, dy);
const offsets: Record<string, string> = {};
for (const member of groupG.members) {
const next = applyManualOffsetDragDraft(member, dx, dy);
offsets[member.key] = `${Math.round(next.x)},${Math.round(next.y)}`;
}
logDragMove({
pointer: `${Math.round(e.clientX - groupG.startX)},${Math.round(e.clientY - groupG.startY)}`,
// Any gap between these two is snapping pulling the group off the pointer.
applied: `${Math.round(dx)},${Math.round(dy)}`,
offsets,
at: readDragPositions(groupG.members),
});
return;
}
@@ -336,6 +348,17 @@ export function createDomEditOverlayGestureHandlers(opts: UseDomEditOverlayGestu
selection: member.selection,
next: applyManualOffsetDragCommit(member, dx, dy),
}));
logDrag("drop", {
pointer: `${Math.round(rawDx)},${Math.round(rawDy)}`,
applied: `${Math.round(dx)},${Math.round(dy)}`,
committed: Object.fromEntries(
updates.map((update, index) => [
groupG.members[index]?.key ?? String(index),
`${Math.round(update.next.x)},${Math.round(update.next.y)}`,
]),
),
at: readDragPositions(groupG.members),
});
void Promise.resolve(opts.onGroupPathOffsetCommitRef.current(updates))
.catch(() => {
for (const member of groupG.members) {
@@ -346,7 +369,15 @@ export function createDomEditOverlayGestureHandlers(opts: UseDomEditOverlayGestu
restoreStudioPathOffset(member.element, member.initialPathOffset);
}
})
.finally(() => endManualOffsetDragMembers(groupG.members));
.finally(() => {
logDrag("committed", { at: readDragPositions(groupG.members) });
endManualOffsetDragMembers(groupG.members);
// The gesture teardown resumes the paused timelines and re-seeks the
// player, which re-renders from whatever the preview currently holds.
// If the reloaded source has not landed yet that is the OLD position,
// so this is where a snap-back would show.
logDragSettle("settle", groupG.members);
});
return;
}
+52
View File
@@ -0,0 +1,52 @@
// Canvas drag diagnostics — grep [hf-drag]. Off by default; opt in per session
// with `localStorage.setItem("hf-drag-debug", "1")` (then reload).
//
// A drag that "jumps" is a position that changed without the pointer asking. The
// pointer delta, what snapping did to it, what each member was told to move, and
// where each member actually ended up are logged at every stage, so the frame the
// position diverges from the pointer is visible rather than inferred.
import { makeStudioDebugLogger } from "./studioDebug";
export const logDrag = makeStudioDebugLogger("drag");
let moveN = 0;
/** Per-pointermove logging, throttled: the first move then every 8th. */
export function logDragMove(data: Record<string, unknown>): void {
moveN += 1;
if (moveN % 8 === 1) logDrag("move", { n: moveN, ...data });
}
export function resetDragMoveLog(): void {
moveN = 0;
}
/** Where these elements are rendered right now, in preview-document pixels. */
export function readDragPositions(
elements: Array<{ key: string; element: HTMLElement }>,
): Record<string, string> {
const positions: Record<string, string> = {};
for (const { key, element } of elements) {
const rect = element.getBoundingClientRect();
positions[key] = `${Math.round(rect.left)},${Math.round(rect.top)}`;
}
return positions;
}
/**
* Sample the group now and again after the commit has had time to land. The drop
* is the one moment a jump can hide: the source write, the preview reload and the
* timeline resume all happen within a few frames of each other, and any of them
* can put the elements back where they started before the new position arrives.
*/
export function logDragSettle(
stage: string,
elements: Array<{ key: string; element: HTMLElement }>,
): void {
logDrag(stage, { at: readDragPositions(elements) });
const win = elements[0]?.element.ownerDocument.defaultView;
if (!win) return;
win.setTimeout(() => logDrag(`${stage}+120ms`, { at: readDragPositions(elements) }), 120);
win.setTimeout(() => logDrag(`${stage}+400ms`, { at: readDragPositions(elements) }), 400);
win.setTimeout(() => logDrag(`${stage}+900ms`, { at: readDragPositions(elements) }), 900);
}