add method

void add({
  1. required Rect source,
  2. double scale = 1.0,
  3. Vector2? anchor,
  4. double rotation = 0,
  5. Vector2? offset,
  6. bool flip = false,
  7. Color? color,
})

Add a new batch item.

The source parameter is the source location on the atlas. You can position it on the canvas using the offset parameter.

You can transform the sprite from its offset using scale, rotation, anchor and flip.

The color parameter allows you to render a color behind the batch item, as a background color.

This method creates a new RSTransform based on the given transform arguments. If many RSTransform objects are being created and there is a way to factor out the computations of the sine and cosine of the rotation (which are computed each time this method is called) and reuse them over multiple RSTransform objects, it may be more efficient to directly use the more direct addTransform method instead.

Implementation

void add({
  required Rect source,
  double scale = 1.0,
  Vector2? anchor,
  double rotation = 0,
  Vector2? offset,
  bool flip = false,
  Color? color,
}) {
  anchor ??= Vector2.zero();
  offset ??= Vector2.zero();
  RSTransform? transform;

  // If any of the transform arguments is different from the defaults,
  // then we create one. This is to prevent unnecessary computations
  // of the sine and cosine of the rotation.
  if (scale != 1.0 ||
      anchor != Vector2.zero() ||
      rotation != 0 ||
      offset != Vector2.zero()) {
    transform = RSTransform.fromComponents(
      scale: scale,
      anchorX: anchor.x,
      anchorY: anchor.y,
      rotation: rotation,
      translateX: offset.x,
      translateY: offset.y,
    );
  }

  addTransform(
    source: source,
    transform: transform,
    flip: flip,
    color: color,
  );
}