|
@@ -2,6 +2,8 @@ import {Viewport} from "../Viewport.js";
|
|
import {Pointer} from "../input/Pointer.js";
|
|
import {Pointer} from "../input/Pointer.js";
|
|
import {Vector2} from "../math/Vector2.js";
|
|
import {Vector2} from "../math/Vector2.js";
|
|
|
|
|
|
|
|
+
|
|
|
|
+
|
|
/**
|
|
/**
|
|
* Viewport controls are used to allow the user to control the viewport.
|
|
* Viewport controls are used to allow the user to control the viewport.
|
|
*
|
|
*
|
|
@@ -36,25 +38,13 @@ function ViewportControls(viewport)
|
|
this.allowScale = true;
|
|
this.allowScale = true;
|
|
|
|
|
|
/**
|
|
/**
|
|
- * Flag to indicate if the viewport should automatically cente ron the pointer position.
|
|
|
|
- *
|
|
|
|
- * The viewport will simulataniously move to the pointer position while scalling.
|
|
|
|
- *
|
|
|
|
- * For some application its easier to focus the target if the viewport moves to the pointer location while scalling.
|
|
|
|
- *
|
|
|
|
- * @type {boolean}
|
|
|
|
- */
|
|
|
|
- this.centerOnPointer = false;
|
|
|
|
-
|
|
|
|
- /**
|
|
|
|
- * Flag to recenter the viewport automatically to the canvas.
|
|
|
|
|
|
+ * Flag to indicate if the viewport should automatically be recentered.
|
|
*
|
|
*
|
|
- * This will ensure that rotation and scaling will not cause the viewport to move around.
|
|
|
|
|
|
+ * This will cause the viewport center property to be automatically set based on an heuristic defined by the user.
|
|
*
|
|
*
|
|
- * @type {boolean}
|
|
|
|
- * @default true
|
|
|
|
|
|
+ * @type {number}
|
|
*/
|
|
*/
|
|
- this.centerOnCanvas = true;
|
|
|
|
|
|
+ this.recenterViewport = ViewportControls.RECENTER_POINTER;
|
|
|
|
|
|
/**
|
|
/**
|
|
* If true allows the viewport to be rotated.
|
|
* If true allows the viewport to be rotated.
|
|
@@ -86,6 +76,35 @@ function ViewportControls(viewport)
|
|
this.rotationInitial = 0;
|
|
this.rotationInitial = 0;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+/**
|
|
|
|
+ * Viewport is not automatically recentered.
|
|
|
|
+ *
|
|
|
|
+ * The center point can be set manually by the developer.
|
|
|
|
+ *
|
|
|
|
+ * @type {number}
|
|
|
|
+ */
|
|
|
|
+ViewportControls.RECENTER_NONE = 0;
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * Recenter the viewport automatically to the canvas.
|
|
|
|
+ *
|
|
|
|
+ * This will ensure that rotation and scaling will not cause the viewport to move around.
|
|
|
|
+ *
|
|
|
|
+ * @type {number}
|
|
|
|
+ */
|
|
|
|
+ViewportControls.RECENTER_CANVAS = 1;
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * Viewport should automatically cente ron the pointer position.
|
|
|
|
+ *
|
|
|
|
+ * The viewport will simulataniously move to the pointer position while scalling.
|
|
|
|
+ *
|
|
|
|
+ * For some application its easier to focus the target if the viewport moves to the pointer location while scalling.
|
|
|
|
+ *
|
|
|
|
+ * @type {number}
|
|
|
|
+ */
|
|
|
|
+ViewportControls.RECENTER_POINTER = 2;
|
|
|
|
+
|
|
/**
|
|
/**
|
|
* Update the viewport controls using the pointer object.
|
|
* Update the viewport controls using the pointer object.
|
|
*
|
|
*
|
|
@@ -131,15 +150,20 @@ ViewportControls.prototype.update = function(pointer)
|
|
this.viewport.matrixNeedsUpdate = true;
|
|
this.viewport.matrixNeedsUpdate = true;
|
|
}
|
|
}
|
|
|
|
|
|
- // Automtical center the viewport.
|
|
|
|
- if (this.centerOnCanvas && pointer.canvas !== null) {
|
|
|
|
|
|
+
|
|
|
|
+ if (pointer.canvas === null) {
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // Center viewport on canvas
|
|
|
|
+ if (this.recenterViewport === ViewportControls.RECENTER_CANVAS) {
|
|
var centerWorld = new Vector2(pointer.canvas.width / 2.0, pointer.canvas.height / 2.0);
|
|
var centerWorld = new Vector2(pointer.canvas.width / 2.0, pointer.canvas.height / 2.0);
|
|
centerWorld = this.viewport.inverseMatrix.transformPoint(centerWorld);
|
|
centerWorld = this.viewport.inverseMatrix.transformPoint(centerWorld);
|
|
this.viewport.center.copy(centerWorld);
|
|
this.viewport.center.copy(centerWorld);
|
|
this.viewport.matrixNeedsUpdate = true;
|
|
this.viewport.matrixNeedsUpdate = true;
|
|
}
|
|
}
|
|
- // Center on pointer
|
|
|
|
- else if(this.centerOnPointer && pointer.canvas !== null)
|
|
|
|
|
|
+ // Center viewport on pointer
|
|
|
|
+ else if(this.recenterViewport === ViewportControls.RECENTER_POINTER)
|
|
{
|
|
{
|
|
var pointerWorld = this.viewport.inverseMatrix.transformPoint(pointer.position)
|
|
var pointerWorld = this.viewport.inverseMatrix.transformPoint(pointer.position)
|
|
this.viewport.center.copy(pointerWorld);
|
|
this.viewport.center.copy(pointerWorld);
|