convex-pixel
Version:
The library for creating pseudo 3d interactive scenes based on webgl
74 lines • 3.13 kB
JavaScript
import { Vector2D } from "../../utils/geom";
export class CameraMouseController {
constructor(appContext) {
this.appContext = appContext;
this._cameraTarget = new Vector2D();
this._handlerStageTouchMove = (event) => {
this.calculate(event.data.global.x, event.data.global.y);
};
this._handlerStageMouseMove = (event) => {
this.calculate(event.data.global.x, event.data.global.y);
};
appContext.pixi.stage.interactive = true;
appContext.pixi.stage.on("mousemove", this._handlerStageMouseMove);
appContext.pixi.stage.on("touchmove", this._handlerStageTouchMove);
}
initialize() {
if (!this.camera) {
throw Error(`Property "camera" is not defined`);
}
this._handlerStageMouseMove({
data: {
global: {
x: this.camera.viewport.width * 0.5,
y: this.camera.viewport.height * 0.5,
},
},
});
}
destroy() {
this.appContext.pixi.stage.off("mousemove", this._handlerStageMouseMove);
this.appContext.pixi.stage.off("touchmove", this._handlerStageTouchMove);
this.onMove = this.onPOV = this.onZoom = this.onRotate = undefined;
}
calculate(x, y) {
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 xx = this.camera.viewport.width * 0.5 - x;
const yy = this.camera.viewport.height * 0.5 - y;
/*if (x > this.camera.viewport.width) xx = this.camera.viewport.width;
else if (x < 0) xx = 0;
if (y > this.camera.viewport.height) yy = this.camera.viewport.height;
else if (y < 0) yy = 0;*/
// const sign = inverse ? -1 : 1;
// let sx =
// this.camera.viewport.width * 0.5 -
// xx /* *
// this.camera.maxXOffset * sign*/;
// let sy =
// this.camera.viewport.height * 0.5 -
// yy /* *
// this.camera.maxYOffset * sign*/;
/*let povX = -sx * 0.0075 * this.camera.povFactor;
let povY = sy * 0.0075 * this.camera.povFactor;
if (Math.abs(povX) > this.camera.maxXOffset) {
povX = this.camera.maxXOffset * Math.sign(povX);
}
if (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) * (x / this.camera.viewport.width);
const sy = (sHeight - this.camera.viewport.height) * (y / this.camera.viewport.height);
if (this.onMove) {
this.onMove(Vector2D.new(sx, sy));
}
}
}
//# sourceMappingURL=camera-mouse-controller.js.map