dom.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. "use strict";
  2. exports.__esModule = true;
  3. exports.isCanvasElement = isCanvasElement;
  4. exports.getPixelRatio = getPixelRatio;
  5. exports.getStyle = getStyle;
  6. exports.getWidth = getWidth;
  7. exports.getHeight = getHeight;
  8. exports.getDomById = getDomById;
  9. exports.getRelativePosition = getRelativePosition;
  10. exports.addEventListener = addEventListener;
  11. exports.removeEventListener = removeEventListener;
  12. exports.createEvent = createEvent;
  13. exports.convertPoints = convertPoints;
  14. exports.measureText = measureText;
  15. exports.isBrowser = exports.isNode = exports.isMy = exports.isWx = void 0;
  16. var _util = require("@antv/util");
  17. /**
  18. * Detects support for options object argument in addEventListener.
  19. * https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener#Safely_detecting_option_support
  20. * @private
  21. */
  22. var supportsEventListenerOptions = function () {
  23. var supports = false;
  24. try {
  25. var options = Object.defineProperty({}, 'passive', {
  26. get: function get() {
  27. supports = true;
  28. }
  29. });
  30. window.addEventListener('e', null, options);
  31. } catch (e) {// continue regardless of error
  32. }
  33. return supports;
  34. }(); // Default passive to true as expected by Chrome for 'touchstart' and 'touchend' events.
  35. // https://github.com/chartjs/Chart.js/issues/4287
  36. var eventListenerOptions = supportsEventListenerOptions ? {
  37. passive: true
  38. } : false;
  39. /* global wx, my */
  40. // weixin miniprogram
  41. var isWx = typeof wx === 'object' && typeof wx.getSystemInfoSync === 'function'; // ant miniprogram
  42. exports.isWx = isWx;
  43. var isMy = typeof my === 'object' && typeof my.getSystemInfoSync === 'function'; // in node
  44. exports.isMy = isMy;
  45. var isNode = typeof global && !typeof window; // in browser
  46. exports.isNode = isNode;
  47. var isBrowser = typeof window !== 'undefined' && typeof window.document !== 'undefined' && typeof window.sessionStorage !== 'undefined';
  48. exports.isBrowser = isBrowser;
  49. function isCanvasElement(el) {
  50. if (!el || typeof el !== 'object') return false;
  51. if (el.nodeType === 1 && el.nodeName) {
  52. // HTMLCanvasElement
  53. return true;
  54. } // CanvasElement
  55. return !!el.isCanvasElement;
  56. }
  57. function getPixelRatio() {
  58. return window && window.devicePixelRatio || 1;
  59. }
  60. function getStyle(el, property) {
  61. return el.currentStyle ? el.currentStyle[property] : document.defaultView.getComputedStyle(el, null).getPropertyValue(property);
  62. }
  63. function getWidth(el) {
  64. var width = getStyle(el, 'width');
  65. if (width === 'auto') {
  66. width = el.offsetWidth;
  67. }
  68. return parseFloat(width);
  69. }
  70. function getHeight(el) {
  71. var height = getStyle(el, 'height');
  72. if (height === 'auto') {
  73. height = el.offsetHeight;
  74. }
  75. return parseFloat(height);
  76. }
  77. function getDomById(id) {
  78. if (!id) {
  79. return null;
  80. }
  81. return document.getElementById(id);
  82. }
  83. function getRelativePosition(point, canvas) {
  84. var canvasDom = canvas.get('el');
  85. if (!canvasDom) return point;
  86. var _canvasDom$getBoundin = canvasDom.getBoundingClientRect(),
  87. top = _canvasDom$getBoundin.top,
  88. left = _canvasDom$getBoundin.left;
  89. var paddingLeft = parseFloat(getStyle(canvasDom, 'padding-left'));
  90. var paddingTop = parseFloat(getStyle(canvasDom, 'padding-top'));
  91. var mouseX = point.x - left - paddingLeft;
  92. var mouseY = point.y - top - paddingTop;
  93. return {
  94. x: mouseX,
  95. y: mouseY
  96. };
  97. }
  98. function addEventListener(source, type, listener) {
  99. source.addEventListener(type, listener, eventListenerOptions);
  100. }
  101. function removeEventListener(source, type, listener) {
  102. source.removeEventListener(type, listener, eventListenerOptions);
  103. }
  104. function landscapePoint(point, canvas) {
  105. var landscape = canvas.get('landscape');
  106. if (!landscape) {
  107. return point;
  108. }
  109. if ((0, _util.isFunction)(landscape)) {
  110. return landscape(point, canvas);
  111. } // 默认顺时针旋转90度
  112. var height = canvas.get('height');
  113. var x = point.y;
  114. var y = height - point.x;
  115. return {
  116. x: x,
  117. y: y
  118. };
  119. }
  120. function convertPoints(ev, canvas) {
  121. var touches = ev.touches; // 认为是mouse事件
  122. if (!touches) {
  123. var point = getRelativePosition({
  124. x: ev.clientX,
  125. y: ev.clientY
  126. }, canvas);
  127. return [landscapePoint(point, canvas)];
  128. } // 单指 touchend 后,touchs 会变空,最后的触点要从changedTouches里拿
  129. if (!touches.length) {
  130. // 为了防止万一,加个空逻辑
  131. touches = ev.changedTouches || [];
  132. }
  133. var points = [];
  134. for (var i = 0, len = touches.length; i < len; i++) {
  135. var touch = touches[i]; // x, y: 相对canvas原点的位置,clientX, clientY 相对于可视窗口的位置
  136. var x = touch.x,
  137. y = touch.y,
  138. clientX = touch.clientX,
  139. clientY = touch.clientY;
  140. var _point = void 0; // 小程序环境会有x,y
  141. if ((0, _util.isNumber)(x) || (0, _util.isNumber)(y)) {
  142. _point = {
  143. x: x,
  144. y: y
  145. };
  146. } else {
  147. // 浏览器环境再计算下canvas的相对位置
  148. _point = getRelativePosition({
  149. x: clientX,
  150. y: clientY
  151. }, canvas);
  152. }
  153. points.push(landscapePoint(_point, canvas));
  154. }
  155. return points;
  156. }
  157. function createEvent(event, chart) {
  158. var canvas = chart.get('canvas');
  159. var points = convertPoints(event, canvas); // touchend会没有points
  160. var point = points[0] || {};
  161. return {
  162. type: event.type,
  163. chart: chart,
  164. "native": event,
  165. x: point.x,
  166. y: point.y
  167. };
  168. }
  169. function measureText(text, font, ctx) {
  170. if (!ctx) {
  171. ctx = document.createElement('canvas').getContext('2d');
  172. }
  173. ctx.font = font || '12px sans-serif';
  174. return ctx.measureText(text);
  175. }