Help with delay between draw events in WebCanvas

I’m hunting for a way to use a DrawString in a webcanvas, wait a second, then use another DrawString.

g.TextFont = "Courier" g.TextSize = 20 g.ForeColor = &c1DBA00 Dim waitUntil As Integer Dim x as Integer = 150 Dim y as Integer = 150 g.DrawString("Test 1", x, y) 'Canvas1.Refresh(false) 'Canvas1.Invalidate(false) waitUntil = Ticks + 120 'While ticks < waitUntil 'Wend g.DrawString("Test 2", x, y+25)

The ticks delay the time for 2 seconds (for testing purposes). But this delay delays all of the drawings by 2 seconds, and then draws them all at once with no delays.

I’ve tried suggestions such as using Canvas.Invalidate(false) and Refresh(false) inside a Timer but I get NilObjectException errors all along the paint cycles. I tried the same in between the DrawStrings but get a NOE on the first one. I know its invalidating the WebGraphics variable and requiring a new one, but I’m trying to keep the drawstring inline inside the method.

Thanks for any help!

You’re not going to have any luck with this line of thinking
ALL drawing commands are sent at once as a group to the web browser so delaying isn’t going to behave like you expect
It will just, as you found, delay everything

The reason is that because the front end (what the user sees) is in a browser somewhere and the code you’re running is on a server (somewhere else) this keeps latency down as much as possible

I was afraid of that but was hopeful there would be a possible method. I’ll move on to a different plan. Thanks!

Ok, so here’s my next attempt.

Property Animate = 0

In a button:

For i as Integer = 0 to 1 Animation = i Canvas1.Invalidate(false) Next i

In a Paint method:

Select Case Animation Case 0 g.ClearRect(0, 0, g.Width, g.Height) g.DrawString("Test 1", x, y) Case 1 waitUntil = Ticks + 120 While ticks < waitUntil Wend g.DrawString("Test 2", x, y+25) End Select

So this almost works except it clears the background between Test 1 and Test 2. Any way of stopping that from happening? I can usually get the draws to not clear the background, but this instance it clears each time.

There is no erasebackground property for webcanvas. So indeed, each draw will erase the background. You will have to draw again.

Instead of using tight loop that freezes all sessions, use a timer.

Something like this will work (tested) :

  • Add Animation as Integer to the WebPage
  • Add a Timer set to off and period 1000
  • In Timer1.Action:

Sub Action() Handles Action Animation = 1 Canvas1.Invalidate Animation = 0 End Sub

  • In the Canvas1 Paint :

Sub Paint(g as WebGraphics) Handles Paint dim x as integer = 100 dim y as integer = 100 Select Case Animation Case 0 g.ClearRect(0, 0, g.Width, g.Height) g.DrawString("Test 1", x, y) Case 1 g.ClearRect(0, 0, g.Width, g.Height) g.DrawString("Test 1", x, y) g.DrawString("Test 2", x, y+25) End Select End Sub

  • In a button Action :

Sub Action() Handles Action Canvas1.Invalidate Timer1.Mode = Timer.ModeSingle End Sub

Well what are you trying to do?
If you tell us specifically what you’re trying to do we might have better solutions than round-trip animating a WebCanvas.

@Michel Bujardet

I ended up doing:

Paint:

g.TextFont = "Courier" g.TextSize = 20 g.ForeColor = &c1DBA00 Dim waitUntil As Integer Dim x as Integer = 150 Dim y as Integer = 150 Dim i as Integer For i = 0 to Animation Select Case i Case 0 g.ClearRect(0, 0, g.Width, g.Height) g.DrawString("Test 1", x, y) Case 1 waitUntil = Ticks + 1200 While ticks < waitUntil Wend g.DrawString("Test 2", x, y+25) End Select Next i

Button:

For i as Integer = 0 to 1 Animation = i Canvas1.Invalidate(false) Next i

I’ve tested the while loop and it doesn’t seem to stop multiple sessions for acting independently, but was only a limited test.

@Tim Parnell
Was just looking for to use canvas to put colorful text in a column, little by little. I know I could try and make a textbox dance, but I’m learning canvas.

Tight loops are a thing of the past. They do interfere with other sessions, as they drag CPU down. In Desktop they freeze the entire app.

On the surface, they seem easier, but they will come back to haunt you.

I will have told you.