# `LiveFlow.Viewport`
[🔗](https://github.com/rocket4ce/live_flow/blob/main/lib/live_flow/core/viewport.ex#L1)

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}

# `t`

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

# `fit_bounds`

```elixir
@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`

```elixir
@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`

```elixir
@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`

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

Pans the viewport by the given delta.

# `pan_to`

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

Sets the viewport pan position.

# `screen_to_flow`

```elixir
@spec screen_to_flow(t(), number(), number(), keyword()) :: {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}

Pass `snap_to_grid: true` (and optionally `snap_grid: {x, y}`, default
`{15, 15}`) to snap the result, e.g. when dropping a node:

    iex> vp = LiveFlow.Viewport.new(x: 0, y: 0, zoom: 1.0)
    iex> LiveFlow.Viewport.screen_to_flow(vp, 22, 38, snap_to_grid: true)
    {15.0, 45.0}

# `set`

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

Sets the viewport position and zoom.

# `snap`

```elixir
@spec snap({number(), number()}, {number(), number()}) :: {float(), float()}
```

Snaps a point to a grid.

## Examples

    iex> LiveFlow.Viewport.snap({22.0, 38.0}, {15, 15})
    {15.0, 45.0}

# `transform_style`

```elixir
@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`

```elixir
@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`

```elixir
@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`)

---

*Consult [api-reference.md](api-reference.md) for complete listing*
