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
546 stars 119 forks source link

if `clearOnResize` false, CSS resize causes de-sync #43

Closed fscutari closed 4 years ago

fscutari commented 4 years ago

First, Thank you for your job ;) I'm trying to develope a form that users will fill from smartphone and then sign. I set clearOnResize to false on Signature and once I rotate the phone the signature changes size properly. If i try to add something to the signature, then the drawing happen somewhere else in the screen than expected. Also if i clear the signature and try to write keeping the phone in this new position, the problem keep showing off.

agilgur5 commented 4 years ago

So the de-sync issue you're describing is not a bug in react-signature-canvas or signature_pad, that's just how a canvas works. In fact, this behavior of the canvas is the reason why clearOnResize is true by default. When you set it to false, you're saying that you'll handle resizes on your own.

The root cause of the de-sync (I believe) is that your canvas doesn't have a fixed width & height and is instead responsive. That means that when the dimensions are changed, the canvas gets stretched out and distorted (can read more about that here and a bit in #41 ). Internally, when clearOnResize is true, resizeCanvas is called to properly handle the resize so that there are no distortions. When clearOnResize is false, your canvas will get distorted upon resize unless you have a fixed width & height.

The de-sync is also detailed in https://github.com/agilgur5/react-signature-canvas/pull/2#issuecomment-310922701 . lopis described a workaround in that comment and in his new example in #9 . There he uses a very large canvas with a fixed width & height along with a responsive container and some CSS to keep the signature centered.

Probably a good idea to add some docs around the caveats of setting clearOnResize to false as this comes up occasionally. It is an advanced feature, the default is set to true for a reason.

agilgur5 commented 2 years ago

Noting here that https://github.com/szimek/signature_pad/pull/446 may implement a workaround in signature_pad for this native canvas behavior.