0
votes

I am using Delphi 10.4 and Delphi XE8 for educational purposes.

I made a canvas painting app for free hand drawing. In the OnMouseDown() event I draw an ellipse at mouse position and I want to draw more ellipses in the OnMouseMove() event. But, on drawing to the canvas, there are blank parts between ellipses. I want to draw repeatedly without blank parts. How can I do that?

image of problem

1
Perhaps you could take a small snip of your drawing so we could see exactly what kind of 'blank parts' you are talking about. Also include your code that actually draws the circles, the OnMouseMove() code.Tom Brunberg
Thanks for the image, but the code?Tom Brunberg

1 Answers

1
votes

As you did not provide any code, I made this up quickly. This code is only to demonstrate usage of line drawing instead of repeatedly drawing circles to make a line. Drawings made with this code are not persistent.

If you can draw lines (with a round StrokeCap) instead of circles, it becomes quite easy as follows:

Save the position of the OnMouseDown event in a pair of singles, e.g. as fields of the form:

private
  xold, yold: single;
  Drawing: boolean;  // to indicate that we should be drawing in the `OnMouseMove` event

procedure TForm65.PaintBox1MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Single);
begin
  Drawing := True; 
  xold := X; yold := Y;
end;

In the OnMouseMove event get the xold, yold values to two local singles (say xpre, ypre) and set xold, yold to current X, Y. Then draw a line from xpre, ypre to current X, Y position

procedure TForm65.PaintBox1MouseMove(Sender: TObject; Shift: TShiftState; X, Y: Single);
var
  xpre, ypre: single;
begin
  if Drawing then
  begin
    Canvas.BeginScene;
    try
      xpre := xold; 
      ypre := yold; // fetch previous position
      xold := X; 
      yold := Y;   // store current position for next event
      
      Canvas.StrokeThickness := 10;
      Canvas.StrokeCap := TStrokeCap.Round;
      Canvas.Stroke.Color := TAlphaColorRec.Red;

      Canvas.DrawLine(PointF(xpre,ypre), PointF(X, Y), 1); // draw line from prev pos to current
    finally
      Canvas.EndScene;
    end;
  end;
end;

Reset Drawing in the OnMouseUp event

procedure TForm65.PaintBox1MouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Single);
begin
  Drawing := False;
end;

enter image description here