Clearing a CanvasGadget
Re: Clearing a CanvasGadget
You have to comment out the FillMemory() part to make the Box() part visible.
- marcoagpinto
- Addict
- Posts: 1045
- Joined: Sun Mar 10, 2013 3:01 pm
- Location: Portugal
- Contact:
Re: Clearing a CanvasGadget
But the FillMemory part doesn't change the canvas to red.#NULL wrote:You have to comment out the FillMemory() part to make the Box() part visible.
What am I doing wrong?
Re: Clearing a CanvasGadget
@#NULL: Got it! Thanks. Now it works.
Re: Clearing a CanvasGadget
On my system DrawingBufferPixelFormat() returns #PB_PixelFormat_24Bits_RGB so maybe you can't write an alpha value that way(?) I Don't know how you can FillMemory with a 3-byte sequence.
Re: Clearing a CanvasGadget
DIsable the Debugger if you test performance.
The following works, but I don't know why. The FillMemory should result in white instead of red as far as I understand.
The following works, but I don't know why. The FillMemory should result in white instead of red as far as I understand.
Code: Select all
OpenWindow(0,0,0,600,600,"Test")
CanvasGadget(1,0,0,500,500)
StartDrawing(CanvasOutput(1))
time1_start=ElapsedMilliseconds()
For f=1 To 1000
Box(1,0,500,500, $ff0000ff)
Next f
time1_end=ElapsedMilliseconds()
While WindowEvent() : Wend
Delay(2000) ; To see the red
time2_start=ElapsedMilliseconds()
For f=1 To 1000
For y=0 To OutputHeight()-1
FillMemory(DrawingBuffer(), DrawingBufferPitch(), #Red)
Next
Next f
time2_end=ElapsedMilliseconds()
StopDrawing()
MessageRequester("",
"time1: "+Str(time1_end-time1_start) + #CRLF$ +
"time2: "+Str(time2_end-time2_start) )
quit=0
Repeat
event=WaitWindowEvent()
; User has pressed the close window gadget
If event=#PB_Event_CloseWindow
quit=1
EndIf
Until quit=1
End
Re: Clearing a CanvasGadget
The output is only done with StopDrawing()marcoagpinto wrote:What is wrong in my test code?
Code: Select all
OpenWindow(0,0,0,600,600,"Test")
CanvasGadget(1,0,0,500,500)
StartDrawing(CanvasOutput(1))
time1_start=ElapsedMilliseconds()
For f=1 To 1000
Box(1,0,500,500,#Red)
Next f
time1_end=ElapsedMilliseconds()
StopDrawing()
Delay(2000) ; To see the red
StartDrawing(CanvasOutput(1))
time2_start=ElapsedMilliseconds()
For f=1 To 1000
FillMemory(DrawingBuffer(), DrawingBufferPitch() * OutputHeight(), #Red)
Next f
time2_end=ElapsedMilliseconds()
StopDrawing()
Debug "time1: "+Str(time1_end-time1_start)
Debug "time2: "+Str(time2_end-time2_start)
quit=0
Repeat
event=WaitWindowEvent()
; User has pressed the close window gadget
If event=#PB_Event_CloseWindow
quit=1
EndIf
Until quit=1
End
sorry for my bad english
- marcoagpinto
- Addict
- Posts: 1045
- Joined: Sun Mar 10, 2013 3:01 pm
- Location: Portugal
- Contact:
Re: Clearing a CanvasGadget
Ahhhh...
I was stressed and didn't notice the StopDrawing only at the end.
@Fred:
Could you implement a function in PB to clear a canvas?
ClearCanvasGadget(gadget, colour)
?
Thank you!
I was stressed and didn't notice the StopDrawing only at the end.
@Fred:
Could you implement a function in PB to clear a canvas?
ClearCanvasGadget(gadget, colour)
?
Thank you!
Re: Clearing a CanvasGadget
This is already implemented and is called SetGadgetColor()marcoagpinto wrote:@Fred:
Could you implement a function in PB to clear a canvas?
ClearCanvasGadget(gadget, colour)

Look at my posting from 17.01.2019 20:47 in this thread.
sorry for my bad english
Re: Clearing a CanvasGadget
I don't know about Windows but on macOS SetGadgetColor is rather slow for clearing the canvas.Josh wrote:his is already implemented and is called SetGadgetColor()![]()
Look at my posting from 17.01.2019 20:47 in this thread.
FillMemory is fastest but what also is very fast on macOS is to create a background image you want and use
Code: Select all
SetGadgetAttribute(CanvasGadget, #PB_Canvas_Image, ImageID(MyBackgroundImage))
Windows (x64)
Raspberry Pi OS (Arm64)
Raspberry Pi OS (Arm64)
- marcoagpinto
- Addict
- Posts: 1045
- Joined: Sun Mar 10, 2013 3:01 pm
- Location: Portugal
- Contact:
Re: Clearing a CanvasGadget
Well,Josh wrote:This is already implemented and is called SetGadgetColor()marcoagpinto wrote:@Fred:
Could you implement a function in PB to clear a canvas?
ClearCanvasGadget(gadget, colour)![]()
Look at my posting from 17.01.2019 20:47 in this thread.
It doesn't work properly

Code: Select all
StartDrawing(CanvasOutput(1))
time2_start=ElapsedMilliseconds()
For f=1 To 1000
DrawText(10,10,"TESTING",#Yellow,#White)
SetGadgetColor(1,#PB_Gadget_BackColor,#Blue)
SetGadgetColor(1,#PB_Gadget_FrontColor,#Blue)
ResizeGadget(1, #PB_Ignore, #PB_Ignore, #PB_Ignore, #PB_Ignore)
Next f
time2_end=ElapsedMilliseconds()
StopDrawing()
Re: Clearing a CanvasGadget
Don't use SetGadgetColor() inside StartDrawing/StopDrawingmarcoagpinto wrote:It doesn't work properly![]()
Code: Select all
OpenWindow(0,0,0,600,600,"Test")
CanvasGadget(1,0,0,500,500)
StartDrawing(CanvasOutput(1))
time1 = ElapsedMilliseconds()
For i = 1 To 1000
Box(1,0,500,500,#Red)
Next
time1 = ElapsedMilliseconds() - time1
StopDrawing()
MessageRequester ("", "")
StartDrawing(CanvasOutput(1))
time2 = ElapsedMilliseconds()
For i = 1 To 1000
FillMemory(DrawingBuffer(), DrawingBufferPitch() * OutputHeight(), #Red)
Next
time2 = ElapsedMilliseconds() - time2
StopDrawing()
MessageRequester ("", "")
SetGadgetColor (1, #PB_Gadget_BackColor, #Green)
time3 = ElapsedMilliseconds()
For i = 1 To 1000
ResizeGadget (1, #PB_Ignore, #PB_Ignore, #PB_Ignore, #PB_Ignore)
Next
time3 = ElapsedMilliseconds() - time3
MessageRequester ("", "")
SetGadgetColor (1, #PB_Gadget_BackColor, #Yellow)
StartDrawing(CanvasOutput(1))
time4 = ElapsedMilliseconds()
For i = 1 To 1000
Next
time4 = ElapsedMilliseconds() - time4
StopDrawing()
Debug "time1: " + time1
Debug "time2: " + time2
Debug "time3: " + time3
Debug "time4: " + time4
Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
sorry for my bad english
- marcoagpinto
- Addict
- Posts: 1045
- Joined: Sun Mar 10, 2013 3:01 pm
- Location: Portugal
- Contact:
Re: Clearing a CanvasGadget
Well,
I give up.
In my code I am drawing in the canvas and then want to clear it, but this last SetGadgetColour is not the fastest solution.
I give up.
In my code I am drawing in the canvas and then want to clear it, but this last SetGadgetColour is not the fastest solution.
Code: Select all
OpenWindow(0,0,0,600,600,"Test")
CanvasGadget(1,0,0,500,500)
time1 = ElapsedMilliseconds()
For i = 1 To 1000
StartDrawing(CanvasOutput(1))
DrawText(10,10,"TEST",#Yellow,#Blue)
Box(1,0,500,500,#Red)
StopDrawing()
Next
time1 = ElapsedMilliseconds() - time1
; MessageRequester ("", "")
time2 = ElapsedMilliseconds()
For i = 1 To 1000
StartDrawing(CanvasOutput(1))
DrawText(10,10,"TEST",#Yellow,#Blue)
FillMemory(DrawingBuffer(), DrawingBufferPitch() * OutputHeight(), #Red)
StopDrawing()
Next
time2 = ElapsedMilliseconds() - time2
; MessageRequester ("", "")
time3 = ElapsedMilliseconds()
For i = 1 To 1000
StartDrawing(CanvasOutput(1))
DrawText(10,10,"TEST",#Yellow,#Blue)
StopDrawing()
SetGadgetColor (1, #PB_Gadget_BackColor, #Green)
Next
time3 = ElapsedMilliseconds() - time3
Debug "time1: " + time1
Debug "time2: " + time2
Debug "time3: " + time3
Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
Re: Clearing a CanvasGadget
Can you explain why you are doing this?In my code I am drawing in the canvas and then want to clear it
- marcoagpinto
- Addict
- Posts: 1045
- Joined: Sun Mar 10, 2013 3:01 pm
- Location: Portugal
- Contact:
Re: Clearing a CanvasGadget
Yes.#NULL wrote:Can you explain why you are doing this?In my code I am drawing in the canvas and then want to clear it
Imagine you have a background image and you have hundreds of insects moving over it (on the canvas gadget).
So, every time I increase a time unit (1 to 1000) I need to clear the canvas, draw the background and draw the new position of the insects which use alpha transparency.

Re: Clearing a CanvasGadget
It sounded to me as if you would draw and then clear, both in a single drawing block, that's why I asked. But just clear and draw makes sense though
. Any reason why you don't use a screen?
