如何检查画布上是否绘制了某些内容
如何检查画布上是否有数据或绘制的内容?
我有这个 元素,我想检查我的画布上是否绘制了某些内容。
How do I check if there is data or something drawn on a canvas?
I have this <canvas id="my-canvas"></canvas>
element, and I want to check if my canvas has something drawn on it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
我发现的一个好方法是使用 toDataURL() 函数并将其与另一个空白画布进行比较。如果它们不同,那么您所比较的那个就具有某些内容。像这样的事情:
这是一个 小提琴
我不能为此归功于我,我只是偶然发现了它,它修复了我的问题问题。也许这会在某个时候对某人有所帮助。
A good way I have found is to use the
toDataURL()
function and compare it with another, blank canvas. If they are different, than the one you are comparing it to has something on it. Something like this:Here is a fiddle
I can't take credit for this, I just stumbled upon it and it fixed my same issue. Maybe this will help somebody at some point.
与检查每个像素相比,仅记录每次画布被填充或抚摸的时间可能会更有效。
一旦填充或描边发生,您就知道已经绘制了某些内容。
当然,“如何”是非常特定于应用程序的,因为我们首先不知道您的画布是如何绘制的。
Instead of checking every single pixel, it may be much more efficient to merely record every time the canvas gets filled or stroked.
Once a fill or stroke or has happened, then you know that something has been drawn.
Of course 'how' is very application specific, since we don't know how your canvas is getting drawn on in the first place.
到目前为止我还没有在 Stackoverflow 中看到过这种问题..但是有一个有趣的问题需要回答..
唯一的方法(我猜)是逐像素检查,即检查 R、G、B , A 每个像素,
如果这些值等于零,那么我们可以说该像素是空的..
这是我用来编写以下代码片段的技术。只需浏览一下
在这里测试
I haven't seen this kind of question in Stackoverflow till now.. but an interesting One to answer..
The only way(I guess) is to check through pixel by pixel, i.e., check the
R, G, B, A
of each pixel,if those values are equal to zero then we can say that the pixel is empty..
This is the technique I used to write the below code snippet.. just go through it
Test it Here
以下是 Phrogz 的提示:https://stackoverflow.com/a/4649358
Here a tips from Phrogz : https://stackoverflow.com/a/4649358
嘿可能是一个迟到的答案,但我有点害怕需要额外的画布来进行比较。所以这是我的解决方案:
我希望这会有所帮助,您可以用它来执行一个功能,但该解决方案比我所看到的要干净得多。
Hey might be a late answer for this but I was kind of horrified to have the need of an extra canvas for comparing. So here is my solution:
I hope this helps, you can do a function with it but the solution is way cleaner than what I have seen around.
HTML 代码:
JS 代码:
注意:如果您为主画布提供高度和宽度,则同样的高度和宽度也适用于空白画布。
HTML Code:
JS Code:
Note: If your provide height and width to main canvas then same height and width to apply on blank canvas also.
要获取画布的像素,您必须使用 getImageData();方法-> getImageData() 参考
并检查 imageData 的每个像素是否为 0 或 255。
您的 ImageData 每个像素有 4 个单元格,rgba,您可以通过将计数增加 4 并查看每 4 个下一个数字来获取您的 rgba 值来访问每个像素。
您还可以获取空图像的 DataURL 并将画布的 DataURL 与它进行比较,看看它是否为空;)
To get the pixels of the canvas you have to use the getImageData(); method -> getImageData() reference
And to check if every pixels of your imageData are at 0 or 255.
Your ImageData will have 4 cells per pixels, r g b a and you cn access every pixels by incrementing your count by 4 and looking each 4 next numbers to hve your rgba value.
You can also get an empty image's DataURL and compare your canvas's DataURL to it to see if it's empty ;)
我知道这可能是一个迟到的答案,但尝试这个解决方案:
西蒙萨里斯说:
虽然这会更有效,但我确实设法找到一种方法来检测画布,通过检查每个像素来查看它是否被绘制/绘制。我尝试了一下,看看当画布为空和被绘制时像素数据会返回什么结果。我发现当画布为空时,所有像素数据在数组的所有索引位置都返回
0
。所以我用它来检测是否有任何0
,结果我做了它,所以它返回一个布尔值。如果画布已绘制,则返回true
,如果尚未绘制,则返回false
。您可以在此处在 JSfiddle 代码上测试它
:
我希望这会有所帮助:)
I know it might be a late answer but try this solution:
Simon Sarris says that:
While that would be more efficient, however I did manage to find a way to detect the canvas to see if it is drawn/painted on by checking every pixel. I had a play around to see what results does the pixel data returns when the canvas is empty and when it is been painted on. I found out that when the canvas is empty all of the pixel data returns
0
in all index positions of the array. So I used that to detect if there is any0
's or not and as the result I made it so it returns a boolean. If the canvas has been drawn on then it returnstrue
and if it hasn't then it returnsfalse
.You can test it Here on JSfiddle
code:
I hope this helps :)