UNPKG

7.5 kBJavaScriptView Raw
1"use strict";
2var __spreadArray = (this && this.__spreadArray) || function (to, from) {
3 for (var i = 0, il = from.length, j = to.length; i < il; i++, j++)
4 to[j] = from[i];
5 return to;
6};
7Object.defineProperty(exports, "__esModule", { value: true });
8exports.Coordinate = void 0;
9var util_1 = require("@antv/util");
10var matrix_util_1 = require("@antv/matrix-util");
11var utils_1 = require("./utils");
12var transforms_1 = require("./transforms");
13var Coordinate = /** @class */ (function () {
14 /**
15 * Create a new Coordinate Object.
16 * @param options Custom options
17 */
18 function Coordinate(options) {
19 // 当前的选项
20 this.options = {
21 x: 0,
22 y: 0,
23 width: 300,
24 height: 150,
25 transformations: [],
26 };
27 // 当前可以使用的变换
28 this.transformers = {
29 cartesian: transforms_1.cartesian,
30 translate: transforms_1.translate,
31 custom: transforms_1.custom,
32 matrix: transforms_1.matrix,
33 polar: transforms_1.polar,
34 transpose: transforms_1.transpose,
35 scale: transforms_1.scale,
36 'shear.x': transforms_1.shearX,
37 'shear.y': transforms_1.shearY,
38 reflect: transforms_1.reflect,
39 'reflect.x': transforms_1.reflectX,
40 'reflect.y': transforms_1.reflectY,
41 rotate: transforms_1.rotate,
42 helix: transforms_1.helix,
43 parallel: transforms_1.parallel,
44 fisheye: transforms_1.fisheye,
45 'fisheye.x': transforms_1.fisheyeX,
46 'fisheye.y': transforms_1.fisheyeY,
47 'fisheye.circular': transforms_1.fisheyeCircular,
48 };
49 this.update(options);
50 }
51 /**
52 * Update options and inner state.
53 * @param options Options to be updated
54 */
55 Coordinate.prototype.update = function (options) {
56 this.options = util_1.deepMix({}, this.options, options);
57 this.recoordinate();
58 };
59 /**
60 * Returns a new Coordinate with same options.
61 * @returns Coordinate
62 */
63 Coordinate.prototype.clone = function () {
64 return new Coordinate(this.options);
65 };
66 /**
67 * Returns current options.
68 * @returns options
69 */
70 Coordinate.prototype.getOptions = function () {
71 return this.options;
72 };
73 /**
74 * Clear transformations and update.
75 */
76 Coordinate.prototype.clear = function () {
77 this.update({
78 transformations: [],
79 });
80 };
81 /**
82 * Returns the size of the bounding box of the coordinate.
83 * @returns [width, height]
84 */
85 Coordinate.prototype.getSize = function () {
86 var _a = this.options, width = _a.width, height = _a.height;
87 return [width, height];
88 };
89 /**
90 * Returns the center of the bounding box of the coordinate.
91 * @returns [centerX, centerY]
92 */
93 Coordinate.prototype.getCenter = function () {
94 var _a = this.options, x = _a.x, y = _a.y, width = _a.width, height = _a.height;
95 return [(x + width) / 2, (y + height) / 2];
96 };
97 /**
98 * Add selected transformation.
99 * @param args transform type and params
100 * @returns Coordinate
101 */
102 Coordinate.prototype.transform = function () {
103 var args = [];
104 for (var _i = 0; _i < arguments.length; _i++) {
105 args[_i] = arguments[_i];
106 }
107 var transformations = this.options.transformations;
108 this.update({
109 transformations: __spreadArray(__spreadArray([], transformations), [__spreadArray([], args)]),
110 });
111 return this;
112 };
113 /**
114 * Apples transformations for the current vector.
115 * @param vector original vector2
116 * @returns transformed vector2
117 */
118 Coordinate.prototype.map = function (vector) {
119 return this.output(vector);
120 };
121 /**
122 * Apples invert transformations for the current vector.
123 * @param vector transformed vector2
124 * @param vector original vector2
125 */
126 Coordinate.prototype.invert = function (vector) {
127 return this.input(vector);
128 };
129 Coordinate.prototype.recoordinate = function () {
130 this.output = this.compose();
131 this.input = this.compose(true);
132 };
133 // 将所有的变换合成一个函数
134 // 变换有两种类型:矩阵变换和函数变换
135 // 处理过程中需要把连续的矩阵变换合成一个变换函数,然后在和其他变换函数合成最终的变换函数
136 Coordinate.prototype.compose = function (invert) {
137 if (invert === void 0) { invert = false; }
138 var transformations = invert ? __spreadArray([], this.options.transformations).reverse() : this.options.transformations;
139 var getter = invert ? function (d) { return d.untransform; } : function (d) { return d.transform; };
140 var matrixes = [];
141 var transforms = [];
142 var add = function (transform, extended) {
143 if (extended === void 0) { extended = true; }
144 return transforms.push(extended ? utils_1.extend(transform) : transform);
145 };
146 for (var _i = 0, transformations_1 = transformations; _i < transformations_1.length; _i++) {
147 var _a = transformations_1[_i], name_1 = _a[0], args = _a.slice(1);
148 var createTransformer = this.transformers[name_1];
149 if (createTransformer) {
150 var _b = this.options, x = _b.x, y = _b.y, width = _b.width, height = _b.height;
151 var transformer = createTransformer(__spreadArray([], args), x, y, width, height);
152 if (utils_1.isMatrix(transformer)) {
153 // 如果当前变换是矩阵变换,那么先保存下来
154 matrixes.push(transformer);
155 }
156 else {
157 // 如果当前变换是函数变换,并且之前有没有合成的矩阵变换,那么现将之前的矩阵变换合成
158 if (matrixes.length) {
159 var transform_1 = this.createMatrixTransform(matrixes, invert);
160 add(transform_1);
161 matrixes.splice(0, matrixes.length);
162 }
163 var transform = getter(transformer) || util_1.identity;
164 add(transform, name_1 !== 'parallel'); // 对于非平行坐标系的变换需要扩展
165 }
166 }
167 }
168 // 合成剩下的矩阵变换
169 if (matrixes.length) {
170 var transform = this.createMatrixTransform(matrixes, invert);
171 add(transform);
172 }
173 return utils_1.compose.apply(void 0, transforms);
174 };
175 // 将连续的矩阵的运算合成一个变换函数
176 Coordinate.prototype.createMatrixTransform = function (matrixes, invert) {
177 var matrix = matrix_util_1.mat3.create();
178 if (invert)
179 matrixes.reverse();
180 matrixes.forEach(function (m) { return matrix_util_1.mat3.mul(matrix, matrix, m); });
181 if (invert) {
182 matrix_util_1.mat3.invert(matrix, matrix_util_1.mat3.clone(matrix));
183 }
184 return function (vector) {
185 var vector3 = [vector[0], vector[1], 1];
186 matrix_util_1.vec3.transformMat3(vector3, vector3, matrix);
187 return [vector3[0], vector3[1]];
188 };
189 };
190 return Coordinate;
191}());
192exports.Coordinate = Coordinate;
193//# sourceMappingURL=coordinate.js.map
\No newline at end of file