How to redraw original image in Canvasgadget?

Just starting out? Need help? Post your questions and find answers here.
Martin Verlaan
Enthusiast
Enthusiast
Posts: 133
Joined: Sun Apr 01, 2018 11:26 am

How to redraw original image in Canvasgadget?

Post by Martin Verlaan »

I have to change an Imagegadget in a Canvasgadget because I need to detect more mouse events.

In my current code I draw a cursor in the shape of line. If the user click somewhere the cursor (line) will move to the mouse position. To avoid multiple lines in the imagegadget, I redraw the original image first.

Here's my code

Code: Select all

  SetGadgetState(WaveformPic, ImageID(WaveImage))    

  CreateImage(123, 700, 200) 
  StartDrawing(CanvasOutput(123))
    DrawImage(CanvasOutput(WaveImage), 0, 0)  
    Line(x y, 1, 200, RGB(255, 255, 0))
  StopDrawing() 
  
  SetGadgetState(WaveformPic, ImageID(123)) 
How can I do the same thing with a Canvasgadget? I guess the solution must be simple but I can't figure it out.
Last edited by Martin Verlaan on Fri Jun 15, 2018 10:51 am, edited 1 time in total.
Marc56us
Addict
Addict
Posts: 1479
Joined: Sat Feb 08, 2014 3:26 pm

Re: How to redraw original image in Canvasgadget?

Post by Marc56us »

How can I do the same thing with a Canvasgadget?
Just change output destination:

ImageOutput :arrow: CanvasOutput

StartDrawing(CanvasOutput(1))

:wink:
Martin Verlaan
Enthusiast
Enthusiast
Posts: 133
Joined: Sun Apr 01, 2018 11:26 am

Re: How to redraw original image in Canvasgadget?

Post by Martin Verlaan »

Sorry,I just edited my code example above, because there was mistake in it (wrong values). However your solution results in an error: "specific #Gadget not initialized", which is logical because i have only one canvasgadget and the value 1 refers to an image.
Marc56us wrote:
How can I do the same thing with a Canvasgadget?
Just change output destination:

ImageOutput :arrow: CanvasOutput

StartDrawing(CanvasOutput(1))

:wink:
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4663
Joined: Sun Apr 12, 2009 6:27 am

Re: How to redraw original image in Canvasgadget?

Post by RASHAD »

Hi

Code: Select all

LoadFont(0,"Tahoma",12)

OpenWindow(0,0,0,600,400,"Drag Line over Canvas",#PB_Window_MinimizeGadget | #PB_Window_ScreenCentered | #PB_Window_MinimizeGadget|#PB_Window_ScreenCentered)
CanvasGadget(0,10,10,580,350)
TextGadget(1,20,370,200,25,"")
SetGadgetFont(1,FontID(0))
SetGadgetColor(1,#PB_Gadget_FrontColor,$0000FF)
StartDrawing(CanvasOutput(0))  
  Circle(150, 150,100, RGBA(Random(255), Random(255), Random(255),250))
  Circle(400, 220,100, RGBA(Random(255), Random(255), Random(255),200))
  DrawingMode(#PB_2DDrawing_AlphaBlend)
  Circle(400, 100,100, RGBA(Random(255), Random(255), Random(255),200))
StopDrawing()

lColor = $0005FE
xColor = lColor ! $FFFFFF
Repeat
  Select WaitWindowEvent()
    Case #PB_Event_CloseWindow
      End
                      
    Case #PB_Event_Gadget
      Select EventGadget()
        Case  0
          Select EventType()                     
            Case #PB_EventType_LeftButtonDown
              x = GetGadgetAttribute(0,#PB_Canvas_MouseX)
              StartDrawing(CanvasOutput(0))
                DrawingMode(#PB_2DDrawing_XOr )
                LineXY(x,0,x,350,xColor)
              StopDrawing()
              Flag = 1              
                 
            Case #PB_EventType_MouseMove
              If Flag = 1
                StartDrawing(CanvasOutput(0))                                  
                  GrabDrawingImage(10, x-1, 0, 2, 350)                  
                  DrawingMode(#PB_2DDrawing_XOr )
                  LineXY(x,0,x,350,xColor)
                  x = GetGadgetAttribute(0,#PB_Canvas_MouseX)                  
                  DrawingMode(#PB_2DDrawing_XOr )
                  LineXY(x,0,x,350,xColor)
                StopDrawing()
               EndIf 
               SetGadgetText(1,"  X : "+Str(GetGadgetAttribute(0,#PB_Canvas_MouseX)) +"  Y : "+Str(GetGadgetAttribute(0,#PB_Canvas_MouseY)))
                  
            Case #PB_EventType_LeftButtonUp 
              Flag = 0
              StartDrawing(CanvasOutput(0))                
                LineXY(x,0,x,350,lColor)
              StopDrawing()
              oldx = x
              
            Case #PB_EventType_RightButtonUp    ;Undo
              StartDrawing(CanvasOutput(0))
                DrawImage(ImageID(10),oldx-1,0)
                DrawingMode(#PB_2DDrawing_XOr )
                LineXY(x,0,x,350,xColor)
              StopDrawing()
            
          EndSelect
      EndSelect
  EndSelect
ForEver
Egypt my love
Martin Verlaan
Enthusiast
Enthusiast
Posts: 133
Joined: Sun Apr 01, 2018 11:26 am

Re: How to redraw original image in Canvasgadget?

Post by Martin Verlaan »

Thank you very much Rashad. GrabDrawingImage() and DrawImage() works great!
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5353
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: How to redraw original image in Canvasgadget?

Post by Kwai chang caine »

Thanks RASHAD works fine here 8)
ImageThe happiness is a road...
Not a destination
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4663
Joined: Sun Apr 12, 2009 6:27 am

Re: How to redraw original image in Canvasgadget?

Post by RASHAD »

Thanks KCC
Next for multi undo

Code: Select all

LoadFont(0,"Tahoma",12)
Dim xl(10,1)

OpenWindow(0,0,0,600,400,"Drag Line over Canvas",#PB_Window_MinimizeGadget | #PB_Window_ScreenCentered | #PB_Window_MinimizeGadget|#PB_Window_ScreenCentered)
CanvasGadget(0,10,10,580,350)
TextGadget(1,20,370,200,25,"")
SetGadgetFont(1,FontID(0))
SetGadgetColor(1,#PB_Gadget_FrontColor,$0000FF)
StartDrawing(CanvasOutput(0)) 
  Circle(150, 150,100, RGBA(Random(255), Random(255), Random(255),250))
  Circle(400, 220,100, RGBA(Random(255), Random(255), Random(255),200))
  DrawingMode(#PB_2DDrawing_AlphaBlend)
  Circle(400, 100,100, RGBA(Random(255), Random(255), Random(255),200))
StopDrawing()

lColor = $0005FE
xColor = lColor ! $FFFFFF
Repeat
  Select WaitWindowEvent()
    Case #PB_Event_CloseWindow
      End
                     
    Case #PB_Event_Gadget
      Select EventGadget()
        Case  0
          Select EventType()                     
            Case #PB_EventType_LeftButtonDown
              x = GetGadgetAttribute(0,#PB_Canvas_MouseX)
              StartDrawing(CanvasOutput(0)) 
                DrawingMode(#PB_2DDrawing_XOr )
                LineXY(x,0,x,350,xColor)
              StopDrawing()
              Flag = 1           
                 
            Case #PB_EventType_MouseMove
              If Flag = 1
                StartDrawing(CanvasOutput(0))    
                  DrawingMode(#PB_2DDrawing_XOr )
                  LineXY(x,0,x,350,xColor)
                  x = GetGadgetAttribute(0,#PB_Canvas_MouseX) 
                  LineXY(x,0,x,350,xColor)
                StopDrawing()
              EndIf
              SetGadgetText(1,"  X : "+Str(GetGadgetAttribute(0,#PB_Canvas_MouseX)) +"  Y : "+Str(GetGadgetAttribute(0,#PB_Canvas_MouseY)))
                 
            Case #PB_EventType_LeftButtonUp
              Flag = 0
              StartDrawing(CanvasOutput(0))
                gi = GrabDrawingImage(#PB_Any, x-1, 0, 2, 350)               
                LineXY(x,0,x,350,lColor)
              StopDrawing()
              xl(line,0) = x 
              xl(line,1) = gi
              line +1
             
            Case #PB_EventType_RightButtonDown    ;Undo
              If line >= 1
                line-1
                StartDrawing(CanvasOutput(0))
                  DrawImage(ImageID(xl(line,1)),xl(line,0)-1,0)
                  DrawingMode(#PB_2DDrawing_XOr )
                  LineXY(xl(line,0),0,xl(line,0),350,xColor)
                StopDrawing()
              EndIf           
          EndSelect
      EndSelect
  EndSelect
ForEver
Egypt my love
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5353
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: How to redraw original image in Canvasgadget?

Post by Kwai chang caine »

Very nice :D
When i see this style of code, even if the drawing is not square, immediatly i think to a behaviour of visual designer 8)
Thanks the undo works perfectly :wink:
ImageThe happiness is a road...
Not a destination
Post Reply