UNPKG

ag-charts-community

Version:

Advanced Charting / Charts supporting Javascript / Typescript / React / Angular / Vue

301 lines 11.5 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); /** * Wraps the native Canvas element and overrides its CanvasRenderingContext2D to * provide resolution independent rendering based on `window.devicePixelRatio`. */ var HdpiCanvas = /** @class */ (function () { // The width/height attributes of the Canvas element default to // 300/150 according to w3.org. function HdpiCanvas(document, width, height) { if (document === void 0) { document = window.document; } if (width === void 0) { width = 600; } if (height === void 0) { height = 300; } this._container = undefined; // `NaN` is deliberate here, so that overrides are always applied // and the `resetTransform` inside the `resize` method works in IE11. this._pixelRatio = NaN; this._width = 0; this._height = 0; this.document = document; this.element = document.createElement('canvas'); this.context = this.element.getContext('2d'); this.element.style.userSelect = 'none'; this.element.style.display = 'block'; this.setPixelRatio(); this.resize(width, height); } Object.defineProperty(HdpiCanvas.prototype, "container", { get: function () { return this._container; }, set: function (value) { if (this._container !== value) { this.remove(); if (value) { value.appendChild(this.element); } this._container = value; } }, enumerable: true, configurable: true }); HdpiCanvas.prototype.remove = function () { var parentNode = this.element.parentNode; if (parentNode != null) { parentNode.removeChild(this.element); } }; HdpiCanvas.prototype.destroy = function () { this.element.remove(); this._canvas = undefined; Object.freeze(this); }; HdpiCanvas.prototype.toImage = function () { var img = this.document.createElement('img'); img.src = this.getDataURL(); return img; }; HdpiCanvas.prototype.getDataURL = function (type) { return this.element.toDataURL(type); }; /** * @param fileName The name of the file upon save. The `.png` extension is going to be added automatically. */ HdpiCanvas.prototype.download = function (fileName) { fileName = ((fileName || '').trim() || 'image') + '.png'; // Chart images saved as JPEG are a few times larger at 50% quality than PNG images, // so we don't support saving to JPEG. var type = 'image/png'; var dataUrl = this.getDataURL(type); var document = this.document; var a = document.createElement('a'); a.href = dataUrl; a.download = fileName; a.style.display = 'none'; document.body.appendChild(a); // required for the `click` to work in Firefox a.click(); document.body.removeChild(a); }; Object.defineProperty(HdpiCanvas.prototype, "pixelRatio", { get: function () { return this._pixelRatio; }, enumerable: true, configurable: true }); /** * Changes the pixel ratio of the Canvas element to the given value, * or uses the window.devicePixelRatio (default), then resizes the Canvas * element accordingly (default). */ HdpiCanvas.prototype.setPixelRatio = function (ratio) { var pixelRatio = ratio || window.devicePixelRatio; if (pixelRatio === this.pixelRatio) { return; } HdpiCanvas.overrideScale(this.context, pixelRatio); this._pixelRatio = pixelRatio; this.resize(this.width, this.height); }; Object.defineProperty(HdpiCanvas.prototype, "pixelated", { get: function () { return this.element.style.imageRendering === 'pixelated'; }, set: function (value) { this.element.style.imageRendering = value ? 'pixelated' : 'auto'; }, enumerable: true, configurable: true }); Object.defineProperty(HdpiCanvas.prototype, "width", { get: function () { return this._width; }, enumerable: true, configurable: true }); Object.defineProperty(HdpiCanvas.prototype, "height", { get: function () { return this._height; }, enumerable: true, configurable: true }); HdpiCanvas.prototype.resize = function (width, height) { if (!(width > 0 && height > 0)) { return; } var _a = this, element = _a.element, context = _a.context, pixelRatio = _a.pixelRatio; element.width = Math.round(width * pixelRatio); element.height = Math.round(height * pixelRatio); element.style.width = width + 'px'; element.style.height = height + 'px'; context.resetTransform(); this._width = width; this._height = height; }; Object.defineProperty(HdpiCanvas, "textMeasuringContext", { get: function () { if (this._textMeasuringContext) { return this._textMeasuringContext; } var canvas = document.createElement('canvas'); return this._textMeasuringContext = canvas.getContext('2d'); }, enumerable: true, configurable: true }); Object.defineProperty(HdpiCanvas, "svgText", { get: function () { if (this._svgText) { return this._svgText; } var xmlns = 'http://www.w3.org/2000/svg'; var svg = document.createElementNS(xmlns, 'svg'); svg.setAttribute('width', '100'); svg.setAttribute('height', '100'); // Add a descriptive class name in case someone sees this SVG element // in devtools and wonders about its purpose: if (svg.classList) { svg.classList.add('text-measuring-svg'); } else { svg.setAttribute('class', 'text-measuring-svg'); } svg.style.position = 'absolute'; svg.style.top = '-1000px'; svg.style.visibility = 'hidden'; var svgText = document.createElementNS(xmlns, 'text'); svgText.setAttribute('x', '0'); svgText.setAttribute('y', '30'); svgText.setAttribute('text', 'black'); svg.appendChild(svgText); document.body.appendChild(svg); this._svgText = svgText; return svgText; }, enumerable: true, configurable: true }); Object.defineProperty(HdpiCanvas, "has", { get: function () { if (this._has) { return this._has; } var isChrome = navigator.userAgent.indexOf('Chrome') > -1; var isFirefox = navigator.userAgent.indexOf('Firefox') > -1; var isSafari = !isChrome && navigator.userAgent.indexOf('Safari') > -1; return this._has = Object.freeze({ textMetrics: this.textMeasuringContext.measureText('test').actualBoundingBoxDescent !== undefined // Firefox implemented advanced TextMetrics object in v74: // https://bugzilla.mozilla.org/show_bug.cgi?id=1102584 // but it's buggy, so we'll keed using the SVG for text measurement in Firefox for now. && !isFirefox && !isSafari, getTransform: this.textMeasuringContext.getTransform !== undefined }); }, enumerable: true, configurable: true }); HdpiCanvas.measureText = function (text, font, textBaseline, textAlign) { var ctx = this.textMeasuringContext; ctx.font = font; ctx.textBaseline = textBaseline; ctx.textAlign = textAlign; return ctx.measureText(text); }; /** * Returns the width and height of the measured text. * @param text The single-line text to measure. * @param font The font shorthand string. */ HdpiCanvas.getTextSize = function (text, font) { if (this.has.textMetrics) { var ctx = this.textMeasuringContext; ctx.font = font; var metrics = ctx.measureText(text); return { width: metrics.width, height: metrics.actualBoundingBoxAscent + metrics.actualBoundingBoxDescent }; } else { return this.measureSvgText(text, font); } }; HdpiCanvas.measureSvgText = function (text, font) { var cache = this.textSizeCache; var fontCache = cache[font]; // Note: consider not caching the size of numeric strings. // For example: if (isNaN(+text)) { // skip if (fontCache) { var size_1 = fontCache[text]; if (size_1) { return size_1; } } else { cache[font] = {}; } var svgText = this.svgText; svgText.style.font = font; svgText.textContent = text; // `getBBox` returns an instance of `SVGRect` with the same `width` and `height` // measurements as `DOMRect` instance returned by the `getBoundingClientRect`. // But the `SVGRect` instance has half the properties of the `DOMRect`, // so we use the `getBBox` method. var bbox = svgText.getBBox(); var size = { width: bbox.width, height: bbox.height }; cache[font][text] = size; return size; }; HdpiCanvas.overrideScale = function (ctx, scale) { var depth = 0; var overrides = { save: function () { this.$save(); depth++; }, restore: function () { if (depth > 0) { this.$restore(); depth--; } }, setTransform: function (a, b, c, d, e, f) { this.$setTransform(a * scale, b * scale, c * scale, d * scale, e * scale, f * scale); }, resetTransform: function () { // As of Jan 8, 2019, `resetTransform` is still an "experimental technology", // and doesn't work in IE11 and Edge 44. this.$setTransform(scale, 0, 0, scale, 0, 0); this.save(); depth = 0; // The scale above will be impossible to restore, // because we override the `ctx.restore` above and // check `depth` there. } }; for (var name_1 in overrides) { if (overrides.hasOwnProperty(name_1)) { // Save native methods under prefixed names, // if this hasn't been done by the previous overrides already. if (!ctx['$' + name_1]) { ctx['$' + name_1] = ctx[name_1]; } // Replace native methods with overrides, // or previous overrides with the new ones. ctx[name_1] = overrides[name_1]; } } }; HdpiCanvas.textSizeCache = {}; return HdpiCanvas; }()); exports.HdpiCanvas = HdpiCanvas; //# sourceMappingURL=hdpiCanvas.js.map