Packets now travel along the cable, and a drag takes its cables with it (#1228) #1229

Merged
jaylouisw merged 1 commit from fix/ping-along-the-cable into main 2026-11-09 16:23:01 +00:00
Owner

Packets now travel along the cable, and a drag takes its cables with it

Fixes #1228.

A ping animated one dot per frame event, and the dot went from the centre of the source chassis to
the centre of the destination. The cable is drawn socket-to-socket, so the dot crossed empty canvas
while the cable ran somewhere else. Drag a device first and it got worse: the dot used the new layout
while the cables were still painted at the old positions, so the animation matched neither.

What changed in web/canvas.js

The dot follows the cable. animateEvents() now resolves each event's fromIface/toIface
through a new socketPoint() — the same portWorld() coordinates the cable is drawn along. The frame
events have carried the ports all along (engine/simulator.js emits { from, fromIface, to, toIface });
the animation just never used them.

A drag moves the cables. The pointermove handler translated the device <g> on its own, so the
box slid out from under its own cables until an unrelated redraw. Cables drawn in the last frame are now
tracked (cableNodes), and dragPreview(deviceId, dx, dy) shifts the endpoints that belong to the
dragged device — exact, because the geometry is straight lines. The far ends stay put.

A drag is redrawn on release. on.move() persisted the layout but nothing re-rendered, so the
socket table (used by hit testing and the animation) kept the pre-drag coordinates. pointerup now
calls render() after the move.

ensureGeoCurrent() before animating. If the layout has moved since the last frame was drawn — a
persisted drag with no redraw, or a move from another client arriving mid-animation — geometry is
recomputed and the cables redrawn, rather than animating to a stale socket.

No invented positions. deviceCenter() returned { x: 100, y: 100 } for a device with no layout
entry, which is a real point in world coordinates: the dot flew to the top-left corner of the grid. The
animation now uses the position the canvas actually drew, and socketPoint() returns null for a
device it has never heard of, so a bad event is skipped instead of drawn somewhere misleading.

Evidence

New test/canvas-ping-path.test.js (20 checks) loads the real web/canvas.js against a recording stub
DOM with a controllable animation clock, runs a real ping frame, and reads every position the dot was
given:

  • the first position is the source socket and the last is the destination socket;
  • every position lies on the cable — worst deviation measured on the fixture below;
  • the path is not the centre-to-centre line: closest approach 0.0 px before, tens of px after.

Reverting just the two lines of animateEvents() reproduces the old failure:

✗ every position lies on the cable  [worst deviation 39.54 px]
✗ the path is not the centre-to-centre line the old code drew  [closest approach to it 0.0 px]

Also asserted: a device moved in the layout without a redraw is still animated at its new socket (the
ensureGeoCurrent() path), dragPreview() moves the attached end by exactly the drag and leaves the far
end alone, the socket table follows the drag, and an unknown device has no position.

docs/ARCHITECTURE.md gains "One source of position": the socket table, the four rules that keep it
true, and why the drawing and the animation must read the same coordinates.

Suite: 131 suites, 5,735 assertions, 0 failed.

Packets now travel along the cable, and a drag takes its cables with it Fixes #1228. A ping animated one dot per `frame` event, and the dot went from the **centre** of the source chassis to the **centre** of the destination. The cable is drawn socket-to-socket, so the dot crossed empty canvas while the cable ran somewhere else. Drag a device first and it got worse: the dot used the new layout while the cables were still painted at the old positions, so the animation matched neither. ## What changed in `web/canvas.js` **The dot follows the cable.** `animateEvents()` now resolves each event's `fromIface`/`toIface` through a new `socketPoint()` — the same `portWorld()` coordinates the cable is drawn along. The frame events have carried the ports all along (`engine/simulator.js` emits `{ from, fromIface, to, toIface }`); the animation just never used them. **A drag moves the cables.** The `pointermove` handler translated the device `<g>` on its own, so the box slid out from under its own cables until an unrelated redraw. Cables drawn in the last frame are now tracked (`cableNodes`), and `dragPreview(deviceId, dx, dy)` shifts the endpoints that belong to the dragged device — exact, because the geometry is straight lines. The far ends stay put. **A drag is redrawn on release.** `on.move()` persisted the layout but nothing re-rendered, so the socket table (used by hit testing and the animation) kept the pre-drag coordinates. `pointerup` now calls `render()` after the move. **`ensureGeoCurrent()` before animating.** If the layout has moved since the last frame was drawn — a persisted drag with no redraw, or a move from another client arriving mid-animation — geometry is recomputed and the cables redrawn, rather than animating to a stale socket. **No invented positions.** `deviceCenter()` returned `{ x: 100, y: 100 }` for a device with no layout entry, which is a real point in world coordinates: the dot flew to the top-left corner of the grid. The animation now uses the position the canvas actually drew, and `socketPoint()` returns `null` for a device it has never heard of, so a bad event is skipped instead of drawn somewhere misleading. ## Evidence New `test/canvas-ping-path.test.js` (20 checks) loads the real `web/canvas.js` against a recording stub DOM with a controllable animation clock, runs a real ping frame, and reads every position the dot was given: * the first position is the source socket and the last is the destination socket; * **every** position lies on the cable — worst deviation measured on the fixture below; * the path is *not* the centre-to-centre line: closest approach 0.0 px before, tens of px after. Reverting just the two lines of `animateEvents()` reproduces the old failure: ``` ✗ every position lies on the cable [worst deviation 39.54 px] ✗ the path is not the centre-to-centre line the old code drew [closest approach to it 0.0 px] ``` Also asserted: a device moved in the layout without a redraw is still animated at its new socket (the `ensureGeoCurrent()` path), `dragPreview()` moves the attached end by exactly the drag and leaves the far end alone, the socket table follows the drag, and an unknown device has no position. `docs/ARCHITECTURE.md` gains "One source of position": the socket table, the four rules that keep it true, and why the drawing and the animation must read the same coordinates. Suite: 131 suites, 5,735 assertions, 0 failed.
Packets now travel along the cable, and a drag takes its cables with it
All checks were successful
CI / Classify the change (pull_request) Successful in 9s
PR rebase / Rebase onto main and arm auto-merge (pull_request) Successful in 13s
CI / Test (Node 18) (pull_request) Successful in 1m30s
CI / Test (Node 20) (pull_request) Successful in 1m30s
CI / Server smoke test (pull_request) Successful in 31s
CI / Docker build (pull_request) Successful in 36s
CI / Test (Node 22) (pull_request) Successful in 1m30s
75d27cd881
Fixes #1228.

A ping animated one dot per `frame` event, and the dot went from the **centre** of the source chassis to
the **centre** of the destination. The cable is drawn socket-to-socket, so the dot crossed empty canvas
while the cable ran somewhere else. Drag a device first and it got worse: the dot used the new layout
while the cables were still painted at the old positions, so the animation matched neither.

## What changed in `web/canvas.js`

**The dot follows the cable.** `animateEvents()` now resolves each event's `fromIface`/`toIface`
through a new `socketPoint()` — the same `portWorld()` coordinates the cable is drawn along. The frame
events have carried the ports all along (`engine/simulator.js` emits `{ from, fromIface, to, toIface }`);
the animation just never used them.

**A drag moves the cables.** The `pointermove` handler translated the device `<g>` on its own, so the
box slid out from under its own cables until an unrelated redraw. Cables drawn in the last frame are now
tracked (`cableNodes`), and `dragPreview(deviceId, dx, dy)` shifts the endpoints that belong to the
dragged device — exact, because the geometry is straight lines. The far ends stay put.

**A drag is redrawn on release.** `on.move()` persisted the layout but nothing re-rendered, so the
socket table (used by hit testing and the animation) kept the pre-drag coordinates. `pointerup` now
calls `render()` after the move.

**`ensureGeoCurrent()` before animating.** If the layout has moved since the last frame was drawn — a
persisted drag with no redraw, or a move from another client arriving mid-animation — geometry is
recomputed and the cables redrawn, rather than animating to a stale socket.

**No invented positions.** `deviceCenter()` returned `{ x: 100, y: 100 }` for a device with no layout
entry, which is a real point in world coordinates: the dot flew to the top-left corner of the grid. The
animation now uses the position the canvas actually drew, and `socketPoint()` returns `null` for a
device it has never heard of, so a bad event is skipped instead of drawn somewhere misleading.

## Evidence

New `test/canvas-ping-path.test.js` (20 checks) loads the real `web/canvas.js` against a recording stub
DOM with a controllable animation clock, runs a real ping frame, and reads every position the dot was
given:

* the first position is the source socket and the last is the destination socket;
* **every** position lies on the cable — worst deviation measured on the fixture below;
* the path is *not* the centre-to-centre line: closest approach 0.0 px before, tens of px after.

Reverting just the two lines of `animateEvents()` reproduces the old failure:

```
✗ every position lies on the cable  [worst deviation 39.54 px]
✗ the path is not the centre-to-centre line the old code drew  [closest approach to it 0.0 px]
```

Also asserted: a device moved in the layout without a redraw is still animated at its new socket (the
`ensureGeoCurrent()` path), `dragPreview()` moves the attached end by exactly the drag and leaves the far
end alone, the socket table follows the drag, and an unknown device has no position.

`docs/ARCHITECTURE.md` gains "One source of position": the socket table, the four rules that keep it
true, and why the drawing and the animation must read the same coordinates.

Suite: 131 suites, 5,735 assertions, 0 failed.
jaylouisw scheduled this pull request to auto merge when all checks succeed 2026-11-09 16:22:57 +00:00
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
jaylouisw/packetforge!1229
No description provided.