# canvas

Draw paths and effects in one portable layout and event target.

<a id="construction"></a>

## Construction

Import the drawing API from refract::prelude::*.

```rust
use refract::prelude::*;

fn badge() -> impl View {
    canvas(|g, frame| {
        let path = Path::ellipse(frame.bounds);
        g.fill(&path, Color::BLUE);
    })
    .width(120.0)
    .height(80.0)
    .label("Blue ellipse")
}
```

<a id="drawing"></a>

## Drawing callback

canvas(draw) returns Canvas<()>. The callback implements Fn(&mut GraphicsContext, &CanvasFrame) + 'static and records portable drawing commands. It may capture owned, immutable values. Use canvas_with when input preparation needs tracked state or environment reads.

CanvasFrame.size is the allocated content size before a view-box transform. bounds is the view box, or the content rectangle at the origin. elapsed is visible animation time; it is zero for OnChange and reduced-motion drawing. Drawing callbacks must not perform effects or mutate state.

Provide dimensions or a parent layout that allocates space. Paths do not supply intrinsic size or expand layout. All drawing, including blur and strokes, is clipped to the content box. Desktop uses Vello; web uses retained SVG, including initial prerendered output.

<a id="view-specific-reference"></a>

## View-specific reference

Configure these methods before wrappers or type erasure consume the concrete builder.

| Method | Purpose |
| --- | --- |
| [`view_box`](#view-box) | Fits logical drawing bounds into the allocated content box, centered and preserving aspect ratio. Width and height must be positive and all values finite. This changes drawing coordinates, not layout size; use width and height to allocate space. |
| [`schedule`](#schedule) | Chooses OnChange (the default), Animation, or Paused. OnChange draws at time zero when inputs or size change. Animation advances visible elapsed time and requests frames. Paused freezes elapsed time while still allowing input and size updates. CanvasSchedule::animation() constructs Animation. |
| [`animatable_inputs`](#animatable-inputs) | Opts prepared inputs into existing animation transactions. A subsequent update needs animation intent, such as with_animation; this method alone starts no motion. Built-in inputs include f32, f64, Point, Size, Color, and pairs of Animatable values. Without this modifier, prepared inputs update immediately. |

<a id="view-box"></a>

### `view_box`

Fits logical drawing bounds into the allocated content box, centered and preserving aspect ratio. Width and height must be positive and all values finite. This changes drawing coordinates, not layout size; use width and height to allocate space.

```rust
fn view_box(self, bounds: Rect) -> Self
```

<a id="schedule"></a>

### `schedule`

Chooses OnChange (the default), Animation, or Paused. OnChange draws at time zero when inputs or size change. Animation advances visible elapsed time and requests frames. Paused freezes elapsed time while still allowing input and size updates. CanvasSchedule::animation() constructs Animation.

```rust
fn schedule(self, schedule: CanvasSchedule) -> Self
```

<a id="animatable-inputs"></a>

### `animatable_inputs`

Opts prepared inputs into existing animation transactions. A subsequent update needs animation intent, such as with_animation; this method alone starts no motion. Built-in inputs include f32, f64, Point, Size, Color, and pairs of Animatable values. Without this modifier, prepared inputs update immediately.

```rust
fn animatable_inputs(self) -> Self
where
    I: Animatable
```

<a id="common-modifiers"></a>

## Common view modifiers

Canvas dimensions, paint, labels, and typed event callbacks preserve Canvas<I>. Configure view_box, schedule, and animatable_inputs before padding, frame, animation, transaction, on_press, or focus-binding wrappers consume the canvas builder. Canvas has no child methods: draw paths inside its callback or compose separate views around it.

A canvas is one layout and event target. Label meaningful drawings and handle pointer events on that target; individual paths are not separate controls. See [View Modifiers](/docs/view-modifiers#drawing) and [Canvas](/docs/canvas).

<a id="source"></a>

## Source reference

The implementation is in `crates/core/src/drawing/canvas.rs`.
