I looked for other questions, but none of them matched my case.
I have a canvas element:
<canvas id="linear-synoptic-map" width="1053px" height="1000px" ng-click="linearSynopticCtrl.canvasClicked($event)" ng-mousemove="linearSynopticCtrl.mouseMovedOverCanvas($event)"> </canvas>
And I get the position using this function:
linearSynopticCtrl.getPositionFromEvent = function (event) { var rect = linearSynopticCtrl.canvas.getBoundingClientRect(); var x = event.x - rect.left; var y = event.y - rect.top; return new Point(x,y); };
The problem is that the canvas must be responsive, so I added the following css rule:
canvas#linear-synoptic-map { width: 100%; }
When resizing occurs (when the canvas size is squeezed above the definition or enlarged by definition: 1053x1000), a space starts to appear between the correct mouse position and the function returned.
I also tried to get a position with this approach:
linearSynopticCtrl.getPositionFromEvent = function (event) { var x = event.x - linearSynopticCtrl.canvas.offsetLeft; var y = event.y - linearSynopticCtrl.canvas.offsetTop; return new Point(x,y); };
But I get much worse results.
Does anyone know how to solve this problem?
source share