cross-gesture
Version:
Gesture lib for the modern browsers
64 lines • 2.51 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getCrossPoint = exports.getDirectionAndSpeed = exports.getRotateDeg = exports.pinchTimes = exports.vectorDot = exports.vectorAbs = void 0;
var types_1 = require("./types");
function vectorAbs(v) {
return Math.sqrt(v.x * v.x + v.y * v.y);
}
exports.vectorAbs = vectorAbs;
function vectorDot(f, t) {
return f.x * t.x + f.y * t.y;
}
exports.vectorDot = vectorDot;
// 返回缩放的倍数
function pinchTimes(from, to) {
var fromLen = Math.sqrt(Math.pow(Math.abs(from[0].x - from[1].x), 2) + Math.pow(Math.abs(from[0].y - from[1].y), 2));
var toLen = Math.sqrt(Math.pow(Math.abs(to[0].x - to[1].x), 2) + Math.pow(Math.abs(to[0].y - to[1].y), 2));
return toLen / fromLen;
}
exports.pinchTimes = pinchTimes;
// 返回旋转的角度
// 顺时针为正数
function getRotateDeg(from, to) {
var fromVector = { x: from[1].x - from[0].x, y: from[1].y - from[0].y };
var toVector = { x: to[1].x - to[0].x, y: to[1].y - to[0].y };
// direction: https://blog.csdn.net/qq_42778110/article/details/81567954
var direction = fromVector.x * toVector.y - fromVector.y * toVector.x;
if (direction === 0) {
return 0;
}
// a * b = |a|*|b|*cos(deg)
// cos(deg) = (a * b) / (|a| * |b|)
var cosDeg = vectorDot(fromVector, toVector) / (vectorAbs(fromVector) * vectorAbs(toVector));
return (direction > 0 ? 1 : -1) * Math.acos(cosDeg);
}
exports.getRotateDeg = getRotateDeg;
// 返回滑动反向和速度
function getDirectionAndSpeed(from, to, duration) {
var distanceX = to.x - from.x;
var distanceY = to.y - from.y;
return {
vDirection: distanceX > 0 ? types_1.SwipeDirection.right : types_1.SwipeDirection.left,
vSpeed: distanceX / duration,
hDirection: distanceY > 0 ? types_1.SwipeDirection.bottom : types_1.SwipeDirection.top,
hSpeed: distanceY / duration,
};
}
exports.getDirectionAndSpeed = getDirectionAndSpeed;
function getCrossPoint(from, to) {
// k = (y2 - y1) / (x2 - x1)
// b = y1 - k * x1
var k1 = (from[1].y - from[0].y) / (from[1].x - from[0].x);
var b1 = from[0].y - k1 * from[0].x;
var k2 = (to[1].y - to[0].y) / (to[1].x - to[0].x);
var b2 = to[0].y - k2 * to[0].x;
// x = (b1 - b2) / (k2 - k1)
// y = k1x + b1
var x = (b1 - b2) / (k2 - k1);
return {
x: x,
y: k1 * x + b1,
};
}
exports.getCrossPoint = getCrossPoint;
//# sourceMappingURL=utils.js.map