Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Expose paint from svgComponent to set opacity and have opacity effects #2092

Merged
merged 11 commits into from
Oct 21, 2022
9 changes: 7 additions & 2 deletions packages/flame_svg/lib/svg.dart
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,19 @@ class Svg {
}

/// Renders the svg on the [canvas] using the dimensions provided by [size].
void render(Canvas canvas, Vector2 size) {
void render(
Canvas canvas,
Vector2 size, {
Paint? overridePaint,
}) {
final _size = size.toSize();
final image = _getImage(_size);

if (image != null) {
canvas.save();
canvas.scale(1 / pixelRatio);
canvas.drawImage(image, Offset.zero, _paint);
final drawPaint = overridePaint ?? _paint;
canvas.drawImage(image, Offset.zero, drawPaint);
canvas.restore();
} else {
_render(canvas, _size);
Expand Down
9 changes: 6 additions & 3 deletions packages/flame_svg/lib/svg_component.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import 'package:flame/components.dart';
import 'package:flame_svg/svg.dart';

/// Wraps [Svg] in a Flame component.
class SvgComponent extends PositionComponent {
class SvgComponent extends PositionComponent with HasPaint {
/// The wrapped instance of [Svg].
Svg? _svg;

Expand All @@ -18,7 +18,10 @@ class SvgComponent extends PositionComponent {
super.anchor,
super.children,
super.priority,
}) : _svg = svg;
Paint? paint,
}) : _svg = svg {
this.paint = paint ?? this.paint;
}

set svg(Svg? svg) {
_svg?.dispose();
Expand All @@ -30,7 +33,7 @@ class SvgComponent extends PositionComponent {

@override
void render(Canvas canvas) {
_svg?.render(canvas, size);
_svg?.render(canvas, size, overridePaint: paint);
dipakp2726 marked this conversation as resolved.
Show resolved Hide resolved
}

@override
Expand Down