All files / src/components/world/entities/scenes/cursor getDragOffset.ts

55.56% Statements 5/9
0% Branches 0/4
50% Functions 1/2
42.86% Lines 3/7

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 261x                             1x           3x        
export const getDragOffset = (
  element: HTMLElement,
  clientX: number,
  clientY: number,
) => {
  const rect = element.getBoundingClientRect();
  const scaleX = element.offsetWidth ? rect.width / element.offsetWidth : 1;
  const scaleY = element.offsetHeight ? rect.height / element.offsetHeight : 1;
 
  return {
    x: Math.max(0, Math.floor((clientX - rect.left) / scaleX)),
    y: Math.max(0, Math.floor((clientY - rect.top) / scaleY)),
  };
};
 
export const applyDragOffset = (
  pointerX: number,
  pointerY: number,
  offsetX: number,
  offsetY: number,
  unitSize: number,
) => ({
  x: pointerX - Math.floor(offsetX / unitSize),
  y: pointerY - Math.floor(offsetY / unitSize),
});