mirror of
https://github.com/flame-engine/flame.git
synced 2025-11-13 03:10:42 +08:00
Added toImage method (#661)
This commit is contained in:
committed by
GitHub
parent
4e5b75eafb
commit
ec09ce0ce6
@@ -21,6 +21,7 @@
|
||||
- Add assertion to make sure Draggables are safe to add
|
||||
- Add utility methods to the Anchor class to make it more "enum like"
|
||||
- Enable user-defined anchors
|
||||
- Added `toImage` method for the `Sprite` class
|
||||
|
||||
## 1.0.0-rc6
|
||||
- Use `Offset` type directly in `JoystickAction.update` calculations
|
||||
|
||||
@@ -39,6 +39,8 @@ class _Composed {
|
||||
}
|
||||
|
||||
/// The [ImageComposition] allows for composing multiple images onto a single image.
|
||||
///
|
||||
/// **Note:** Composing images is a heavy async operation and should not be called on each [Game.render].
|
||||
class ImageComposition {
|
||||
/// The values that will be used to compose the image
|
||||
final List<_Composed> _composes = [];
|
||||
|
||||
@@ -15,6 +15,13 @@ extension Vector2Extension on Vector2 {
|
||||
/// Creates a [Point] from the [Vector2]
|
||||
Point toPoint() => Point(x, y);
|
||||
|
||||
/// A rectangle constructor operator.
|
||||
///
|
||||
/// Combines two [Vector2]s to form a Rect whose top-left coordinate is the
|
||||
/// point given by adding this vector, the left-hand-side operand,
|
||||
/// to the origin, and whose size is the right-hand-side operand.
|
||||
Rect operator &(Vector2 size) => toPositionedRect(size);
|
||||
|
||||
/// Creates a [Rect] starting from [x, y] and having the size of the
|
||||
/// argument [Vector2]
|
||||
Rect toPositionedRect(Vector2 size) => Rect.fromLTWH(x, y, size.x, size.y);
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import 'dart:ui';
|
||||
|
||||
import '../image_composition.dart';
|
||||
import 'anchor.dart';
|
||||
import 'extensions/offset.dart';
|
||||
import 'extensions/vector2.dart';
|
||||
@@ -76,4 +77,13 @@ class Sprite {
|
||||
|
||||
canvas.drawImageRect(image, src, drawRect, drawPaint);
|
||||
}
|
||||
|
||||
/// Return a new Image based on the [src] of the Sprite.
|
||||
///
|
||||
/// **Note:** This is a heavy async operation and should not be called on each [Game.render].
|
||||
Future<Image> toImage() async {
|
||||
final composition = ImageComposition()
|
||||
..add(image, Vector2.zero(), source: src);
|
||||
return composition.compose();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user