enableSnapToGrid method

void enableSnapToGrid(
  1. bool enable
)

Quick access to frequently used configuration properties. Enable or disable zooming in the node editor.

Implementation

/// Enable or disable zooming in the node editor.
void enableSnapToGrid(bool enable) async {
  setConfig(config.copyWith(enableSnapToGrid: enable));

  if (!enable) {
    for (final node in nodes.values) {
      node.offset = _unboundNodeOffsets[node.id]!;
    }
  } else {
    for (final node in nodes.values) {
      if (enable) {
        node.offset = Offset(
          (node.offset.dx / config.snapToGridSize).round() *
              config.snapToGridSize,
          (node.offset.dy / config.snapToGridSize).round() *
              config.snapToGridSize,
        );
      }
    }
  }

  nodesDataDirty = true;
  linksDataDirty = true;
}