Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/add rest and transitionstart events #207

Merged
merged 7 commits into from
Aug 30, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 18 additions & 15 deletions dist/CameraControls.d.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import * as _THREE from 'three';
import type * as _THREE from 'three';
import { THREESubset, ACTION, PointerInput, MouseButtons, Touches, FitToOptions, CameraControlsEventMap } from './types';
import { EventDispatcher } from './EventDispatcher';
export declare class CameraControls extends EventDispatcher {
static install(libs: {
THREE: THREESubset;
}): void;
static readonly ACTION: Readonly<typeof ACTION>;
static get ACTION(): Readonly<typeof ACTION>;
minPolarAngle: number;
maxPolarAngle: number;
minAzimuthAngle: number;
Expand All @@ -25,6 +25,7 @@ export declare class CameraControls extends EventDispatcher {
dragToOffset: boolean;
verticalDragToForward: boolean;
boundaryFriction: number;
restEpsilon: number;
colliderMeshes: _THREE.Object3D[];
mouseButtons: MouseButtons;
touches: Touches;
Expand All @@ -50,26 +51,28 @@ export declare class CameraControls extends EventDispatcher {
protected _focalOffset0: _THREE.Vector3;
protected _dollyControlAmount: number;
protected _dollyControlCoord: _THREE.Vector2;
protected _nearPlaneCorners: [
_THREE.Vector3,
_THREE.Vector3,
_THREE.Vector3,
_THREE.Vector3
];
protected _nearPlaneCorners: [_THREE.Vector3, _THREE.Vector3, _THREE.Vector3, _THREE.Vector3];
protected _hasRested: boolean;
protected _boundary: _THREE.Box3;
protected _boundaryEnclosesCamera: boolean;
protected _needsUpdate: boolean;
protected _updatedLastTime: boolean;
protected _elementRect: _THREE.Vector4;
protected _activePointers: PointerInput[];
constructor(camera: _THREE.PerspectiveCamera | _THREE.OrthographicCamera, domElement: HTMLElement);
camera: _THREE.PerspectiveCamera | _THREE.OrthographicCamera;
enabled: boolean;
readonly currentAction: ACTION;
distance: number;
azimuthAngle: number;
polarAngle: number;
boundaryEnclosesCamera: boolean;
get camera(): _THREE.PerspectiveCamera | _THREE.OrthographicCamera;
set camera(camera: _THREE.PerspectiveCamera | _THREE.OrthographicCamera);
get enabled(): boolean;
set enabled(enabled: boolean);
get currentAction(): ACTION;
get distance(): number;
set distance(distance: number);
get azimuthAngle(): number;
set azimuthAngle(azimuthAngle: number);
get polarAngle(): number;
set polarAngle(polarAngle: number);
get boundaryEnclosesCamera(): boolean;
set boundaryEnclosesCamera(boundaryEnclosesCamera: boolean);
addEventListener<K extends keyof CameraControlsEventMap>(type: K, listener: (event: CameraControlsEventMap[K]) => any): void;
removeEventListener<K extends keyof CameraControlsEventMap>(type: K, listener: (event: CameraControlsEventMap[K]) => any): void;
rotate(azimuthAngle: number, polarAngle: number, enableTransition?: boolean): void;
Expand Down
59 changes: 59 additions & 0 deletions dist/camera-controls.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@
function approxZero(number) {
return Math.abs(number) < EPSILON;
}
function lessThanEpsilon(number, EPS) {
return Math.abs(number) < EPS;
}
function approxEquals(a, b) {
return approxZero(a - b);
}
Expand Down Expand Up @@ -211,12 +214,14 @@
_this.dragToOffset = false;
_this.verticalDragToForward = false;
_this.boundaryFriction = 0.0;
_this.restEpsilon = 0.0025;
_this.colliderMeshes = [];
_this.cancel = function () { };
_this._enabled = true;
_this._state = ACTION.NONE;
_this._viewport = null;
_this._dollyControlAmount = 0;
_this._hasRested = false;
_this._boundaryEnclosesCamera = false;
_this._needsUpdate = true;
_this._updatedLastTime = false;
Expand Down Expand Up @@ -557,6 +562,7 @@
break;
}
}
_this._hasRested = false;
_this.dispatchEvent({ type: 'control' });
};
var onContextMenu_1 = function (event) {
Expand All @@ -581,6 +587,7 @@
var y = (_this._activePointers[0].clientY + _this._activePointers[1].clientY) * 0.5;
lastDragPosition_1.set(x, y);
}
_this._hasRested = false;
_this.dispatchEvent({ type: 'controlstart' });
};
var dragging_1 = function () {
Expand Down Expand Up @@ -643,6 +650,7 @@
break;
}
}
_this._hasRested = false;
_this.dispatchEvent({ type: 'control' });
};
var endDragging_1 = function () {
Expand Down Expand Up @@ -831,6 +839,10 @@
this._spherical.theta = this._sphericalEnd.theta;
this._spherical.phi = this._sphericalEnd.phi;
}
else {
this._hasRested = false;
this.dispatchEvent({ type: 'transitionstart' });
}
this._needsUpdate = true;
};
CameraControls.prototype.dolly = function (distance, enableTransition) {
Expand All @@ -856,6 +868,10 @@
if (!enableTransition) {
this._spherical.radius = this._sphericalEnd.radius;
}
else {
this._hasRested = false;
this.dispatchEvent({ type: 'transitionstart' });
}
this._needsUpdate = true;
};
CameraControls.prototype.zoom = function (zoomStep, enableTransition) {
Expand All @@ -868,6 +884,10 @@
if (!enableTransition) {
this._zoom = this._zoomEnd;
}
else {
this._hasRested = false;
this.dispatchEvent({ type: 'transitionstart' });
}
this._needsUpdate = true;
};
CameraControls.prototype.pan = function (x, y, enableTransition) {
Expand All @@ -887,6 +907,10 @@
if (!enableTransition) {
this._target.copy(this._targetEnd);
}
else {
this._hasRested = false;
this.dispatchEvent({ type: 'transitionstart' });
}
this._needsUpdate = true;
};
CameraControls.prototype.forward = function (distance, enableTransition) {
Expand All @@ -898,6 +922,10 @@
if (!enableTransition) {
this._target.copy(this._targetEnd);
}
else {
this._hasRested = false;
this.dispatchEvent({ type: 'transitionstart' });
}
this._needsUpdate = true;
};
CameraControls.prototype.moveTo = function (x, y, z, enableTransition) {
Expand All @@ -906,6 +934,10 @@
if (!enableTransition) {
this._target.copy(this._targetEnd);
}
else {
this._hasRested = false;
this.dispatchEvent({ type: 'transitionstart' });
}
this._needsUpdate = true;
};
CameraControls.prototype.fitToBox = function (box3OrObject, enableTransition, _a) {
Expand Down Expand Up @@ -1003,6 +1035,10 @@
this._target.copy(this._targetEnd);
this._spherical.copy(this._sphericalEnd);
}
else {
this._hasRested = false;
this.dispatchEvent({ type: 'transitionstart' });
}
this._needsUpdate = true;
};
CameraControls.prototype.lerpLookAt = function (positionAX, positionAY, positionAZ, targetAX, targetAY, targetAZ, positionBX, positionBY, positionBZ, targetBX, targetBY, targetBZ, t, enableTransition) {
Expand All @@ -1023,6 +1059,10 @@
this._target.copy(this._targetEnd);
this._spherical.copy(this._sphericalEnd);
}
else {
this._hasRested = false;
this.dispatchEvent({ type: 'transitionstart' });
}
this._needsUpdate = true;
};
CameraControls.prototype.setPosition = function (positionX, positionY, positionZ, enableTransition) {
Expand All @@ -1040,6 +1080,10 @@
if (!enableTransition) {
this._focalOffset.copy(this._focalOffsetEnd);
}
else {
this._hasRested = false;
this.dispatchEvent({ type: 'transitionstart' });
}
this._needsUpdate = true;
};
CameraControls.prototype.setBoundary = function (box3) {
Expand Down Expand Up @@ -1210,13 +1254,28 @@
}
var updated = this._needsUpdate;
if (updated && !this._updatedLastTime) {
this._hasRested = false;
this.dispatchEvent({ type: 'wake' });
this.dispatchEvent({ type: 'update' });
}
else if (updated) {
this.dispatchEvent({ type: 'update' });
if (lessThanEpsilon(deltaTheta, this.restEpsilon) &&
lessThanEpsilon(deltaPhi, this.restEpsilon) &&
lessThanEpsilon(deltaRadius, this.restEpsilon) &&
lessThanEpsilon(deltaTarget.x, this.restEpsilon) &&
lessThanEpsilon(deltaTarget.y, this.restEpsilon) &&
lessThanEpsilon(deltaTarget.z, this.restEpsilon) &&
lessThanEpsilon(deltaOffset.x, this.restEpsilon) &&
lessThanEpsilon(deltaOffset.y, this.restEpsilon) &&
lessThanEpsilon(deltaOffset.z, this.restEpsilon) &&
!this._hasRested) {
this.dispatchEvent({ type: 'rest' });
this._hasRested = true;
}
}
else if (!updated && this._updatedLastTime) {
this._hasRested = false;
this.dispatchEvent({ type: 'sleep' });
}
this._updatedLastTime = updated;
Expand Down
Loading