UNPKG

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