dom.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. var env = require("./env");
  2. var _fourPointsTransform = require("./fourPointsTransform");
  3. var buildTransformer = _fourPointsTransform.buildTransformer;
  4. var EVENT_SAVED_PROP = '___zrEVENTSAVED';
  5. var _calcOut = [];
  6. /**
  7. * Transform "local coord" from `elFrom` to `elTarget`.
  8. * "local coord": the coord based on the input `el`. The origin point is at
  9. * the position of "left: 0; top: 0;" in the `el`.
  10. *
  11. * Support when CSS transform is used.
  12. *
  13. * Having the `out` (that is, `[outX, outY]`), we can create an DOM element
  14. * and set the CSS style as "left: outX; top: outY;" and append it to `elTarge`
  15. * to locate the element.
  16. *
  17. * For example, this code below positions a child of `document.body` on the event
  18. * point, no matter whether `body` has `margin`/`paddin`/`transfrom`/... :
  19. * ```js
  20. * transformLocalCoord(out, container, document.body, event.offsetX, event.offsetY);
  21. * if (!eqNaN(out[0])) {
  22. * // Then locate the tip element on the event point.
  23. * var tipEl = document.createElement('div');
  24. * tipEl.style.cssText = 'position: absolute; left:' + out[0] + ';top:' + out[1] + ';';
  25. * document.body.appendChild(tipEl);
  26. * }
  27. * ```
  28. *
  29. * Notice: In some env this method is not supported. If called, `out` will be `[NaN, NaN]`.
  30. *
  31. * @param {Array.<number>} out [inX: number, inY: number] The output..
  32. * If can not transform, `out` will not be modified but return `false`.
  33. * @param {HTMLElement} elFrom The `[inX, inY]` is based on elFrom.
  34. * @param {HTMLElement} elTarget The `out` is based on elTarget.
  35. * @param {number} inX
  36. * @param {number} inY
  37. * @return {boolean} Whether transform successfully.
  38. */
  39. function transformLocalCoord(out, elFrom, elTarget, inX, inY) {
  40. return transformCoordWithViewport(_calcOut, elFrom, inX, inY, true) && transformCoordWithViewport(out, elTarget, _calcOut[0], _calcOut[1]);
  41. }
  42. /**
  43. * Transform between a "viewport coord" and a "local coord".
  44. * "viewport coord": the coord based on the left-top corner of the viewport
  45. * of the browser.
  46. * "local coord": the coord based on the input `el`. The origin point is at
  47. * the position of "left: 0; top: 0;" in the `el`.
  48. *
  49. * Support the case when CSS transform is used on el.
  50. *
  51. * @param {Array.<number>} out [inX: number, inY: number] The output. If `inverse: false`,
  52. * it represents "local coord", otherwise "vireport coord".
  53. * If can not transform, `out` will not be modified but return `false`.
  54. * @param {HTMLElement} el The "local coord" is based on the `el`, see comment above.
  55. * @param {number} inX If `inverse: false`,
  56. * it represents "vireport coord", otherwise "local coord".
  57. * @param {number} inY If `inverse: false`,
  58. * it represents "vireport coord", otherwise "local coord".
  59. * @param {boolean} [inverse=false]
  60. * `true`: from "viewport coord" to "local coord".
  61. * `false`: from "local coord" to "viewport coord".
  62. * @return {boolean} Whether transform successfully.
  63. */
  64. function transformCoordWithViewport(out, el, inX, inY, inverse) {
  65. if (el.getBoundingClientRect && env.domSupported && !isCanvasEl(el)) {
  66. var saved = el[EVENT_SAVED_PROP] || (el[EVENT_SAVED_PROP] = {});
  67. var markers = prepareCoordMarkers(el, saved);
  68. var transformer = preparePointerTransformer(markers, saved, inverse);
  69. if (transformer) {
  70. transformer(out, inX, inY);
  71. return true;
  72. }
  73. }
  74. return false;
  75. }
  76. function prepareCoordMarkers(el, saved) {
  77. var markers = saved.markers;
  78. if (markers) {
  79. return markers;
  80. }
  81. markers = saved.markers = [];
  82. var propLR = ['left', 'right'];
  83. var propTB = ['top', 'bottom'];
  84. for (var i = 0; i < 4; i++) {
  85. var marker = document.createElement('div');
  86. var stl = marker.style;
  87. var idxLR = i % 2;
  88. var idxTB = (i >> 1) % 2;
  89. stl.cssText = ['position: absolute', 'visibility: hidden', 'padding: 0', 'margin: 0', 'border-width: 0', 'user-select: none', 'width:0', 'height:0', // 'width: 5px',
  90. // 'height: 5px',
  91. propLR[idxLR] + ':0', propTB[idxTB] + ':0', propLR[1 - idxLR] + ':auto', propTB[1 - idxTB] + ':auto', ''].join('!important;');
  92. el.appendChild(marker);
  93. markers.push(marker);
  94. }
  95. return markers;
  96. }
  97. function preparePointerTransformer(markers, saved, inverse) {
  98. var transformerName = inverse ? 'invTrans' : 'trans';
  99. var transformer = saved[transformerName];
  100. var oldSrcCoords = saved.srcCoords;
  101. var oldCoordTheSame = true;
  102. var srcCoords = [];
  103. var destCoords = [];
  104. for (var i = 0; i < 4; i++) {
  105. var rect = markers[i].getBoundingClientRect();
  106. var ii = 2 * i;
  107. var x = rect.left;
  108. var y = rect.top;
  109. srcCoords.push(x, y);
  110. oldCoordTheSame = oldCoordTheSame && oldSrcCoords && x === oldSrcCoords[ii] && y === oldSrcCoords[ii + 1];
  111. destCoords.push(markers[i].offsetLeft, markers[i].offsetTop);
  112. } // Cache to avoid time consuming of `buildTransformer`.
  113. return oldCoordTheSame && transformer ? transformer : (saved.srcCoords = srcCoords, saved[transformerName] = inverse ? buildTransformer(destCoords, srcCoords) : buildTransformer(srcCoords, destCoords));
  114. }
  115. function isCanvasEl(el) {
  116. return el.nodeName.toUpperCase() === 'CANVAS';
  117. }
  118. exports.transformLocalCoord = transformLocalCoord;
  119. exports.transformCoordWithViewport = transformCoordWithViewport;
  120. exports.isCanvasEl = isCanvasEl;