@orca-fe/pocket
Version:
UI components by orca-team
236 lines (226 loc) • 11.2 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
exports.matrix2Viewport3d = matrix2Viewport3d;
var _glMatrix = require("gl-matrix");
var _tools = require("@orca-fe/tools");
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
var round01 = (0, _tools.roundBy)(0.1);
var round001 = (0, _tools.roundBy)(0.01);
function rad2deg(rad) {
return rad * 180 / Math.PI;
}
function deg2rad(deg) {
return deg * Math.PI / 180;
}
function matrix2Viewport3d(matrix) {
var scale = Math.hypot(matrix[0], matrix[1]);
var viewport = {
left: round01(matrix[12]),
top: round01(matrix[13]),
zoom: round001(Math.log2(scale)),
rotate: round01(rad2deg(Math.atan2(matrix[1], matrix[0]))),
pitch: rad2deg(Math.atan2(matrix[6], matrix[10]))
};
return viewport;
}
var Viewport3d = exports.default = /*#__PURE__*/function () {
function Viewport3d() {
var props = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
_classCallCheck(this, Viewport3d);
_defineProperty(this, "left", void 0);
_defineProperty(this, "top", void 0);
_defineProperty(this, "zoom", void 0);
_defineProperty(this, "rotate", void 0);
_defineProperty(this, "pitch", void 0);
var _props$left = props.left,
left = _props$left === void 0 ? 0 : _props$left,
_props$top = props.top,
top = _props$top === void 0 ? 0 : _props$top,
_props$zoom = props.zoom,
zoom = _props$zoom === void 0 ? 0 : _props$zoom,
_props$rotate = props.rotate,
rotate = _props$rotate === void 0 ? 0 : _props$rotate,
_props$pitch = props.pitch,
pitch = _props$pitch === void 0 ? 0 : _props$pitch;
this.left = left;
this.top = top;
this.zoom = zoom;
this.rotate = rotate;
this.pitch = pitch;
}
_createClass(Viewport3d, [{
key: "setViewport",
value: function setViewport(viewport) {
var left = viewport.left,
top = viewport.top,
zoom = viewport.zoom,
rotate = viewport.rotate,
pitch = viewport.pitch;
if (left != null) this.left = left;
if (top != null) this.top = top;
if (zoom != null) this.zoom = zoom;
if (rotate != null) this.rotate = rotate;
if (pitch != null) this.pitch = pitch;
return this;
}
}, {
key: "getViewport",
value: function getViewport() {
return {
left: this.left,
top: this.top,
zoom: this.zoom,
rotate: this.rotate,
pitch: this.pitch
};
}
// 将 Viewport3d 转换为 mat4
}, {
key: "toMatrix",
value: function toMatrix() {
var matrix = _glMatrix.mat4.create();
_glMatrix.mat4.translate(matrix, matrix, [this.left, this.top, 0]);
_glMatrix.mat4.scale(matrix, matrix, [Math.pow(2, this.zoom), Math.pow(2, this.zoom), 1]);
_glMatrix.mat4.rotateZ(matrix, matrix, deg2rad(this.rotate));
_glMatrix.mat4.rotateX(matrix, matrix, deg2rad(this.pitch));
return matrix;
}
// 将 mat4 转换为 Viewport3d
}, {
key: "fromMatrix",
value: function fromMatrix(matrix) {
var viewport = matrix2Viewport3d(matrix);
this.left = viewport.left;
this.top = viewport.top;
this.zoom = viewport.zoom;
this.rotate = viewport.rotate;
this.pitch = viewport.pitch;
return this;
}
}, {
key: "toInverseMatrix",
value: function toInverseMatrix() {
var matrix = this.toMatrix();
var inverseMatrix = _glMatrix.mat4.create();
_glMatrix.mat4.invert(inverseMatrix, matrix);
return inverseMatrix;
}
/**
* world -> screen
*/
}, {
key: "project",
value: function project(point) {
var _point = _slicedToArray(point, 2),
x = _point[0],
y = _point[1];
var result = _glMatrix.vec3.create();
_glMatrix.vec3.transformMat4(result, _glMatrix.vec3.fromValues(x, y, 0), this.toMatrix());
var _ref = [result[0], result[1]],
left = _ref[0],
top = _ref[1];
return [left, top];
}
/**
* screen -> world
*/
}, {
key: "unproject",
value: function unproject(point) {
var _point2 = _slicedToArray(point, 2),
left = _point2[0],
top = _point2[1];
var result = _glMatrix.vec3.create();
_glMatrix.vec3.transformMat4(result, [left, top, 0], this.toInverseMatrix());
var _ref2 = [result[0], result[1]],
x = _ref2[0],
y = _ref2[1];
return [x, y];
}
/**
* 将世界坐标点对齐至屏幕坐标点(平移)并返回新的 Viewport
* @param originWorldPoint 原始世界坐标点
* @param screenPoint 屏幕坐标点
*/
}, {
key: "getViewportByScreenPoint",
value: function getViewportByScreenPoint(originWorldPoint, screenPoint) {
// 计算原始坐标点映射在屏幕的位置
var originScreenPoint = this.project(originWorldPoint);
// 计算屏幕偏移量
var offset = _glMatrix.vec3.create();
var originScreenVec3 = _glMatrix.vec3.fromValues(originScreenPoint[0], originScreenPoint[1], 0);
var targetScreenVec3 = _glMatrix.vec3.fromValues(screenPoint[0], screenPoint[1], 0);
_glMatrix.vec3.sub(offset, targetScreenVec3, originScreenVec3);
// 得出偏移后的矩阵
var matTranslate = _glMatrix.mat4.create();
_glMatrix.mat4.translate(matTranslate, matTranslate, offset);
var matrix = this.toMatrix();
_glMatrix.mat4.multiply(matrix, matTranslate, matrix);
return matrix2Viewport3d(matrix);
}
/**
* 根据屏幕点和缩放级别计算 Viewport
* @param screenPoint
* @param zoom
*/
}, {
key: "getViewportByZoom",
value: function getViewportByZoom(screenPoint, zoom) {
// 计算当前屏幕点映射的世界坐标点
var worldPoint = this.unproject(screenPoint);
var viewport3d = new Viewport3d(_objectSpread(_objectSpread({}, this.getViewport()), {}, {
zoom: zoom
}));
var vp = viewport3d.pan(worldPoint, screenPoint).getViewport();
return vp;
}
}, {
key: "getViewportByRotate",
value: function getViewportByRotate(screenPoint, deg) {
var matrix = this.toMatrix();
// 创建旋转矩阵
var matrixRotation = _glMatrix.mat4.create();
_glMatrix.mat4.translate(matrixRotation, matrixRotation, [screenPoint[0], screenPoint[1], 0]);
_glMatrix.mat4.rotateZ(matrixRotation, matrixRotation, deg2rad(deg));
_glMatrix.mat4.translate(matrixRotation, matrixRotation, [-screenPoint[0], -screenPoint[1], 0]);
// 计算旋转后的矩阵
_glMatrix.mat4.multiply(matrix, matrixRotation, matrix);
return matrix2Viewport3d(matrix);
}
}, {
key: "pan",
value: function pan(originWorldPoint, screenPoint) {
var newViewport = this.getViewportByScreenPoint(originWorldPoint, screenPoint);
// 更新 Viewport
this.setViewport(newViewport);
return this;
}
}, {
key: "zoomBy",
value: function zoomBy(screenPoint, zoom) {
var newViewport = this.getViewportByZoom(screenPoint, zoom);
// 更新 Viewport
this.setViewport(newViewport);
return this;
}
}]);
return Viewport3d;
}();