compose method

Future<Image> compose()

Compose all the images into a single composition.

Implementation

Future<Image> compose() async {
  // Rect used to determine how big the output image will be.
  var output = const Rect.fromLTWH(0, 0, 0, 0);
  final recorder = PictureRecorder();
  final canvas = Canvas(recorder);

  for (final compose in _composes) {
    final image = compose.image;
    final position = compose.position;
    final source = compose.source;
    final rotation = compose.angle;
    final anchor = compose.anchor;
    final isAntiAlias = compose.isAntiAlias;
    final blendMode = compose.blendMode;
    final destination = Rect.fromLTWH(0, 0, source.width, source.height);
    final realDest = destination.translate(position.x, position.y);

    canvas
      ..save()
      ..translateVector(position)
      ..translateVector(anchor)
      ..rotate(rotation)
      ..translateVector(-anchor)
      ..drawImageRect(
        image,
        source,
        destination,
        Paint()
          ..blendMode = blendMode
          ..isAntiAlias = isAntiAlias,
      )
      ..restore();

    // Expand the output so it can be used later on when the output image gets
    // created.
    output = output.expandToInclude(realDest);
  }

  return recorder
      .endRecording()
      .toImage(output.width.toInt(), output.height.toInt());
}