Skip to content

Commit 15b7452

Browse files
Copilotmattleibow
andcommitted
Fix touch coordinate scaling in sample app: multiply by DPI so lines draw where the user touches
Touch events from JS arrive in CSS pixels, but the SkiaSharp canvas surface renders at device pixel ratio when IgnorePixelScaling is false (default). The app now scales touch coordinates, stroke widths, and legend elements by DPI. This is done in the app per the reviewer's guidance — the library should not apply scaling. Co-authored-by: mattleibow <1096616+mattleibow@users.noreply.github.com>
1 parent eafaddb commit 15b7452

File tree

1 file changed

+19
-10
lines changed

1 file changed

+19
-10
lines changed

samples/SkiaSharpDemo.Blazor/Pages/Touch.razor

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -129,26 +129,32 @@
129129
if (_log.Count > MaxLogEntries)
130130
_log.RemoveAt(_log.Count - 1);
131131

132+
// Scale CSS-pixel coordinates to canvas pixels (device DPI).
133+
// Touch events arrive in CSS pixels, but the SkiaSharp surface
134+
// renders at device pixel ratio when IgnorePixelScaling is false.
135+
var dpi = _canvasView?.Dpi ?? 1f;
136+
var scaledLocation = new SKPoint(e.Location.X * dpi, e.Location.Y * dpi);
137+
132138
// Drawing strokes
133139
switch (e.ActionType)
134140
{
135141
case SKTouchAction.Pressed:
136142
var stroke = new Stroke { DeviceType = e.DeviceType };
137-
stroke.Points.Add(e.Location);
143+
stroke.Points.Add(scaledLocation);
138144
_strokes.Add(stroke);
139145
_activeStrokes[e.Id] = stroke;
140146
break;
141147

142148
case SKTouchAction.Moved:
143149
if (_activeStrokes.TryGetValue(e.Id, out var active))
144-
active.Points.Add(e.Location);
150+
active.Points.Add(scaledLocation);
145151
break;
146152

147153
case SKTouchAction.Released:
148154
case SKTouchAction.Cancelled:
149155
if (_activeStrokes.TryGetValue(e.Id, out var finished))
150156
{
151-
finished.Points.Add(e.Location);
157+
finished.Points.Add(scaledLocation);
152158
_activeStrokes.Remove(e.Id);
153159
}
154160
break;
@@ -165,10 +171,13 @@
165171
var canvas = e.Surface.Canvas;
166172
canvas.Clear(SKColors.White);
167173

174+
// Scale stroke width and legend by DPI so they look correct at device resolution
175+
var dpi = _canvasView?.Dpi ?? 1f;
176+
168177
using var paint = new SKPaint
169178
{
170179
IsAntialias = true,
171-
StrokeWidth = 3,
180+
StrokeWidth = 3 * dpi,
172181
Style = SKPaintStyle.Stroke,
173182
StrokeCap = SKStrokeCap.Round,
174183
StrokeJoin = SKStrokeJoin.Round,
@@ -188,17 +197,17 @@
188197
canvas.DrawPath(path, paint);
189198
}
190199

191-
// Legend in top-left corner
192-
using var legendFont = new SKFont { Size = 12, Edging = SKFontEdging.SubpixelAntialias };
200+
// Legend in top-left corner (scaled by DPI)
201+
using var legendFont = new SKFont { Size = 12 * dpi, Edging = SKFontEdging.SubpixelAntialias };
193202
using var legendPaint = new SKPaint { IsAntialias = true };
194-
var y = 16f;
203+
var y = 16f * dpi;
195204
foreach (var (name, _, color) in DeviceColors)
196205
{
197206
legendPaint.Color = color;
198-
canvas.DrawCircle(12, y - 4, 5, legendPaint);
207+
canvas.DrawCircle(12 * dpi, y - 4 * dpi, 5 * dpi, legendPaint);
199208
legendPaint.Color = SKColors.Black;
200-
canvas.DrawText(name, 22, y, SKTextAlign.Left, legendFont, legendPaint);
201-
y += 18;
209+
canvas.DrawText(name, 22 * dpi, y, SKTextAlign.Left, legendFont, legendPaint);
210+
y += 18 * dpi;
202211
}
203212
}
204213

0 commit comments

Comments
 (0)