UNPKG

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