Dragging a point in the vector art tab keeps up with the mouse (#129)
Dragging a point only redrew once the hand stopped moving, which the user reported after stage 3 and which measures as a real backlog, not a feeling: every pointer move wrote the model and fired state.preview(), and a project event rebuilds the panels, the run in the editor and the picture the drawing is rasterised into. A mouse reports about 125 times a second while the screen takes 60, so the queue grew instead of the drawing moving. - Measured with a throwaway e2e probe sending 120 moves at the 8 ms of a real mouse, against a growing scene. Per move event: 10 -> 7 ms with an empty scene, 19 -> 4 ms at 40 objects, 36 -> 3 ms at 120. The 120 moves took 4173 ms before and 960 ms after, which is the time they were supposed to take. - New onFrame(key, fn) in src/ui/render.ts collects such work per key and runs it in the ticker before app.render(), because afterwards it would be one frame late in the picture. The rule lives in ui/ rather than in the canvas so a second caller gets it for free. - VectorArtCanvas only queues in pointermove now, and cancels its slot on pointer up, where the command writes the final state anyway. During the gesture the drag's own copy of the rings is the truth and the model is never read back, so nothing is lost by the delay. The same cost sits in the scene viewport, the texture tab and the graph, which all call preview() from their move handler. They are untouched: that is outside the reported bug. Tests: check.sh 127 files, 1883 tests, exit 0 (was 1878). Full e2e suite locally instead of CI only because the change sits in the render loop: ALL PASSED - 1172 checks, the sum unchanged - a timing check in the browser would flutter under CI load. Five new unit tests in tests/ui/render.test.ts. Two counter-checks hold: running the work after the frame fails one unit test, and taking the coalescing out of the canvas puts the measurement back at 36 ms and 4153 ms. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
1 parent
3d7ad65f34
commit
3a68067079
4 files changed
+132
-3
No files matched your search
@@ -4,7 +4,9 @@ export { Theme, textStyle, type TextStyleToken } from './Theme';
|
||||
export { initUI, ui, type UIContext } from './UIContext';
|
||||
export {
|
||||
afterFrames,
|
||||
cancelFrameWork,
|
||||
initRenderLoop,
|
||||
onFrame,
|
||||
isRendering,
|
||||
renderCount,
|
||||
requestRender,
|
||||
|
||||
@@ -37,6 +37,28 @@ export function renderCount(): number {
|
||||
return rendered;
|
||||
}
|
||||
|
||||
/** Work put off to the next frame by `onFrame`, one entry per key. */
|
||||
let frameWork = new Map<string, () => void>();
|
||||
|
||||
/**
|
||||
* Runs `fn` once before the next frame is drawn, and only once however often it is asked for under
|
||||
* the same key – the answer to a gesture that costs more per pointer event than a frame is worth.
|
||||
*
|
||||
* A drag writes the model on every pointer move, and a model event rebuilds panels, the run in the
|
||||
* editor and any picture drawn from it. A mouse reports around 125 times a second while the screen
|
||||
* takes 60, so without this the pointer outruns the drawing and the editor only catches up once the
|
||||
* hand stops (#129). Coalescing here rather than in each canvas keeps the rule in one place.
|
||||
*/
|
||||
export function onFrame(key: string, fn: () => void): void {
|
||||
frameWork.set(key, fn);
|
||||
requestRender();
|
||||
}
|
||||
|
||||
/** Drops work queued under `key` – a gesture that ended has nothing left to catch up on. */
|
||||
export function cancelFrameWork(key: string): void {
|
||||
frameWork.delete(key);
|
||||
}
|
||||
|
||||
/** Resolved once the next frame has been drawn; see `afterFrames`. */
|
||||
let frameWaiters: Array<() => void> = [];
|
||||
|
||||
@@ -80,6 +102,12 @@ export function initRenderLoop(app: Application): void {
|
||||
app.ticker.add(
|
||||
() => {
|
||||
if (!isRendering()) return;
|
||||
// Before the frame, not after: what the work writes is meant to be in the picture it delays.
|
||||
if (frameWork.size) {
|
||||
const due = frameWork;
|
||||
frameWork = new Map();
|
||||
for (const fn of due.values()) fn();
|
||||
}
|
||||
rendered += 1;
|
||||
app.render();
|
||||
// Taken first: a waiter that queues itself again must land in the next round, not this one.
|
||||
@@ -98,4 +126,5 @@ export function resetRenderLoop(): void {
|
||||
deadline = 0;
|
||||
rendered = 0;
|
||||
frameWaiters = [];
|
||||
frameWork = new Map();
|
||||
}
|
||||
Reference in new issue
Block a user