( event: Event | ChartEvent | TouchEvent | MouseEvent, chart: Chart | PrivateChart )
| 103 | */ |
| 104 | |
| 105 | export function getRelativePosition( |
| 106 | event: Event | ChartEvent | TouchEvent | MouseEvent, |
| 107 | chart: Chart | PrivateChart |
| 108 | ): { x: number; y: number } { |
| 109 | if ('native' in event) { |
| 110 | return event; |
| 111 | } |
| 112 | |
| 113 | const {canvas, currentDevicePixelRatio} = chart; |
| 114 | const style = getComputedStyle(canvas); |
| 115 | const borderBox = style.boxSizing === 'border-box'; |
| 116 | const paddings = getPositionedStyle(style, 'padding'); |
| 117 | const borders = getPositionedStyle(style, 'border', 'width'); |
| 118 | const {x, y, box} = getCanvasPosition(event, canvas); |
| 119 | const xOffset = paddings.left + (box && borders.left); |
| 120 | const yOffset = paddings.top + (box && borders.top); |
| 121 | |
| 122 | let {width, height} = chart; |
| 123 | if (borderBox) { |
| 124 | width -= paddings.width + borders.width; |
| 125 | height -= paddings.height + borders.height; |
| 126 | } |
| 127 | return { |
| 128 | x: Math.round((x - xOffset) / width * canvas.width / currentDevicePixelRatio), |
| 129 | y: Math.round((y - yOffset) / height * canvas.height / currentDevicePixelRatio) |
| 130 | }; |
| 131 | } |
| 132 | |
| 133 | function getContainerSize(canvas: HTMLCanvasElement, width: number, height: number): Partial<Scale> { |
| 134 | let maxWidth: number, maxHeight: number; |
no test coverage detected