agilgur5 / react-signature-canvas

A React wrapper component around signature_pad (in < 150 LoC). Unopinionated and heavily updated fork of react-signature-pad
https://agilgur5.github.io/react-signature-canvas/
Other
539 stars 117 forks source link

How to check if a dataURL is empty? #67

Closed momolly1024 closed 2 years ago

momolly1024 commented 2 years ago

Hi :) Dose any way to check if image is empty ? First I try to

let str = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAAtJREFUGFdjYAACAAAFAAGq1chRAAAAAElFTkSuQmCC";
console.log( image === str )

It can work on my local , but not work for other computer .

So, i wonder if there have another solution.

Thanks.

agilgur5 commented 2 years ago

I'm not sure what you mean by "image" in this context.

isEmpty() method

If you mean checking if the signature canvas is empty, there is already a method for that, isEmpty().

dataURL may not be possible, canvas is, but is out-of-scope

dataURL

If you mean checking if a dataURL is empty as you do in your code sample, I'm not sure if that is even possible. A quick search finds this unanswered SO question.

It can work on my local , but not work for other computer .

I've done some dataURL computation before, and indeed, if I recall correctly, an empty canvas has a different dataURL on different browsers. Similar to my research for #49 , I believe this depends on the browser's implementation of the canvas, which will differ.

canvas

If you're not using isEmpty() and want to check if a canvas element is empty, here's an SO answer with a few different ways of checking if a canvas is empty. trim-canvas (used by this library for .getTrimmedCanvas() and also maintained by me) implements some similar logic as one of the ways in that SO answer.

Those will avoid using a raw dataURL and compute on the canvas element instead.


In any case, either computation on a raw dataURL or on a raw canvas element is out-of-scope for this library, which maintains a signature API wrapper specifically and already implements isEmpty() for signatures as per my first paragraph.