2025-08-12 18:01:11 +08:00

184 lines
6.5 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;
this.isMiniProgram = typeof common_vendor.wx$1 !== "undefined" || typeof common_vendor.index !== "undefined";
if (this.isMiniProgram) {
common_vendor.index.__f__("log", "at pages/index/gesture-handler.js:13", "小程序环境,使用降级手势处理器");
this.catchEvent = this.createSimpleGestureHandler();
return;
}
try {
const atOptions = {
getPoint: (touch) => ({
x: touch.clientX - this.containerRect.left,
y: touch.clientY - this.containerRect.top
}),
preventDefault: false
};
atOptions.element = {
style: {},
addEventListener: () => {
},
removeEventListener: () => {
},
ownerDocument: {
documentElement: {
style: {}
}
}
};
"../../common/vendor.js".then((n) => n.index_es).then((AnyTouch) => {
this.at = new AnyTouch.default(atOptions);
this.setupGestures();
common_vendor.index.__f__("log", "at pages/index/gesture-handler.js:44", "AnyTouch手势处理器已初始化");
}).catch((e) => {
common_vendor.index.__f__("error", "at pages/index/gesture-handler.js:46", "AnyTouch加载失败:", e);
this.catchEvent = this.createSimpleGestureHandler();
});
} catch (e) {
common_vendor.index.__f__("error", "at pages/index/gesture-handler.js:50", "AnyTouch初始化失败:", e);
this.catchEvent = this.createSimpleGestureHandler();
}
}
// 创建小程序专用的简化手势处理器
createSimpleGestureHandler() {
let isClick = true;
let startPoint = null;
let startTime = null;
let lastPoint = null;
return (event) => {
const touches = event.touches || [];
if (touches.length === 1) {
const getPoint = (t2) => ({
x: t2.x || t2.clientX - this.containerRect.left,
y: t2.y || t2.clientY - this.containerRect.top
});
const currentPoint = getPoint(touches[0]);
if (!startPoint) {
startPoint = currentPoint;
startTime = Date.now();
}
const dx = currentPoint.x - startPoint.x;
const dy = currentPoint.y - startPoint.y;
const distance = Math.sqrt(dx * dx + dy * dy);
if (isClick && distance < 5 && Date.now() - startTime < 200) {
return;
}
isClick = false;
if (lastPoint) {
const moveDx = currentPoint.x - lastPoint.x;
const moveDy = currentPoint.y - lastPoint.y;
this.transformMatrix.tx += moveDx;
this.transformMatrix.ty += moveDy;
}
lastPoint = currentPoint;
} else if (touches.length > 1) {
this._handlePinch(touches);
}
if (event.type === "touchend" || event.type === "touchcancel") {
isClick = true;
startPoint = null;
startTime = null;
lastPoint = null;
}
};
}
setupGestures() {
this.at.on("pan", (event) => {
if (event.type === "panstart") {
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) {
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;
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;
this.transformMatrix.scale(scaleChange, scaleChange, centerX, centerY);
}
});
}
// 公共接口
catchEvent(event) {
if (this.lastTouchCount !== event.touches.length) {
this.panStarted = false;
this.lastPoints = null;
}
this.lastTouchCount = event.touches.length;
try {
if (this.at) {
this.at.run(event);
} else if (this.catchEvent) {
this.catchEvent(event);
}
} catch (e) {
common_vendor.index.__f__("error", "at pages/index/gesture-handler.js:178", "手势处理错误:", e);
}
}
// 基础平移手势处理
_handlePan(touch) {
const getPoint = (t2) => ({
x: t2.x || t2.clientX - this.containerRect.left,
y: t2.y || t2.clientY - this.containerRect.top
});
const currentPoint = getPoint(touch);
if (this.lastPoint) {
const dx = currentPoint.x - this.lastPoint.x;
const dy = currentPoint.y - this.lastPoint.y;
this.transformMatrix.tx += dx;
this.transformMatrix.ty += dy;
}
this.lastPoint = currentPoint;
}
// 基础缩放手势处理
_handlePinch(touches) {
const point1 = touches[0];
const point2 = touches[1];
const getPoint = (touch) => ({
x: touch.x || touch.clientX - this.containerRect.left,
y: touch.y || t.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