B4J Question Canvas delete old trace

hibrid0

Active Member
Licensed User
Longtime User
Hi, I'm learning how to use canvas object.
I paint a line, text or anything, when I release the click and touch to start drawing again my last draw is deleted.
I have a line of code canvas1.ClearRect(canvas1.TargetRect), but If I delete it, the draw is repeated on every moved of mouse.

How can I continue drawing without lost the old draw and not show again the same draw.
I want to do a basic paint tool, first on B4J to learn and then write it on b4a.
I see a nice library called CanvasView, but not have all I want.

B4X:
Sub Pane1_Touch (Action As Int, X As Float, Y As Float)
    'Action 0 Click
    'Action 1 Soltar click
    'Action 2 Click mantenido
    'Action 100 mover mouse encima

   
   
    If Action=0 Then
        posX_inicio=X
        posY_inicio=Y
    else if Action=2 Then
        canvas1.ClearRect(canvas1.TargetRect)
        'canvas1.DrawLine(x, y,posX_inicio , posY_inicio, xui.Color_Black, 2)
        canvas1.DrawText("This is a test", X,Y, font1, xui.Color_Black,"LEFT")
'        canvas1.DrawCircle(X, Y, 10+posX_inicio, xui.Color_Black, False, 2)
   
       
    Else if Action=1 Then
'        canvas1.DrawLine(x, y,posX_inicio , posY_inicio, xui.Color_BlAck, 2)
       
'        Dim out As OutputStream
'        out = File.OpenOutput(File.DirApp, "test.jpg", False)
'        Pane1.Snapshot(imagen1.WriteToStream(out, 100, "JPG"))
'      
'        Dim imagen2 As B4XBitmap
'        imagen2 = File.OpenInput(File.DirApp, "test.jpg")
'        canvas1.DrawBitmap(imagen2, canvas1.TargetRect)
    End If
End Sub
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
I see a nice library called CanvasView, but not have all I want.
You don't need any other library for this.

Don't use the action constant values. Use Pane1.TOUCH_ACTION_DOWN and the other constants.

Why are you calling canvas1.ClearRect?

Note that you must also call Canvas1.Invalidate for changes to apply (not needed in B4J).
 
Upvote 0

hibrid0

Active Member
Licensed User
Longtime User
You don't need any other library for this.

Don't use the action constant values. Use Pane1.TOUCH_ACTION_DOWN and the other constants.

Why are you calling canvas1.ClearRect?

Note that you must also call Canvas1.Invalidate for changes to apply (not needed in B4J).
Hi and thanks for your answer, I'm using B4J, if I dont use canvas.ClearReact, the draw repeat again and again, if I put the draw code on action 1 dont preview the draw.
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
What exactly do you want to do?
If you want to show the 'dynamic' drawing during the move of the cursor, you might use two B4XViews and two B4XCursors.
One for the 'dynamic' drawing and one for the main drawing.
On Action_DOWN you define the beginning coordinates.
On Action_MOVE you draw onto the dynamic Canvas.
On Action_UP you draw onto the main Canvas and you clear the dynamic Canvas.
 
Upvote 0

hibrid0

Active Member
Licensed User
Longtime User
What exactly do you want to do?
If you want to show the 'dynamic' drawing during the move of the cursor, you might use two B4XViews and two B4XCursors.
One for the 'dynamic' drawing and one for the main drawing.
On Action_DOWN you define the beginning coordinates.
On Action_MOVE you draw onto the dynamic Canvas.
On Action_UP you draw onto the main Canvas and you clear the dynamic Canvas.
Thanks Klaus that's the trick!!!
Another question is if I want to make redo and undo functions, I need to make a list with all traces, and delete the from the list.
Thats a good way?
Or any suggestions
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
For me, two possibilities:
- Save the bitmap after each drawing and reload it when needed, but you need to limit the number of undos.
- Save the different drawing operations and redo the whole drawing.
It depends on the depth of the number of undos you want to support, saving a lot of bitmaps can be memory consuming.
Memorising a lot of drawings, and with undos inbetween, may become conplicated.
I think that the first solution would be the best, but I don't have real experience in this domain.
Perhaps other users have more concrete experience and could share it.
 
Upvote 0
Top