How to freehand erase on a SKCanvasView?

737 Views Asked by At

I am trying to make a drawing app in Xamarin.Forms. I am able to draw paths. My problem is, how can I remove part of a path, when user try to erase on the drawing. I have tried the following:

if (IsEraseClicked)
{
    switch (args.Type)
    {
        case TouchActionType.Pressed:
            if (!inProgressPaths.ContainsKey(args.Id))
            {
                SKPath path = new SKPath();
                path.MoveTo(ConvertToPixel(args.Location));
                inProgressPaths.Add(args.Id, path);
                UpdateBitmap();
            }
            break;

        case TouchActionType.Moved:
            if (inProgressPaths.ContainsKey(args.Id))
            {
                SKPath path = inProgressPaths[args.Id];
                path.LineTo(ConvertToPixel(args.Location));
                UpdateBitmap();
            }
            break;

        case TouchActionType.Released:
            if (inProgressPaths.ContainsKey(args.Id))
            {
                if (completedPaths.Contains(inProgressPaths[args.Id]))
                {
                    completedPaths.Remove(inProgressPaths[args.Id]);
                    inProgressPaths.Remove(args.Id);
                    UpdateBitmap();
                }
            }
            break;

        case TouchActionType.Cancelled:
            if (inProgressPaths.ContainsKey(args.Id))
            {
                inProgressPaths.Remove(args.Id);
                UpdateBitmap();
            }
            break;
    }
}

I am using the SkiaSharp library.

1

There are 1 best solutions below

2
On

Just need to change the SKPaint to these setting when the erase is clicked.

paint.BlendMode = SKBlendMode.Src;
paint.Color = SKColors.Transparent;

And set back to when pencil is clicked.

paint.BlendMode = SKBlendMode.SrcOver;
paint.Color = SKColors.Red;