# Transforms, clipping, and layers

Move or scale a view while keeping its input aligned with the picture. A Canvas transform alone changes drawing; a `TransformView` also maps routed input and render bounds.

The card below scales to 82 percent and moves right and down. Its rounded clip removes the corners. Tapping the visible card changes its color.

<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 ClippedCard : TransformView
{
    public ClippedCard()
    {
        Content = new Card();
        Transform = AffineTransform.Translate((24, 32)) * AffineTransform.Scale(((NFloat)0.82, (NFloat)0.82));
    }

    protected override void RenderTransformedContent(IContext context)
    {
        context.BeginPath();
        context.RoundRect(Frame, 32);
        context.Clip();
        base.RenderTransformedContent(context);
    }

    // Clip the interaction region too; a Canvas clip only changes pixels.
    public override bool HitTest(Point point)
    {
        if (!Frame.Contains(point))
            return false;
        NFloat radius = NFloat.Min(32, NFloat.Min(Frame.Width, Frame.Height) / 2);
        NFloat x = NFloat.Clamp(point.X, Frame.X + radius, Frame.Right - radius);
        NFloat y = NFloat.Clamp(point.Y, Frame.Y + radius, Frame.Bottom - radius);
        var dx = point.X - x;
        var dy = point.Y - y;
        return dx * dx + dy * dy <= radius * radius;
    }

    private sealed class Card : View
    {
        private bool alternate;
        public override void OnPointerEvent(ref PointerEventRef e, EventPhase phase)
        {
            if (phase == EventPhase.Bubble && e.Type == PointerEventType.Down)
            {
                alternate = !alternate;
                InvalidateRender();
            }
        }

        protected override void RenderCore(IContext context)
        {
            context.SetFill(new Color(alternate ? 0xD957A8FFu : 0x8538CEFFu));
            context.FillRect(Frame);
            context.SetFill(Colors.White);
            context.SetFont(new Font(22, "Inter"));
            context.TextAlign = TextAlign.Center;
            context.TextBaseline = TextBaseline.Middle;
            context.FillText("Tap the transformed card", Frame.Center);
        }
    }
}
```

<figure class="docs-demo" data-demo-url="../../../wasm/?embed=1&amp;demo=views/clipped-card">
<div class="docs-demo-stage">
<img src="views-clipped-card.svg" width="400" height="600" alt="Transformed card: 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>Transformed card · <a href="views-clipped-card.cs">C# source</a> · <a href="views-clipped-card.svg">SVG</a> · <a href="../../../wasm/?embed=1&amp;demo=views/clipped-card">Open demo</a></figcaption>
</figure>

</div>


## Keep layout and presentation separate

`TransformView` gives its child the normal layout rectangle. Its `Transform` changes how that rectangle appears without asking siblings to move. This is useful for slide transitions, zoomable content, and temporary emphasis.

The expression `Translate * Scale` applies the scale first and the translation second. Matrix multiplication order matters. Keep the matrix invertible: a zero scale has no usable inverse for input mapping.

`TransformPoint` and `InverseTransformPoint` map between the view's spaces. `TransformRect` and `InverseTransformRect` conservatively map rectangle bounds for culling. Use the framework host instead of manually painting a translated child while leaving its hit test at the old location.

## Clip drawing and interaction

The subclass builds a rounded path, calls `Clip`, then renders the child. `TransformView` saves and restores the drawing state around this operation, so the clip does not leak to later siblings.

A drawing clip does not automatically make invisible corners non-interactive. The `HitTest` override checks the same rounded region. The event router already maps a point through the inverse transform before this test, so the calculation uses the untransformed frame.

When a custom visual extends outside its arranged rectangle, update its culling bounds appropriately. Otherwise a renderer may skip visible artwork because its layout box lies outside the viewport.

## Use layers for composition, not extra native windows

Core's `LayerView<TView, TLayer>` stores a struct layer and forwards layout, rendering, and input to it. This supports reusable pieces such as borders and focus decoration inside one view. It is a different concept from a separate platform window.

`TransformView.UseCompositionGroup` can retain a child's drawing while changing its presentation transform. Keep the default for this clipped subclass: its custom clipping lives in `RenderTransformedContent`, while the composition-group path renders through a separate branch. Opting into retention requires reviewing that branch and the target backend; flipping the flag is not a universal optimization.

Try a translation-only transform before introducing scale or rotation. Verify both clicking and clipping at every stage, including the corners and the edges of the viewport.
