1

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.

Cheesebaron
  • 24,131
  • 15
  • 66
  • 118
  • do you want to erase specific paths, or do you want more of a freehand eraser like in a drawing program? – Jason Jul 16 '20 at 15:51
  • yeah, freehand, user can remove or erase where they want . – muhammad-zubair Jul 16 '20 at 16:30
  • just capture the path like you do normally, but set the color to the background color of the canvas – Jason Jul 16 '20 at 16:32
  • i thought about this , but i'm not sure , is this a right way. because in this way it's add a new path and this will increase the size of list , may be it effect on memory . – muhammad-zubair Jul 16 '20 at 16:38
  • I've done this and it works great. A path is a pretty small data construct. Any other method would still need to capture a path-like structure to allow for freehand. – Jason Jul 16 '20 at 16:40
  • okay , i will try this.but if you have any other method then please share it . and thanks for your time . – muhammad-zubair Jul 16 '20 at 16:52
  • @Jason i tried the method that you told me , but unfortunately it's not working. when i draw again after removing then the removed part displayed again .i think i'm missing some thing .could you please share your code? – muhammad-zubair Jul 17 '20 at 09:15

1 Answers1

1

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;
TPG
  • 2,811
  • 1
  • 31
  • 52