Viewport

data class Viewport(val offset: Offset = Offset.Zero, val scale: Float = 1.0f)

Camera transform for the infinite canvas. Elements are stored in world coordinates; the viewport projects them onto the screen using a translate

  • uniform-scale transform.

Screen-pixel point s and world point w relate as: s = w * scale + offset w = (s - offset) / scale

Camera state is session state — not serialized, not in undo history. Mutate by returning a new Viewport via panBy, zoomBy, or zoomTo.

Constructors

Link copied to clipboard
constructor(offset: Offset = Offset.Zero, scale: Float = 1.0f)

Types

Link copied to clipboard
object Companion

Properties

Link copied to clipboard
Link copied to clipboard
Link copied to clipboard

Integer percent suitable for UI display.

Functions

Link copied to clipboard
fun panBy(delta: Offset): Viewport

Translate the camera by delta screen pixels.

Link copied to clipboard
fun screenToWorld(screenPos: Offset): Offset
Link copied to clipboard
fun worldToScreen(worldPos: Offset): Offset
Link copied to clipboard
fun zoomBy(factor: Float, focalScreen: Offset): Viewport

Zoom by factor keeping the world point under focalScreen anchored to the same screen pixel. Same math as compose-infinite-canvas, just lifted onto an immutable value type.

Link copied to clipboard
fun zoomTo(targetScale: Float, focalScreen: Offset): Viewport

Set scale to targetScale anchored at focalScreen.