mirror of
https://github.com/flame-engine/flame.git
synced 2025-11-01 19:12:31 +08:00
feat: Adding ImageExtension.resize (#2418)
Adds a helper method to ImageExtension to make it easier to resize an image.
This commit is contained in:
23
examples/lib/stories/image/image.dart
Normal file
23
examples/lib/stories/image/image.dart
Normal file
@ -0,0 +1,23 @@
|
||||
import 'package:dashbook/dashbook.dart';
|
||||
|
||||
import 'package:examples/commons/commons.dart';
|
||||
import 'package:examples/stories/image/resize.dart';
|
||||
import 'package:flame/game.dart';
|
||||
|
||||
void addImageStories(Dashbook dashbook) {
|
||||
dashbook.storiesOf('Image')
|
||||
..decorator(CenterDecorator())
|
||||
..add(
|
||||
'resize',
|
||||
(context) => GameWidget(
|
||||
game: ImageResizeExample(
|
||||
Vector2(
|
||||
context.numberProperty('width', 200),
|
||||
context.numberProperty('height', 300),
|
||||
),
|
||||
),
|
||||
),
|
||||
codeLink: baseLink('image/resize.dart'),
|
||||
info: ImageResizeExample.description,
|
||||
);
|
||||
}
|
||||
29
examples/lib/stories/image/resize.dart
Normal file
29
examples/lib/stories/image/resize.dart
Normal file
@ -0,0 +1,29 @@
|
||||
import 'package:flame/components.dart';
|
||||
import 'package:flame/extensions.dart';
|
||||
import 'package:flame/game.dart';
|
||||
|
||||
class ImageResizeExample extends FlameGame {
|
||||
ImageResizeExample(this.sizeTarget);
|
||||
|
||||
static const String description = '''
|
||||
Shows how a dart:ui `Image` can be resized using Flame Image extensions.
|
||||
Use the properties on the side to change the size of the image.
|
||||
''';
|
||||
|
||||
final Vector2 sizeTarget;
|
||||
|
||||
@override
|
||||
Future<void> onLoad() async {
|
||||
final image = await images.load('flame.png');
|
||||
|
||||
final resized = await image.resize(sizeTarget);
|
||||
add(
|
||||
SpriteComponent(
|
||||
sprite: Sprite(resized),
|
||||
position: size / 2,
|
||||
size: resized.size,
|
||||
anchor: Anchor.center,
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user