comicBalloonPath function
A balloon's whole outline for a body of w by h: rounded body and tail, one path. at places the tail's tip: how far along
its edge, 0 to 1, or the usual distance from the start if null. A tail's base stays on the straight of the edge.
Implementation
Path comicBalloonPath(
double w,
double h,
ComicTail tail,
double radius, {
double? at,
bool rtl = false,
double scale = 1,
}) {
final r = math.min(radius, math.min(w, h) / 2);
final body = Path()
..addRRect(
RRect.fromRectAndRadius(
Rect.fromLTWH(0, 0, w, h).deflate(1.25),
Radius.circular(math.max(0, r - 1.25)),
),
);
if (tail == ComicTail.none) return body;
final t = Path();
double mx(double x) => rtl ? w - x : x;
if (tail == ComicTail.side) {
// The concept's left tail, its top a little under the top corner, held inside the body.
final dy = math.max(0.0, math.min(14.5, h - 26)) - 14.5;
double y(double v) => v + dy;
double sx(double v) => mx(v < 1.25 ? 1.25 + (v - 1.25) * scale : v);
t
..moveTo(sx(5), y(19))
..lineTo(sx(1.25), y(19))
..cubicTo(sx(-4), y(21.5), sx(-11), y(20.5), sx(-18), y(16))
..cubicTo(sx(-14), y(25.5), sx(-6), y(32.5), sx(1.25), y(36))
..lineTo(sx(5), y(36))
..close();
return Path.combine(PathOperation.union, body, t);
}
final up = tail == ComicTail.top;
final baseY = up ? 1.25 : h - 1.25;
double ty(double v) => up ? baseY - v * scale : baseY + v * scale;
final span = 30 * scale;
final lean = tail == ComicTail.bottomEnd;
final room = math.max(0.0, w - span);
var a = at != null
? (at * w - 1.5 * scale).clamp(0.0, room)
: (lean ? room - 26.5 * scale : 26.5 * scale).clamp(0.0, room);
// Past the corner's curve a tail's base would stand clear of the body.
final least = math.min(r * .4, room);
a = a.clamp(least, math.max(room - r * .4, least));
double tx(double v) {
final x = lean ? a + (span - v * scale) : a + v * scale;
return mx(x);
}
t
..moveTo(tx(5), up ? baseY + 4 : baseY - 4)
..lineTo(tx(5), ty(0))
..cubicTo(tx(6.6), ty(4.75), tx(6), ty(11.75), tx(1.5), ty(18.75))
..cubicTo(tx(11), ty(14.75), tx(18), ty(6.75), tx(21), ty(0))
..lineTo(tx(21), up ? baseY + 4 : baseY - 4)
..close();
return Path.combine(PathOperation.union, body, t);
}