Vector Drawing

Just starting out? Need help? Post your questions and find answers here.
greycheek
New User
New User
Posts: 2
Joined: Tue Apr 09, 2024 6:02 pm

Vector Drawing

Post by greycheek »

The following code does not produce a vector line, or anything on the canvas. Foŕthe life of me I don't know what I'm doing wrong. Any insights?

Code: Select all

Procedure VectorLine(color,x,y,pw,ph,brush_width)
	If StartVectorDrawing(CanvasVectorOutput(Canvas_0))
		Repeat
			x1 = (GetGadgetAttribute(Canvas_0, #PB_Canvas_MouseX) / pw) * pw
			y1 = (GetGadgetAttribute(Canvas_0, #PB_Canvas_MouseY) / ph) * ph
			MovePathCursor(x1,y1)
			AddPathLine(x,y)
			VectorSourceColor(color)
			StrokePath(brush_width)
		Until Not MouseButton(#PB_MouseButton_Left)
		StopVectorDrawing()
	EndIf
EndProcedure
User avatar
jacdelad
Addict
Addict
Posts: 1991
Joined: Wed Feb 03, 2021 12:46 pm
Location: Riesa

Re: Vector Drawing

Post by jacdelad »

1. Please use code tags.
2. Please post a runable code. We can't know whether the problem is within this procedure or the code around it.
3. What do you expect

Code: Select all

x1 = (GetGadgetAttribute(Canvas_0, #PB_Canvas_MouseX) / pw) * pw
y1 = (GetGadgetAttribute(Canvas_0, #PB_Canvas_MouseY) / ph) * ph
to do???

Edit: Ok, you just added code tags while I was typing.
Good morning, that's a nice tnetennba!

PureBasic 6.21/Windows 11 x64/Ryzen 7900X/32GB RAM/3TB SSD
Synology DS1821+/DX517, 130.9TB+50.8TB+2TB SSD
greycheek
New User
New User
Posts: 2
Joined: Tue Apr 09, 2024 6:02 pm

Re: Vector Drawing

Post by greycheek »

Code: Select all

x1 = (GetGadgetAttribute(Canvas_0, #PB_Canvas_MouseX) / pw) * pw
y1 = (GetGadgetAttribute(Canvas_0, #PB_Canvas_MouseY) / ph) * ph
These two lines of code capture the mouse position that will be used to draw back to the origin (x,y). The division and multiplication is used to round off to the nearest 8 pixels (pw,ph). The code that calls this procedure is much to extensive to post.
Andesdaf
User
User
Posts: 83
Joined: Sun Mar 22, 2009 2:53 pm
Location: GER, Saxony

Re: Vector Drawing

Post by Andesdaf »

StopVectorDrawing() is needed to finish and display the drawing operation, so maybe moving StartVectorDrawing() and StopVectorDrawing() into the loop helps. MouseButton() and the Mouse library are used on screens, better use the event types the CanvasGadget provides for mouse keys.
User avatar
jacdelad
Addict
Addict
Posts: 1991
Joined: Wed Feb 03, 2021 12:46 pm
Location: Riesa

Re: Vector Drawing

Post by jacdelad »

greycheek wrote: Tue Apr 09, 2024 6:34 pm These two lines of code capture the mouse position that will be used to draw back to the origin (x,y). The division and multiplication is used to round off to the nearest 8 pixels (pw,ph). The code that calls this procedure is much to extensive to post.
I would have never guessed that without context. That's why we need a runable code...
Good morning, that's a nice tnetennba!

PureBasic 6.21/Windows 11 x64/Ryzen 7900X/32GB RAM/3TB SSD
Synology DS1821+/DX517, 130.9TB+50.8TB+2TB SSD
Mr.L
Enthusiast
Enthusiast
Posts: 146
Joined: Sun Oct 09, 2011 7:39 am

Re: Vector Drawing

Post by Mr.L »

Maybe you use RGB values instead of RGBA values?
Maybe the code "Until Not MouseButton(#PB_MouseButton_Left)" is missing an "ExamineMouse"?
To get the MouseButton state on a CanvasGadget use "GetGadgetAttribute(Canvas_0, #PB_Canvas_Buttons)"

I don't know if that is what you had in mind, but maybe this does help.

Code: Select all

Global Canvas_0

Procedure VectorLine(color,x,y,pw,ph,brush_width)
	If StartVectorDrawing(CanvasVectorOutput(Canvas_0))
		x1 = (GetGadgetAttribute(Canvas_0, #PB_Canvas_MouseX) / pw) * pw
		y1 = (GetGadgetAttribute(Canvas_0, #PB_Canvas_MouseY) / ph) * ph

		MovePathCursor(x1,y1)
		AddPathLine(x,y)
		VectorSourceColor(color)
		StrokePath(brush_width)
		StopVectorDrawing()
	EndIf
EndProcedure

OpenWindow(0,0,0,800,600,"!")
CanvasGadget(Canvas_0,0,0,800,600)
SetActiveGadget(0)

Repeat
	Select WaitWindowEvent()
		Case #PB_Event_CloseWindow
			End
		Case #PB_Event_Gadget
			If GetGadgetAttribute(Canvas_0, #PB_Canvas_Buttons) & #PB_Canvas_LeftButton
				VectorLine(RGBA(255,0,0,255), GetGadgetAttribute(Canvas_0, #PB_Canvas_MouseX), GetGadgetAttribute(Canvas_0, #PB_Canvas_MouseY), 32, 32, 1)
			EndIf
	EndSelect
ForEver
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: Vector Drawing

Post by netmaestro »

Simple example:

Code: Select all

Declare procCanvas0()

OpenWindow(0,0,0,800,600,"Click to start a line, move cursor and click to set end point and draw", #PB_Window_ScreenCentered|#PB_Window_SystemMenu)
CanvasGadget(0,0,0,800,600)
BindGadgetEvent(0, @procCanvas0())

Repeat:Until WaitWindowEvent() = #PB_Event_CloseWindow

Procedure procCanvas0()
  Static.i drawing=#False
  Static.POINT drawstart
  
  Select EventType()
    Case #PB_EventType_LeftButtonDown
      If Not drawing
        drawing=#True
        drawstart\x = GetGadgetAttribute(EventGadget(), #PB_Canvas_MouseX)
        drawstart\y = GetGadgetAttribute(EventGadget(), #PB_Canvas_MouseY)
        StartVectorDrawing(CanvasVectorOutput(EventGadget()))
          VectorSourceColor(RGBA(255,0,0,255))
          MovePathCursor(drawstart\x, drawstart\y)
          AddPathCircle(drawstart\x, drawstart\y, 1.0)
          FillPath()
        StopVectorDrawing()
      Else
        StartVectorDrawing(CanvasVectorOutput(EventGadget()))
          VectorSourceColor(RGBA(255,0,0,255))
          MovePathCursor(drawstart\x, drawstart\y)
          AddPathLine(GetGadgetAttribute(EventGadget(), #PB_Canvas_MouseX), GetGadgetAttribute(EventGadget(), #PB_Canvas_MouseY))
          StrokePath(2)
        StopVectorDrawing()
        drawing=#False
      EndIf
      
  EndSelect
  
EndProcedure
BERESHEIT
Post Reply