convex-pixel
Version:
The library for creating pseudo 3d interactive scenes based on webgl
57 lines (56 loc) • 2.92 kB
JavaScript
import { Vector2D } from "../../utils/geom/vector";
var GAIN = 4;
var CameraGyroscopeController = /** @class */ (function () {
function CameraGyroscopeController(appContext) {
var _this = this;
this.appContext = appContext;
this._handlerOrientationEvent = function (event) {
if (!_this.camera) {
throw Error("Property \"camera\" is not defined.");
}
var sceneBounds = _this.camera.room.roomBound;
var sWidth = sceneBounds.width * _this.camera.room.scale.x;
var sHeight = sceneBounds.height * _this.camera.room.scale.y;
var x = event.gamma ? event.gamma : (event.x / 90) * _this.camera.viewport.width;
var y = event.beta ? event.beta : (event.y / 90) * _this.camera.viewport.width;
var z = event.alpha ? event.alpha : (event.z / 90) * _this.camera.viewport.height;
if (_this._initialX === undefined || _this._initialY === undefined || _this._initialZ === undefined) {
_this._initialX = x;
_this._initialY = y;
_this._initialZ = z;
}
else {
var xx = (_this._initialX - x) * GAIN;
var yy = (_this._initialY - y) * GAIN;
/*
let sx = xx * this.camera.xOffset * 20;
let sy = yy * this.camera.yOffset * 20;
let povX = sx * 0.0075 * this.camera.povFactor;
let povY = sy * 0.0075 * this.camera.povFactor;
if (this.camera.maxXOffset && Math.abs(povX) > this.camera.maxXOffset) {
povX = this.camera.maxXOffset * Math.sign(povX);
}
if (this.camera.maxYOffset && Math.abs(povY) > this.camera.maxYOffset) {
povY = this.camera.maxYOffset * Math.sign(povY);
}*/
if (_this.onPOV) {
_this.onPOV(Vector2D.new(xx, yy));
}
var sx = (sWidth - _this.camera.viewport.width) / (_this.camera.viewport.width / xx);
var sy = (sHeight - _this.camera.viewport.height) / (_this.camera.viewport.height / yy);
if (_this.onMove) {
_this.onMove(Vector2D.new(sx, sy));
}
}
};
appContext.pixi.stage.interactive = true;
window.addEventListener("deviceorientation", this._handlerOrientationEvent, true);
}
CameraGyroscopeController.prototype.destroy = function () {
window.removeEventListener("deviceorientation", this._handlerOrientationEvent);
this.onMove = this.onPOV = this.onZoom = this.onRotate = undefined;
};
return CameraGyroscopeController;
}());
export { CameraGyroscopeController };
//# sourceMappingURL=camera-gyroscope-controller.js.map