1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- "use strict";
- import {Vector2} from "./math/Vector2.js";
- import {Matrix} from "./math/Matrix.js";
- import {UUID} from "./math/UUID.js";
- import {Pointer} from "./input/Pointer.js";
- /**
- * Used to indicate how the user views the content inside of the canvas.
- *
- * @class
- */
- function Viewport()
- {
- /**
- * UUID of the object.
- */
- this.uuid = UUID.generate();
- /**
- * Position of the object.
- */
- this.position = new Vector2(0, 0);
- /**
- * Scale of the object.
- */
- this.scale = 1.0
- /**
- * Rotation of the object relative to its center.
- */
- this.rotation = 0.0;
- /**
- * Local transformation matrix applied to the object.
- */
- this.matrix = new Matrix();
- /**
- * Inverse of the local transformation matrix.
- */
- this.inverseMatrix = new Matrix();
- /**
- * If true the matrix is updated before rendering the object.
- */
- this.matrixNeedsUpdate = true;
- /**
- * Flag to indicate if the viewport should move when scalling.
- *
- * For some application its easier to focus the target if the viewport moves to the pointer location while scalling.
- */
- this.moveOnScale = true;
- }
- /**
- * Update the viewport controls using the pointer object.
- */
- Viewport.prototype.updateControls = function(pointer)
- {
- if(pointer.wheel !== 0)
- {
- this.scale -= pointer.wheel * 1e-3 * this.scale;
- if(this.moveOnScale)
- {
- var speed = pointer.wheel / this.scale;
- var halfWidth = pointer.canvas.width / 2;
- var halfWeight = pointer.canvas.height / 2;
- this.position.x += ((pointer.position.x - halfWidth) / halfWidth) * speed;
- this.position.y += ((pointer.position.y - halfWeight) / halfWeight) * speed;
- }
- }
- if(pointer.buttonPressed(Pointer.RIGHT))
- {
- this.position.x += pointer.delta.x;
- this.position.y += pointer.delta.y;
- }
- };
- /**
- * Calculate and update the viewport transformation matrix.
- */
- Viewport.prototype.updateMatrix = function()
- {
- if(this.matrixNeedsUpdate)
- {
- this.matrix.compose(this.position.x, this.position.y, this.scale, this.scale, this.rotation);
- this.inverseMatrix = this.matrix.getInverse();
- //this.matrixNeedsUpdate = false;
- }
- };
- export {Viewport};
|