LiveFlow.Viewport (LiveFlow v0.2.3)

Copy Markdown View Source

Viewport data structure for LiveFlow.

The viewport represents the current pan/zoom state of the flow canvas. It tracks the translation (x, y) and zoom level to transform flow coordinates to screen coordinates.

Fields

  • :x - Horizontal pan offset
  • :y - Vertical pan offset
  • :zoom - Zoom level (1.0 = 100%)

Coordinate Transformation

To convert from flow space to screen space:

screen_x = flow_x * zoom + viewport.x
screen_y = flow_y * zoom + viewport.y

To convert from screen space to flow space:

flow_x = (screen_x - viewport.x) / zoom
flow_y = (screen_y - viewport.y) / zoom

Examples

iex> vp = LiveFlow.Viewport.new()
%LiveFlow.Viewport{x: 0.0, y: 0.0, zoom: 1.0}

iex> LiveFlow.Viewport.pan(vp, 100, 50)
%LiveFlow.Viewport{x: 100.0, y: 50.0, zoom: 1.0}

Summary

Functions

Calculates viewport to fit given bounds with optional padding.

Converts flow coordinates to screen coordinates.

Creates a new viewport with optional initial values.

Pans the viewport by the given delta.

Sets the viewport pan position.

Converts screen coordinates to flow coordinates.

Sets the viewport position and zoom.

Gets the CSS transform string for the viewport.

Zooms the viewport by a factor.

Sets the zoom level directly.

Types

t()

@type t() :: %LiveFlow.Viewport{x: float(), y: float(), zoom: float()}

Functions

fit_bounds(container, bounds, opts \\ [])

@spec fit_bounds(map(), map(), keyword()) :: t()

Calculates viewport to fit given bounds with optional padding.

Options

  • :padding - Padding ratio (default: 0.1 = 10%)
  • :min_zoom - Minimum zoom level (default: 0.1)
  • :max_zoom - Maximum zoom level (default: 4.0)

Examples

iex> bounds = %{x: 0, y: 0, width: 400, height: 300}
iex> LiveFlow.Viewport.fit_bounds(%{width: 800, height: 600}, bounds)
%LiveFlow.Viewport{x: 80.0, y: 60.0, zoom: 1.6}

flow_to_screen(viewport, flow_x, flow_y)

@spec flow_to_screen(t(), number(), number()) :: {float(), float()}

Converts flow coordinates to screen coordinates.

Examples

iex> vp = LiveFlow.Viewport.new(x: 100, y: 50, zoom: 2.0)
iex> LiveFlow.Viewport.flow_to_screen(vp, 50, 50)
{200.0, 150.0}

new(opts \\ [])

@spec new(keyword()) :: t()

Creates a new viewport with optional initial values.

Options

  • :x - Initial x pan (default: 0.0)
  • :y - Initial y pan (default: 0.0)
  • :zoom - Initial zoom level (default: 1.0)

Examples

iex> LiveFlow.Viewport.new()
%LiveFlow.Viewport{x: 0.0, y: 0.0, zoom: 1.0}

iex> LiveFlow.Viewport.new(x: 100, y: 50, zoom: 1.5)
%LiveFlow.Viewport{x: 100.0, y: 50.0, zoom: 1.5}

pan(vp, dx, dy)

@spec pan(t(), number(), number()) :: t()

Pans the viewport by the given delta.

pan_to(vp, x, y)

@spec pan_to(t(), number(), number()) :: t()

Sets the viewport pan position.

screen_to_flow(viewport, screen_x, screen_y)

@spec screen_to_flow(t(), number(), number()) :: {float(), float()}

Converts screen coordinates to flow coordinates.

Examples

iex> vp = LiveFlow.Viewport.new(x: 100, y: 50, zoom: 2.0)
iex> LiveFlow.Viewport.screen_to_flow(vp, 200, 150)
{50.0, 50.0}

set(viewport, x, y, zoom)

@spec set(t(), number(), number(), number()) :: t()

Sets the viewport position and zoom.

transform_style(viewport)

@spec transform_style(t()) :: String.t()

Gets the CSS transform string for the viewport.

Examples

iex> vp = LiveFlow.Viewport.new(x: 100, y: 50, zoom: 1.5)
iex> LiveFlow.Viewport.transform_style(vp)
"translate(100.0px, 50.0px) scale(1.5)"

zoom_by(vp, factor, opts \\ [])

@spec zoom_by(t(), number(), keyword()) :: t()

Zooms the viewport by a factor.

Options

  • :center - Point to zoom towards {x, y} in screen coordinates
  • :min_zoom - Minimum zoom level (default: 0.1)
  • :max_zoom - Maximum zoom level (default: 4.0)

Examples

iex> vp = LiveFlow.Viewport.new()
iex> LiveFlow.Viewport.zoom_by(vp, 1.5)
%LiveFlow.Viewport{x: 0.0, y: 0.0, zoom: 1.5}

zoom_to(vp, zoom, opts \\ [])

@spec zoom_to(t(), number(), keyword()) :: t()

Sets the zoom level directly.

Options

  • :center - Point to zoom towards {x, y} in screen coordinates
  • :min_zoom - Minimum zoom level (default: 0.1)
  • :max_zoom - Maximum zoom level (default: 4.0)