Skip to content

feat: add pan, zoom, and auto-fit to whiteboard canvas#31

Open
YizukiAme wants to merge 4 commits intoTHU-MAIC:mainfrom
YizukiAme:fix/whiteboard-overflow-pan
Open

feat: add pan, zoom, and auto-fit to whiteboard canvas#31
YizukiAme wants to merge 4 commits intoTHU-MAIC:mainfrom
YizukiAme:fix/whiteboard-overflow-pan

Conversation

@YizukiAme
Copy link

Summary

Fixes #19 — Whiteboard content that exceeds the fixed canvas bounds was clipped by overflow-hidden with no way to view hidden parts.

Changes

components/whiteboard/whiteboard-canvas.tsx

  • Auto-fit scaling: When elements overflow the 1000×562.5 canvas, computes bounding box and applies scale + translate to ensure all content is visible (with 24px padding). When content fits normally, no transform is applied — zero visual regression.
  • Drag-to-pan: Hold mouse and drag to pan around the whiteboard (pointer events with capture)
  • Scroll-to-zoom: Mouse wheel zooms in/out (0.2x–5x range), using native addEventListener({ passive: false }) for proper preventDefault
  • Double-click reset: Double-click or click "Reset View" button to return to default view (with smooth 0.25s animation)
  • Text selection prevention: select-none + preventDefault prevents accidental text selection during drag/double-click
  • Auto-reset on content change: View resets when whiteboard content changes (tracked by stable element ID key, not object reference)
  • Grab cursor: Shows grab/grabbing cursor when pan/zoom is available

lib/i18n/stage.ts

  • Added resetView translation key for zh-CN ("重置视图") and en-US ("Reset View")

How it works

Content fits canvas → No change, original behavior
Content overflows → Auto-fit scales down to show all
User scrolls wheel → Zoom in/out (centered)
User drags → Pan around
User double-clicks/button → Smooth reset to auto-fit
New content loads → Auto-reset view

Testing

Tested with injected overflow elements (left/right/bottom + negative coordinates) and confirmed:

  • ✅ Auto-fit scales content to fit
  • ✅ Scroll zoom works
  • ✅ Drag-to-pan works (no text selection)
  • ✅ Double-click reset with smooth animation
  • ✅ Reset View button appears and works
  • ✅ No visual regression when content fits normally

When AI generates whiteboard elements that exceed the fixed 1000x562.5
canvas, content was clipped by overflow-hidden with no way to view it.

This commit adds interactive navigation to the whiteboard:

- Auto-fit: when elements overflow, content is scaled and centered to
  ensure nothing is clipped (with 24px padding)
- Drag-to-pan: hold and drag to pan around the whiteboard
- Scroll-to-zoom: mouse wheel zooms in/out (0.2x to 5x range)
- Double-click or Reset View button to return to default view
- View auto-resets when new whiteboard content loads

When content fits within the canvas, behavior is unchanged - no extra
cursor, no transforms, no visual regression.

Fixes THU-MAIC#19
- Remove dead code: ?? 'Reset View' fallback (i18n key exists)
- Add useMemo for elementsKey (perf optimization)
- Fix el.height type narrowing (remove implicit any)
- Disable pan/zoom/cursor when whiteboard is empty (UX improvement)
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.

[Bug]: 互动白板内容超出画布时无法拖拽查看

1 participant