render method

void render(
  1. Canvas canvas, {
  2. Vector2? position,
  3. Vector2? size,
  4. Anchor anchor = Anchor.topLeft,
  5. Paint? overridePaint,
})

Renders this sprite onto the canvas.

  • position: x,y coordinates where it will be drawn; default to origin.
  • size: width/height dimensions; it can be bigger or smaller than the original size -- but it defaults to the original texture size.
  • anchor: where in the sprite the x/y coordinates refer to; defaults to topLeft.
  • overridePaint: paint to use. You can also change the paint on your Sprite instance. Default is white.

Implementation

void render(
  Canvas canvas, {
  Vector2? position,
  Vector2? size,
  Anchor anchor = Anchor.topLeft,
  Paint? overridePaint,
}) {
  if (position != null) {
    _tmpRenderPosition.setFrom(position);
  } else {
    _tmpRenderPosition.setZero();
  }

  _tmpRenderSize.setFrom(size ?? srcSize);

  _tmpRenderPosition.setValues(
    _tmpRenderPosition.x - (anchor.x * _tmpRenderSize.x),
    _tmpRenderPosition.y - (anchor.y * _tmpRenderSize.y),
  );

  final drawRect = _tmpRenderPosition.toPositionedRect(_tmpRenderSize);
  final drawPaint = overridePaint ?? paint;

  canvas.drawImageRect(image, src, drawRect, drawPaint);
}