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
528 stars 115 forks source link

ink offset from mouse pointer #62

Closed jch02140 closed 2 years ago

jch02140 commented 3 years ago

I am having trouble with react-signature-canvas. I am able to get the signature pad working using Material UI Modal. However, I noticed that the ink is offset from the mouse pointer.

Here is the link to the codesandbox with the offset ink problem replicated. How do I fix it?

https://codesandbox.io/s/signature-pad-demo-lsqyu?file=/demo.js

ShiiRochi commented 3 years ago

@jch02140, try to wrap canvas component inside relative positioned div. It should take all the space required for Canvas component. Canvas component itself should take 100% of width and height of its parent.

kkieffer commented 2 years ago

I have the same problem. I believe it is related to using a Retina display (MacBook, in my case) where the dpi is 2x. See this issue on stackoverflow here. I do not know how to solve it within this library, the fix may need to be applied in the underlying signature-pad.

jameskiminfotrack commented 2 years ago

Likewise, would like to have this fixed. It may have been fixed with the latest Signature Pad version (4.0.0), so if we can update to the latest then we may see a lot of improvements as this library is currently referencing 2.3.2.

Bertiscoding commented 2 years ago

I tested it with the updated version (4.0.0) for the Signature Pad and did not resolve the issue. Nevertheless it would be good to update it to the latest anyway. But I believe that @ShiiRochi comment does not get enough attention, because it really does fix the offset issue. Key part here is to set the canvas to position: absolute; and width and height 100% (don't specify the height and width in the canvasProps, but instead in css) and wrap it in a div with position: relative; and height and width specified in px

agilgur5 commented 2 years ago

native canvas behavior, duplicate

So this issue has popped up several times, but this is native canvas behavior when you don't have fixed dimensions on your canvas; neither react-signature-canvas nor signature_pad can control or change native canvas behavior. One of those cases where knowing how the underlying technology functions can be helpful.

See #43 and #41 (which this duplicates) which have detailed descriptions.

categorizing

I've tried categorizing most of these issues by renaming them with "de-sync", but "offset" is another term that might get some visibility in searches. These issues usually occur during resizes when clearOnResize={false}, but this is the first duplicate to provide a repro (thank you!), and one that doesn't use clearOnResize. It is still answered in #43 and #41 though, as the root cause is equivalent.

As #43 is marked for docs, I have been meaning to add some documentation around this as it is the most frequent issue reported despite this categorization. Will need to figure out where to put that and how to word that in simple terms.

responsive workarounds

But I believe that @ShiiRochi comment does not get enough attention, because it really does fix the offset issue. Key part here is to set the canvas to position: absolute; and width and height 100% (don't specify the height and width in the canvasProps, but instead in css) and wrap it in a div with position: relative; and height and width specified in px

The official example does something similar

43 mentions another workaround as well that is implemented in #9 's example. Been meaning to make a Storybook for all these examples which should hopefully help with this recurrent issue too.

I added some edits to the answers for the respective SO question for this issue.

upgrading

It may have been fixed with the latest Signature Pad version (4.0.0), so if we can update to the latest then we may see a lot of improvements as this library is currently referencing 2.3.2.

It's how the native canvas element works, so an upgrade cannot "fix" this. signature_pad's changelog does not suggest this either. As such, upgrading is unrelated to this. Please try to check these things before making such recommendations that may confuse others. EDIT: It looks like https://github.com/szimek/signature_pad/pull/446 may implement a workaround for this behavior inside of signature_pad itself. That being said, I'm not sure if/when it will be merged, and it is a workaround for native behavior (as is clearing on resize).

In any case, v2.3.2 was latest before the v3.0.0 betas, and those never graduated from beta. They also were mainly around an internal TS re-write. Both of those facts resulted in my decision in https://github.com/agilgur5/react-signature-canvas/pull/52#issuecomment-664022828 to not update to them.

v4.0.0 was recently released after a long hiatus. There are lots of improvements in there, but it will take some time to upgrade to it as it has breaking changes; see https://github.com/agilgur5/react-signature-canvas/pull/68#discussion_r800113860 for more details on that