I need draw rectangle in canvas. I know how to draw. But I did not get to do so would draw on a 360-degree
Example. blue, lilac, green they are one and the same rect
If someone is interested, this is solution for UWP. (based on @Kris answer).
It works for mouse, touch and pen.
Code behind:
private Point startPoint;
private Rectangle rect;
private void canvas_PointerPressed(object sender, PointerRoutedEventArgs e)
{
startPoint = e.GetCurrentPoint(canvas).Position;
rect = new Rectangle()
{
Stroke = new SolidColorBrush(Colors.LightCoral),
StrokeThickness = 5
};
Canvas.SetLeft(rect, startPoint.X);
Canvas.SetTop(rect, startPoint.Y);
canvas.Children.Add(rect);
}
private void canvas_PointerMoved(object sender, PointerRoutedEventArgs e)
{
if (!e.Pointer.IsInContact || rect == null)
return;
var pos = e.GetCurrentPoint(canvas).Position;
var x = Math.Min(pos.X, startPoint.X);
var y = Math.Min(pos.Y, startPoint.Y);
var w = Math.Max(pos.X, startPoint.X) - x;
var h = Math.Max(pos.Y, startPoint.Y) - y;
rect.Width = w;
rect.Height = h;
Canvas.SetLeft(rect, x);
Canvas.SetTop(rect, y);
}
private void canvas_PointerReleased(object sender, PointerRoutedEventArgs e)
{
rect = null;
}
XAML: