Jump to content
JusJJ

Drawing on canvas

Recommended Posts

Hi,

Is there a way to prevent drawing outside the canvas?
If, for example, I have a rectangular shape on which I draw an ellipse and I want to limit the drawing to the area of the shape.

procedure TForm1.shapePaint(Sender: TObject; Canvas: TCanvas;
  const ARect: TRectF);
  var
  x,y :integer;
  circle : TRectF;
begin

    x:=round(shape.Width/2);
    y:=round(shape.Width/2);

    circle:=TRectF.Create(x-190,y-190,x+100,y+100);
    shape.Canvas.Stroke.Color:=TAlphaColors.Black;
    shape.Canvas.DrawEllipse(circle,50);
end;

Now part of it is also drawn outside the shape.

form.jpg

Share this post


Link to post
8 hours ago, PeaShooter_OMO said:

Have a look at Clipping Regions.. Here is a little example (Obviously this for the Windows platform):

 

https://docwiki.embarcadero.com/CodeExamples/Sydney/en/ClipRect_(Delphi)

Clipping is the right solution.  But ClipRect() won't work for FMX.

 

In FMX, to clip a drawing to a rectangular area, use the TCanvas.IntersectClipRect() and TCanvas.ExcludeClipRect() methods.  To clip in a non-rectangular area, use TCanvas.DrawPath() and TCanvas.FillPath() instead.

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×