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,
) {
  final anchorPoint = worldToScreen(x, y);
  final targetWidth = worldWidth * scale;
  final targetHeight = targetWidth * image.height / image.width;

  final leftPosition = anchorPoint.dx - targetWidth * (alignment.x + 1) / 2;
  final topPosition = anchorPoint.dy - targetHeight * (alignment.y + 1) / 2;

  final sourceRect = Rect.fromLTWH(
    0,
    0,
    image.width.toDouble(),
    image.height.toDouble(),
  );
  final destinationRect = Rect.fromLTWH(
    leftPosition,
    topPosition,
    targetWidth,
    targetHeight,
  );

  canvas.drawImageRect(
    image,
    sourceRect,
    destinationRect,
    Paint()..filterQuality = FilterQuality.medium,
  );
}