steveruizok / perfect-freehand-dart

Draw perfect freehand lines—in Flutter.
https://steveruizok.github.io/perfect-freehand-dart/
MIT License
242 stars 36 forks source link

Pressure processing when simulatePressure = false & isComplete = false #4

Open ercgeek opened 2 years ago

ercgeek commented 2 years ago

First big thanks for publishing this.

Using the library to render strokes passing on iPad stylus pressure, the stroke is incorrectly rendered with a constant pressure, whatever is passed on points[0].

I think I found the issue on the use of lrp() to smooth out the points. Seems lrp() returns the pressure of the first point and not the interpolation of the pressures from both points.

Changed lib\src\get_stroke_points.dart as follows and seems to corrects the issue. Using the pressure from the second point, not an interpolated value.

// Original code
// if (isComplete && i == pts.length - 1) {
//   point = pts[i];
// } else {
//   point = lrp(prev.point, pts[i], t);
// }

// New code
if (isComplete && i == pts.length - 1) {
  point = pts[i];
} else {
  final tempPoint = lrp(prev.point, pts[i], t);
  point = Point(tempPoint.x, tempPoint.y, pts[i].p);
}

I'm not a dart expert, please let me know if I'm missing something here.

ercgeek commented 2 years ago

An additional change is needed in lib\src\get_stroke_outline_points.dart

// Original code
// if (thinning != 0) {
//   if (simulatePressure) {
//     sp = min(1, curr.distance / size);
//     rp = min(1, 1 - sp);
//     pressure = min(
//       1,
//       prevPressure + (rp - prevPressure) * (sp * rateOfPressureChange),
//     );
//     radius = getStrokeRadius(
//       size,
//       thinning,
//       pressure,
//     );
//   } else {
//     radius = size / 2;
//   }
// }

// New code
if (thinning != 0) {
  if (simulatePressure) {
    sp = min(1, curr.distance / size);
    rp = min(1, 1 - sp);
    pressure = min(
      1,
      prevPressure + (rp - prevPressure) * (sp * rateOfPressureChange),
    );
  }
  radius = getStrokeRadius(
    size,
    thinning,
    pressure,
  );
}