Skip to content

Adjust pinch event coordinates to container bounds - #57

Closed
hiroshisuga wants to merge 3 commits into
bigbluebutton:mainfrom
hiroshisuga:patch-7
Closed

hiroshisuga wants to merge 3 commits into
bigbluebutton:mainfrom
hiroshisuga:patch-7

Conversation

@hiroshisuga

@hiroshisuga hiroshisuga commented Aug 22, 2026 •

Copy link
Copy Markdown

In short, this PR fixes bigbluebutton/bigbluebutton#25661.

The pinch origin reported by the gesture handler is based on browser/client coordinates, while the camera expects coordinates relative to the tldraw container. When the tldraw container is offset from the browser viewport, this causes the zoom center to shift away from the midpoint between the two fingers.

This PR converts the pinch origin to container-relative coordinates by subtracting the container's left and top offsets.

It also reduces the distance threshold used to distinguish pinch zoom from two-finger panning from 24px to 8px. This makes pinch zoom respond earlier and reduces the initial delay and jump before zooming begins.

These changes make pinch-to-zoom use the expected point between the two fingers as its center and improve the responsiveness of the gesture in BigBlueButton.

And importantly, this PR is essentially a backport of tldraw#2719 , which was merged to the main stream code (perhaps at ver 2.0b) after we split up.

Change Type

  • patch — Bug fix
  • minor — New feature
  • major — Breaking change
  • dependencies — Changes to package dependencies1
  • documentation — Changes to the documentation only2
  • tests — Changes to any test code only2
  • internal — Any other changes that don't affect the published package2
  • I don't know

Footnotes

  1. publishes a patch release, for devDependencies use internal ↩

  2. will not publish a new version ↩ ↩2 ↩3

@hiroshisuga
hiroshisuga marked this pull request as ready for review August 22, 2026 12:45
@hiroshisuga
hiroshisuga marked this pull request as draft August 23, 2026 01:22
they were not the best places to modify...
Refactor pinch handling to extract x and y coordinates after screen bounds adjustment.
@hiroshisuga

hiroshisuga commented Aug 23, 2026 •

Copy link
Copy Markdown
Author

The previous implementation (9c79af3) corrected the pinch origin in useGestureEvents.ts by converting the gesture origin from client coordinates to to those relative to the editor before dispatching the event. My investigation, however, showed that this changes the coordinate system of the dispatched input event itself and causes the editor offset to be applied twice when the input state is updated.

The revised implementation (d77d455) keeps pinch event points in client space and applies the editor's screenBounds offset only where the pinch point is used for camera calculations in Editor.ts.

This is also consistent with the upstream fix introduced in tldraw#2719. That PR explicitly treats event points as the coordinates within the client space, converts them relative to the editor (although the author said it "weird"), and applies the same conversion when calculating the pinch center for camera zooming.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The offset pinch behavior needs a regression test before approval.

Get a fresh assessment by requesting another Copilot review.

Pull request overview

Fixes pinch-to-zoom positioning and responsiveness for offset editor containers.

Changes:

  • Converts pinch origins from client to container-relative coordinates.
  • Reduces the pinch detection threshold from 24px to 8px.
File summaries
File Description
Editor.ts Corrects the pinch center used by camera calculations.
useGestureEvents.ts Makes pinch zoom activate earlier.
Review details
  • Files reviewed: 2/2 changed files
  • Comments generated: 1
  • Review effort level: Balanced

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment on lines +8616 to +8618
const { screenBounds } = this.getInstanceState()
const x = info.point.x - screenBounds.x
const y = info.point.y - screenBounds.y
@imdt-claudiop

Copy link
Copy Markdown

Thanks for digging into this one, and especially for the follow-up comment explaining why the first
approach was reverted. That reasoning is correct and it saved me a lot of time, so I want to confirm it
with evidence rather than just agree.

I tested this on both supported lines, each built from source, with the patch applied to the
@bigbluebutton/editor build that the client actually bundles:

  • BigBlueButton 3.0 (v3.0.x-develop @ 4c3a477), the line the issue was filed against
  • BigBlueButton 4.0 (v4.0.x-develop @ a1f2512)

Both lines pin the same @bigbluebutton/tldraw 2.0.0-alpha.35, so one published alpha reaches both.

Short version

The coordinate fix in Editor.ts is correct, it measurably fixes the reported symptom on 3.0, and I have
a red/green test to prove the math. Two things I would like to see before merge: a regression test (there
is currently none for pinch at all), and the 24 -> 8 threshold change either split out or justified
separately, because it is not part of the upstream change this PR cites.

The coordinate fix is right

Confirmed by instrumenting the running client on both lines. During a real two-finger pinch on the
whiteboard, the handler receives client coordinates while the camera math expects container
coordinates:

BBB 3.0:  info.point = { x: 960, y: 440 }   screenBounds = { x: 520.00, y: 192.80 }
BBB 4.0:  info.point = { x: 879, y: 440 }   screenBounds = { x: 371.19, y: 154.89 }

So the unpatched code feeds the raw client point into camera math that expects container coordinates, and
the error is exactly the container offset: 520 px / 193 px on 3.0, and 371 px / 155 px on 4.0. That
matches the symptom in bigbluebutton/bigbluebutton#25661 (zoom center shifted toward the bottom-right),
and it is worse on 3.0 because that layout places the whiteboard further from the viewport origin, which
fits the issue having been reported on 3.0.35.

Your choice to convert at the point of use rather than at the dispatch site is also the right one for
this fork, and not just a matter of taste:

  • screenToPage() (packages/editor/src/lib/editor/Editor.ts) subtracts screenBounds from its
    argument, so the public API already treats event points as client space.
  • _updateInputsFromEvent() (packages/editor/src/lib/editor/Editor.ts:8334) does the same when
    computing currentPagePoint.

Converting inside useGestureEvents.ts would therefore have been subtracted a second time in
_updateInputsFromEvent, exactly as you described.

I wrote the regression test this needs and ran it both ways:

main    -> FAIL  (expected 300, received 350: the focal point drifts by the container offset)
patch-7 -> PASS

Full packages/tldraw suite on your branch: 1253 passed, 0 failed (1251 existing plus the 2 I added).
main runs the same 1251 existing tests green, so nothing already in the suite changes status.

Measured end to end

Same synthesized two-finger pinch, fingers held at the centre of the whiteboard container, identical
starting camera, fresh meeting per run. "Drift" is the distance between where the zoom actually anchored
and where the fingers were, so 0 is the goal.

line build zoom drift x drift y
3.0 unpatched 1.53x 204.2 px 76.7 px
3.0 unpatched (repeat) 1.53x 144.0 px 54.4 px
3.0 this PR 1.50x -1.4 px -1.3 px
4.0 unpatched 1.50x -44.4 px -19.3 px
4.0 this PR 1.57x -17.6 px -11.4 px

On 3.0 the fix lands the anchor within about 1 px of the fingers, which is the behaviour the issue asks
for. Side by side at the same 153 percent zoom, the unpatched 3.0 client throws the slide far enough
up-left that the "Welcome To BigBlueButton" heading is clipped off the top of the viewport; with the
patch the heading stays put and centred. That is the clearest demonstration I got.

Two honest caveats on these numbers. The unpatched drift is much smaller than the raw 520 px / 371 px
coordinate error because BBB runs its own pinch handler on the same gesture (see finding 4), which
partly compensates. And the 4.0 run keeps a residual of roughly 18 px / 11 px where 3.0 goes to zero. I
did not determine why the two lines differ there, so I am reporting it rather than explaining it.

Findings

1. No test for this, and there is no pinch coverage at all (should fix)

packages/tldraw/src/test/commands/pinch.test.ts is entirely commented out and contains only
it.todo('Pinches the camera'). This is the exact class of bug that silently comes back, and the
upstream PR you cite added regression tests for it (zoomIn.test.ts, "preserves the screen center when
offset").

Here is the test I used, if it helps. It fails on main and passes here:

// packages/tldraw/src/test/commands/pinch.test.ts
import { TestEditor } from '../TestEditor'

let editor: TestEditor
beforeEach(() => {
	editor = new TestEditor()
})

function pinch(focal: { x: number; y: number }, z: number) {
	const base = { delta: { x: 0, y: 0 }, shiftKey: false, altKey: false, ctrlKey: false } as const
	editor.dispatch({ type: 'pinch', name: 'pinch_start', point: { ...focal, z: 1 }, ...base })
	editor.dispatch({ type: 'pinch', name: 'pinch', point: { ...focal, z }, ...base })
}

describe('When pinching', () => {
	it('keeps the focal point anchored when the container is not offset', () => {
		editor.setScreenBounds({ x: 0, y: 0, w: 1080, h: 720 })
		const focal = { x: 400, y: 500 }
		const before = editor.screenToPage(focal)
		pinch(focal, 2)
		const after = editor.screenToPage(focal)
		expect(editor.getZoomLevel()).toBeCloseTo(2)
		expect(after.x).toBeCloseTo(before.x)
		expect(after.y).toBeCloseTo(before.y)
	})

	it('keeps the focal point anchored when the container is offset', () => {
		editor.setScreenBounds({ x: 100, y: 200, w: 1080, h: 720 })
		const focal = { x: 400, y: 500 }
		const before = editor.screenToPage(focal)
		pinch(focal, 2)
		const after = editor.screenToPage(focal)
		expect(editor.getZoomLevel()).toBeCloseTo(2)
		expect(after.x).toBeCloseTo(before.x)
		expect(after.y).toBeCloseTo(before.y)
	})
})

Heads up if you use the existing helpers instead: TestEditor.pinchTo()
(packages/tldraw/src/test/TestEditor.ts:421) dispatches name: 'pinch_start' rather than
name: 'pinch', so a test written on top of it never reaches the code this PR changes. That looks like a
copy/paste slip in the harness, unrelated to your change, but it will bite whoever writes the test.

2. The 24 -> 8 threshold is a second, independent change (should split or justify)

packages/editor/src/lib/hooks/useGestureEvents.ts:193

I want to flag that this part is not covered by the backport argument. tldraw#2719 does not touch
useGestureEvents.ts at all, and upstream main today still has touchDistance > 24. So this is a
BigBlueButton-specific behavior change riding along with a coordinate fix.

The reason I am wary: in the 'not sure' state the checks are ordered

if (touchDistance > 8)        pinchState = 'zooming'
else if (originDistance > 16) pinchState = 'panning'

touchDistance is tested first, so any two-finger gesture whose finger separation wobbles more than 8 px
before the midpoint has travelled 16 px is classified as a zoom. 8 px is within ordinary finger-tracking
noise on a coarse pointer. And the classification is sticky in one direction only: there is a
panning -> zooming escape at touchDistance > 64, but no zooming -> panning escape, so a
misclassified two-finger pan stays a zoom for the rest of the gesture.

I could not test this properly (see "not covered" below), so I am raising it as a risk rather than a
confirmed regression. Two suggestions, either is fine by me:

  • split it into its own PR with touch-device evidence, so the coordinate fix can merge on its own merit, or
  • keep it here, but say why 8 specifically, and confirm two-finger panning still works on a real device.

Worth asking too: how much of the "jump" you describe survives once the origin is correct? Part of that
jump looks like a symptom of the wrong zoom center rather than of the threshold.

3. The PR description oversells the backport (nit, but worth fixing)

"essentially a backport of tldraw#2719" reads as if the whole upstream change came across. It is
one hunk of six. Upstream also normalized _updateInputsFromEvent so that currentScreenPoint becomes
container-relative, and then fixed the fallout of that: getViewportScreenCenter(), edgeScrolling.ts,
and the internal pointer_move re-dispatch in _setCamera, plus tests.

Taking only the pinch hunk is a defensible, low-risk choice for this fork and I am not asking you to
widen it. I would just describe it as "the pinch portion of" so nobody later assumes the coordinate
cleanup already landed.

One consequence that stays behind, for awareness only (pre-existing, not introduced here): event points
are client space everywhere except the wheel handler, which already subtracts the container offset before
dispatch (packages/editor/src/lib/hooks/useGestureEvents.ts:125). _updateInputsFromEvent then
subtracts screenBounds again, so currentPagePoint is double-offset for wheel events. Out of scope
here, but it is the same coordinate confusion and would be a good follow-up.

4. This probably does not fully close bigbluebutton/bigbluebutton#25661

Your own note on the issue ("BBB currently has its own presenter pinch handling in addition to tldraw's")
is confirmed. In bigbluebutton-html5/imports/ui/components/whiteboard/hooks.js (handleTouchMove, line
287 on 3.0 and line 306 on 4.0), BBB also drives the camera on the same gesture. It is throttled to
175 ms, applies a fixed 0.1 zoom step per tick, animates with setCamera(next, { duration: 175 }), and
already does the container conversion correctly with rect.left / rect.top. It calls preventDefault()
and stopPropagation() only when the user is not the presenter, so for a presenter both handlers run.
That block is byte-identical between v3.0.x-develop and v4.0.x-develop, so this applies equally to
both lines.

I can see the effect directly in the instrumented log: the camera is rewritten between consecutive pinch
events (for example cx jumping from -192.58 to -148.97 across two events of one gesture). It is also
why the unpatched drift in the table above is much smaller than the raw 520 px / 371 px coordinate error,
BBB's correct handler is partly compensating for tldraw's incorrect one.

So the tldraw side is necessary, and on 3.0 it is close to sufficient (about 1 px residual), but the two
handlers still race. I would not auto-close 25661 on merge, and I would track the duplicated BBB-side
handler separately.

This also colors the threshold change: lowering to 8 makes tldraw's handler engage earlier, which means
it starts competing with the BBB handler sooner rather than later.

5. Smaller things

  • packages/editor/src/lib/editor/Editor.ts:8616: this.getInstanceState() is a @computed read, while
    _updateInputsFromEvent deliberately uses this.store.unsafeGetWithoutCapture(TLINSTANCE_ID) for the
    same value, as does upstream in this hunk. In practice it changes nothing here, since
    getInstanceState() is already called three lines up for canMoveCamera, so treat this as a
    consistency nit only.

  • packages/editor/src/lib/editor/Editor.ts:8612: destructuring only z from info.point and then
    reading info.point.x / .y separately reads a little oddly. Upstream used
    const { x, y, z = 1 } = Vec.SubXY(info.point, screenBounds.x, screenBounds.y), which keeps it in one
    place. Taste, not a problem.

  • The branch is 8 commits behind main, and carries a commit plus its revert. Worth a rebase and a
    squash on merge so the history shows the one change that lands.

  • CI is red here, but it is red on main too (the checks.yml runs on main for 2026-08-31 and
    2026-09-10 both fail the same way). It fails in "Install dependencies" with
    YN0028: The lockfile would have been modified by this install, which is repository-level lockfile
    drift, not something your diff caused. Rebasing will not fix it either: none of the 8 commits on main
    touch yarn.lock or any package.json.

    For whoever picks this up: running yarn install on a clean checkout reproduces it, and the drift is
    three dependencies declared by the scripts workspace but missing from the committed
    public-yarn.lock (chokidar ^3.6.0, commander ^9.5.0, ora ^5.4.1), which are exactly the entries
    the CI log flags. A separate PR that just commits the regenerated lockfile should turn CI green again
    for everyone. Flagging so nobody reads the red X as yours.

If you want to reproduce this against a BBB server

One trap that cost me a couple of builds, on both 3.0 and 4.0. bigbluebutton-html5/webpack.config.js
has an escape hatch for local tldraw work: snapshot.managedPaths deliberately excludes
@bigbluebutton/tldraw from webpack's "node_modules never changes" assumption. It does not exclude
@bigbluebutton/editor, which is where this change actually lives. So editing the editor package and
rebuilding silently reuses the cached module and produces a byte-identical bundle. Deleting
bigbluebutton-html5/node_modules/.cache/webpack before the rebuild is what makes the change take.

What I tested, and what I did not

Tested:

  • BBB 3.0 from source (v3.0.x-develop @ 4c3a477) and BBB 4.0 from source (v4.0.x-develop @
    a1f2512), whiteboard as presenter, default slide, on each line before and after the patch.
  • Two-finger pinch synthesized as real touch events, with the handler instrumented on both lines to
    confirm the changed line is on the live pinch path (it is) and to capture the actual info.point and
    screenBounds.
  • Unit level: red on main, green on patch-7, and the full packages/tldraw suite for regressions.
    Note this test lives in this repo and is not BBB-line specific, so it is one result, not two.
  • Whiteboard still loads, zooms and renders normally after the change on both lines.

Not covered, and I do not want to imply otherwise:

  • No real touch hardware. Everything was synthesized touch input in Chromium, so the threshold question
    in finding 2 is unresolved. Two-finger panning on an actual iPad or Android device is the test that
    matters there, and I could not run it.
  • No Safari, so the gesturestart / gesturechange trackpad path is untested.
  • Pinch-to-zoom-out, and pinch while the whiteboard is in infinite mode
    (allowInfiniteWhiteboard: true), were not exercised. The measurements above were all zoom-in with the
    default bounded whiteboard.
  • Only the presenter case. A viewer with whiteboard access was not tested, though note BBB swallows the
    two-finger touch for non-presenters, so tldraw's pinch path is mostly presenter-only in practice.
  • I patched the built @bigbluebutton/editor package in place on each server rather than publishing a
    new alpha and installing it. The patched output is the same transformation as the diff, but the real
    publish path (new alpha picked up through ^2.0.0-alpha.35) was not exercised.

End-to-end evidence (BBB 3.0 and 4.0, from-source, synthesized two-finger pinch in Chromium)

Build During pinch
BBB 3.0 unpatched 3.0 unpatched
BBB 3.0 with this PR 3.0 patched
BBB 4.0 unpatched 4.0 unpatched
BBB 4.0 with this PR 4.0 patched

@antobinary

Copy link
Copy Markdown
Member

@antobinary antobinary closed this Oct 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[3.0 whiteboard] Pinch-to-zoom uses an incorrect zoom center for presenters on touch devices

4 participants