Erasing the Canvas??? lazarus / 21-General ---------------------------------------------------------------------- OC DelGuy · 2021-02-06 05:18 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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.Button1Click(Sender: TObject);  begin    Canvas.Clear;  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? ---------------------------------------------------------------------- speter · 2021-02-06 09:13 Try Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i canvas.invalidate; cheers S. ---------------------------------------------------------------------- wp · 2021-02-06 10:34 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 · 2021-02-07 23:23 Quote from: speter on February 06, 2021, 10:13:19 amTry Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i canvas.invalidate; cheers S. Yeah, My compiler didn't like that... ---------------------------------------------------------------------- jamie · 2021-02-08 00:02 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; ---------------------------------------------------------------------- speter · 2021-02-08 06:00 Quote from: OC DelGuy on February 08, 2021, 12:23:51 amQuote from: speter on February 06, 2021, 10:13:19 amCode: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i canvas.invalidate;Yeah, My compiler didn't like that...  Oops; that should be: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i 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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i 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. ---------------------------------------------------------------------- lucamar · 2021-02-08 07:44 Quote from: speter on February 08, 2021, 07:00:37 amThis 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. ---------------------------------------------------------------------- OC DelGuy · 2021-02-10 20:00 Quote from: jamie on February 08, 2021, 01:02:41 amI 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. ---------------------------------------------------------------------- dsiders · 2021-02-10 20:27 Quote from: OC DelGuy on February 10, 2021, 09:00:52 pmQuote from: jamie on February 08, 2021, 01:02:41 amI 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 · 2021-02-10 21:59 Quote from: OC DelGuy on February 10, 2021, 09:00:52 pmQuote from: jamie on February 08, 2021, 01:02:41 amI 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.. ---------------------------------------------------------------------- BubikolRamios · 2026-02-09 17:34 Not mentioned above: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   canvas.Repaint;  That one works for me.