[flutter] Expand debug rendering example to show how to render in canvas coordinates

This commit is contained in:
Mario Zechner 2024-09-09 11:20:08 +02:00
parent 44c7b9120a
commit 0ffcc63b5e

View File

@ -40,7 +40,32 @@ class DebugRendering extends StatelessWidget {
const debugRenderer = DebugRenderer();
final controller = SpineWidgetController(onInitialized: (controller) {
controller.animationState.setAnimationByName(0, "walk", true);
}, onAfterPaint: (controller, canvas, commands) {
}, onBeforePaint: (controller, canvas) {
// Save the current transform and other canvas state
canvas.save();
// Get the current canvas transform an invert it, so we can work in the
// canvas coordinate system.
final currentMatrix = canvas.getTransform();
final invertedMatrix = Matrix4.tryInvert(Matrix4.fromFloat64List(currentMatrix));
if (invertedMatrix != null) {
canvas.transform(invertedMatrix.storage);
}
// Draw something.
final Paint paint = Paint()
..color = Colors.black
..strokeWidth = 2.0;
canvas.drawLine(
Offset(0, 0),
Offset(canvas.getLocalClipBounds().width, canvas.getLocalClipBounds().height),
paint,
);
// Restore the old transform and canvas state
canvas.restore();
}, onAfterPaint: (controller, canvas, commands) {
debugRenderer.render(controller.drawable, canvas, commands);
});