dragPath function

List<TouchPoint> dragPath(
  1. int x1,
  2. int y1,
  3. int x2,
  4. int y2, {
  5. required int durationMs,
  6. int holdMs = 0,
  7. int frameMs = 16,
})

A straight drag from (x1, y1) to (x2, y2) over durationMs in frame-sized steps, then resting at the end for holdMs so the lift starts no fling.

Implementation

List<TouchPoint> dragPath(int x1, int y1, int x2, int y2, {required int durationMs, int holdMs = 0, int frameMs = 16}) {
  final steps = (durationMs / frameMs).ceil().clamp(2, 400);
  final rests = holdMs > 0 ? (holdMs / frameMs).ceil().clamp(1, 400) : 0;
  return [
    (x: x1, y: y1, ms: 0),
    for (var i = 1; i <= steps; i++)
      (
        x: (x1 + (x2 - x1) * i / steps).round(),
        y: (y1 + (y2 - y1) * i / steps).round(),
        ms: (durationMs * i / steps).round(),
      ),
    for (var i = 1; i <= rests; i++) (x: x2, y: y2, ms: durationMs + (holdMs * i / rests).round()),
  ];
}