Page 1 of 1

Vector Drawing

Posted: Tue Apr 09, 2024 6:07 pm
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

Re: Vector Drawing

Posted: Tue Apr 09, 2024 6:12 pm
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.

Re: Vector Drawing

Posted: Tue Apr 09, 2024 6:34 pm
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.

Re: Vector Drawing

Posted: Tue Apr 09, 2024 7:05 pm
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.

Re: Vector Drawing

Posted: Tue Apr 09, 2024 7:19 pm
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...

Re: Vector Drawing

Posted: Tue Apr 09, 2024 7:40 pm
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

Re: Vector Drawing

Posted: Wed Apr 10, 2024 11:03 pm
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