Created
April 15, 2025 15:29
-
-
Save roipeker/0990920cfa33ea3d46d4c4760e0fb02d to your computer and use it in GitHub Desktop.
Jovial Svg Graphx support
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
import 'package:graphx/graphx.dart' hide MatrixUtils; | |
import 'package:graphx/graphx.dart' as graphx show MatrixUtils; | |
import 'package:jovial_svg/jovial_svg.dart'; | |
/// A GraphX DisplayObject that renders a `ScalableImage` from | |
/// [jovial_svg](https://pub.dev/packages/jovial_svg) | |
/// | |
/// Use this class to render a ScalableImage from | |
/// ```dart | |
/// final svgData = await ScalableImage.fromSvgHttpUrl( | |
/// Uri.parse('https://www.svgrepo.com/show/535115/alien.svg'), | |
/// ); | |
/// final jovial = JovialShape(svgData); | |
/// jovial.colorize = Colors.red; | |
/// jovial.alpha = .3; | |
/// jovial.rotation = .4; | |
/// jovial.width = 100; | |
/// jovial.height = 100; | |
/// addChild(jovial); | |
/// ``` | |
/// | |
class JovialShape extends GDisplayObject { | |
/// A [GMatrix] helper object used for transformations | |
static final GMatrix _sHelperMatrix = GMatrix(); | |
/// The ScalableImage to render | |
late ScalableImage scalableImage; | |
/// Creates a new JovialShape with the given ScalableImage | |
JovialShape(this.scalableImage); | |
@override | |
GRect? getBounds(GDisplayObject? targetSpace, [GRect? out]) { | |
final matrix = _sHelperMatrix; | |
matrix.identity(); | |
getTransformationMatrix(targetSpace, matrix); | |
final viewport = scalableImage.viewport; | |
final rect = GRect( | |
viewport.left, | |
viewport.top, | |
viewport.width, | |
viewport.height, | |
); | |
out = graphx.MatrixUtils.getTransformedBoundsRect(matrix, rect, out); | |
return out; | |
} | |
@override | |
void $applyPaint(Canvas? canvas) { | |
var doSaveLayer = $alpha != 1; | |
if (doSaveLayer) { | |
final rect = getBounds(this)!.toNative(); | |
canvas!.saveLayer( | |
rect, | |
Paint()..color = Colors.black.withValues(alpha: $alpha), | |
); | |
} | |
scalableImage.paint(canvas!); | |
if (doSaveLayer) { | |
canvas.restore(); | |
} | |
} | |
/// Updates the tint color and mode and redraws the shape | |
void updateTint({required Color? tintColor, required BlendMode tintMode}) { | |
scalableImage = scalableImage.modifyTint( | |
newTintColor: tintColor, | |
newTintMode: tintMode, | |
); | |
requiresRedraw(); | |
} | |
/// Replaces the viewport of the scalable image | |
void replaceViewport( | |
Rect viewport, { | |
bool prune = false, | |
double pruningTolerance = 0, | |
}) { | |
scalableImage = scalableImage.withNewViewport( | |
viewport, | |
prune: prune, | |
pruningTolerance: pruningTolerance, | |
); | |
requiresRedraw(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment