UNPKG

5.29 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.fisheyeCircular = exports.fisheye = exports.fisheyeY = exports.fisheyeX = void 0;
4function fisheyeTransform(x, focus, distortion, min, max) {
5 var left = x < focus;
6 var m = (left ? focus - min : max - focus) || max - min;
7 var f = left ? -1 : 1;
8 return (f * m * (distortion + 1)) / (distortion + m / ((x - focus) * f)) + focus;
9}
10function fisheyeUntransform(tx, focus, distortion, min, max) {
11 var left = tx < focus;
12 var m = (left ? focus - min : max - focus) || max - min;
13 var f = left ? -1 : 1;
14 return m / ((m * (distortion + 1)) / (tx - focus) - distortion * f) + focus;
15}
16/**
17 * Applies cartesian fisheye transforms for the first dimension of vector2.
18 * @param params [focus, distortion]
19 * @param x x of the the bounding box of coordinate
20 * @param y y of the the bounding box of coordinate
21 * @param width width of the the bounding box of coordinate
22 * @param height height of the the bounding box of coordinate
23 * @returns transformer
24 */
25var fisheyeX = function (params, x, y, width, height) {
26 var _a = params, focus = _a[0], distortion = _a[1];
27 return {
28 transform: function (vector) {
29 var vx = vector[0], vy = vector[1];
30 var fx = fisheyeTransform(vx, focus, distortion, x, x + width);
31 return [fx, vy];
32 },
33 untransform: function (vector) {
34 var fx = vector[0], vy = vector[1];
35 var vx = fisheyeUntransform(fx, focus, distortion, x, x + width);
36 return [vx, vy];
37 },
38 };
39};
40exports.fisheyeX = fisheyeX;
41/**
42 * Applies cartesian fisheye transforms for the second dimension of vector2.
43 * @param params [focus, distortion]
44 * @param x x of the the bounding box of coordinate
45 * @param y y of the the bounding box of coordinate
46 * @param width width of the the bounding box of coordinate
47 * @param height height of the the bounding box of coordinate
48 * @returns transformer
49 */
50var fisheyeY = function (params, x, y, width, height) {
51 var _a = params, focus = _a[0], distortion = _a[1];
52 return {
53 transform: function (vector) {
54 var vx = vector[0], vy = vector[1];
55 var fy = fisheyeTransform(vy, focus, distortion, y, y + height);
56 return [vx, fy];
57 },
58 untransform: function (vector) {
59 var vx = vector[0], fy = vector[1];
60 var vy = fisheyeUntransform(fy, focus, distortion, y, y + height);
61 return [vx, vy];
62 },
63 };
64};
65exports.fisheyeY = fisheyeY;
66/**
67 * Applies cartesian fisheye transforms for both dimensions of vector2.
68 * @param params [focusX, focusY, distortionX, distortionY]
69 * @param x x of the the bounding box of coordinate
70 * @param y y of the the bounding box of coordinate
71 * @param width width of the the bounding box of coordinate
72 * @param height height of the the bounding box of coordinate
73 * @returns transformer
74 */
75var fisheye = function (params, x, y, width, height) {
76 var _a = params, focusX = _a[0], focusY = _a[1], distortionX = _a[2], distortionY = _a[3];
77 return {
78 transform: function (vector) {
79 var vx = vector[0], vy = vector[1];
80 var fx = fisheyeTransform(vx, focusX, distortionX, x, x + width);
81 var fy = fisheyeTransform(vy, focusY, distortionY, y, y + height);
82 return [fx, fy];
83 },
84 untransform: function (vector) {
85 var fx = vector[0], fy = vector[1];
86 var vx = fisheyeUntransform(fx, focusX, distortionX, x, x + width);
87 var vy = fisheyeUntransform(fy, focusY, distortionY, y, y + height);
88 return [vx, vy];
89 },
90 };
91};
92exports.fisheye = fisheye;
93/**
94 * Applies circular fisheye transforms.
95 * @param params [focusX, focusY, radius, distortion]
96 * @param x x of the the bounding box of coordinate
97 * @param y y of the the bounding box of coordinate
98 * @param width width of the the bounding box of coordinate
99 * @param height height of the the bounding box of coordinate
100 * @returns transformer
101 */
102var fisheyeCircular = function (params, x, y, width, height) {
103 var _a = params, focusX = _a[0], focusY = _a[1], radius = _a[2], distortion = _a[3];
104 return {
105 transform: function (vector) {
106 var x = vector[0], y = vector[1];
107 var dx = x - focusX;
108 var dy = y - focusY;
109 var dd = Math.sqrt(dx * dx + dy * dy);
110 if (dd > radius)
111 return [x, y];
112 var r = fisheyeTransform(dd, 0, distortion, 0, radius);
113 var theta = Math.atan2(dy, dx);
114 return [focusX + r * Math.cos(theta), focusY + r * Math.sin(theta)];
115 },
116 untransform: function (vector) {
117 var tx = vector[0], ty = vector[1];
118 var dx = tx - focusX;
119 var dy = ty - focusY;
120 var dd = Math.sqrt(dx * dx + dy * dy);
121 if (dd > radius)
122 return [tx, ty];
123 var x = fisheyeUntransform(dd, 0, distortion, 0, radius);
124 var theta = Math.atan2(dy, dx);
125 return [focusX + x * Math.cos(theta), focusY + x * Math.sin(theta)];
126 },
127 };
128};
129exports.fisheyeCircular = fisheyeCircular;
130//# sourceMappingURL=fisheye.js.map
\No newline at end of file