paint method

  1. @override
void paint(
  1. Canvas canvas
)
override

Paints this node to the canvas.

Subclasses, such as Sprite, override this method to do the actual painting of the node. To do custom drawing override this method and make calls to the canvas object. All drawing is done in the node's local coordinate system, relative to the node's position. If you want to make the drawing relative to the node's bounding box's origin, override NodeWithSize and call the applyTransformForPivot method before making calls for drawing.

void paint(Canvas canvas) {
  canvas.save();
  applyTransformForPivot(canvas);

  // Do painting here

  canvas.restore();
}

Implementation

@override
void paint(Canvas canvas) {
  // Account for pivot point
  applyTransformForPivot(canvas);

  double w = texture.size.width;
  double h = texture.size.height;

  if (w <= 0 || h <= 0) return;

  double scaleX = size.width / w;
  double scaleY = size.height / h;

  if (constrainProportions) {
    // Constrain proportions, using the smallest scale and by centering the
    // image
    if (scaleX < scaleY) {
      canvas.translate(0.0, (size.height - scaleX * h) / 2.0);
      scaleY = scaleX;
    } else {
      canvas.translate((size.width - scaleY * w) / 2.0, 0.0);
      scaleX = scaleY;
    }
  }

  canvas.scale(scaleX, scaleY);

  // Setup paint object for opacity and transfer mode
  _updatePaint(_cachedPaint);

  // Do actual drawing of the sprite
  texture.drawTexture(canvas, Offset.zero, _cachedPaint);

  // Debug drawing
//      canvas.drawRect(Offset.zero & texture.size, new Paint()..color=const Color(0x33ff0000));
}