Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to undraw, hide, remove, or delete an image from an html canvas?

this.context.drawImage(myimage, 0, 0);

Putting the image on the canvas is pretty well covered all over the web. But how do I remove it after it's there?

like image 900
John Mee Avatar asked Sep 27 '11 23:09

John Mee


People also ask

How do I remove a picture from a canvas?

Click the profile pictures folder. Click the settings icon next to the picture you want to remove. Click Delete.

How do I remove objects from canvas?

To delete an Object you don't want: Tap+hold your object in the Import menu on canvas. A popup menu will appear. Tap the Delete button (the trash can).

How do you delete a rectangle in canvas?

The clearRect() method in HTML canvas is used to clear the pixels in a given rectangle.

How do I close a canvas in HTML?

The hidden attribute hides the <canvas> element. You can specify either 'hidden' (without value) or 'hidden="hidden"'. Both are valid. A hidden <canvas> element is not visible, but it maintains its position on the page.


1 Answers

Canvas is an immediate drawing surface. This means that you execute a command on it (drawImage or fillRect) and it does that command, and it doesn't give a damn what has just done. There is no undoing of something.

You had a hard time searching for it because there's no such thing as "removing" for a Canvas. All it knows is that it has some pixels of some color from somewhere. It has no idea where.

To simplify a bit, there are generally two ways:

  1. Clear the entire canvas, and draw everything all over again EXCEPT the one image you do not want drawn
  2. Use two canvases, one that only has the image and one with all the other stuff. Clear this canvas with clearRect(0,0,width,height) and you're done.

You'll notice in 1. that you will probably have to start keeping track of the things that you draw on canvas if you want some of them selectively removed or repositioned. Instilling object persistence, or rather turning canvas from an immediate drawing surface to a retained drawing surface, is something that a lot of canvas libraries do. If you want to do it yourself, I've written a few tutorails to help people get started.

If you want to look into libraries, take a peek at easel.js. It's pretty learnable.

like image 69
Simon Sarris Avatar answered Oct 09 '22 15:10

Simon Sarris