瀏覽代碼

Viewport center options

tentone 3 年之前
父節點
當前提交
0399d17ecc
共有 2 個文件被更改,包括 86 次插入40 次删除
  1. 42 20
      build/escher.js
  2. 44 20
      source/controls/ViewportControls.js

+ 42 - 20
build/escher.js

@@ -2340,25 +2340,13 @@
 		this.allowScale = true;
 
 		/**
-		 * Flag to indicate if the viewport should automatically cente ron the pointer position.
+		 * Flag to indicate if the viewport should automatically be recentered.
 		 * 
-		 * 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 = true;
-
-		/**
-		 * Flag to recenter the viewport automatically to the canvas.
+		 * This will cause the viewport center property to be automatically set based on an heuristic defined by the user.
 		 * 
-		 * This will ensure that rotation and scaling will not cause the viewport to move around.
-		 * 
-		 * @type {boolean}
-		 * @default true
+		 * @type {number}
 		 */
-		this.centerOnCanvas = false;
+		this.recenterViewport = ViewportControls.RECENTER_POINTER;
 
 		/**
 		 * If true allows the viewport to be rotated.
@@ -2390,6 +2378,35 @@
 		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.
 	 *
@@ -2435,15 +2452,20 @@
 			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);
 			centerWorld = this.viewport.inverseMatrix.transformPoint(centerWorld);
 			this.viewport.center.copy(centerWorld);
 			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);
 			this.viewport.center.copy(pointerWorld);

+ 44 - 20
source/controls/ViewportControls.js

@@ -2,6 +2,8 @@ import {Viewport} from "../Viewport.js";
 import {Pointer} from "../input/Pointer.js";
 import {Vector2} from "../math/Vector2.js";
 
+
+
 /**
  * Viewport controls are used to allow the user to control the viewport.
  *
@@ -36,25 +38,13 @@ function ViewportControls(viewport)
 	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.
@@ -86,6 +76,35 @@ function ViewportControls(viewport)
 	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.
  *
@@ -131,15 +150,20 @@ ViewportControls.prototype.update = function(pointer)
 		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);
 		centerWorld = this.viewport.inverseMatrix.transformPoint(centerWorld);
 		this.viewport.center.copy(centerWorld);
 		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)
 		this.viewport.center.copy(pointerWorld);