UNPKG

convex-pixel

Version:

The library for creating pseudo 3d interactive scenes based on webgl

54 lines (53 loc) 2.71 kB
import { Vector2D } from "../../utils/geom/vector"; const GAIN = 4; export class CameraGyroscopeController { constructor(appContext) { this.appContext = appContext; this._handlerOrientationEvent = (event) => { if (!this.camera) { throw Error(`Property "camera" is not defined.`); } const sceneBounds = this.camera.room.roomBound; const sWidth = sceneBounds.width * this.camera.room.scale.x; const sHeight = sceneBounds.height * this.camera.room.scale.y; const x = event.gamma ? event.gamma : (event.x / 90) * this.camera.viewport.width; const y = event.beta ? event.beta : (event.y / 90) * this.camera.viewport.width; const 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 { const xx = (this._initialX - x) * GAIN; const 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)); } const sx = (sWidth - this.camera.viewport.width) / (this.camera.viewport.width / xx); const 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); } destroy() { window.removeEventListener("deviceorientation", this._handlerOrientationEvent); this.onMove = this.onPOV = this.onZoom = this.onRotate = undefined; } } //# sourceMappingURL=camera-gyroscope-controller.js.map