Dessiner sur un canvas
Publié : ven. 08/août/2014 17:57
■ Dessiner sur un canvas (ou une image) revient à faire un dessin souvent en pointillé comme le montre cette animation. Veuillez pardonner mon piètre talent de dessinateur.

Le code de base pour vous rendre compte par vous même.
■ L'astuce pour dessiner sans pointiller, consiste à connaitre à tout moment quel est le dernier point dessiner et le nouveau et d'utiliser LineXY() entre les deux.
le code pour vous rendre compte par vous même.


Le code de base pour vous rendre compte par vous même.
Code : Tout sélectionner
Enumeration Window
#MainForm
EndEnumeration
Enumeration Gadgets
#Canvas
EndEnumeration
Global Draw
Declare EventDraw()
If OpenWindow(#MainForm, 0, 0, 1024, 768, "Canvas Draw", #PB_Window_SystemMenu|#PB_Window_ScreenCentered)
CanvasGadget(#Canvas, 0, 0, 1024, 768)
BindGadgetEvent(#Canvas, @EventDraw())
Repeat : Until WaitWindowEvent(10) = #PB_Event_CloseWindow
EndIf
Procedure EventDraw()
Protected x.i = GetGadgetAttribute(#Canvas, #PB_Canvas_MouseX)
Protected y.i = GetGadgetAttribute(#Canvas, #PB_Canvas_MouseY)
Protected image
Select EventType()
Case #PB_EventType_LeftButtonDown
Draw = #True
Case #PB_EventType_LeftButtonUp
Draw = #False
EndSelect
If Draw
If StartDrawing(CanvasOutput(#Canvas))
Circle(x, y, 2, $000000)
StopDrawing()
EndIf
EndIf
EndProcedure
le code pour vous rendre compte par vous même.
Code : Tout sélectionner
Enumeration Window
#MainForm
EndEnumeration
Enumeration Gadgets
#Canvas
EndEnumeration
Structure Canvas
LeftButtonDown.i
RightButtonDown.i
LastX.i
LastY.i
EndStructure
Global Draw, This.Canvas
Declare EventDraw()
If OpenWindow(#MainForm, 0, 0, 1024, 768, "Canvas Draw", #PB_Window_SystemMenu|#PB_Window_ScreenCentered)
CanvasGadget(#Canvas, 0, 0, 1024, 768)
BindGadgetEvent(#Canvas, @EventDraw())
Repeat : Until WaitWindowEvent(10) = #PB_Event_CloseWindow
EndIf
Procedure EventDraw()
Protected x.i = GetGadgetAttribute(#Canvas, #PB_Canvas_MouseX)
Protected y.i = GetGadgetAttribute(#Canvas, #PB_Canvas_MouseY)
With This
Select EventType()
Case #PB_EventType_LeftButtonDown
\LeftButtonDown = #True
\LastX = x
\LastY = y
Case #PB_EventType_LeftButtonUp
\LeftButtonDown = #False
Case #PB_EventType_RightButtonDown
\RightButtonDown = #True
\lastX = x
\lastY = y
Case #PB_EventType_RightButtonUp
\RightButtonDown = #False
Case #PB_EventType_MouseMove
Draw = #False
If (\LeftButtonDown Or \RightButtonDown)
Draw = #True
EndIf
EndSelect
If Draw
If StartDrawing(CanvasOutput(#Canvas))
Protected color.l = $000000
If (\rightButtonDown)
color = $ffffff ;RGB(255, 255, 255)
EndIf
LineXY(\lastX, \lastY, x, y, color)
\lastX = x
\lastY = y
StopDrawing()
EndIf
EndIf
EndWith
EndProcedure