rasterize method

Future<Sprite> rasterize({
  1. String? cacheKey,
  2. Images? images,
})

Returns a new Sprite where the image in memory is just the region defined by the original sprite.

If the images argument is passed it will be used to cache the rasterized image, otherwise the global Flame.images will be used. If the cacheKey is passed in, that will be the key for the cached image, otherwise the hash code of the rasterized image will be used as the key.

Implementation

Future<Sprite> rasterize({String? cacheKey, Images? images}) async {
  final key = cacheKey ?? _createRasterizeCacheKey();

  final imagesCache = images ?? Flame.images;

  if (imagesCache.containsKey(key)) {
    final cachedImage = imagesCache.fromCache(key);
    return Sprite(
      cachedImage,
      srcSize: srcSize,
    );
  }

  final rasterizedImage = await toImage();
  imagesCache.add(key, rasterizedImage);

  return Sprite(
    rasterizedImage,
    srcSize: srcSize,
  );
}