# Transforms and coordinate spaces

Draw a dial with twelve marks and a hand that rotates one step per press. The geometry stays simple because each part is drawn near its own origin.

A drawing transform changes where later commands land. It does not change `Frame`, rearrange siblings, or automatically move a view's interaction region.

<div class="docs-demo-layout">

```csharp
using Xui.Core.Canvas;
using Xui.Core.Math2D;
using Xui.Core.UI;
using Xui.Core.UI.Input;
using NFloat = System.Runtime.InteropServices.NFloat;

public sealed class TransformDial : View
{
    private int step = 1;
    protected override void RenderCore(IContext context)
    {
        context.SetFill(new Color(0xFAF7FFFF));
        context.FillRect(Frame);
        context.Save();
        try
        {
            context.Translate((Frame.Center.X, Frame.Center.Y));
            NFloat scale = NFloat.Min(Frame.Width, Frame.Height) / 320;
            context.Scale((scale, scale));
            for (int i = 0; i < 12; i++)
            {
                context.Save();
                context.Rotate(i * NFloat.Pi / 6);
                context.SetFill(new Color(0xD9B6F4FF));
                context.FillRect(new Rect(-3, -120, 6, 16));
                context.Restore();
            }

            context.Rotate(step * NFloat.Pi / 6);
            context.SetFill(new Color(0x8538CEFF));
            context.FillRect(new Rect(-8, -90, 16, 105));
            context.BeginPath();
            context.Arc((0, 0), 14, 0, NFloat.Pi * 2);
            context.SetFill(new Color(0xD957A8FF));
            context.Fill();
        }
        finally
        {
            context.Restore();
        }

        context.SetFill(new Color(0x492571FF));
        context.SetFont(new Font(18, "Inter"));
        context.TextAlign = TextAlign.Center;
        context.FillText("Tap to rotate one step", (Frame.Center.X, Frame.Y + 56));
    }

    public override void OnPointerEvent(ref PointerEventRef e, EventPhase phase)
    {
        if (phase == EventPhase.Bubble && e.Type == PointerEventType.Down)
        {
            step = (step + 1) % 12;
            InvalidateRender();
        }
    }
}
```

<figure class="docs-demo" data-demo-url="../../../wasm/?embed=1&amp;demo=canvas/transforms">
<div class="docs-demo-stage">
<img src="canvas-transforms.svg" width="400" height="600" alt="Canvas transforms: initial rendered view" />
<button type="button" class="docs-demo-start" hidden><span class="docs-demo-pill">Start demo</span></button>
</div>
<div class="docs-demo-actions">
<button type="button" class="docs-demo-close" hidden>Close demo</button>
<span class="docs-demo-status" role="status" aria-live="polite"></span>
</div>
<figcaption>Canvas transforms · <a href="canvas-transforms.cs">C# source</a> · <a href="canvas-transforms.svg">SVG</a> · <a href="../../../wasm/?embed=1&amp;demo=canvas/transforms">Open demo</a></figcaption>
</figure>

</div>


## Establish a useful origin

Translate to `Frame.Center` first. The hand can now be a rectangle around `(0, 0)` rather than a collection of window coordinates. Scale uniformly so a 320-unit design fits the available frame.

Each mark saves the current state, rotates by its angle, draws a small rectangle above the origin, and restores. Without that inner restore, each iteration would add its rotation to all previous rotations.

Angles use radians. Twelve equally spaced marks are separated by `NFloat.Pi / 6`. With the usual downward screen Y axis, positive rotation moves clockwise.

## Understand composition order

`Translate`, `Scale`, and `Rotate` multiply the current transform. Their order changes the result. Translating to the center and then rotating makes the hand turn around that center. Rotating before applying a translation rotates the translation vector too.

With `AffineTransform`, `left * right` applies `right` first. For example, `Translate(offset) * Scale(factors)` scales local geometry, then moves it. The same matrix can transform a `Point`; transforming a `Vector` ignores translation, which is appropriate for a movement delta or direction.

Prefer `Transform(matrix)` when adding to an inherited context transform. `SetTransform(matrix)` replaces it; using that inside a child can discard a parent scroll or zoom transform.

## Convert input using the inverse

If `screen = transform * world`, then `world = transform.Inverse * screen`. Subtract any frame-relative origin that you introduced separately before applying the inverse. Keep those steps consistent with the render path.

A zero scale creates a singular transform. Clamp zoom to a positive range and reject invalid values before using an inverse. Also distinguish points from deltas: adding a translation to a drag delta makes the object jump.

The dial reacts anywhere within its untransformed frame, so it does not need geometry hit testing. For a transformed child view, use [Transforms, clipping, and layers](../../views/transforms-layers/index.md). For individual objects inside a canvas, [Build an interactive drawing surface](../drawing-surface/index.md) uses the same camera calculation for both drawing and selection.

Try moving the origin to the upper-left corner while keeping the rotation. The new pivot makes the multiplication order visible immediately.
