| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140 |
- var DomUtil;
- /**
- * Detects support for options object argument in addEventListener.
- * https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener#Safely_detecting_option_support
- * @private
- */
- var supportsEventListenerOptions = function () {
- var supports = false;
- try {
- var options = Object.defineProperty({}, 'passive', {
- get: function get() {
- supports = true;
- }
- });
- window.addEventListener('e', null, options);
- } catch (e) {// continue regardless of error
- }
- return supports;
- }(); // Default passive to true as expected by Chrome for 'touchstart' and 'touchend' events.
- // https://github.com/chartjs/Chart.js/issues/4287
- var eventListenerOptions = supportsEventListenerOptions ? {
- passive: true
- } : false;
- function createEvent(type, chart, x, y, nativeEvent) {
- return {
- type: type,
- chart: chart,
- native: nativeEvent || null,
- x: x !== undefined ? x : null,
- y: y !== undefined ? y : null
- };
- }
- function fromNativeEvent(event, chart) {
- var type = event.type;
- var point = {};
- var touches = event.targetTouches;
- if (touches && touches.length > 0) {
- point.x = touches[0].clientX;
- point.y = touches[0].clientY;
- } else {
- point.x = event.clientX;
- point.y = event.clientY;
- }
- var canvas = chart.get('canvas');
- var pos = DomUtil.getRelativePosition(point, canvas);
- return createEvent(type, chart, pos.x, pos.y, event);
- }
- DomUtil = {
- /* global wx, my, module */
- isWx: typeof wx === 'object' && typeof wx.getSystemInfoSync === 'function',
- // weixin miniprogram
- isMy: typeof my === 'object' && typeof my.getSystemInfoSync === 'function',
- // ant miniprogram
- isNode: typeof module !== 'undefined' && typeof module.exports !== 'undefined',
- // in node
- isBrowser: typeof window !== 'undefined' && typeof window.document !== 'undefined' && typeof window.sessionStorage !== 'undefined',
- // in browser
- getPixelRatio: function getPixelRatio() {
- return window && window.devicePixelRatio || 1;
- },
- getStyle: function getStyle(el, property) {
- return el.currentStyle ? el.currentStyle[property] : document.defaultView.getComputedStyle(el, null).getPropertyValue(property);
- },
- getWidth: function getWidth(el) {
- var width = this.getStyle(el, 'width');
- if (width === 'auto') {
- width = el.offsetWidth;
- }
- return parseFloat(width);
- },
- getHeight: function getHeight(el) {
- var height = this.getStyle(el, 'height');
- if (height === 'auto') {
- height = el.offsetHeight;
- }
- return parseFloat(height);
- },
- getDomById: function getDomById(id) {
- if (!id) {
- return null;
- }
- return document.getElementById(id);
- },
- getRelativePosition: function getRelativePosition(point, canvas) {
- var canvasDom = canvas.get('el');
- var _canvasDom$getBoundin = canvasDom.getBoundingClientRect(),
- top = _canvasDom$getBoundin.top,
- right = _canvasDom$getBoundin.right,
- bottom = _canvasDom$getBoundin.bottom,
- left = _canvasDom$getBoundin.left;
- var paddingLeft = parseFloat(this.getStyle(canvasDom, 'padding-left'));
- var paddingTop = parseFloat(this.getStyle(canvasDom, 'padding-top'));
- var paddingRight = parseFloat(this.getStyle(canvasDom, 'padding-right'));
- var paddingBottom = parseFloat(this.getStyle(canvasDom, 'padding-bottom'));
- var width = right - left - paddingLeft - paddingRight;
- var height = bottom - top - paddingTop - paddingBottom;
- var pixelRatio = canvas.get('pixelRatio');
- var mouseX = (point.x - left - paddingLeft) / width * canvasDom.width / pixelRatio;
- var mouseY = (point.y - top - paddingTop) / height * canvasDom.height / pixelRatio;
- return {
- x: mouseX,
- y: mouseY
- };
- },
- addEventListener: function addEventListener(source, type, listener) {
- DomUtil.isBrowser && source.addEventListener(type, listener, eventListenerOptions);
- },
- removeEventListener: function removeEventListener(source, type, listener) {
- DomUtil.isBrowser && source.removeEventListener(type, listener, eventListenerOptions);
- },
- createEvent: function createEvent(event, chart) {
- return fromNativeEvent(event, chart);
- },
- measureText: function measureText(text, font, ctx) {
- if (!ctx) {
- ctx = document.createElement('canvas').getContext('2d');
- }
- ctx.font = font || '12px sans-serif';
- return ctx.measureText(text);
- }
- };
- module.exports = DomUtil;
|