Skip to content

Commit

Permalink
feat: Expose paint from svgComponent to set opacity and have opacity …
Browse files Browse the repository at this point in the history
…effects (#2092)
  • Loading branch information
dipakp2726 authored Oct 21, 2022
1 parent 8395126 commit bedacd0
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
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);
}

@override
Expand Down

0 comments on commit bedacd0

Please sign in to comment.