tileOf method

Rect tileOf(
  1. Size box
)

The first tile in the box: the tile's own size on the axes it states, the box's on the ones it fills -- resized to a whole number of tiles on a round axis -- placed by x and y in what is left and moved on by dx and dy.

Implementation

Rect tileOf(Size box) {
  var w = _width ?? box.width, h = _height ?? box.height;
  if (_ratio case final r? when r > 0) {
    w = math.min(box.width, box.height * r);
    h = w / r;
  }
  // In Blink's layout units, 1/64 px: the room over the count with the
  // remainder dropped (a LayoutUnit division truncates). 300px of 7px
  // tiles is 43 of 6.96875, not of 6.9767 -- a third of a pixel short by
  // the last one, which is where Chrome draws them.
  double rounded(double room, double tile) =>
      ((room * 64 + 1e-6) / math.max(1, (room / tile).round())).floorToDouble() / 64;
  if (_roundX && _width != null && w > 0 && box.width > 0) w = rounded(box.width, w);
  if (_roundY && _height != null && h > 0 && box.height > 0) h = rounded(box.height, h);
  return Rect.fromLTWH((box.width - w) * _x + _dx, (box.height - h) * _y + _dy, w, h);
}