Canvas - Mouse over

Just starting out? Need help? Post your questions and find answers here.
Johanson
User
User
Posts: 40
Joined: Sat Aug 01, 2020 9:53 am

Canvas - Mouse over

Post by Johanson »

How to check if the mouse is over a specific triangle?

Code: Select all

If OpenWindow(0, 0, 0, 400, 200, "VectorDrawing", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  Can=CanvasGadget(#PB_Any, 0, 0, 400, 200)
  
  If StartVectorDrawing(CanvasVectorOutput(Can))
    
    MovePathCursor(20, 160)
    AddPathLine(100, 20)
    AddPathLine(180, 160)
    ClosePath()
    VectorSourceColor(RGBA(255,0,0, 255))
    FillPath()    
    
    MovePathCursor(220, 160)
    AddPathLine(300, 20)
    AddPathLine(380, 160)
    ClosePath()      

    VectorSourceColor(RGBA(0, 0, 255, 255))
    FillPath()
    
    StopVectorDrawing()
  EndIf
    
  Repeat
  Until WaitWindowEvent() = #PB_Event_CloseWindow
EndIf
User avatar
STARGÅTE
Addict
Addict
Posts: 2288
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: Canvas - Mouse over

Post by STARGÅTE »

Der einfache Weg wäre über IsInsidePath():

Code: Select all

Procedure Update()
 
  If StartVectorDrawing(CanvasVectorOutput(EventGadget()))
   
    MovePathCursor(20, 160)
    AddPathLine(100, 20)
    AddPathLine(180, 160)
    ClosePath()
		If IsInsidePath(GetGadgetAttribute(EventGadget(), #PB_Canvas_MouseX), GetGadgetAttribute(EventGadget(), #PB_Canvas_MouseY))
    	VectorSourceColor(RGBA(0, 255, 0, 255))
    Else
    	VectorSourceColor(RGBA(255, 0, 0, 255))
    EndIf
    FillPath()   
   
    MovePathCursor(220, 160)
    AddPathLine(300, 20)
    AddPathLine(380, 160)
    ClosePath()     
		
		If IsInsidePath(GetGadgetAttribute(EventGadget(), #PB_Canvas_MouseX), GetGadgetAttribute(EventGadget(), #PB_Canvas_MouseY))
    	VectorSourceColor(RGBA(0, 255, 0, 255))
    Else
    	VectorSourceColor(RGBA(0, 0, 255, 255))
    EndIf
    FillPath()
   
    StopVectorDrawing()
  EndIf

EndProcedure

If OpenWindow(0, 0, 0, 400, 200, "VectorDrawing", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  Can=CanvasGadget(#PB_Any, 0, 0, 400, 200)
  BindGadgetEvent(Can, @Update())
  PostEvent(#PB_Event_Gadget, 0, Can, #PB_EventType_Resize)
  Repeat
  Until WaitWindowEvent() = #PB_Event_CloseWindow
EndIf
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
Johanson
User
User
Posts: 40
Joined: Sat Aug 01, 2020 9:53 am

Re: Canvas - Mouse over

Post by Johanson »

STARGÅTE wrote:Der einfache Weg wäre über IsInsidePath():

Code: Select all

Procedure Update()
 
  If StartVectorDrawing(CanvasVectorOutput(EventGadget()))
   
    MovePathCursor(20, 160)
    AddPathLine(100, 20)
    AddPathLine(180, 160)
    ClosePath()
		If IsInsidePath(GetGadgetAttribute(EventGadget(), #PB_Canvas_MouseX), GetGadgetAttribute(EventGadget(), #PB_Canvas_MouseY))
    	VectorSourceColor(RGBA(0, 255, 0, 255))
    Else
    	VectorSourceColor(RGBA(255, 0, 0, 255))
    EndIf
    FillPath()   
   
    MovePathCursor(220, 160)
    AddPathLine(300, 20)
    AddPathLine(380, 160)
    ClosePath()     
		
		If IsInsidePath(GetGadgetAttribute(EventGadget(), #PB_Canvas_MouseX), GetGadgetAttribute(EventGadget(), #PB_Canvas_MouseY))
    	VectorSourceColor(RGBA(0, 255, 0, 255))
    Else
    	VectorSourceColor(RGBA(0, 0, 255, 255))
    EndIf
    FillPath()
   
    StopVectorDrawing()
  EndIf

EndProcedure

If OpenWindow(0, 0, 0, 400, 200, "VectorDrawing", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  Can=CanvasGadget(#PB_Any, 0, 0, 400, 200)
  BindGadgetEvent(Can, @Update())
  PostEvent(#PB_Event_Gadget, 0, Can, #PB_EventType_Resize)
  Repeat
  Until WaitWindowEvent() = #PB_Event_CloseWindow
EndIf
Thanks a lot
That's exactly it!!!

... Is there a method of changing positions (e.g. olympic rings): move to bottom, move to top?
User avatar
STARGÅTE
Addict
Addict
Posts: 2288
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: Canvas - Mouse over

Post by STARGÅTE »

Johanson wrote:... Is there a method of changing positions (e.g. olympic rings): move to bottom, move to top?
What do you mean?
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
Johanson
User
User
Posts: 40
Joined: Sat Aug 01, 2020 9:53 am

Re: Canvas - Mouse over

Post by Johanson »

STARGÅTE wrote:
Johanson wrote:... Is there a method of changing positions (e.g. olympic rings): move to bottom, move to top?
What do you mean?
similar to zIndex in JS
User avatar
Mijikai
Addict
Addict
Posts: 1520
Joined: Sun Sep 11, 2016 2:17 pm

Re: Canvas - Mouse over

Post by Mijikai »

Just change the drawing order and keep track of it.
zIndex is most likely just a depth buffer.
Post Reply