@pmndrs/handle
Version:
framework agnostic expandable handle implementation for threejs
33 lines (32 loc) • 1.37 kB
JavaScript
import { PerspectiveCamera, Vector2 } from 'three';
import { defaultScreenCameraApply } from './camera.js';
import { ScreenHandleStore } from './store.js';
import { average } from './utils.js';
const vector2Helper = new Vector2();
const initialHelper = new Vector2();
export class RotateScreenHandleStore extends ScreenHandleStore {
filter;
customApply;
speed;
constructor(store, getCamera, filter, customApply, speed) {
super(([initialPitch, initialYaw], map) => {
if (!this.filter(map)) {
return;
}
average(vector2Helper, map, 'currentScreenPosition');
average(initialHelper, map, 'initialScreenPosition');
vector2Helper.sub(initialHelper).multiplyScalar(-Math.PI * (this.speed ?? 1));
const camera = getCamera();
const aspect = camera instanceof PerspectiveCamera
? camera.aspect
: (camera.right - camera.left) / (camera.top - camera.bottom);
(this.customApply ?? defaultScreenCameraApply)({
pitch: initialPitch - vector2Helper.y,
yaw: initialYaw + vector2Helper.x * aspect,
}, store);
}, () => [store.getState().pitch, store.getState().yaw]);
this.filter = filter;
this.customApply = customApply;
this.speed = speed;
}
}