Chapter 21 — Coordinates, Canvas, and 2D Drawing
Part 2A — 2D Game Fundamentals · Chapter 21 of 62 · 7 h
Chapter 21 — Coordinates, Canvas, and 2D Drawing
Level: intermediate Estimated time: 7 h Prerequisites: Chapter 19 (Flutter crash course,
CustomPainter) and Chapter 20 (game loop,Ticker, delta time) What you'll be able to do by the end: draw any 2D scene by hand withCanvasandPaint, position it pixel-perfectly in the screen's coordinate system, transform it (translation, rotation, scale), and animate it with the game loop'sdt.
21.0 — Chapter Objectives
By the end of this chapter, you will be able to:
- explain why the screen's
Yaxis points down, unlike the mathematical coordinate system; - locate the origin
(0, 0)of a drawing area and reason in local coordinates; - distinguish a logical pixel from a physical pixel and use
devicePixelRatio; - read the size of the drawing area passed to
paint()and adapt to it; - work with
Offset,Rect, andSize, the three basic types of Flutter geometry; - anticipate the difference between
Vector2(Flame) andOffset(Flutter); - write a clean
CustomPainter, with a correctshouldRepaint(); - configure a
Paintobject: color, style, stroke width; - draw rectangles, circles, lines, ellipses, rounded rectangles, paths, and points;
- draw text in a
CanvaswithTextPainter; - build colors in ARGB and apply transparency;
- apply a linear or radial gradient to a shape;
- explain and take advantage of the drawing order (the "painter's algorithm");
- use
save()/restore()without ever unbalancing the stack; - apply
translate(),rotate(), andscale(), and understand why their order matters; - draw a shape around its center rather than its corner (the notion of an anchor);
- restrict drawing to an area with
clipRect(); - explain double buffering and why the screen doesn't flicker;
- identify what is expensive in
paint()and avoid it; - draw a dungeon grid and a character entirely out of geometric shapes;
- animate a drawing with the
dtfrom Chapter 20.
Comments
Comments are powered by GitHub Discussions
Sign in with GitHub to join the discussion