Sub topic 9
Transforms, Save/Restore, and Clipping
This chapter assumes you know Dart classes, Flutter widget trees, Size, and the basic Canvas drawing methods. You should also understand that a canvas uses logical pixels, with its origin at the painter's top-left corner. The x-coordinate increases to the right, and the y-coordinate increases downward.
Why canvas state matters
A canvas has state that affects later drawing operations. This state includes:
- The current coordinate transform.
- The current clipping region.
- Other settings applied by the canvas.
Methods such as translate, rotate, and scale change the coordinate system. They do not move or change shapes that were already drawn. They change how future coordinates are interpreted.
Use save() before a local change and restore() afterward. restore() returns the canvas to the state that existed at the matching save() call.
Translating, rotating, and scaling
translate(dx, dy) moves the origin. After translating by (100, 50), drawing at (0, 0) appears at (100, 50).
rotate(angle) rotates the coordinate system around the current origin. Angles use radians. scale(x, y) changes the size of later drawings. A scale of 2 doubles the drawing.
This painter draws a rotated rectangle around the center of its available area:
import 'dart:math' as math;
import 'package:flutter/material.dart';
class RotatedBoxPainter extends CustomPainter {
@override
void paint(Canvas canvas, Size size) {
final paint = Paint()..color = Colors.indigo;
canvas.save();
canvas.translate(size.width / 2, size.height / 2);
canvas.rotate(math.pi / 8);
canvas.scale(1.2, 0.8);
canvas.drawRect(
Rect.fromCenter(
center: Offset.zero,
width: 120,
height: 70,
),
paint,
);
canvas.restore();
}
@override
bool shouldRepaint(covariant RotatedBoxPainter oldDelegate) => false;
}The rectangle uses Offset.zero because the translation places the local origin at the widget's center. The scale makes the rectangle wider than it is tall before rotation.
A common error is to omit restore(). Later shapes then inherit the transform, which can make unrelated drawing appear shifted, rotated, or stretched.
Clipping drawing to a region
Clipping limits where drawing can appear. clipRect restricts drawing to a rectangle, while clipRRect uses rounded corners and clipPath uses an arbitrary path.
Clipping also affects later commands, so save before clipping:
canvas.save();
canvas.clipRRect(
RRect.fromRectAndRadius(
Offset.zero & size,
const Radius.circular(20),
),
);
canvas.drawCircle(
Offset(size.width / 2, size.height / 2),
size.width,
Paint()..color = Colors.orange,
);
canvas.restore();Only the part of the circle inside the rounded rectangle is visible. The clip does not erase existing pixels outside the region. It only prevents affected drawing commands from changing those pixels.
Clipping can improve correctness, but complicated clips may cost more to render. Use the simplest clip that expresses the design.
Exercise
Why should a painter call restore() after using translate and clipRRect?
Show answer
Both operations change canvas state for subsequent drawing. restore() removes those local changes so later drawing uses the original coordinate system and clipping region.