paint method

  1. @override
void paint(
  1. Canvas canvas,
  2. Size size,
  3. WorldToScreen worldToScreen,
  4. ScreenToWorld screenToWorld,
  5. double scale,
)
override

Called by the canvas to paint this item onto the screen.

The canvas and size are provided by the Flutter framework. Use worldToScreen to translate mathematical coordinates into on-screen pixels.

Implementation

@override
void paint(
  Canvas canvas,
  Size size,
  WorldToScreen worldToScreen,
  ScreenToWorld screenToWorld,
  double scale,
) {
  if (worldPoints.length < 2) return;
  final strokePaint = Paint()
    ..color = isEraser ? Colors.transparent : color
    ..blendMode = isEraser ? BlendMode.clear : BlendMode.srcOver
    ..strokeWidth = strokeWidth * (scale / baseScale)
    ..style = PaintingStyle.stroke
    ..strokeCap = cap
    ..strokeJoin = StrokeJoin.round;

  final strokePath = Path()
    ..moveTo(
      worldToScreen(worldPoints[0].dx, worldPoints[0].dy).dx,
      worldToScreen(worldPoints[0].dx, worldPoints[0].dy).dy,
    );
  for (int i = 1; i < worldPoints.length; i++) {
    final currentScreenPoint = worldToScreen(
      worldPoints[i].dx,
      worldPoints[i].dy,
    );
    strokePath.lineTo(currentScreenPoint.dx, currentScreenPoint.dy);
  }
  canvas.drawPath(strokePath, strokePaint);
}