index.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', { value: true });
  3. var gLite = require('@antv/g-lite');
  4. var util = require('@antv/util');
  5. var glMatrix = require('gl-matrix');
  6. function _inheritsLoose(subClass, superClass) {
  7. subClass.prototype = Object.create(superClass.prototype);
  8. subClass.prototype.constructor = subClass;
  9. _setPrototypeOf(subClass, superClass);
  10. }
  11. function _setPrototypeOf(o, p) {
  12. _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function _setPrototypeOf(o, p) {
  13. o.__proto__ = p;
  14. return o;
  15. };
  16. return _setPrototypeOf(o, p);
  17. }
  18. /**
  19. * Provides camera action & animation.
  20. */
  21. var AdvancedCamera = /*#__PURE__*/function (_Camera) {
  22. _inheritsLoose(AdvancedCamera, _Camera);
  23. function AdvancedCamera() {
  24. var _this;
  25. for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
  26. args[_key] = arguments[_key];
  27. }
  28. _this = _Camera.call.apply(_Camera, [this].concat(args)) || this;
  29. /**
  30. * switch between multiple landmarks
  31. */
  32. _this.landmarks = [];
  33. _this.landmarkAnimationID = void 0;
  34. return _this;
  35. }
  36. var _proto = AdvancedCamera.prototype;
  37. /**
  38. * Changes the azimuth and elevation with respect to the current camera axes
  39. * @param {Number} azimuth the relative azimuth
  40. * @param {Number} elevation the relative elevation
  41. * @param {Number} roll the relative roll
  42. */
  43. _proto.rotate = function rotate(azimuth, elevation, roll) {
  44. this.relElevation = gLite.getAngle(elevation);
  45. this.relAzimuth = gLite.getAngle(azimuth);
  46. this.relRoll = gLite.getAngle(roll);
  47. this.elevation += this.relElevation;
  48. this.azimuth += this.relAzimuth;
  49. this.roll += this.relRoll;
  50. if (this.type === gLite.CameraType.EXPLORING) {
  51. var rotX = glMatrix.quat.setAxisAngle(glMatrix.quat.create(), [1, 0, 0], gLite.deg2rad((this.rotateWorld ? 1 : -1) * this.relElevation));
  52. var rotY = glMatrix.quat.setAxisAngle(glMatrix.quat.create(), [0, 1, 0], gLite.deg2rad((this.rotateWorld ? 1 : -1) * this.relAzimuth));
  53. var rotZ = glMatrix.quat.setAxisAngle(glMatrix.quat.create(), [0, 0, 1], gLite.deg2rad(this.relRoll));
  54. var rotQ = glMatrix.quat.multiply(glMatrix.quat.create(), rotY, rotX);
  55. rotQ = glMatrix.quat.multiply(glMatrix.quat.create(), rotQ, rotZ);
  56. var rotMatrix = glMatrix.mat4.fromQuat(glMatrix.mat4.create(), rotQ);
  57. glMatrix.mat4.translate(this.matrix, this.matrix, [0, 0, -this.distance]);
  58. glMatrix.mat4.multiply(this.matrix, this.matrix, rotMatrix);
  59. glMatrix.mat4.translate(this.matrix, this.matrix, [0, 0, this.distance]);
  60. } else {
  61. if (Math.abs(this.elevation) > 90) {
  62. return this;
  63. }
  64. this.computeMatrix();
  65. }
  66. this._getAxes();
  67. if (this.type === gLite.CameraType.ORBITING || this.type === gLite.CameraType.EXPLORING) {
  68. this._getPosition();
  69. } else if (this.type === gLite.CameraType.TRACKING) {
  70. this._getFocalPoint();
  71. }
  72. this._update();
  73. return this;
  74. }
  75. /**
  76. * 沿水平(right) & 垂直(up)平移相机
  77. */;
  78. _proto.pan = function pan(tx, ty) {
  79. var coords = gLite.createVec3(tx, ty, 0);
  80. var pos = glMatrix.vec3.clone(this.position);
  81. glMatrix.vec3.add(pos, pos, glMatrix.vec3.scale(glMatrix.vec3.create(), this.right, coords[0]));
  82. glMatrix.vec3.add(pos, pos, glMatrix.vec3.scale(glMatrix.vec3.create(), this.up, coords[1]));
  83. this._setPosition(pos);
  84. this.triggerUpdate();
  85. return this;
  86. }
  87. /**
  88. * 沿 n 轴移动,当距离视点远时移动速度较快,离视点越近速度越慢
  89. */;
  90. _proto.dolly = function dolly(value) {
  91. var n = this.forward;
  92. var pos = glMatrix.vec3.clone(this.position);
  93. var step = value * this.dollyingStep;
  94. var updatedDistance = this.distance + value * this.dollyingStep;
  95. // 限制视点距离范围
  96. step = Math.max(Math.min(updatedDistance, this.maxDistance), this.minDistance) - this.distance;
  97. pos[0] += step * n[0];
  98. pos[1] += step * n[1];
  99. pos[2] += step * n[2];
  100. this._setPosition(pos);
  101. if (this.type === gLite.CameraType.ORBITING || this.type === gLite.CameraType.EXPLORING) {
  102. // 重新计算视点距离
  103. this._getDistance();
  104. } else if (this.type === gLite.CameraType.TRACKING) {
  105. // 保持视距,移动视点位置
  106. glMatrix.vec3.add(this.focalPoint, pos, this.distanceVector);
  107. }
  108. this.triggerUpdate();
  109. return this;
  110. };
  111. _proto.cancelLandmarkAnimation = function cancelLandmarkAnimation() {
  112. if (this.landmarkAnimationID !== undefined) {
  113. this.canvas.cancelAnimationFrame(this.landmarkAnimationID);
  114. }
  115. };
  116. _proto.createLandmark = function createLandmark(name, params) {
  117. var _position$, _position$2, _focalPoint$, _focalPoint$2;
  118. if (params === void 0) {
  119. params = {};
  120. }
  121. var _params = params,
  122. _params$position = _params.position,
  123. position = _params$position === void 0 ? this.position : _params$position,
  124. _params$focalPoint = _params.focalPoint,
  125. focalPoint = _params$focalPoint === void 0 ? this.focalPoint : _params$focalPoint,
  126. roll = _params.roll,
  127. zoom = _params.zoom;
  128. var camera = new gLite.runtime.CameraContribution();
  129. camera.setType(this.type, undefined);
  130. camera.setPosition(position[0], (_position$ = position[1]) !== null && _position$ !== void 0 ? _position$ : this.position[1], (_position$2 = position[2]) !== null && _position$2 !== void 0 ? _position$2 : this.position[2]);
  131. camera.setFocalPoint(focalPoint[0], (_focalPoint$ = focalPoint[1]) !== null && _focalPoint$ !== void 0 ? _focalPoint$ : this.focalPoint[1], (_focalPoint$2 = focalPoint[2]) !== null && _focalPoint$2 !== void 0 ? _focalPoint$2 : this.focalPoint[2]);
  132. camera.setRoll(roll !== null && roll !== void 0 ? roll : this.roll);
  133. camera.setZoom(zoom !== null && zoom !== void 0 ? zoom : this.zoom);
  134. var landmark = {
  135. name: name,
  136. matrix: glMatrix.mat4.clone(camera.getWorldTransform()),
  137. right: glMatrix.vec3.clone(camera.right),
  138. up: glMatrix.vec3.clone(camera.up),
  139. forward: glMatrix.vec3.clone(camera.forward),
  140. position: glMatrix.vec3.clone(camera.getPosition()),
  141. focalPoint: glMatrix.vec3.clone(camera.getFocalPoint()),
  142. distanceVector: glMatrix.vec3.clone(camera.getDistanceVector()),
  143. distance: camera.getDistance(),
  144. dollyingStep: camera.getDollyingStep(),
  145. azimuth: camera.getAzimuth(),
  146. elevation: camera.getElevation(),
  147. roll: camera.getRoll(),
  148. relAzimuth: camera.relAzimuth,
  149. relElevation: camera.relElevation,
  150. relRoll: camera.relRoll,
  151. zoom: camera.getZoom()
  152. };
  153. this.landmarks.push(landmark);
  154. return landmark;
  155. };
  156. _proto.gotoLandmark = function gotoLandmark(name, options) {
  157. var _this2 = this;
  158. if (options === void 0) {
  159. options = {};
  160. }
  161. var landmark = util.isString(name) ? this.landmarks.find(function (l) {
  162. return l.name === name;
  163. }) : name;
  164. if (landmark) {
  165. var _ref = util.isNumber(options) ? {
  166. duration: options
  167. } : options,
  168. _ref$easing = _ref.easing,
  169. easing = _ref$easing === void 0 ? 'linear' : _ref$easing,
  170. _ref$duration = _ref.duration,
  171. duration = _ref$duration === void 0 ? 100 : _ref$duration,
  172. _ref$easingFunction = _ref.easingFunction,
  173. easingFunction = _ref$easingFunction === void 0 ? undefined : _ref$easingFunction,
  174. _ref$onfinish = _ref.onfinish,
  175. onfinish = _ref$onfinish === void 0 ? undefined : _ref$onfinish;
  176. var epsilon = 0.01;
  177. if (duration === 0) {
  178. this.syncFromLandmark(landmark);
  179. if (onfinish) {
  180. onfinish();
  181. }
  182. return;
  183. }
  184. // cancel ongoing animation
  185. this.cancelLandmarkAnimation();
  186. var destPosition = landmark.position;
  187. var destFocalPoint = landmark.focalPoint;
  188. var destZoom = landmark.zoom;
  189. var destRoll = landmark.roll;
  190. var easingFunc = easingFunction || gLite.runtime.EasingFunction(easing);
  191. var timeStart;
  192. var endAnimation = function endAnimation() {
  193. _this2.setFocalPoint(destFocalPoint);
  194. _this2.setPosition(destPosition);
  195. _this2.setRoll(destRoll);
  196. _this2.setZoom(destZoom);
  197. _this2.computeMatrix();
  198. _this2.triggerUpdate();
  199. if (onfinish) {
  200. onfinish();
  201. }
  202. };
  203. var animate = function animate(timestamp) {
  204. if (timeStart === undefined) {
  205. timeStart = timestamp;
  206. }
  207. var elapsed = timestamp - timeStart;
  208. if (elapsed > duration) {
  209. endAnimation();
  210. return;
  211. }
  212. // use the same ease function in animation system
  213. var t = easingFunc(elapsed / duration);
  214. var interFocalPoint = glMatrix.vec3.create();
  215. var interPosition = glMatrix.vec3.create();
  216. var interZoom = 1;
  217. var interRoll = 0;
  218. glMatrix.vec3.lerp(interFocalPoint, _this2.focalPoint, destFocalPoint, t);
  219. glMatrix.vec3.lerp(interPosition, _this2.position, destPosition, t);
  220. interRoll = _this2.roll * (1 - t) + destRoll * t;
  221. interZoom = _this2.zoom * (1 - t) + destZoom * t;
  222. _this2.setFocalPoint(interFocalPoint);
  223. _this2.setPosition(interPosition);
  224. _this2.setRoll(interRoll);
  225. _this2.setZoom(interZoom);
  226. var dist = glMatrix.vec3.dist(interFocalPoint, destFocalPoint) + glMatrix.vec3.dist(interPosition, destPosition);
  227. if (dist <= epsilon && destZoom == undefined && destRoll == undefined) {
  228. endAnimation();
  229. return;
  230. }
  231. _this2.computeMatrix();
  232. _this2.triggerUpdate();
  233. if (elapsed < duration) {
  234. _this2.landmarkAnimationID = _this2.canvas.requestAnimationFrame(animate);
  235. }
  236. };
  237. this.canvas.requestAnimationFrame(animate);
  238. }
  239. };
  240. _proto.syncFromLandmark = function syncFromLandmark(landmark) {
  241. this.matrix = glMatrix.mat4.copy(this.matrix, landmark.matrix);
  242. this.right = glMatrix.vec3.copy(this.right, landmark.right);
  243. this.up = glMatrix.vec3.copy(this.up, landmark.up);
  244. this.forward = glMatrix.vec3.copy(this.forward, landmark.forward);
  245. this.position = glMatrix.vec3.copy(this.position, landmark.position);
  246. this.focalPoint = glMatrix.vec3.copy(this.focalPoint, landmark.focalPoint);
  247. this.distanceVector = glMatrix.vec3.copy(this.distanceVector, landmark.distanceVector);
  248. this.azimuth = landmark.azimuth;
  249. this.elevation = landmark.elevation;
  250. this.roll = landmark.roll;
  251. this.relAzimuth = landmark.relAzimuth;
  252. this.relElevation = landmark.relElevation;
  253. this.relRoll = landmark.relRoll;
  254. this.dollyingStep = landmark.dollyingStep;
  255. this.distance = landmark.distance;
  256. this.zoom = landmark.zoom;
  257. };
  258. return AdvancedCamera;
  259. }(gLite.Camera);
  260. gLite.runtime.CameraContribution = AdvancedCamera;
  261. exports.AdvancedCamera = AdvancedCamera;