157 lines
5.9 KiB
JavaScript

"use strict";
const common_vendor = require("../../common/vendor.js");
class GestureHandler {
constructor(context, transformMatrix, { container }) {
this.transformMatrix = transformMatrix;
this.containerRect = container;
this.panThreshold = 5;
this.panStarted = false;
const atOptions = {
getPoint: (touch) => ({
x: touch.clientX - this.containerRect.left,
y: touch.clientY - this.containerRect.top
}),
preventDefault: false
};
if (typeof window === "undefined" || typeof HTMLElement === "undefined") {
atOptions._element = {
style: {},
addEventListener: () => {
},
removeEventListener: () => {
}
};
}
try {
this.at = new common_vendor.i(atOptions);
} catch (e) {
common_vendor.index.__f__("error", "at pages/index/gesture-handler.js:32", "AnyTouch初始化失败:", e);
this.handleGesture = this.createSimpleGestureHandler();
}
if (this.at) {
this.setupGestures();
common_vendor.index.__f__("log", "at pages/index/gesture-handler.js:39", "AnyTouch手势处理器已初始化");
} else {
common_vendor.index.__f__("warn", "at pages/index/gesture-handler.js:41", "使用简化手势处理器");
}
}
// 创建小程序专用的简化手势处理器
createSimpleGestureHandler() {
return {
run: (event) => {
const touches = event.touches || [];
if (touches.length === 1) {
this._handlePan(touches[0]);
} else if (touches.length > 1) {
this._handlePinch(touches);
}
}
};
}
setupGestures() {
this.at.on("pan", (event) => {
if (event.type === "panstart") {
common_vendor.index.__f__("log", "at pages/index/gesture-handler.js:67", "panstart", event);
this.panStarted = false;
} else if (event.type === "panmove") {
const distance = Math.sqrt(event.deltaX ** 2 + event.deltaY ** 2);
if (!this.panStarted && distance < this.panThreshold) {
return;
}
if (!this.panStarted) {
common_vendor.index.__f__("log", "at pages/index/gesture-handler.js:80", "拖动手势开始");
this.panStarted = true;
}
const currentScale = Math.sqrt(
this.transformMatrix.a * this.transformMatrix.a + this.transformMatrix.b * this.transformMatrix.b
);
const dx = event.deltaX / currentScale;
const dy = event.deltaY / currentScale;
common_vendor.index.__f__("log", "at pages/index/gesture-handler.js:95", `平移: deltaX=${event.deltaX}, deltaY=${event.deltaY} | 修正后: dx=${dx}, dy=${dy}`);
this.transformMatrix.translate(dx, dy);
} else if (event.type === "panend") {
this.panStarted = false;
}
});
this.at.on("pinch", (event) => {
if (event.type === "pinchstart") {
this.lastScale = 1;
} else if (event.type === "pinchmove") {
const scaleChange = event.scale / this.lastScale;
this.lastScale = event.scale;
const centerX = event.center.x;
const centerY = event.center.y;
common_vendor.index.__f__("log", "at pages/index/gesture-handler.js:117", `缩放: scale=${event.scale} | 变化=${scaleChange} | 中心点: (${centerX}, ${centerY})`);
this.transformMatrix.scale(scaleChange, scaleChange, centerX, centerY);
}
});
this.at.on("tap", (event) => {
common_vendor.index.__f__("log", "at pages/index/gesture-handler.js:126", "点击事件", event);
});
this.at.on("press", (event) => {
common_vendor.index.__f__("log", "at pages/index/gesture-handler.js:131", "长按事件", event);
});
}
// 公共接口保持不变
catchEvent(event) {
try {
if (this.at) {
this.at.run(event);
} else {
const touches = event.touches || [];
const eventType = event.type;
if (eventType === "touchstart") {
this.lastPoint = null;
this.lastPoints = null;
this.lastDistance = null;
}
if (touches.length === 1) {
this._handlePan(touches[0]);
} else if (touches.length > 1) {
this._handlePinch(touches);
}
}
} catch (e) {
common_vendor.index.__f__("error", "at pages/index/gesture-handler.js:161", "手势处理错误:", e);
}
}
// 基础平移手势处理
_handlePan(touch) {
const pointX = touch.x || touch.clientX - this.containerRect.left;
const pointY = touch.y || touch.clientY - this.containerRect.top;
if (this.lastPoint) {
const dx = pointX - this.lastPoint.x;
const dy = pointY - this.lastPoint.y;
this.transformMatrix.translate(dx, dy);
}
this.lastPoint = { x: pointX, y: pointY };
}
// 基础缩放手势处理
_handlePinch(touches) {
const point1 = touches[0];
const point2 = touches[1];
const getPoint = (touch) => ({
x: touch.x || touch.clientX - this.containerRect.left,
y: touch.y || touch.clientY - this.containerRect.top
});
const currentPoints = [getPoint(point1), getPoint(point2)];
if (this.lastPoints) {
const currentDistance = Math.sqrt(
Math.pow(currentPoints[1].x - currentPoints[0].x, 2) + Math.pow(currentPoints[1].y - currentPoints[0].y, 2)
);
const prevDistance = Math.sqrt(
Math.pow(this.lastPoints[1].x - this.lastPoints[0].x, 2) + Math.pow(this.lastPoints[1].y - this.lastPoints[0].y, 2)
);
if (prevDistance > 0 && currentDistance > 0) {
const scaleChange = currentDistance / prevDistance;
const centerX = (currentPoints[0].x + currentPoints[1].x) / 2;
const centerY = (currentPoints[0].y + currentPoints[1].y) / 2;
this.transformMatrix.scale(scaleChange, scaleChange, centerX, centerY);
}
}
this.lastPoints = currentPoints;
}
}
exports.GestureHandler = GestureHandler;
//# sourceMappingURL=../../../.sourcemap/mp-weixin/pages/index/gesture-handler.js.map