qwc2
Version:
QGIS Web Client
181 lines (180 loc) • 9.6 kB
JavaScript
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 _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); }
function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } }
function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; }
function _callSuper(t, o, e) { return o = _getPrototypeOf(o), _possibleConstructorReturn(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], _getPrototypeOf(t).constructor) : o.apply(t, e)); }
function _possibleConstructorReturn(t, e) { if (e && ("object" == _typeof(e) || "function" == typeof e)) return e; if (void 0 !== e) throw new TypeError("Derived constructors may only return object or undefined"); return _assertThisInitialized(t); }
function _assertThisInitialized(e) { if (void 0 === e) throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); return e; }
function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); }
function _superPropGet(t, o, e, r) { var p = _get(_getPrototypeOf(1 & r ? t.prototype : t), o, e); return 2 & r && "function" == typeof p ? function (t) { return p.apply(e, t); } : p; }
function _get() { return _get = "undefined" != typeof Reflect && Reflect.get ? Reflect.get.bind() : function (e, t, r) { var p = _superPropBase(e, t); if (p) { var n = Object.getOwnPropertyDescriptor(p, t); return n.get ? n.get.call(arguments.length < 3 ? e : r) : n.value; } }, _get.apply(null, arguments); }
function _superPropBase(t, o) { for (; !{}.hasOwnProperty.call(t, o) && null !== (t = _getPrototypeOf(t));); return t; }
function _getPrototypeOf(t) { return _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function (t) { return t.__proto__ || Object.getPrototypeOf(t); }, _getPrototypeOf(t); }
function _inherits(t, e) { if ("function" != typeof e && null !== e) throw new TypeError("Super expression must either be null or a function"); t.prototype = Object.create(e && e.prototype, { constructor: { value: t, writable: !0, configurable: !0 } }), Object.defineProperty(t, "prototype", { writable: !1 }), e && _setPrototypeOf(t, e); }
function _setPrototypeOf(t, e) { return _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function (t, e) { return t.__proto__ = e, t; }, _setPrototypeOf(t, e); }
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : 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); }
/**
* Copyright 2025 Sourcepole AG
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
*/
import { Vector3 } from 'three';
import { MapControls } from 'three/addons/controls/MapControls';
import { v4 as uuidv4 } from 'uuid';
var OrbitControls3D = /*#__PURE__*/function (_MapControls) {
function OrbitControls3D(object, mouseButtons) {
var _this;
_classCallCheck(this, OrbitControls3D);
_this = _callSuper(this, OrbitControls3D, [object]);
_defineProperty(_this, "updateControlsTarget", function () {
var _this$sceneContext$ge;
if (_this.animationId) {
// Do nothing if animating
return;
}
var camerapos = _this.object.position;
var x = _this.target.x;
var y = _this.target.y;
var height = (_this$sceneContext$ge = _this.sceneContext.getTerrainHeightFromMap([x, y])) !== null && _this$sceneContext$ge !== void 0 ? _this$sceneContext$ge : 0;
// If camera height is at terrain height, target height should be at terrain height
// If camera height is at twice the terrain height or further, target height should be zero
var newHeight = Math.max(0, 1 - (camerapos.z - height) / height) * height;
_this.target.z = newHeight;
});
_defineProperty(_this, "stopAnimations", function () {
_this.animationId = null;
});
_this.animationId = null;
_this.sceneContext = null;
_this.enabled = false;
_this.zoomToCursor = true;
_this.enableDamping = true;
_this.dampingFactor = 0.2;
_this.keyPanSpeed = 10.0;
_this.maxPolarAngle = Math.PI * 0.5;
_this.mouseButtons = mouseButtons;
_this.zoomSpeed = 5;
return _this;
}
_inherits(OrbitControls3D, _MapControls);
return _createClass(OrbitControls3D, [{
key: "connect",
value: function connect(sceneContext) {
this.domElement = sceneContext.scene.domElement;
this.sceneContext = sceneContext;
this.enabled = true;
_superPropGet(OrbitControls3D, "connect", this, 3)([]);
this.listenToKeyEvents(this.domElement);
this.domElement.addEventListener('pointerdown', this.stopAnimations);
this.domElement.addEventListener('wheel', this.stopAnimations);
this.addEventListener('change', this.updateControlsTarget);
this.object.near = 2;
this.sceneContext.scene.view.setControls(this);
}
}, {
key: "disconnect",
value: function disconnect() {
this.animationId = null;
_superPropGet(OrbitControls3D, "disconnect", this, 3)([]);
this.enabled = false;
this.sceneContext.scene.view.setControls(null);
this.domElement.removeEventListener('pointerdown', this.stopAnimations);
this.domElement.removeEventListener('wheel', this.stopAnimations);
this.removeEventListener('change', this.updateControlsTarget);
}
}, {
key: "setView",
value: function setView(camerapos, target) {
this.object.position.copy(camerapos);
this.target.copy(target);
this.update();
}
}, {
key: "panView",
value: function panView(dx, dy) {
if (dx || dy) {
this._pan(-dx * 10, dy * 10);
this.update();
}
}
}, {
key: "tiltView",
value: function tiltView(azimuth, polar) {
if (azimuth) {
this._rotateLeft(azimuth);
}
if (polar) {
this._rotateUp(polar);
}
if (azimuth || polar) {
this.update();
}
}
}, {
key: "zoomView",
value: function zoomView(delta) {
if (delta > 0) {
this._dollyIn(this._getZoomScale(-delta));
this.update();
} else if (delta < 0) {
this._dollyOut(this._getZoomScale(-delta));
this.update();
}
}
}, {
key: "animateTo",
value: function animateTo(camerapos, target, azimuth) {
var _this2 = this;
var callback = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : null;
var oldPosition = this.object.position.clone();
var oldTarget = this.target.clone();
var oldYaw = this.getAzimuthalAngle();
var newPosition = camerapos;
var newTarget = target;
var rotateAngle = -oldYaw + azimuth;
while (rotateAngle > Math.PI) rotateAngle -= 2 * Math.PI;
while (rotateAngle < -Math.PI) rotateAngle += 2 * Math.PI;
var startTime = new Date() / 1000;
var animationId = uuidv4();
this.animationId = animationId;
this.enableDamping = false;
var _animate = function animate() {
if (_this2.animationId !== animationId) {
return;
}
var duration = 2;
var elapsed = new Date() / 1000 - startTime;
var x = elapsed / duration;
var k = 0.5 * (1 - Math.cos(x * Math.PI));
var currentPosition = new Vector3().lerpVectors(oldPosition, newPosition, k);
var currentTarget = new Vector3().lerpVectors(oldTarget, newTarget, k);
currentPosition.x -= currentTarget.x;
currentPosition.y -= currentTarget.y;
currentPosition.applyAxisAngle(new Vector3(0, 0, 1), rotateAngle * k);
currentPosition.x += currentTarget.x;
currentPosition.y += currentTarget.y;
_this2.object.position.copy(currentPosition);
_this2.target.copy(currentTarget);
_this2.update();
if (elapsed < duration) {
requestAnimationFrame(_animate);
} else {
_this2.object.position.copy(newPosition);
_this2.target.copy(newTarget);
_this2.update();
_this2._rotateLeft(_this2.getAzimuthalAngle() - azimuth);
_this2.update();
_this2.enableDamping = true;
_this2.animationId = null;
callback === null || callback === void 0 || callback();
}
};
requestAnimationFrame(_animate);
}
}]);
}(MapControls);
export { OrbitControls3D as default };