Canvas 鼠标画图参考点改变我们放大

Canvas Refrence point of drawing using mouse change we zoom in

我想使用 canvas 在图像上书写,但是当我缩放图像时,鼠标指针发生变化,它没有写在正确的位置

请在我需要更改代码的地方帮助我 提前致谢

您可以做的一种方法是计算比例随着放大增加多少,然后将 x 和 y 坐标乘以该比例

 var rect = canvas.getBoundingClientRect();  // abs. size of element
 scaleX = canvas.width / rect.width;  // relationship bitmap vs. element for X
 scaleY = canvas.height / rect.height; // relationship bitmap vs. element for Y
 mouseX = mouseX*scaleX;
 mouseY = mouseY*scaleY;