drawTexture method

void drawTexture (
  1. Canvas canvas,
  2. Offset position,
  3. Paint paint
)

Draws the texture to a Canvas at a specified position and with the specified paint.

Implementation

void drawTexture(Canvas canvas, Offset position, Paint paint) {
  // Get drawing position
  double x = position.dx;
  double y = position.dy;

  // Draw the texture
  if (rotated) {
    // Account for position
    bool translate = (x != 0 || y != 0);
    if (translate) {
      canvas.translate(x, y);
    }

    // Calculate the rotated frame and spriteSourceSize
    Size originalFrameSize = frame.size;
    Rect rotatedFrame = frame.topLeft & new Size(originalFrameSize.height, originalFrameSize.width);
    Offset rotatedSpriteSourcePoint = new Offset(
        -spriteSourceSize.top - (spriteSourceSize.bottom - spriteSourceSize.top),
        spriteSourceSize.left);
    Rect rotatedSpriteSourceSize = rotatedSpriteSourcePoint & new Size(originalFrameSize.height, originalFrameSize.width);

    // Draw the rotated sprite
    canvas.rotate(-math.pi/2.0);
    canvas.drawImageRect(image, rotatedFrame, rotatedSpriteSourceSize, paint);
    canvas.rotate(math.pi/2.0);

    // Translate back
    if (translate) {
      canvas.translate(-x, -y);
    }
  } else {
    // Draw the sprite
    Rect dstRect = new Rect.fromLTWH(x + spriteSourceSize.left, y + spriteSourceSize.top, spriteSourceSize.width, spriteSourceSize.height);
    canvas.drawImageRect(image, frame, dstRect, paint);
  }
}