# Zoom and coordinate conversion

Treat magnification as its own input. A native trackpad pinch can arrive as `ZoomEventRef`, while a touch pinch consists of multiple pointer contacts that your surface must recognize.

This circle accepts native zoom when the host sends it. Tap to cycle the scale, or focus it and use Up/Down, so the same demo remains useful in a browser that does not supply zoom events.

<div class="docs-demo-layout">

```csharp
using System.Globalization;
using Xui.Core.Abstract.Events;
using Xui.Core.Canvas;
using Xui.Core.UI;
using Xui.Core.UI.Input;
using NFloat = System.Runtime.InteropServices.NFloat;

public sealed class ZoomPad : View
{
    private NFloat scale = 1;
    private int displayedPercent = 100;
    private string scaleLabel = "Scale: 100%";
    public override bool Focusable => true;

    public override void OnZoom(ref ZoomEventRef e)
    {
        if (e.Handled || !NFloat.IsFinite(e.ScaleFactor) || e.ScaleFactor <= 0)
            return;
        SetScale(scale * e.ScaleFactor);
        e.Handled = true;
    }

    public override void OnPointerEvent(ref PointerEventRef e, EventPhase phase)
    {
        if (phase != EventPhase.Bubble || e.Type != PointerEventType.Down)
            return;
        Focus();
        SetScale(scale >= 2 ? 0.5f : scale + 0.25f);
    }

    public override void OnKeyDown(ref KeyEventRef e)
    {
        if (e.Key == VirtualKey.Up)
            SetScale(scale * 1.1f);
        else if (e.Key == VirtualKey.Down)
            SetScale(scale / 1.1f);
        else
            return;
        e.Handled = true;
    }

    private void SetScale(NFloat value)
    {
        scale = NFloat.Clamp(value, 0.5f, 2);
        var percent = (int)(scale * 100);
        if (percent != displayedPercent)
        {
            displayedPercent = percent;
            scaleLabel = "Scale: " + percent.ToString(CultureInfo.InvariantCulture) + "%";
        }

        InvalidateRender();
    }

    protected override void OnDeactivate()
    {
        Blur();
        base.OnDeactivate();
    }

    protected override void RenderCore(IContext context)
    {
        context.Save();
        try
        {
            context.SetFill(new Color(0xFAF7FFFF));
            context.FillRect(Frame);
            context.SetFill(new Color(0x8538CEFF));
            context.BeginPath();
            context.Arc((Frame.X + Frame.Width / 2, Frame.Y + Frame.Height / 2), 60 * scale, 0, NFloat.Pi * 2);
            context.Fill();
            context.SetFont(new Font(16, "Inter"));
            context.TextAlign = TextAlign.Center;
            context.TextBaseline = TextBaseline.Middle;
            context.FillText(scaleLabel, (Frame.X + Frame.Width / 2, Frame.Y + 80));
            context.FillText("Tap to zoom, or use Up / Down", (Frame.X + Frame.Width / 2, Frame.Bottom - 80));
        }
        finally
        {
            context.Restore();
        }
    }
}
```

<figure class="docs-demo" data-demo-url="../../../wasm/?embed=1&amp;demo=input/zoom-pad">
<div class="docs-demo-stage">
<img src="input-zoom-pad.svg" width="400" height="600" alt="Zoom pad: 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>Zoom pad · <a href="input-zoom-pad.cs">C# source</a> · <a href="input-zoom-pad.svg">SVG</a> · <a href="../../../wasm/?embed=1&amp;demo=input/zoom-pad">Open demo</a></figcaption>
</figure>

</div>


## Apply incremental scale

`ScaleFactor` is a multiplier: one means no change; 1.1 makes the current scale ten percent larger. Multiply your current scale and clamp it to a meaningful range. Reject non-finite or non-positive values before they can corrupt geometry.

Set `Handled` after consuming the gesture so an ancestor does not zoom again. `Position` is a window-space anchor only when `HasPosition` is true. The circle deliberately scales around its own center; a document editor should keep the content under the gesture anchor stationary.

For an editor with mapping `screen = origin + world * scale`, compute `world = (anchor - origin) / oldScale`, choose the new scale, then set `origin = anchor - world * newScale`. Use the same mapping for drawing and hit testing.

## Know which space you received

Routed pointer `State.Position` is already mapped through the view's ancestors. Calling `GlobalToLocal` again applies the mapping twice. Conversely, zoom and wheel event position fields remain window-space data; their router uses transformed positions for hit testing without rewriting those fields. Convert their anchor with `GlobalToLocal` when you need the receiving view's space.

`LocalToGlobal` and `GlobalToLocal` honor view transform overrides. They do not automatically subtract every ordinary frame's origin: the default view mapping is identity to preserve Xui's layout coordinate model. If you draw after `context.Translate((Frame.X, Frame.Y))`, subtract that frame origin when deriving coordinates for the drawing.

## Keep a fallback

macOS forwards native magnification. The current browser bridge sends wheel events but does not forward a separate `ZoomEventRef`; do not advertise browser trackpad pinch support just because the method exists. A native mobile touch pair also needs explicit gesture recognition. Provide visible zoom controls or a keyboard path, and verify native gestures on the platform you ship.
