( stepX: number, stepY: number = stepX )
| 258 | * ``` |
| 259 | */ |
| 260 | export function snapToGrid( |
| 261 | stepX: number, |
| 262 | stepY: number = stepX |
| 263 | ): LayoutConstraint { |
| 264 | // Validate step values to prevent division by zero or invalid snapping |
| 265 | if (stepX <= 0 || stepY <= 0) { |
| 266 | throw new Error( |
| 267 | `snapToGrid: step values must be positive (got stepX=${stepX}, stepY=${stepY})` |
| 268 | ); |
| 269 | } |
| 270 | |
| 271 | return { |
| 272 | name: `snapToGrid(${stepX}, ${stepY})`, |
| 273 | |
| 274 | constrainPosition( |
| 275 | _item: LayoutItem, |
| 276 | x: number, |
| 277 | y: number |
| 278 | ): { x: number; y: number } { |
| 279 | return { |
| 280 | x: Math.round(x / stepX) * stepX, |
| 281 | y: Math.round(y / stepY) * stepY |
| 282 | }; |
| 283 | } |
| 284 | }; |
| 285 | } |
| 286 | |
| 287 | /** |
| 288 | * Create a minimum size constraint. |
no outgoing calls
no test coverage detected
searching dependent graphs…