Recent

Author Topic: Erasing the Canvas???  (Read 2802 times)

OC DelGuy

  • Full Member
  • ***
  • Posts: 218
  • 123
Erasing the Canvas???
« on: February 06, 2021, 06:18:13 am »
So, I've been drawing all kinds of things using TCanvas.
Now, I need to erase all the lines and shapes I've been drawing all over the canvas.  So I made a button:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2.   begin
  3.     Canvas.Clear;
  4.   end;
But this doesn't erase the canvas!  All the shapes are still there.  I even tried Canvas.Refresh; just in case that might erase the canvas.  But, no.

How do you erase the canvas?  Is there something other than Clear and Refresh that I'm missing?
Free Pascal Lazarus Version #: 2.2.4
Date: 24 SEP 2022
FPC Version: 3.2.2
Revision: Lazarus_2_2_4
x86_64-win64-win32/win64

speter

  • Sr. Member
  • ****
  • Posts: 494
Re: Erasing the Canvas???
« Reply #1 on: February 06, 2021, 10:13:19 am »
Try
Code: Pascal  [Select][+][-]
  1. canvas.invalidate;

cheers
S.
I climbed mighty mountains, and saw that they were actually tiny foothills. :)

wp

  • Hero Member
  • *****
  • Posts: 13396
Re: Erasing the Canvas???
« Reply #2 on: February 06, 2021, 11:34:17 am »
I am afraid you are following the wrong concept. You seem to draw on the form's canvas upon mouse clicks, mouse drag operations etc. The problem is that the operating system may command the form at any time to redraw itself, for example because the user drags another form over your form. In this case the mouse operations are long gone, and your painting will be erased because the form does not remember your drawing.

If you want a persistent drawing you must store what's to be drawin and you must draw this only in the OnPaint event of the form (or panel, or paintbox etc). This way the form (panel, paintbox) knows what to do when the OS requests repainting.

One thing that you could do is to use a TBitmap to buffer your painting operations, and to use the OnPaint event to draw the bitmap.

Of course, you must force the control to repaint itself after each drawing step if you want to see a live change. This can be done by calling the control's Invalidate method.

I am attaching a very simple example which creates a bitmap and paints it on the canvas of a panel - I selected a paintbox rather than the form because it is easier to define a distinct drawing area.

OC DelGuy

  • Full Member
  • ***
  • Posts: 218
  • 123
Re: Erasing the Canvas???
« Reply #3 on: February 08, 2021, 12:23:51 am »
Try
Code: Pascal  [Select][+][-]
  1. canvas.invalidate;

cheers
S.
Yeah, My compiler didn't like that...   :D
Free Pascal Lazarus Version #: 2.2.4
Date: 24 SEP 2022
FPC Version: 3.2.2
Revision: Lazarus_2_2_4
x86_64-win64-win32/win64

jamie

  • Hero Member
  • *****
  • Posts: 7586
Re: Erasing the Canvas???
« Reply #4 on: February 08, 2021, 01:02:41 am »
I don't know what surface you re drawing on so I'll just take a guess for now...


With Form1 do
  begin
    Canvas.Brush.Color := clRed;
    Canvas.FillRect(ClientRect);
  End;
The only true wisdom is knowing you know nothing

speter

  • Sr. Member
  • ****
  • Posts: 494
Re: Erasing the Canvas???
« Reply #5 on: February 08, 2021, 07:00:37 am »
Code: Pascal  [Select][+][-]
  1. canvas.invalidate;
Yeah, My compiler didn't like that...   :D
Oops; that should be:
Code: Pascal  [Select][+][-]
  1. form1.invalidate;
where "form1" is your drawing surface.

So, if you are drawing (for example) on a tpaintbox called paintbox1 you would use:
Code: Pascal  [Select][+][-]
  1. paintbox1.invalidate;
This will clear the drawing surface and then call the surface's paint method; so make sure you have a paint method handling the "onPaint" event - usually:

  procedure Form1Paint(Sender: TObject);
or
  procedure PaintBox1Paint(Sender: TObject);


cheers
S.
I climbed mighty mountains, and saw that they were actually tiny foothills. :)

lucamar

  • Hero Member
  • *****
  • Posts: 4217
Re: Erasing the Canvas???
« Reply #6 on: February 08, 2021, 08:44:32 am »
This will clear the drawing surface and then call the surface's paint method; [...]

Tha's not quite true, is it? All that Invalidate does is "mark" the control as "needs to be redrawn" and (for most, if not all, widgetsets) schedule a "repaint" message, which results, in turn, in a call to OnPaint. Few controls override Invalidate (TImage is one, for example) and no control that I know of includes clearing the canvas as part of processig Invalidate; that would result in lots of flickr and unneeded repainting of what was already there most times.
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

OC DelGuy

  • Full Member
  • ***
  • Posts: 218
  • 123
Re: Erasing the Canvas???
« Reply #7 on: February 10, 2021, 09:00:52 pm »
I don't know what surface you re drawing on so I'll just take a guess for now...


With Form1 do
  begin
    Canvas.Brush.Color := clRed;
    Canvas.FillRect(ClientRect);
  End;
What does with..do do?  I've never seen that.  I searched and nothing comes up.
Free Pascal Lazarus Version #: 2.2.4
Date: 24 SEP 2022
FPC Version: 3.2.2
Revision: Lazarus_2_2_4
x86_64-win64-win32/win64

dsiders

  • Hero Member
  • *****
  • Posts: 1545
Re: Erasing the Canvas???
« Reply #8 on: February 10, 2021, 09:27:43 pm »
I don't know what surface you re drawing on so I'll just take a guess for now...


With Form1 do
  begin
    Canvas.Brush.Color := clRed;
    Canvas.FillRect(ClientRect);
  End;
What does with..do do?  I've never seen that.  I searched and nothing comes up.

https://www.freepascal.org/docs-html/ref/refsu61.html


jamie

  • Hero Member
  • *****
  • Posts: 7586
Re: Erasing the Canvas???
« Reply #9 on: February 10, 2021, 10:59:43 pm »
I don't know what surface you re drawing on so I'll just take a guess for now...


With Form1 do
  begin
    Canvas.Brush.Color := clRed;
    Canvas.FillRect(ClientRect);
  End;
What does with..do do?  I've never seen that.  I searched and nothing comes up.

Just think of that as a relative path to all the members , variables, properties etc of the Form1 class.

its like Changing the Directory of your system to a specific folder name, from that point on all file access that does not specify a full path name will look there..

but in the case of WITH here, it does that but if it does not find a member of that name locally (relative) then it looks outside like a full path would do.

it just makes for shorted code writing so you don't have to keep repeating the same FORM1.dothis, Form1.ThatThat, Form1.SomethingElse etc..

The only true wisdom is knowing you know nothing

BubikolRamios

  • Sr. Member
  • ****
  • Posts: 390
Re: Erasing the Canvas???
« Reply #10 on: February 09, 2026, 06:34:49 pm »
Not mentioned above:
Code: Pascal  [Select][+][-]
  1.   canvas.Repaint;
  2.  

That one works for me.
lazarus 3.2-fpc-3.2.2-win32/win64

 

TinyPortal © 2005-2018