MCPcopy Index your code
hub / github.com/gridstack/gridstack.js / getCellFromPixel

Method getCellFromPixel

src/gridstack.ts:1179–1197  ·  view source on GitHub ↗

* Get the position of the cell under a pixel on screen. * @param position the position of the pixel to resolve in * absolute coordinates, as an object with top and left properties * @param useDocRelative if true, value will be based on document position vs parent position (Optional. Default

(position: MousePosition, useDocRelative = false)

Source from the content-addressed store, hash-verified

1177 * Returns an object with properties `x` and `y` i.e. the column and row in the grid.
1178 */
1179 public getCellFromPixel(position: MousePosition, useDocRelative = false): CellPosition {
1180 const box = this.el.getBoundingClientRect();
1181 // console.log(`getBoundingClientRect left: ${box.left} top: ${box.top} w: ${box.w} h: ${box.h}`)
1182 let containerPos: { top: number, left: number };
1183 if (useDocRelative) {
1184 containerPos = { top: box.top + document.documentElement.scrollTop, left: box.left };
1185 // console.log(`getCellFromPixel scrollTop: ${document.documentElement.scrollTop}`)
1186 } else {
1187 containerPos = { top: this.el.offsetTop, left: this.el.offsetLeft }
1188 // console.log(`getCellFromPixel offsetTop: ${containerPos.left} offsetLeft: ${containerPos.top}`)
1189 }
1190 const relativeLeft = position.left - containerPos.left;
1191 const relativeTop = position.top - containerPos.top;
1192
1193 const columnWidth = (box.width / this.getColumn());
1194 const rowHeight = (box.height / parseInt(this.el.getAttribute('gs-current-row')));
1195
1196 return { x: Math.floor(relativeLeft / columnWidth), y: Math.floor(relativeTop / rowHeight) };
1197 }
1198
1199 /**
1200 * Returns the current number of rows, which will be at least `minRow` if set.

Callers 1

Calls 1

getColumnMethod · 0.95

Tested by

no test coverage detected