Miro logoMiro
CodingFree preview

Position Canvas and Viewport-Pinned Widgets on an Infinite Board

Given a viewport (size, pan, zoom) and a list of widgets that are either canvas-pinned or viewport-pinned with a normalized 0–1 anchor, return each widget's top-left screen position. Canvas widgets use the pan-zoom transform; viewport widgets interpolate the anchor against the viewport size minus the widget size so they stay fully visible.

SWE
math
array
frontend
easy
Frequency
Single report
Last asked
—
Stage
tech-screen

Problem

A board may contain "sticky" elements — titles, legends, navigation widgets — that must stay visible no matter how the user pans or zooms. Given the viewport state and a list of widgets, return each widget's top-left position in screen pixels.

Each widget is either canvas-pinned (moves with the canvas; has canvas coordinates) or viewport-pinned (fixed to the viewport; has a normalized anchor in 0.0–1.0 on each axis).

viewport: { width, height, panX, panY, zoom }
widgets: Array<{
  id: string,
  type: 'canvas' | 'viewport',
  canvasX?, canvasY?,      // canvas widgets
  anchorX?, anchorY?,      // viewport widgets, 0–1
  width, height
}>
→ Array<{ id, screenX, screenY }>   // top-left corner, screen pixels

Constraints: 1 ≤ widgets.length ≤ 10,000; 0.1 ≤ zoom ≤ 100; canvas coordinates are any real number; viewport dimensions are positive integers.

Examples

viewport = { width: 1200, height: 800, panX: 300, panY: 150, zoom: 2.0 }
w1 = { type: 'canvas',   canvasX: 100, canvasY: 50, width: 80,  height: 40 }
w2 = { type: 'viewport', anchorX: 1.0, anchorY: 0.0, width: 120, height: 60 }

w1 → screenX = 100*2 - 300 = -100, screenY = 50*2 - 150 = -50   (off-screen)
w2 → screenX = 1080 (right edge flush), screenY = 0 (top edge flush)

Hints

Pitfalls

  • anchor * viewport - size reproduces the anchorX = 1.0 case but gives screenY = -60 for anchorY = 0.0 (example expects 0) and pushes anchor-0 widgets off the left edge. Verify both example widgets before committing to a formula.
  • Sign of the pan: panning right moves content left, so pan is subtracted.
  • Viewport widgets must ignore zoom and pan entirely in the base problem.
  • Don't sort or group — output order should match input order.

Talking Points

  • State the anchor convention explicitly (anchor 0 → left/top edge at 0, anchor 1 → right/bottom edge flush) — getting it wrong flips behaviour for every non-extreme anchor.
  • Describe the canvas transform as "scale by zoom, shift by the negative pan" — it sets up the follow-up about zooming around a point other than the origin.
  • Work the example numbers aloud (100 * 2 - 300 = -100, 1080, 0) to catch sign errors before coding.
  • Preserving input order is the simplest correct behaviour and avoids an unnecessary O(n log n) sort.
  • Keep the math in floating point (canvas coordinates are real numbers); rounding to integer pixels belongs to rendering.
  • Point out early that viewport widgets stay a fixed pixel size regardless of zoom — that pre-empts the most natural extension.

Follow-ups

Preparation

  • Derive both transforms on paper from the example, then code the function in under 10 minutes.
  • Test anchors 0.0, 0.5 and 1.0 on each axis and confirm the widget stays fully inside the viewport.
  • Extend the function so viewport widgets scale with a function of zoom.
Was this article helpful?

Comments

Sign in to join the discussion
Loading...