Shapes, paths, and curves
Draw a rounded crest from a cubic curve and two straight edges, then switch its fill on and off. Paths let you build a shape once and choose how to paint it.
The example also draws a separate circle. That separation is intentional: a new shape needs a new path unless you want both shapes to participate in the same fill or stroke.
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 PathView : View
{
private bool fill = true;
protected override void RenderCore(IContext context)
{
context.SetFill(new Color(0xFAF7FFFF));
context.FillRect(Frame);
context.Save();
try
{
context.Translate((Frame.X + Frame.Width / 2, Frame.Y + Frame.Height / 2));
NFloat scale = NFloat.Min(Frame.Width / 320, Frame.Height / 440);
context.Scale((scale, scale));
context.BeginPath();
context.MoveTo((-120, 40));
context.CurveTo((-120, -160), (120, -160), (120, 40));
context.LineTo((0, 120));
context.ClosePath();
if (fill)
{
context.SetFill(new Color(0xD9B6F4FF));
context.Fill();
}
context.SetStroke(new Color(0x8538CEFF));
context.LineWidth = 6;
context.LineJoin = LineJoin.Round;
context.Stroke();
context.BeginPath();
context.Arc((0, 0), 28, 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 switch fill on or off", (Frame.Center.X, Frame.Y + 56));
}
public override void OnPointerEvent(ref PointerEventRef e, EventPhase phase)
{
if (phase == EventPhase.Bubble && e.Type == PointerEventType.Down)
{
fill = !fill;
InvalidateRender();
}
}
}
Describe geometry before painting
BeginPath clears the current path. MoveTo starts a contour without drawing a segment. CurveTo adds a cubic Bezier segment using two control points and an endpoint. LineTo adds a straight segment; ClosePath joins the final point back to the contour's start.
The control points pull the curve upward. They are not points that the curve must pass through. Move both control points closer to the top edge and watch the crest become flatter.
Fill paints the inside. Stroke paints along the boundary using the current line width, joins, and caps. The example fills first and strokes second so the outline remains visible. Painting does not clear the path, which allows both operations on the same geometry.
Choose a primitive
Use FillRect or StrokeRect when you only need a rectangle immediately. Use Rect or RoundRect when the rectangle belongs to a path, for example to clip subsequent drawing.
Arc takes a center, radius, start angle, and end angle in radians. A complete circle spans NFloat.Pi * 2. Ellipse adds separate horizontal and vertical radii plus a rotation. ArcTo constructs a rounded connection between tangents, which is useful when rounding a corner in a larger contour.
LineWidth uses logical units and is affected by the active transform. A centered stroke extends to both sides of the path. Leave room for it at a clipped boundary instead of placing a thick outline directly on the viewport edge.
Fit artwork into the available frame
This example defines its geometry around the origin, translates that origin to the frame's center, then applies a uniform scale. Taking the smaller width or height scale preserves the design's proportions.
The view restores the context before drawing its instruction label, so that label stays upright and uses normal page coordinates. This pattern lets one render method combine scaled artwork with unscaled UI.
Try changing LineJoin.Round to another join and increasing the stroke width. Then remove ClosePath: compare the stroked boundary with the filled interior. A path's painting rules are easier to learn when you can see which operation creates each edge.
Continue with Colors, gradients, and compositing to change the paint without changing the geometry.