How to detect if html5 canvas is blank?

末鹿安然 提交于 2020-01-11 11:41:47

问题


I have a doodle application that saves an html5 canvas to an image in s3. I want to avoid saving blank (totally transparent) pngs if the user accidentally clicks "save" without having drawn anything. I found that even for a blank canvas the toDataURL() is never an empty string, but huge dump of data. This string appears to always remain the same value if no data is added to the canvas. So one way of detecting an empty canvas is to compare the submitted canvas data to this empty canvas data string to see if they match.

My question is, is there a more efficient way of doing this? Doing a string compare of two strings that are thousands of characters long means that we have to compare all the way to the last character if there is indeed an empty canvas submitted. Is it more efficient to first compute the md5 hash of the newly submitted canvas and then compare it to the md5 hash of the known empty canvas data string? Which is faster?

Are they any other ways of determining empty canvas?

For example: Is comparing the length of the strings a reliable test for matching empty canvas?


回答1:


Can you easily detect clicks/draws? If so, you could have an initial 'canvas empty' boolean that gets flipped the first time the user draws anything.

Unfortunately, this wouldn't help if the user draws something, then erases it, then saves.



来源:https://stackoverflow.com/questions/6476074/how-to-detect-if-html5-canvas-is-blank

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!