javascript - React SVG coordinate system mismatch - cursor position doesn't match where elements are rendered - Stack Ov

I'm building a floor plan application in React where users can place desk icons by clicking on an

I'm building a floor plan application in React where users can place desk icons by clicking on an SVG floor plan. However, I'm encountering a coordinate system mismatch where desks aren't appearing where users click.

The Issue

  • When a user clicks on the floor plan, the desk appears offset from the cursor position. The offset isn't consistent - it seems to increase the further the click is from the center of the SVG.
  • For example, when I click at position x:22.5%, y:17.7%, the desk appears at approximately x:30.6%, y:17.7% - showing a horizontal offset of about 8.1% that grows larger the further I click from the center.

Technical Details

  • Floor plan is an SVG with viewBox="0 0 2200 1700" and a group transformation transform="translate(0, 1700) scale(0.1, -0.1)"
  • I'm calculating click position as percentage of container width/height:

const x = ((e.clientX - rect.left) / rect.width) * 100;
const y = ((e.clientY - rect.top) / rect.height) * 100;

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744955275a4603155.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信