Implementing the snap to grid behaviour