snapSelectedToGrid method

bool snapSelectedToGrid([
  1. double? size
])

Snaps selected objects to the nearest grid coordinates.

Implementation

bool snapSelectedToGrid([double? size]) {
  final grid = size ?? gridSize;
  if (grid <= 0 || _selectedObjectIds.isEmpty) return false;
  _remember();
  final changed = <CanvasObject>[];
  for (final id in _selectedObjectIds) {
    final index = _objects.indexWhere((object) => object.id == id);
    if (index < 0 || _objects[index].locked) continue;
    final bounds = _boundsFor(_objects[index]);
    final dx = _roundTo(bounds.x, grid) - bounds.x;
    final dy = _roundTo(bounds.y, grid) - bounds.y;
    final moved = _moveObject(_objects[index], dx, dy);
    _objects[index] = moved;
    changed.add(moved);
  }
  if (changed.isEmpty) return false;
  render();
  onMove?.call(List.unmodifiable(changed));
  _emitChange();
  return true;
}