canvas problem

Just starting out? Need help? Post your questions and find answers here.
User avatar
William Van Hoecke
Posts: 50
Joined: Tue Oct 22, 2019 12:09 pm

canvas problem

Post by William Van Hoecke »

Hello,
The only way I can find to clear a transparent canvas from its previous drawing is
to FREE the canvas an create it again.... works fine
However after another window was shown the canvas never shows again, until I refresh the browserpage (F5)
QUESTION: WHAT am I doing wrong here.

Code: Select all

Enumeration
#grid 
#canvas
#redraw_button
#menu_button
#mainwindow
#popupwindow
EndEnumeration

Procedure drawbar()
  For tmp.i = 0 To 800 Step 50
    h.i = Random(0,400)  
    AddPathBox(tmp,500-h,40,h)
    VectorSourceColor(RGBA(Random(0,255),Random(0,255),Random(0,255),255))
    FillPath(#PB_Path_Preserve)
    StrokePath(2)
  Next tmp
EndProcedure

Procedure Event_on_gadget()
  Select EventType()
    Case #PB_EventType_LeftClick ;left clicks
      Select EventGadget()
        Case #redraw_button  
          CloseWindow(#popupwindow)
          SetActiveWindow(#mainwindow)
          ;remark below line to NOT recreate the drawing canvas
          If IsGadget(#canvas):FreeGadget(#canvas):CanvasGadget(#canvas,100,100,800,500,#PB_Canvas_Border | #PB_Canvas_Transparent): EndIf
          StartVectorDrawing(CanvasVectorOutput(#canvas))
            drawbar()
            ResetPath()
          StopVectorDrawing()
        Case #menu_button
          OpenWindow(#popupwindow,500,50, 740, 410, "WINDOW WILL BE KILLED with REDRAW button",#PB_Window_SystemMenu | #PB_Window_TitleBar)
      EndSelect  
  EndSelect  
EndProcedure

If OpenWindow(#mainwindow, 100, 100, 1000, 700, "2DDrawing Example")
  CanvasGadget(#grid,100,100,800,500,#PB_Canvas_Border)
  StartDrawing(CanvasOutput(#grid))
  For tmp.i = 0 To 800 Step 10
    LineXY(tmp,0,tmp,500,RGB(200,200,200))  
  Next tmp
  For tmp.i = 0 To 500 Step 10
    LineXY(0,tmp,800,tmp,RGB(200,200,200))  
  Next tmp
  StopDrawing()
  ButtonGadget(#redraw_button,20,20,50,25,"redraw")
  ButtonGadget(#menu_button,20,60,50,25,"menu")
  CanvasGadget(#canvas,100,100,800,500,#PB_Canvas_Transparent)
  BindEvent(#PB_Event_Gadget,@Event_on_gadget())
EndIf
PS: When the line to recreate the canvas is removed... notice the weared canvas behavior (already previously reported)
User avatar
Paul
Posts: 195
Joined: Wed Feb 26, 2014 6:46 pm
Location: Canada
Contact:

Re: canvas problem

Post by Paul »

William Van Hoecke wrote: Tue Jan 18, 2022 11:57 pm QUESTION: WHAT am I doing wrong here.
Your graphs are drawn properly when you press the "redraw" button because the other Window does not exist yet.

Once the other Window exists, you then have to specify which Window the Canvas should be created in.
Example...

Code: Select all

If IsGadget(#canvas)
  FreeGadget(#canvas)
  UseGadgetList(WindowID(#mainwindow)) ; <----- you need this
  CanvasGadget(#canvas,100,100,800,500,#PB_Canvas_Border | #PB_Canvas_Transparent)
EndIf
User avatar
William Van Hoecke
Posts: 50
Joined: Tue Oct 22, 2019 12:09 pm

Re: canvas problem

Post by William Van Hoecke »

SOLVED...
Thanks very much Paul

Since the second window is killed, only mainwindow exists, I thought the canvas was automatically added to this only window, I even did 'SetActiveWindow(#mainwindow)' to make sure....

Pity however that my malpractice did not show ANY error
Been searching to solve this for long time and never thought of the gadgetlist....grrr
I will use gadgetlists all the time from now on :D
Post Reply