简体   繁体   English

如何清除包含图像的画布?

[英]How to clear a canvas containing an image?

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 我尝试了几种方法,你可以在函数ag_pub_clear_photo中看到这个小提琴

here are the 3 ways I tried : (ag_photo_picture is the id of my canvas 这是我尝试的3种方式:(ag_photo_picture是我的画布的ID

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

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

3) var canvas = $('#ag_photo_picture')[0]; 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 编辑:似乎我的清除功能是在页面加载时调用的,而不是在我单击按钮时调用

感谢azs06,这是答案:

$('#ag_photo_clear_button').on('click',ag_pub_clear_photo);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM