fisheye.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.fisheyeCircular = exports.fisheye = exports.fisheyeY = exports.fisheyeX = void 0;
  4. /* eslint-disable @typescript-eslint/no-unused-vars */
  5. // https://github.com/d3/d3-plugins/blob/master/fisheye/fisheye.js
  6. var scale_1 = require("@antv/scale");
  7. function fisheyeTransform(x, focus, distortion, min, max) {
  8. var left = x < focus;
  9. var m = (left ? focus - min : max - focus) || max - min;
  10. var f = left ? -1 : 1;
  11. return (f * m * (distortion + 1)) / (distortion + m / ((x - focus) * f)) + focus;
  12. }
  13. function fisheyeUntransform(tx, focus, distortion, min, max) {
  14. var left = tx < focus;
  15. var m = (left ? focus - min : max - focus) || max - min;
  16. var f = left ? -1 : 1;
  17. return m / ((m * (distortion + 1)) / (tx - focus) - distortion * f) + focus;
  18. }
  19. /**
  20. * Map actual visual point to abstract focus point(0, 1)
  21. */
  22. function normalize(focus, length, isVisual) {
  23. if (!isVisual)
  24. return focus;
  25. var s = new scale_1.Linear({
  26. range: [0, 1],
  27. domain: [0, length],
  28. });
  29. return s.map(focus);
  30. }
  31. /**
  32. * Applies cartesian fisheye transforms for the first dimension of vector2.
  33. * @param params [focus, distortion]
  34. * @param x x of the the bounding box of coordinate
  35. * @param y y of the the bounding box of coordinate
  36. * @param width width of the the bounding box of coordinate
  37. * @param height height of the the bounding box of coordinate
  38. * @returns transformer
  39. */
  40. var fisheyeX = function (params, x, y, width, height) {
  41. var _a = params, focus = _a[0], distortion = _a[1], _b = _a[2], isVisual = _b === void 0 ? false : _b;
  42. var normalizedFocusX = normalize(focus, width, isVisual);
  43. return {
  44. transform: function (vector) {
  45. var vx = vector[0], vy = vector[1];
  46. var fx = fisheyeTransform(vx, normalizedFocusX, distortion, 0, 1);
  47. return [fx, vy];
  48. },
  49. untransform: function (vector) {
  50. var fx = vector[0], vy = vector[1];
  51. var vx = fisheyeUntransform(fx, normalizedFocusX, distortion, 0, 1);
  52. return [vx, vy];
  53. },
  54. };
  55. };
  56. exports.fisheyeX = fisheyeX;
  57. /**
  58. * Applies cartesian fisheye transforms for the second dimension of vector2.
  59. * @param params [focus, distortion]
  60. * @param x x of the the bounding box of coordinate
  61. * @param y y of the the bounding box of coordinate
  62. * @param width width of the the bounding box of coordinate
  63. * @param height height of the the bounding box of coordinate
  64. * @returns transformer
  65. */
  66. var fisheyeY = function (params, x, y, width, height) {
  67. var _a = params, focus = _a[0], distortion = _a[1], _b = _a[2], isVisual = _b === void 0 ? false : _b;
  68. var normalizedFocusY = normalize(focus, height, isVisual);
  69. return {
  70. transform: function (vector) {
  71. var vx = vector[0], vy = vector[1];
  72. var fy = fisheyeTransform(vy, normalizedFocusY, distortion, 0, 1);
  73. return [vx, fy];
  74. },
  75. untransform: function (vector) {
  76. var vx = vector[0], fy = vector[1];
  77. var vy = fisheyeUntransform(fy, normalizedFocusY, distortion, 0, 1);
  78. return [vx, vy];
  79. },
  80. };
  81. };
  82. exports.fisheyeY = fisheyeY;
  83. /**
  84. * Applies cartesian fisheye transforms for both dimensions of vector2.
  85. * @param params [focusX, focusY, distortionX, distortionY]
  86. * @param x x of the the bounding box of coordinate
  87. * @param y y of the the bounding box of coordinate
  88. * @param width width of the the bounding box of coordinate
  89. * @param height height of the the bounding box of coordinate
  90. * @returns transformer
  91. */
  92. var fisheye = function (params, x, y, width, height) {
  93. var _a = params, focusX = _a[0], focusY = _a[1], distortionX = _a[2], distortionY = _a[3], _b = _a[4], isVisual = _b === void 0 ? false : _b;
  94. var normalizedFocusX = normalize(focusX, width, isVisual);
  95. var normalizedFocusY = normalize(focusY, height, isVisual);
  96. return {
  97. transform: function (vector) {
  98. var vx = vector[0], vy = vector[1];
  99. var fx = fisheyeTransform(vx, normalizedFocusX, distortionX, 0, 1);
  100. var fy = fisheyeTransform(vy, normalizedFocusY, distortionY, 0, 1);
  101. return [fx, fy];
  102. },
  103. untransform: function (vector) {
  104. var fx = vector[0], fy = vector[1];
  105. var vx = fisheyeUntransform(fx, normalizedFocusX, distortionX, 0, 1);
  106. var vy = fisheyeUntransform(fy, normalizedFocusY, distortionY, 0, 1);
  107. return [vx, vy];
  108. },
  109. };
  110. };
  111. exports.fisheye = fisheye;
  112. /**
  113. * Applies circular fisheye transforms.
  114. * @param params [focusX, focusY, radius, distortion, isVisual?]
  115. * @param x x of the the bounding box of coordinate
  116. * @param y y of the the bounding box of coordinate
  117. * @param width width of the the bounding box of coordinate
  118. * @param height height of the the bounding box of coordinate
  119. * @returns transformer
  120. */
  121. var fisheyeCircular = function (params, x, y, width, height) {
  122. var _a = params, focusX = _a[0], focusY = _a[1], radius = _a[2], distortion = _a[3], _b = _a[4], isVisual = _b === void 0 ? false : _b;
  123. var scaleX = new scale_1.Linear({
  124. range: [0, width],
  125. });
  126. var scaleY = new scale_1.Linear({
  127. range: [0, height],
  128. });
  129. // focus point => visual point
  130. var nx = isVisual ? focusX : scaleX.map(focusX);
  131. var ny = isVisual ? focusY : scaleY.map(focusY);
  132. return {
  133. transform: function (vector) {
  134. var x = vector[0], y = vector[1];
  135. // focus point => visual point
  136. var dx = scaleX.map(x) - nx;
  137. var dy = scaleY.map(y) - ny;
  138. var dd = Math.sqrt(dx * dx + dy * dy);
  139. if (dd > radius)
  140. return [x, y];
  141. var r = fisheyeTransform(dd, 0, distortion, 0, radius);
  142. var theta = Math.atan2(dy, dx);
  143. var fx = nx + r * Math.cos(theta);
  144. var fy = ny + r * Math.sin(theta);
  145. // visual point => focus point
  146. return [scaleX.invert(fx), scaleY.invert(fy)];
  147. },
  148. untransform: function (vector) {
  149. var tx = vector[0], ty = vector[1];
  150. var dx = scaleX.map(tx) - nx;
  151. var dy = scaleY.map(ty) - ny;
  152. var dd = Math.sqrt(dx * dx + dy * dy);
  153. if (dd > radius)
  154. return [tx, ty];
  155. var x = fisheyeUntransform(dd, 0, distortion, 0, radius);
  156. var theta = Math.atan2(dy, dx);
  157. var fx = nx + x * Math.cos(theta);
  158. var fy = ny + x * Math.sin(theta);
  159. return [scaleX.invert(fx), scaleY.invert(fy)];
  160. },
  161. };
  162. };
  163. exports.fisheyeCircular = fisheyeCircular;
  164. //# sourceMappingURL=fisheye.js.map