dom.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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.measureText = measureText;
  14. exports.isBrowser = exports.isNode = exports.isMy = exports.isWx = void 0;
  15. /**
  16. * Detects support for options object argument in addEventListener.
  17. * https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener#Safely_detecting_option_support
  18. * @private
  19. */
  20. var supportsEventListenerOptions = function () {
  21. var supports = false;
  22. try {
  23. var options = Object.defineProperty({}, 'passive', {
  24. get: function get() {
  25. supports = true;
  26. }
  27. });
  28. window.addEventListener('e', null, options);
  29. } catch (e) {// continue regardless of error
  30. }
  31. return supports;
  32. }(); // Default passive to true as expected by Chrome for 'touchstart' and 'touchend' events.
  33. // https://github.com/chartjs/Chart.js/issues/4287
  34. var eventListenerOptions = supportsEventListenerOptions ? {
  35. passive: true
  36. } : false;
  37. /* global wx, my */
  38. // weixin miniprogram
  39. var isWx = typeof wx === 'object' && typeof wx.getSystemInfoSync === 'function'; // ant miniprogram
  40. exports.isWx = isWx;
  41. var isMy = typeof my === 'object' && typeof my.getSystemInfoSync === 'function'; // in node
  42. exports.isMy = isMy;
  43. var isNode = typeof module !== 'undefined' && typeof module.exports !== 'undefined'; // in browser
  44. exports.isNode = isNode;
  45. var isBrowser = typeof window !== 'undefined' && typeof window.document !== 'undefined' && typeof window.sessionStorage !== 'undefined';
  46. exports.isBrowser = isBrowser;
  47. function isCanvasElement(el) {
  48. if (!el || typeof el !== 'object') return false;
  49. if (el.nodeType === 1 && el.nodeName) {
  50. // HTMLCanvasElement
  51. return true;
  52. } // CanvasElement
  53. return !!el.isCanvasElement;
  54. }
  55. function getPixelRatio() {
  56. return window && window.devicePixelRatio || 1;
  57. }
  58. function getStyle(el, property) {
  59. return el.currentStyle ? el.currentStyle[property] : document.defaultView.getComputedStyle(el, null).getPropertyValue(property);
  60. }
  61. function getWidth(el) {
  62. var width = getStyle(el, 'width');
  63. if (width === 'auto') {
  64. width = el.offsetWidth;
  65. }
  66. return parseFloat(width);
  67. }
  68. function getHeight(el) {
  69. var height = getStyle(el, 'height');
  70. if (height === 'auto') {
  71. height = el.offsetHeight;
  72. }
  73. return parseFloat(height);
  74. }
  75. function getDomById(id) {
  76. if (!id) {
  77. return null;
  78. }
  79. return document.getElementById(id);
  80. }
  81. function getRelativePosition(point, canvas) {
  82. var canvasDom = canvas.get('el');
  83. if (!canvasDom) return point;
  84. var _canvasDom$getBoundin = canvasDom.getBoundingClientRect(),
  85. top = _canvasDom$getBoundin.top,
  86. right = _canvasDom$getBoundin.right,
  87. bottom = _canvasDom$getBoundin.bottom,
  88. left = _canvasDom$getBoundin.left;
  89. var paddingLeft = parseFloat(getStyle(canvasDom, 'padding-left'));
  90. var paddingTop = parseFloat(getStyle(canvasDom, 'padding-top'));
  91. var paddingRight = parseFloat(getStyle(canvasDom, 'padding-right'));
  92. var paddingBottom = parseFloat(getStyle(canvasDom, 'padding-bottom'));
  93. var width = right - left - paddingLeft - paddingRight;
  94. var height = bottom - top - paddingTop - paddingBottom;
  95. var pixelRatio = canvas.get('pixelRatio');
  96. var mouseX = (point.x - left - paddingLeft) / width * canvasDom.width / pixelRatio;
  97. var mouseY = (point.y - top - paddingTop) / height * canvasDom.height / pixelRatio;
  98. return {
  99. x: mouseX,
  100. y: mouseY
  101. };
  102. }
  103. function addEventListener(source, type, listener) {
  104. source.addEventListener(type, listener, eventListenerOptions);
  105. }
  106. function removeEventListener(source, type, listener) {
  107. source.removeEventListener(type, listener, eventListenerOptions);
  108. }
  109. function createEventObj(type, chart, x, y, nativeEvent) {
  110. return {
  111. type: type,
  112. chart: chart,
  113. "native": nativeEvent || null,
  114. x: x !== undefined ? x : null,
  115. y: y !== undefined ? y : null
  116. };
  117. }
  118. function createEvent(event, chart) {
  119. var type = event.type;
  120. var clientPoint; // 说明是touch相关事件
  121. if (event.touches) {
  122. // https://developer.mozilla.org/zh-CN/docs/Web/API/TouchEvent/changedTouches
  123. // 这里直接拿changedTouches就可以了,不管是touchstart, touchmove, touchend changedTouches 都是有的
  124. // 为了以防万一,做个空判断
  125. var touch = event.changedTouches[0] || {}; // x, y: 相对canvas原点的位置,clientX, clientY 相对于可视窗口的位置
  126. var x = touch.x,
  127. y = touch.y,
  128. clientX = touch.clientX,
  129. clientY = touch.clientY; // 小程序环境会有x,y,这里就直接返回
  130. if (x && y) {
  131. return createEventObj(type, chart, x, y, event);
  132. }
  133. clientPoint = {
  134. x: clientX,
  135. y: clientY
  136. };
  137. } else {
  138. // mouse相关事件
  139. clientPoint = {
  140. x: event.clientX,
  141. y: event.clientY
  142. };
  143. } // 理论上应该是只有有在浏览器环境才会走到这里
  144. var canvas = chart.get('canvas'); // 通过clientX, clientY 计算x, y
  145. var point = getRelativePosition(clientPoint, canvas);
  146. return createEventObj(type, chart, point.x, point.y, event);
  147. }
  148. function measureText(text, font, ctx) {
  149. if (!ctx) {
  150. ctx = document.createElement('canvas').getContext('2d');
  151. }
  152. ctx.font = font || '12px sans-serif';
  153. return ctx.measureText(text);
  154. }