createImage method
Creates an image representation of the current state of the
GDisplayObject.
If adjustOffset
is true, the resulting image will be offset so that its
top-left corner corresponds to the top-left corner of the current
bounds.
If resolution
is provided and is different than 1, the image will be
scaled by this factor before being returned.
If rect
is provided, it defines a rectangle that will be used as the
bounds for the image instead of the getFilterBounds of the
GDisplayObject.
Returns a Future that resolves to a ui.Image representing the current
state of the GDisplayObject in the form of an image.
Implementation
Future<ui.Image> createImage([
bool adjustOffset = true,
double resolution = 1,
GRect? rect,
]) async {
rect ??= getFilterBounds(); //getBounds($parent);
rect = rect!.clone();
if (resolution != 1) {
rect *= resolution;
}
final needsAdjust =
(rect.left != 0 || rect.top != 0) && adjustOffset || resolution != 1;
late ui.Picture picture;
if (needsAdjust) {
picture = createPicture((canvas) {
if (adjustOffset) canvas.translate(-rect!.left, -rect.top);
if (resolution != 1) canvas.scale(resolution);
}, (canvas) {
if (adjustOffset) canvas.restore();
if (resolution != 1) canvas.restore();
});
} else {
picture = createPicture();
}
final width = adjustOffset ? rect.width.toInt() : rect.right.toInt();
final height = adjustOffset ? rect.height.toInt() : rect.bottom.toInt();
final output = await picture.toImage(width, height);
picture.dispose();
return output;
}