How to prevent Canvas to erase background?


I’m programming a game using the canvas on macOS and experiencing problems. I tried a simple test-app:

I’m drawing a circle that moves and should leave a trail.

The only way to draw to a canvas is the paint event so there I draw the circle and increment its x and y-positions.

From a timer I call canvas1.fresh(false) and the ball moves as expected.

But it does not leave a trail, the old drawings of the ball are erased.

I know, I could draw into a picture and draw the picture in the paint event, but I don’t want to (big screen, big canvas = slow).

How do I preserve the background? I remember Xojo behaved different years ago.

Thanks for your input!


Create a picture, same size as the canvas.
Draw on the picture
In the paint event of the canvas, use .drawpicture to draw the picture onto the canvas

Better yet: put the picture a backdrop, and draw directly to it. No need to draw again in paint.

Hi Jeff,

thank you for your answer, know this method, but as stated earlier, this is to slow (very big screen, very big canvas) so I just wanted to update what changed.

I thought the canvas.eraseBackground=false setting would prevent that — what is this setting for?

Use the areas() parameter of the Paint event to only paint the region you’ve just updated.

[code]Sub Paint(g As Graphics, areas() As REALbasic.Rect) Handles Paint
If areas.LastRowIndex > -1 Then
For n As Integer = 0 To areas.LastRowIndex
g.DrawPicture(p, areas(n).Left, areas(n).top, areas(n).width, areas(n).height, areas(n).Left, areas(n).top, areas(n).width, areas(n).height)
g.DrawPicture(p, 0, 0)
End If

End Sub

Hi Michael,

this works, thank you. I’ll will compare the speed of the method with the other options.

Hi Julian,

that works very well, I try this out too. In the game I’m using a list which tiles have changed and only they are redrawn, I could pass this info on to the canvas areas()