|
| 1 | +@page "/drawing" |
| 2 | + |
| 3 | +@* Drawing Canvas Demo |
| 4 | + Demonstrates SKCanvasView with pointer events, wheel delta for |
| 5 | + brush size control, and basic stroke rendering. |
| 6 | + Shows multi-stroke drawing with color picker and on-demand rendering |
| 7 | + via manual Invalidate() (no render loop). *@ |
| 8 | + |
| 9 | +<h1>Drawing Canvas</h1> |
| 10 | + |
| 11 | +<p> |
| 12 | + Draw with touch or mouse. Use the scroll wheel to change brush size. |
| 13 | +</p> |
| 14 | + |
| 15 | +<div class="canvas-container border rounded p-2"> |
| 16 | + |
| 17 | + <SKCanvasView @ref="skiaView" |
| 18 | + OnPaintSurface="OnPaintSurface" IgnorePixelScaling="true" |
| 19 | + @onpointerdown="OnPointerDown" @onpointermove="OnPointerMove" @onpointerup="OnPointerUp" |
| 20 | + @onwheel="OnWheel" @onwheel:preventDefault="true" /> |
| 21 | + |
| 22 | + <div class="overlay-info"> |
| 23 | + Brush: @(brushSize.ToString("F2"))px |
| 24 | + </div> |
| 25 | + |
| 26 | + <div class="overlay-toolbar"> |
| 27 | + @foreach (var (name, color) in colors) |
| 28 | + { |
| 29 | + <div class="color-swatch @(currentColor == color ? "selected" : "")" |
| 30 | + style="background:@name" |
| 31 | + @onclick="() => SetColor(color)" /> |
| 32 | + } |
| 33 | + <button class="btn btn-outline-light btn-sm" @onclick="ClearCanvas">Clear</button> |
| 34 | + </div> |
| 35 | + |
| 36 | +</div> |
| 37 | + |
| 38 | +@code { |
| 39 | + |
| 40 | + SKCanvasView skiaView = null!; |
| 41 | + float brushSize = 25f; |
| 42 | + SKColor currentColor = SKColors.Black; |
| 43 | + List<DrawingStroke> strokes = []; |
| 44 | + DrawingStroke? currentStroke = null; |
| 45 | + SKPoint lastPoint; |
| 46 | + |
| 47 | + // The paint that is reused for drawing strokes. |
| 48 | + readonly SKPaint strokePaint = new() |
| 49 | + { |
| 50 | + IsAntialias = true, |
| 51 | + Style = SKPaintStyle.Stroke, |
| 52 | + StrokeCap = SKStrokeCap.Round, |
| 53 | + StrokeJoin = SKStrokeJoin.Round, |
| 54 | + }; |
| 55 | + |
| 56 | + // The available brush colors with their CSS names. |
| 57 | + static readonly (string Name, SKColor Color)[] colors = |
| 58 | + { |
| 59 | + ("black", SKColors.Black), |
| 60 | + ("red", SKColors.Red), |
| 61 | + ("blue", SKColors.Blue), |
| 62 | + ("green", SKColors.Green), |
| 63 | + ("orange", SKColors.Orange), |
| 64 | + ("purple", SKColors.Purple), |
| 65 | + }; |
| 66 | + |
| 67 | + void OnPaintSurface(SKPaintSurfaceEventArgs e) |
| 68 | + { |
| 69 | + var canvas = e.Surface.Canvas; |
| 70 | + |
| 71 | + // Clear to white before drawing the strokes. |
| 72 | + canvas.Clear(SKColors.White); |
| 73 | + |
| 74 | + // Draw completed strokes. |
| 75 | + foreach (var stroke in strokes) |
| 76 | + { |
| 77 | + strokePaint.Color = stroke.Color; |
| 78 | + strokePaint.StrokeWidth = stroke.StrokeWidth; |
| 79 | + canvas.DrawPath(stroke.Path, strokePaint); |
| 80 | + } |
| 81 | + |
| 82 | + // Draw in-progress stroke. |
| 83 | + if (currentStroke is not null) |
| 84 | + { |
| 85 | + strokePaint.Color = currentStroke.Color; |
| 86 | + strokePaint.StrokeWidth = currentStroke.StrokeWidth; |
| 87 | + canvas.DrawPath(currentStroke.Path, strokePaint); |
| 88 | + } |
| 89 | + |
| 90 | + // Brush size indicator at last pointer position. |
| 91 | + strokePaint.Color = SKColors.Gray; |
| 92 | + strokePaint.StrokeWidth = 1; |
| 93 | + canvas.DrawCircle(lastPoint, brushSize / 2f, strokePaint); |
| 94 | + } |
| 95 | + |
| 96 | + void OnPointerDown(PointerEventArgs e) |
| 97 | + { |
| 98 | + if (!e.IsPrimary) |
| 99 | + return; |
| 100 | + lastPoint = new SKPoint((float)e.OffsetX, (float)e.OffsetY); |
| 101 | + currentStroke = new DrawingStroke(new(), currentColor, brushSize); |
| 102 | + currentStroke.Path.MoveTo(lastPoint); |
| 103 | + skiaView.Invalidate(); |
| 104 | + } |
| 105 | + |
| 106 | + void OnPointerMove(PointerEventArgs e) |
| 107 | + { |
| 108 | + if (!e.IsPrimary) |
| 109 | + return; |
| 110 | + lastPoint = new SKPoint((float)e.OffsetX, (float)e.OffsetY); |
| 111 | + if (currentStroke is not null) |
| 112 | + currentStroke.Path.LineTo(lastPoint); |
| 113 | + skiaView.Invalidate(); |
| 114 | + } |
| 115 | + |
| 116 | + void OnPointerUp(PointerEventArgs e) |
| 117 | + { |
| 118 | + if (!e.IsPrimary) |
| 119 | + return; |
| 120 | + if (currentStroke is not null) |
| 121 | + { |
| 122 | + strokes.Add(currentStroke); |
| 123 | + currentStroke = null; |
| 124 | + skiaView.Invalidate(); |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + void OnWheel(WheelEventArgs e) |
| 129 | + { |
| 130 | + // DeltaY > 0 = scroll down = decrease brush size; DeltaY < 0 = scroll up = increase. |
| 131 | + // Normalize ~100 CSS pixels per notch to ~1px brush change per notch. |
| 132 | + brushSize -= (float)(e.DeltaY / 100.0); |
| 133 | + brushSize = Math.Clamp(brushSize, 1f, 50f); |
| 134 | + StateHasChanged(); |
| 135 | + } |
| 136 | + |
| 137 | + void SetColor(SKColor color) |
| 138 | + { |
| 139 | + currentColor = color; |
| 140 | + skiaView.Invalidate(); |
| 141 | + StateHasChanged(); |
| 142 | + } |
| 143 | + |
| 144 | + void ClearCanvas() |
| 145 | + { |
| 146 | + foreach (var stroke in strokes) |
| 147 | + { |
| 148 | + stroke.Path.Dispose(); |
| 149 | + } |
| 150 | + strokes.Clear(); |
| 151 | + currentStroke?.Path.Dispose(); |
| 152 | + currentStroke = null; |
| 153 | + skiaView.Invalidate(); |
| 154 | + StateHasChanged(); |
| 155 | + } |
| 156 | + |
| 157 | + // Represents a single drawing stroke with its path, color, and width. |
| 158 | + // Path is owned — callers must dispose when removing strokes. |
| 159 | + record DrawingStroke(SKPath Path, SKColor Color, float StrokeWidth = 25); |
| 160 | +} |
0 commit comments