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
@@ -2,8 +2,10 @@ import type { Application } from 'pixi.js';
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
||||
import {
|
||||
afterFrames,
|
||||
cancelFrameWork,
|
||||
initRenderLoop,
|
||||
isRendering,
|
||||
onFrame,
|
||||
renderCount,
|
||||
requestRender,
|
||||
resetRenderLoop,
|
||||
@@ -125,6 +127,73 @@ describe('render loop', () => {
|
||||
expect(app.drawn).toBe(1);
|
||||
});
|
||||
|
||||
it('runs work put off to a frame once, however often it was asked for', () => {
|
||||
const { app, tick } = fakeApp();
|
||||
initRenderLoop(app as unknown as Application);
|
||||
now += 500;
|
||||
|
||||
let ran = 0;
|
||||
for (let i = 0; i < 20; i++) onFrame('drag', () => (ran += 1));
|
||||
expect(ran).toBe(0);
|
||||
tick();
|
||||
expect(ran).toBe(1);
|
||||
|
||||
// And it is gone afterwards: the next frame has nothing left to do.
|
||||
tick();
|
||||
expect(ran).toBe(1);
|
||||
});
|
||||
|
||||
it('keeps the work of two keys apart, and the last one wins per key', () => {
|
||||
const { app, tick } = fakeApp();
|
||||
initRenderLoop(app as unknown as Application);
|
||||
now += 500;
|
||||
|
||||
const ran: string[] = [];
|
||||
onFrame('a', () => ran.push('a1'));
|
||||
onFrame('a', () => ran.push('a2'));
|
||||
onFrame('b', () => ran.push('b'));
|
||||
tick();
|
||||
expect(ran).toEqual(['a2', 'b']);
|
||||
});
|
||||
|
||||
it('runs the work before the frame it is meant to be in', () => {
|
||||
const { app, tick } = fakeApp();
|
||||
initRenderLoop(app as unknown as Application);
|
||||
now += 500;
|
||||
|
||||
let drawnWhenRun = -1;
|
||||
onFrame('drag', () => (drawnWhenRun = app.drawn));
|
||||
tick();
|
||||
expect(drawnWhenRun).toBe(0);
|
||||
expect(app.drawn).toBe(1);
|
||||
});
|
||||
|
||||
it('wakes the loop itself, so work asked for by an idle editor is not lost', () => {
|
||||
const { app, tick } = fakeApp();
|
||||
initRenderLoop(app as unknown as Application);
|
||||
now += 500;
|
||||
tick();
|
||||
expect(app.drawn).toBe(0);
|
||||
|
||||
let ran = 0;
|
||||
onFrame('drag', () => (ran += 1));
|
||||
tick();
|
||||
expect(ran).toBe(1);
|
||||
});
|
||||
|
||||
it('cancelled work never runs – a gesture that ended writes nothing more', () => {
|
||||
const { app, tick } = fakeApp();
|
||||
initRenderLoop(app as unknown as Application);
|
||||
now += 500;
|
||||
|
||||
let ran = 0;
|
||||
onFrame('drag', () => (ran += 1));
|
||||
onFrame('other', () => (ran += 10));
|
||||
cancelFrameWork('drag');
|
||||
tick();
|
||||
expect(ran).toBe(10);
|
||||
});
|
||||
|
||||
it('afterFrames gives up rather than hanging when no frame ever comes', async () => {
|
||||
vi.useFakeTimers();
|
||||
try {
|
||||
|
||||
Reference in new issue
Block a user