Skip to content

Commit cc58bbe

Browse files
committed
Embedding Projector: fix camera controls
1 parent e5d1771 commit cc58bbe

File tree

1 file changed

+17
-47
lines changed

1 file changed

+17
-47
lines changed

tensorboard/plugins/projector/vz_projector/scatterPlot.ts

+17-47
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,6 @@ const PERSP_CAMERA_FOV_VERTICAL = 70;
3838
const PERSP_CAMERA_NEAR_CLIP_PLANE = 0.01;
3939
const PERSP_CAMERA_FAR_CLIP_PLANE = 100;
4040
const ORTHO_CAMERA_FRUSTUM_HALF_EXTENT = 1.2;
41-
// Key presses.
42-
const SHIFT_KEY = 16;
43-
const CTRL_KEY = 17;
4441
const ORBIT_MOUSE_ROTATION_SPEED = 1;
4542
const ORBIT_ANIMATION_ROTATION_CYCLE_IN_SECONDS = 7;
4643
export type OnCameraMoveListener = (
@@ -85,7 +82,7 @@ export class ScatterPlot {
8582
private cameraDef: CameraDef | null = null;
8683
private camera: THREE.Camera;
8784
private orbitAnimationOnNextCameraCreation: boolean = false;
88-
private orbitCameraControls: any;
85+
private orbitCameraControls: OrbitControls;
8986
private orbitAnimationId: number | null;
9087
private worldSpacePointPositions: Float32Array;
9188
private pointColors: Float32Array;
@@ -133,7 +130,7 @@ export class ScatterPlot {
133130
window.addEventListener('keydown', this.onKeyDown.bind(this), false);
134131
window.addEventListener('keyup', this.onKeyUp.bind(this), false);
135132
}
136-
private addCameraControlsEventListeners(cameraControls: any) {
133+
private addCameraControlsEventListeners(cameraControls: OrbitControls) {
137134
// Start is called when the user stars interacting with
138135
// controls.
139136
cameraControls.addEventListener('start', () => {
@@ -158,7 +155,7 @@ export class ScatterPlot {
158155
if (this.orbitCameraControls != null) {
159156
this.orbitCameraControls.dispose();
160157
}
161-
const occ = new OrbitControls(camera, this.renderer.domElement) as any;
158+
const occ = new OrbitControls(camera, this.renderer.domElement);
162159
occ.target0 = new THREE.Vector3(
163160
cameraDef.target[0],
164161
cameraDef.target[1],
@@ -170,11 +167,11 @@ export class ScatterPlot {
170167
occ.autoRotate = false;
171168
occ.rotateSpeed = ORBIT_MOUSE_ROTATION_SPEED;
172169
if (cameraIs3D) {
173-
occ.mouseButtons.ORBIT = THREE.MOUSE.LEFT;
174-
occ.mouseButtons.PAN = THREE.MOUSE.RIGHT;
170+
occ.mouseButtons.LEFT = THREE.MOUSE.ROTATE;
171+
occ.mouseButtons.RIGHT = THREE.MOUSE.PAN;
175172
} else {
176-
occ.mouseButtons.ORBIT = null;
177-
occ.mouseButtons.PAN = THREE.MOUSE.LEFT;
173+
occ.mouseButtons.RIGHT = null;
174+
occ.mouseButtons.LEFT = THREE.MOUSE.PAN;
178175
}
179176
occ.reset();
180177
this.camera = camera;
@@ -312,28 +309,10 @@ export class ScatterPlot {
312309
this.orbitCameraControls.enabled = false;
313310
this.rectangleSelector.onMouseDown(e.offsetX, e.offsetY);
314311
this.setNearestPointToMouse(e);
315-
} else if (
316-
!e.ctrlKey &&
317-
this.sceneIs3D() &&
318-
this.orbitCameraControls.mouseButtons.ORBIT === THREE.MOUSE.RIGHT
319-
) {
320-
// The user happened to press the ctrl key when the tab was active,
321-
// unpressed the ctrl when the tab was inactive, and now he/she
322-
// is back to the projector tab.
323-
this.orbitCameraControls.mouseButtons.ORBIT = THREE.MOUSE.LEFT;
324-
this.orbitCameraControls.mouseButtons.PAN = THREE.MOUSE.RIGHT;
325-
} else if (
326-
e.ctrlKey &&
327-
this.sceneIs3D() &&
328-
this.orbitCameraControls.mouseButtons.ORBIT === THREE.MOUSE.LEFT
329-
) {
330-
// Similarly to the situation above.
331-
this.orbitCameraControls.mouseButtons.ORBIT = THREE.MOUSE.RIGHT;
332-
this.orbitCameraControls.mouseButtons.PAN = THREE.MOUSE.LEFT;
333312
}
334313
}
335314
/** When we stop dragging/zooming, return to normal behavior. */
336-
private onMouseUp(e: any) {
315+
private onMouseUp(e: MouseEvent) {
337316
if (this.selecting) {
338317
this.orbitCameraControls.enabled = true;
339318
this.rectangleSelector.onMouseUp();
@@ -356,27 +335,18 @@ export class ScatterPlot {
356335
this.projectorEventContext.notifyHoverOverPoint(this.nearestPoint!);
357336
}
358337
}
359-
/** For using ctrl + left click as right click, and for circle select */
360-
private onKeyDown(e: any) {
361-
// If ctrl is pressed, use left click to orbit
362-
if (e.keyCode === CTRL_KEY && this.sceneIs3D()) {
363-
this.orbitCameraControls.mouseButtons.ORBIT = THREE.MOUSE.RIGHT;
364-
this.orbitCameraControls.mouseButtons.PAN = THREE.MOUSE.LEFT;
365-
}
338+
/** For for circle select */
339+
private onKeyDown(e: KeyboardEvent) {
366340
// If shift is pressed, start selecting
367-
if (e.keyCode === SHIFT_KEY) {
341+
if (e.shiftKey) {
368342
this.selecting = true;
369343
this.container.style.cursor = 'crosshair';
370344
}
371345
}
372-
/** For using ctrl + left click as right click, and for circle select */
373-
private onKeyUp(e: any) {
374-
if (e.keyCode === CTRL_KEY && this.sceneIs3D()) {
375-
this.orbitCameraControls.mouseButtons.ORBIT = THREE.MOUSE.LEFT;
376-
this.orbitCameraControls.mouseButtons.PAN = THREE.MOUSE.RIGHT;
377-
}
346+
/** For for circle select */
347+
private onKeyUp(e: KeyboardEvent) {
378348
// If shift is released, stop selecting
379-
if (e.keyCode === SHIFT_KEY) {
349+
if (e.shiftKey) {
380350
this.selecting = this.getMouseMode() === MouseMode.AREA_SELECT;
381351
if (!this.selecting) {
382352
this.container.style.cursor = 'default';
@@ -468,7 +438,7 @@ export class ScatterPlot {
468438
return axes;
469439
}
470440
private add3dAxis() {
471-
const axes = new (THREE as any).AxesHelper();
441+
const axes = new THREE.AxesHelper();
472442
axes.name = 'axes';
473443
this.scene.add(axes);
474444
}
@@ -493,7 +463,7 @@ export class ScatterPlot {
493463
def.orthographic = !this.sceneIs3D();
494464
def.position = [pos.x, pos.y, pos.z];
495465
def.target = [tgt.x, tgt.y, tgt.z];
496-
def.zoom = (this.camera as any).zoom;
466+
def.zoom = this.camera.zoom;
497467
return def;
498468
}
499469
/** Sets parameters for the next camera recreation. */
@@ -705,7 +675,7 @@ export class ScatterPlot {
705675
const renderCanvasSize = new THREE.Vector2();
706676
// TODO(stephanwlee): Remove casting to any after three.js typing is
707677
// proper.
708-
(this.renderer as any).getSize(renderCanvasSize);
678+
this.renderer.getSize(renderCanvasSize);
709679
const pixelRatio = this.renderer.getPixelRatio();
710680
this.pickingTexture = new THREE.WebGLRenderTarget(
711681
renderCanvasSize.width * pixelRatio,

0 commit comments

Comments
 (0)