( positionParams: PositionParams, top: number, left: number, w: number, h: number )
| 174 | * @returns x and y in grid units |
| 175 | */ |
| 176 | export function calcXY( |
| 177 | positionParams: PositionParams, |
| 178 | top: number, |
| 179 | left: number, |
| 180 | w: number, |
| 181 | h: number |
| 182 | ): { x: number; y: number } { |
| 183 | const { margin, containerPadding, cols, rowHeight, maxRows } = positionParams; |
| 184 | const colWidth = calcGridColWidth(positionParams); |
| 185 | |
| 186 | // left = containerPaddingX + x * (colWidth + marginX) |
| 187 | // x = (left - containerPaddingX) / (colWidth + marginX) |
| 188 | let x = Math.round((left - containerPadding[0]) / (colWidth + margin[0])); |
| 189 | let y = Math.round((top - containerPadding[1]) / (rowHeight + margin[1])); |
| 190 | |
| 191 | // Clamp to grid bounds |
| 192 | x = clamp(x, 0, cols - w); |
| 193 | y = clamp(y, 0, maxRows - h); |
| 194 | |
| 195 | return { x, y }; |
| 196 | } |
| 197 | |
| 198 | /** |
| 199 | * Translate pixel coordinates to grid units without clamping. |
no test coverage detected
searching dependent graphs…