1

I'm having an issue with my canvas. I have a webpage that allows me to take a picture with my camera and then display it into a canvas. This part works fine, but I want to be able to clear the canvas on a button click.

I tried a few ways to do it, as you can see in this fiddle in the function ag_pub_clear_photo

here are the 3 ways I tried : (ag_photo_picture is the id of my canvas

1) $('#ag_photo_picture').clear();

2) context.clearRect(0, 0, canvas.width, canvas.height);

3) var canvas = $('#ag_photo_picture')[0]; canvas.width = canvas.width;

No matter which way I try, it always seems like nothing is happening and I don't understand why or what I am doing wrong.

I'd like to precise that by "clearing" canvas, I want it to be blank/invisible like at the beginning, before touching any button.

Edit : Seems like my clear function is being called at the load off the page, and not when I click the button

halfer
  • 19,824
  • 17
  • 99
  • 186
darzang
  • 156
  • 2
  • 17
  • 1
    You have to invoke function on click event, like this `$('#ag_photo_clear_button').on('click',ag_pub_clear_photo);` here is working jsfiddle https://jsfiddle.net/azs06/2435eav9/4/ – azs06 Feb 20 '17 at 10:58
  • Possible duplicate of [How to clear rectangle on Image in Canvas](http://stackoverflow.com/questions/31258273/how-to-clear-rectangle-on-image-in-canvas) – Ashish Bahl Feb 20 '17 at 11:11
  • Okay i was adding () at the end of ag_pub_clear_photo that were not supposed to be here, thanks you it works fine now :) – darzang Feb 20 '17 at 11:19
  • @darzang: if you don't agree with a suggested duplicate, please keep your responses in the comments. Question posts should be focussed on the question material itself, so that meta-commentary is not left in forever. – halfer Feb 20 '17 at 22:48
  • If this is solved, please add an answer rather than adding a [solved] device in the title. Thanks! – halfer Feb 20 '17 at 22:48

1 Answers1

0

Thanks to azs06, here's the answer :

$('#ag_photo_clear_button').on('click',ag_pub_clear_photo);
darzang
  • 156
  • 2
  • 17