@@ -193,12 +193,16 @@ const loadImageAndInit = async () => {
193193
194194 await nextTick ()
195195 if (editImgRef .value ) {
196- // 设置canvas的像素宽高
196+ // 重置为简单明确的设置方式,避免多重缩放问题
197+
198+ // 设置canvas的实际尺寸与其CSS尺寸相同
197199 editImgRef .value .width = drawWidth
198200 editImgRef .value .height = drawHeight
199- // 设置canvas的样式宽高
200- editImgRef .value .style .width = ' 100%'
201- editImgRef .value .style .height = ' auto'
201+
202+ // 设置canvas的CSS尺寸
203+ editImgRef .value .style .width = ` ${ drawWidth} px`
204+ editImgRef .value .style .height = ` ${ drawHeight} px`
205+
202206 const ctx = editImgRef .value .getContext (' 2d' )
203207 ctx .clearRect (0 , 0 , drawWidth, drawHeight)
204208 ctx .drawImage (img, 0 , 0 , drawWidth, drawHeight)
@@ -249,45 +253,59 @@ function onTouchStart(e) {
249253 if (isMobile .value && scrollMode .value ) return
250254 e .preventDefault ()
251255 const touch = e .touches [0 ]
252- const rect = editImgRef .value .getBoundingClientRect ()
253- const x = touch .clientX - rect .left
254- const y = touch .clientY - rect .top
256+ const { x , y } = getCanvasCoordinates (touch .clientX , touch .clientY )
255257 handlePointerStart (x, y)
256258}
257259
258260function onTouchMove (e ) {
259261 if (isMobile .value && scrollMode .value ) return
260262 e .preventDefault ()
261263 const touch = e .touches [0 ]
262- const rect = editImgRef .value .getBoundingClientRect ()
263- const x = touch .clientX - rect .left
264- const y = touch .clientY - rect .top
264+ const { x , y } = getCanvasCoordinates (touch .clientX , touch .clientY )
265265 handlePointerMove (x, y)
266266}
267267
268- function onTouchEnd (e ) {
269- if (isMobile .value && scrollMode .value ) return
270- e .preventDefault ()
271- handlePointerEnd ()
272- }
273-
274268// 鼠标事件处理
275269function onMouseDown (e ) {
276270 if (! imageLoaded .value || (isMobile .value && scrollMode .value )) return
277- const rect = editImgRef .value .getBoundingClientRect ()
278- const x = e .clientX - rect .left
279- const y = e .clientY - rect .top
271+ const { x , y } = getCanvasCoordinates (e .clientX , e .clientY )
280272 handlePointerStart (x, y)
281273}
282274
283275function onMouseMove (e ) {
284276 if (! imageLoaded .value || (isMobile .value && scrollMode .value )) return
285- const rect = editImgRef .value .getBoundingClientRect ()
286- const x = e .clientX - rect .left
287- const y = e .clientY - rect .top
277+ const { x , y } = getCanvasCoordinates (e .clientX , e .clientY )
288278 handlePointerMove (x, y)
289279}
290280
281+ /**
282+ * 准确地将客户端坐标转换为画布坐标
283+ * @param {number} clientX - 客户端X坐标
284+ * @param {number} clientY - 客户端Y坐标
285+ * @returns {{x: number, y: number}} - 画布坐标
286+ */
287+ function getCanvasCoordinates (clientX , clientY ) {
288+ // 获取canvas元素在页面中的位置
289+ const rect = editImgRef .value .getBoundingClientRect ()
290+
291+ // 计算点击位置相对于canvas元素左上角的偏移
292+ // 这里需要考虑canvas的CSS尺寸与实际尺寸的比例
293+ const scaleX = editImgRef .value .width / rect .width
294+ const scaleY = editImgRef .value .height / rect .height
295+
296+ // 转换为canvas内部坐标
297+ const x = (clientX - rect .left ) * scaleX
298+ const y = (clientY - rect .top ) * scaleY
299+
300+ return { x, y }
301+ }
302+
303+ function onTouchEnd (e ) {
304+ if (isMobile .value && scrollMode .value ) return
305+ e .preventDefault ()
306+ handlePointerEnd ()
307+ }
308+
291309function onMouseUp () {
292310 if (isMobile .value && scrollMode .value ) return
293311 handlePointerEnd ()
@@ -518,8 +536,13 @@ function isInText(x, y, m) {
518536function drawAll () {
519537 if (! editImgRef .value || ! imageObj .value ) return
520538 const ctx = editImgRef .value .getContext (' 2d' )
521- ctx .clearRect (0 , 0 , canvasWidth .value , canvasHeight .value )
522- ctx .drawImage (imageObj .value , 0 , 0 , canvasWidth .value , canvasHeight .value )
539+
540+ // 使用Canvas的实际尺寸进行绘制
541+ const renderWidth = editImgRef .value .width
542+ const renderHeight = editImgRef .value .height
543+
544+ ctx .clearRect (0 , 0 , renderWidth, renderHeight)
545+ ctx .drawImage (imageObj .value , 0 , 0 , renderWidth, renderHeight)
523546 markers .value .forEach (m => {
524547 if (m .type === ' rect' ) {
525548 drawRect (ctx, m .startX , m .startY , m .endX , m .endY )
0 commit comments