SpriteBatch class

The SpriteBatch API allows for rendering multiple items at once.

This class allows for optimization when you want to draw many parts of an image onto the canvas. It is more efficient than using multiple calls to Canvas.drawImageRect and provides more functionality by allowing each BatchItem to have their own transform rotation and color.

By collecting all the necessary transforms on a single image and sending those transforms in a single batch to the GPU, we can render multiple parts of a single image at once.

Note: If you are experiencing problems with ghost lines, the less performant Canvas.drawImageRect can be used instead of Canvas.drawAtlas. To activate this mode, pass in useAtlas = false to the constructor or load method that you are using and each BatchItem will be rendered using the Canvas.drawImageRect method instead.

Constructors

SpriteBatch(Image atlas, {Color defaultColor = const Color(0x00000000), BlendMode defaultBlendMode = BlendMode.srcOver, RSTransform? defaultTransform, bool useAtlas = true})

Properties

atlas Image
The atlas used by the SpriteBatch.
final
colors UnmodifiableListView<Color>
The colors list shouldn't be modified directly, that is why an UnmodifiableListView is used. If you want to add colors use the add or addTransform method.
no setter
defaultBlendMode BlendMode
The default blend mode, used for blending a batch item.
final
defaultColor Color
The default color, used as a background color for a BatchItem.
final
defaultTransform RSTransform?
The default transform, used when a transform was not supplied for a BatchItem.
final
hashCode int
The hash code for this object.
no setterinherited
height int
The height of the atlas.
no setter
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
size Vector2
The size of the atlas.
no setter
sources UnmodifiableListView<Rect>
The sources list shouldn't be modified directly, that is why an UnmodifiableListView is used. If you want to add sources use the add or addTransform method.
no setter
transforms UnmodifiableListView<RSTransform>
The transforms list shouldn't be modified directly, that is why an UnmodifiableListView is used. If you want to add transforms use the add or addTransform method.
no setter
useAtlas bool
Whether to use Canvas.drawAtlas or not.
final
width int
The width of the atlas.
no setter

Methods

add({required Rect source, double scale = 1.0, Vector2? anchor, double rotation = 0, Vector2? offset, Color? color}) → void
Add a new batch item.
addTransform({required Rect source, RSTransform? transform, Color? color}) → void
Add a new batch item using a RSTransform.
clear() → void
Clear the SpriteBatch so it can be reused.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
render(Canvas canvas, {BlendMode? blendMode, Rect? cullRect, Paint? paint}) → void
toString() String
A string representation of this object.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited

Static Methods

load(String path, {Color defaultColor = const Color(0x00000000), BlendMode defaultBlendMode = BlendMode.srcOver, RSTransform? defaultTransform, Images? images, bool useAtlas = true}) Future<SpriteBatch>
Takes a path of an image, and optional arguments for the SpriteBatch.