@react-vertex/orbit-camera
Version:
Orbit Camera and Controls for React Vertex
166 lines (137 loc) • 4.31 kB
JavaScript
"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.OrbitControls = void 0;
var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
var OrbitControls = /*#__PURE__*/function () {
function OrbitControls(camera, element) {
var _this = this;
(0, _classCallCheck2["default"])(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 = (0, _slicedToArray2["default"])(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 = (0, _slicedToArray2["default"])(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;
}
(0, _createClass2["default"])(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;
}();
exports.OrbitControls = OrbitControls;
//# sourceMappingURL=OrbitControls.js.map