computePathFromLine function

Path computePathFromLine(
  1. PaintLine line
)

ストロークをPathに変換するユーティリティ

Implementation

ui.Path computePathFromLine(PaintLine line) {
  final options = switch (line.brushType) {
    BrushType.pen => line.strokeOptions.copyWith(
      thinning: 0.5,
      smoothing: 0.5,
      streamline: 0.5,
    ),
    BrushType.marker => line.strokeOptions.copyWith(
      thinning: 0,
      smoothing: 0,
      streamline: 1,
      simulatePressure: false,
    ),
    BrushType.neon => line.strokeOptions.copyWith(
      thinning: -0.10,
      smoothing: 1,
      streamline: 1,
    ),
  };
  final stroke = getStroke(line.points, options: options);

  final path = ui.Path();
  if (stroke.isEmpty) {
    return path;
  }

  if (stroke.length < 2) {
    // 単一点は円として描画(後でdrawCircleで処理するが、Pathとして返す)
    path.addOval(
      Rect.fromCircle(
        center: stroke.first,
        radius: line.strokeOptions.size / 2,
      ),
    );
  } else {
    path.moveTo(stroke.first.dx, stroke.first.dy);
    for (var i = 0; i < stroke.length - 1; ++i) {
      final p0 = stroke[i];
      final p1 = stroke[i + 1];
      path.quadraticBezierTo(
        p0.dx,
        p0.dy,
        (p0.dx + p1.dx) / 2,
        (p0.dy + p1.dy) / 2,
      );
    }
  }
  return path;
}