* @param e * @param canvas * @returns Canvas position
( e: Event | TouchEvent | MouseEvent, canvas: HTMLCanvasElement )
| 71 | * @returns Canvas position |
| 72 | */ |
| 73 | function getCanvasPosition( |
| 74 | e: Event | TouchEvent | MouseEvent, |
| 75 | canvas: HTMLCanvasElement |
| 76 | ): { |
| 77 | x: number; |
| 78 | y: number; |
| 79 | box: boolean; |
| 80 | } { |
| 81 | const touches = (e as TouchEvent).touches; |
| 82 | const source = (touches && touches.length ? touches[0] : e) as MouseEvent; |
| 83 | const {offsetX, offsetY} = source as MouseEvent; |
| 84 | let box = false; |
| 85 | let x, y; |
| 86 | if (useOffsetPos(offsetX, offsetY, e.target)) { |
| 87 | x = offsetX; |
| 88 | y = offsetY; |
| 89 | } else { |
| 90 | const rect = canvas.getBoundingClientRect(); |
| 91 | x = source.clientX - rect.left; |
| 92 | y = source.clientY - rect.top; |
| 93 | box = true; |
| 94 | } |
| 95 | return {x, y, box}; |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Gets an event's x, y coordinates, relative to the chart area |
no test coverage detected