137 lines
5.1 KiB
JavaScript

"use strict";
const common_vendor = require("../../common/vendor.js");
class GestureHandler {
constructor(context, transformMatrix, { container }) {
this.transformMatrix = transformMatrix;
this.containerRect = container;
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:30", "AnyTouch初始化失败:", e);
this.handleGesture = this.createSimpleGestureHandler();
}
if (this.at) {
this.setupGestures();
common_vendor.index.__f__("log", "at pages/index/gesture-handler.js:37", "AnyTouch手势处理器已初始化");
} else {
common_vendor.index.__f__("warn", "at pages/index/gesture-handler.js:39", "使用简化手势处理器");
}
}
// 创建小程序专用的简化手势处理器
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:65", "panstart", event);
} else if (event.type === "panmove") {
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:78", `平移: deltaX=${event.deltaX}, deltaY=${event.deltaY} | 修正后: dx=${dx}, dy=${dy}`);
this.transformMatrix.translate(dx, dy);
}
});
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:98", `缩放: 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:107", "点击事件", event);
});
this.at.on("press", (event) => {
common_vendor.index.__f__("log", "at pages/index/gesture-handler.js:112", "长按事件", event);
});
}
catchEvent(event) {
try {
if (this.at) {
this.at.run(event);
} else if (this.handleGesture) {
this.handleGesture.run(event);
}
} catch (e) {
common_vendor.index.__f__("error", "at pages/index/gesture-handler.js:125", "手势处理错误:", e);
const touches = event.touches || [];
if (touches.length > 0) {
this.handlePan(touches[0]);
}
}
}
// 基础平移手势处理
handlePan(touch) {
if (this.lastPoint) {
const dx = touch.clientX - this.lastPoint.x;
const dy = touch.clientY - this.lastPoint.y;
const currentScale = Math.sqrt(
this.transformMatrix.a * this.transformMatrix.a + this.transformMatrix.b * this.transformMatrix.b
);
const correctedDx = dx / currentScale;
const correctedDy = dy / currentScale;
this.transformMatrix.translate(correctedDx, correctedDy);
}
this.lastPoint = { x: touch.clientX, y: touch.clientY };
}
// 基础缩放手势处理
handlePinch(touches) {
const point1 = touches[0];
const point2 = touches[1];
if (this.lastPoints) {
const currentDistance = Math.sqrt(
Math.pow(point2.clientX - point1.clientX, 2) + Math.pow(point2.clientY - point1.clientY, 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) {
const scaleChange = currentDistance / prevDistance;
const centerX = (point1.clientX + point2.clientX) / 2 - this.containerRect.left;
const centerY = (point1.clientY + point2.clientY) / 2 - this.containerRect.top;
this.transformMatrix.scale(scaleChange, scaleChange, centerX, centerY);
}
}
this.lastPoints = [
{ x: point1.clientX, y: point1.clientY },
{ x: point2.clientX, y: point2.clientY }
];
}
}
exports.GestureHandler = GestureHandler;
//# sourceMappingURL=../../../.sourcemap/mp-weixin/pages/index/gesture-handler.js.map