steveruizok / perfect-freehand

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

getStroke cannot handle a single input point if there's no tapering or it's the last point #19

Closed glenn-allen closed 3 years ago

glenn-allen commented 3 years ago

Firstly, thanks for the amazing library Steve!

I've noticed an issue with the latest release where a single point will result in a stroke of NaNs if either the taper is 0 or the last parameter is false. E.g:

const stroke = getStroke([ [1, 1, 0] ], {
        size: 1,
        thinning: 0.6,
        smoothing: 0.5,
        streamline: 0.5,
        start: {
            taper: 0,
        },
        end: {
            taper: 0,
        },
        simulatePressure: true,
        last: false,
});
// [ [ NaN, NaN ], [ NaN, NaN ],  [ NaN, NaN ], [ NaN, NaN ],  [ NaN, NaN ], [ NaN, NaN ],  [ NaN, NaN ], [ NaN, NaN ],  [ NaN, NaN ], [ NaN, NaN ],  [ NaN, NaN ]]

This means that single clicks don't create a stroke. I've tested this in the example app and while it works in your hosted version it doesn't seem to work locally, so I assume the hosted version is on a previous library version?

As far as I can gather it's due to the following line - I guess it's trying to take a unit vector of a point with 0 length, but I'm not really sure what the fix should be: https://github.com/steveruizok/perfect-freehand/blob/8c5a9907e7df2a44c5cdf46a93678a2f7e65e104/src/index.ts#L323

steveruizok commented 3 years ago

Hey, looked at this today. Should be fixed on 0.4.10!

glenn-allen commented 3 years ago

Working perfectly, thanks Steve!