注意:
1、组件支持横屏,此横屏时手机的自适应横屏,是竖屏顺时针旋转90度的横屏;
2、组件支持设置svg触摸开始时的回调方法;
3、组件支持设置svg缩放比例变化时的回调方法;
/**
* @author: yiwenli
* @desc: svg移动缩放
* @date: 2023/8/15
*/
class EwlPanZoom {
/**
* 构造器
*
* @param {Element} svgElement svg元素
* @param {Object} panZoomConfig 移动缩放配置
*/
constructor(svgElement, panZoomConfig = {
}) {
// 1、属性和变量初始化
// svg元素
this.svgElement = svgElement;
// viewBox初始信息
const {
width, height } = this.svgElement.viewBox.animVal;
this.viewBoxInitWidth = parseFloat(width);
this.viewBoxInitHeight = parseFloat(height);
// viewBox当前信息
this.viewBoxCurrentX = '';
this.viewBoxCurrentY = '';
this.viewBoxCurrentWidth = '';
this.viewBoxCurrentHeight = '';
// 是否是移动状态
this.isMoveStatus = false;
// 移动开始坐标
this.startX = '';
this.startY = '';
// 缩放开始时两手指的距离
this.startDistance = '';
// 是否横屏 横屏认为是顺时针旋转90度(默认非横屏)
this.isHorizontal = panZoomConfig.isHorizontal || false;
// 2、添加回调函数属性
// 缩放倍数修改回调函数
this.onZoomChange = panZoomConfig.onZoomChange;
// 触摸开始回调函数
this.onTouchStart = panZoomConfig.onTouchStart;
// 3、执行初始化
this.init();
}
/**
* 初始化
*/
init() {
this.svgElement.addEventListener('touchstart', this