UNPKG

@react-vertex/orbit-camera

Version:
154 lines (130 loc) 3.92 kB
import _slicedToArray from "@babel/runtime/helpers/slicedToArray"; import _classCallCheck from "@babel/runtime/helpers/classCallCheck"; import _createClass from "@babel/runtime/helpers/createClass"; export var OrbitControls = /*#__PURE__*/function () { function OrbitControls(camera, element) { var _this = this; _classCallCheck(this, OrbitControls); this.camera = void 0; this.element = void 0; this.dollyStep = void 0; this.dollyCurr = void 0; this.dragging = false; this.distance = null; this.x = 0; this.y = 0; this.lastX = 0; this.lastY = 0; this.rotationSpeed = 1000; this.onMouseUp = function (e) { e.stopPropagation(); e.preventDefault(); _this.dragging = false; _this.distance = null; }; this.onTouchEnd = function (e) { e.stopPropagation(); e.preventDefault(); _this.dragging = false; _this.distance = null; }; this.onMouseDown = function (e) { _this.dragging = true; e.stopPropagation(); e.preventDefault(); _this.x = e.pageX; _this.y = e.pageY; }; this.onTouchStart = function (e) { var _e$touches = _slicedToArray(e.touches, 2), t1 = _e$touches[0], t2 = _e$touches[1]; e.stopPropagation(); e.preventDefault(); if (t2) { var a = t1.pageX - t2.pageX; var b = t1.pageY - t2.pageY; _this.distance = Math.sqrt(a * a + b * b); } else { _this.x = t1.pageX; _this.y = t1.pageY; _this.dragging = true; } }; this.onMouseMove = function (e) { _this.lastX = _this.x; _this.lastY = _this.y; e.stopPropagation(); e.preventDefault(); _this.x = e.pageX; _this.y = e.pageY; if (!_this.dragging) return; var dx = _this.lastX - _this.x; var dy = _this.lastY - _this.y; _this.rotate(dx, dy); }; this.onTouchMove = function (e) { var _e$touches2 = _slicedToArray(e.touches, 2), t1 = _e$touches2[0], t2 = _e$touches2[1]; e.stopPropagation(); e.preventDefault(); if (t2) { if (!_this.distance) { return; } var a = t1.pageX - t2.pageX; var b = t1.pageY - t2.pageY; var nextDistance = Math.sqrt(a * a + b * b); if (_this.distance > nextDistance) { _this.dollyCurr -= _this.dollyStep; } else { _this.dollyCurr += _this.dollyStep; } _this.camera.dolly(_this.dollyCurr); _this.distance = nextDistance; } else if (_this.dragging) { _this.lastX = _this.x; _this.lastY = _this.y; _this.x = t1.pageX; _this.y = t1.pageY; var dx = _this.lastX - _this.x; var dy = _this.lastY - _this.y; _this.rotate(dx, dy); } }; this.onMouseWheel = function (e) { e.stopPropagation(); e.preventDefault(); _this.dolly(e.deltaY); }; this.camera = camera; this.element = element; this.dollyStep = Math.max(this.camera.position[0], this.camera.position[1], this.camera.position[2]) / 100; this.dollyCurr = 0; } _createClass(OrbitControls, [{ key: "dolly", value: function dolly(value) { if (value < 0) { this.dollyCurr += this.dollyStep; } else { this.dollyCurr -= this.dollyStep; } this.camera.dolly(this.dollyCurr); } }, { key: "rotate", value: function rotate(dx, dy) { var _this$element = this.element, width = _this$element.width, height = _this$element.height; var incX = dx / width * this.rotationSpeed; var incY = dy / height * this.rotationSpeed; dx && this.camera.incRotationY(incX); dy && this.camera.incRotationX(incY); } }]); return OrbitControls; }(); //# sourceMappingURL=OrbitControls.js.map