steveruizok / perfect-freehand

Draw perfect pressure-sensitive freehand lines.
https://perfectfreehand.com
MIT License
4.52k stars 159 forks source link

Suggestion: Skip duplicate points #11

Closed donpark closed 3 years ago

donpark commented 3 years ago

getStroke() returns array withNaN values If first two points are same (mousedown+mousemove) which apparently happens frequently (1 out of 5). I don't know if it's just at the beginning of a stroke or at any two consecutive points in the stroke.

While I've already worked around the problem by ignoring new point if it's same as previous, I thought you may want to add the logic internally to avoid future issues as it can result in misleading behaviors.

BTW, thanks for sharing this wonderful library. It's great.

steveruizok commented 3 years ago

Thanks Don! We'd noticed some failures on Excalidraw but I hadn't had a chance to look into it yet. This looks like it could be the cause.

steveruizok commented 3 years ago

Added this in the most recent version. Seems to be working well. :)

oskosk commented 3 years ago

Hi @steveruizok. Thanks for this beautiful lib...

I have a question. I got to this issue after seeing a similar symptom to the one reported here... Just to clarify. Should something like this work?

getStroke([ [0, 0, 0] ] );

I see it returning the following with 0.4.91:

[
  [ null, null
  ],
  [ null, null
  ],
  [ null, null
  ],
  [ null, null
  ],
  [ null, null
  ],
  [ null, null
  ],
  [ null, null
  ],
  [ null, null
  ],
  [ null, null
  ],
  [ null, null
  ],
  [ null, null
  ]
]

Showing in the console as

image

The same goes for getStroke([ [0, 0, 0],[0, 0, 0] ] ); or getStroke([ [0, 0, 0],[0, 0, 0] ],[0, 0, 0] ] );

steveruizok commented 3 years ago

Hey, yes that should work (or at least, it should fail correctly). I'll add a test and fix it if it's failing.

Separately, but maybe still relevant here, it might be better to handle such data in a different way. For example, in most of the projects that use freehand, I handle short lines (lines with 0-2 points) with a dot rather than calling to freehand.

oskosk commented 3 years ago

Ah I see. Thanks for the tip!