在手机上操作SVG,想要对某一个坐标点在屏幕可视区的中间显示
代码如下
//SVG图层的大小
const viewBoxWidth = 13269;
const viewBoxHeight = 7475;
//屏幕宽高(svg宽高)
const svgWidth = window.innerWidth;
const svgHeight = window.innerHeight;
//this.viewFocusObj 坐标点
let pX = this.viewFocusObj.x / viewBoxWidth * svgWidth;
let pY = this.viewFocusObj.y / viewBoxHeight * svgHeight;
var clientRect = document.querySelector('svg').getBoundingClientRect()
//坐标点转换后PX,对PX中心点的偏移量
var x1 = -pX + clientRect.width / 2
var y1 = -pY + clientRect.height / 2
// return { viewX:x1*3 ,viewY:y1}
// scale(3) 是SVG缩放的比例 没需求可以不要 (*3)
svg.style.transform = 'scale(3)'
svg.style.left = `${x1 * 3}px`;
svg.style.top = `${y1}px`