Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 | 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 3x 12x 12x 12x 12x 42x 39x 37x 2x 40x 10x 3x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 18x 18x 18x 18x 6x 6x 6x 3x 2x 2x 2x 2x 2x 2x 2x 2x 3x 2x 2x 2x 2x 2x 2x 2x 2x 3x 3x | import scale from "../utils/matrices/scale";
import rotation from "../utils/matrices/rotation";
import translation from "../utils/matrices/translation";
import multiply from "../utils/matrices/multiply";
import Dispatcher from "./Dispatcher";
/**
* The camera constructor. It just initializes its attributes and methods.
*
* @param {string} id The id.
* @param {graph} graph The graph.
* @param {configurable} settings The settings function.
* @param {?object} options Eventually some overriding options.
* @return {camera} Returns the fresh new camera instance.
*/
export default function Camera(id, graph, settings, options) {
Dispatcher.extend(this);
Object.defineProperty(this, "graph", {
value: graph
});
Object.defineProperty(this, "id", {
value: id
});
Object.defineProperty(this, "readPrefix", {
value: `read_cam${id}:`
});
Object.defineProperty(this, "prefix", {
value: `cam${id}:`
});
this.x = 0;
this.y = 0;
this.ratio = 1;
this.angle = 0;
this.isAnimated = false;
this.settings =
typeof options === "object" && options
? settings.embedObject(options)
: settings;
}
/**
* Updates the camera position.
*
* @param {object} coordinates The new coordinates object.
* @return {camera} Returns the camera.
*/
Camera.prototype.goTo = function goto(coordinates) {
Iif (!this.settings("enableCamera")) return this;
const c = coordinates || {};
const keys = ["x", "y", "ratio", "angle"];
keys.forEach(key => {
if (c[key] !== undefined) {
if (typeof c[key] === "number" && !Number.isNaN(c[key]))
this[key] = c[key];
else throw new Error(`Value for "${key}" is not a number.`);
}
});
keys.forEach(() => this.dispatchEvent("coordinatesUpdated"));
return this;
};
/**
* This method takes a graph and computes for each node and edges its
* coordinates relatively to the center of the camera. Basically, it will
* compute the coordinates that will be used by the graphic renderers.
*
* Since it should be possible to use different cameras and different
* renderers, it is possible to specify a prefix to put before the new
* coordinates (to get something like "node.camera1_x")
*
* @param {?string} read The prefix of the coordinates to read.
* @param {?string} write The prefix of the coordinates to write.
* @param {?object} options Eventually an object of options. Those can be:
* - A restricted nodes array.
* - A restricted edges array.
* - A width.
* - A height.
* @return {camera} Returns the camera.
*/
Camera.prototype.applyView = function applyView(read, write, options) {
options = options || {};
write = write !== undefined ? write : this.prefix;
read = read !== undefined ? read : this.readPrefix;
const nodes = options.nodes || this.graph.nodes();
const edges = options.edges || this.graph.edges();
let i;
let l;
let node;
const relCos = Math.cos(this.angle) / this.ratio;
const relSin = Math.sin(this.angle) / this.ratio;
const nodeRatio = this.ratio ** this.settings("nodesPowRatio");
const edgeRatio = this.ratio ** this.settings("edgesPowRatio");
const xOffset = (options.width || 0) / 2 - this.x * relCos - this.y * relSin;
const yOffset = (options.height || 0) / 2 - this.y * relCos + this.x * relSin;
for (i = 0, l = nodes.length; i < l; i++) {
node = nodes[i];
node[`${write}x`] =
(node[`${read}x`] || 0) * relCos +
(node[`${read}y`] || 0) * relSin +
xOffset;
node[`${write}y`] =
(node[`${read}y`] || 0) * relCos -
(node[`${read}x`] || 0) * relSin +
yOffset;
node[`${write}size`] = (node[`${read}size`] || 0) / nodeRatio;
}
for (i = 0, l = edges.length; i < l; i++) {
edges[i][`${write}size`] = (edges[i][`${read}size`] || 0) / edgeRatio;
}
return this;
};
/**
* This function converts the coordinates of a point from the frame of the
* camera to the frame of the graph.
*
* @param {number} x The X coordinate of the point in the frame of the
* camera.
* @param {number} y The Y coordinate of the point in the frame of the
* camera.
* @return {object} The point coordinates in the frame of the graph.
*/
Camera.prototype.graphPosition = function graphPosition(x, y, vector) {
let X = 0;
let Y = 0;
const cos = Math.cos(this.angle);
const sin = Math.sin(this.angle);
// Revert the origin differential vector:
Eif (!vector) {
X = -(this.x * cos + this.y * sin) / this.ratio;
Y = -(this.y * cos - this.x * sin) / this.ratio;
}
return {
x: (x * cos + y * sin) / this.ratio + X,
y: (y * cos - x * sin) / this.ratio + Y
};
};
/**
* This function converts the coordinates of a point from the frame of the
* graph to the frame of the camera.
*
* @param {number} x The X coordinate of the point in the frame of the
* graph.
* @param {number} y The Y coordinate of the point in the frame of the
* graph.
* @return {object} The point coordinates in the frame of the camera.
*/
Camera.prototype.cameraPosition = function cameraPosition(x, y, vector) {
let X = 0;
let Y = 0;
const cos = Math.cos(this.angle);
const sin = Math.sin(this.angle);
// Revert the origin differential vector:
Eif (!vector) {
X = -(this.x * cos + this.y * sin) / this.ratio;
Y = -(this.y * cos - this.x * sin) / this.ratio;
}
return {
x: ((x - X) * cos - (y - Y) * sin) * this.ratio,
y: ((y - Y) * cos + (x - X) * sin) * this.ratio
};
};
/**
* This method returns the transformation matrix of the camera. This is
* especially useful to apply the camera view directly in shaders, in case of
* WebGL rendering.
*
* @return {array} The transformation matrix.
*/
Camera.prototype.getMatrix = function getMatrix() {
const scaled = scale(1 / this.ratio);
const rotated = rotation(this.angle);
const translated = translation(-this.x, -this.y);
const matrix = multiply(translated, multiply(rotated, scaled));
return matrix;
};
/**
* Taking a width and a height as parameters, this method returns the
* coordinates of the rectangle representing the camera on screen, in the
* graph's referentiel.
*
* To keep displaying labels of nodes going out of the screen, the method
* keeps a margin around the screen in the returned rectangle.
*
* @param {number} width The width of the screen.
* @param {number} height The height of the screen.
* @return {object} The rectangle as x1, y1, x2 and y2, representing
* two opposite points.
*/
Camera.prototype.getRectangle = function getRectangle(width, height) {
const widthVect = this.cameraPosition(width, 0, true);
const heightVect = this.cameraPosition(0, height, true);
const centerVect = this.cameraPosition(width / 2, height / 2, true);
const marginX = this.cameraPosition(width / 4, 0, true).x;
const marginY = this.cameraPosition(0, height / 4, true).y;
return {
x1: this.x - centerVect.x - marginX,
y1: this.y - centerVect.y - marginY,
x2: this.x - centerVect.x + marginX + widthVect.x,
y2: this.y - centerVect.y - marginY + widthVect.y,
height: Math.sqrt(heightVect.x ** 2 + (heightVect.y + 2 * marginY) ** 2)
};
};
|