Source
xxxxxxxxxx
* @param {mixed} type Type of SVG element or array of objects containing type, attribute and content fields.
/*
** Copyright (C) 2001-2025 Zabbix SIA
**
** This program is free software: you can redistribute it and/or modify it under the terms of
** the GNU Affero General Public License as published by the Free Software Foundation, version 3.
**
** This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
** without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
** See the GNU Affero General Public License for more details.
**
** You should have received a copy of the GNU Affero General Public License along with this program.
** If not, see <https://www.gnu.org/licenses/>.
**/
/**
* SVGCanvas class.
*
* Implements basic functionality needed to render SVG from JS.
*
* @param {object} options Canvas options.
* @param {number} options.width Canvas width (width attribute of a SVG image).
* @param {number} options.height Canvas height (height attribute of a SVG image).
* @param {boolean} options.mask Masking option for textarea elements (@see SVGCanvas.prototype.createTextarea)
* @param {boolean} shadowBuffer Shadow buffer (double buffering) support. If set to true, additional hidden
* group element is created within SVG.
*/
function SVGCanvas(options, shadowBuffer) {
this.options = options;
this.id = 0;
this.elements = [];
this.textPadding = 5;
this.maskColor = '#3d3d3d';
this.mask = false;
if (typeof options.mask !== 'undefined') {
this.mask = (options.mask === true);
}
if (typeof options.useViewBox !== 'boolean') {
options.useViewBox = false;
}
this.buffer = null;
var svg_options = options.useViewBox
? {
'viewBox': '0 0 ' + options.width + ' ' + options.height,
'style': 'max-width: ' + options.width + 'px; max-height: ' + options.height + 'px;',
'preserveAspectRatio': 'xMinYMin meet'
}
: {
'width': options.width,
'height': options.height
};
this.root = this.createElement('svg', svg_options, null);
if (shadowBuffer === true) {
this.buffer = this.root.add('g', {
class: 'shadow-buffer',
style: 'visibility: hidden;'
});
}
}
// Predefined namespaces for SVG as key => value
SVGCanvas.NAMESPACES = {
xlink: 'http://www.w3.org/1999/xlink'
};
/**
* Generate unique id.
* Id is unique within page context.
*
* @return {number} Unique id.
*/
SVGCanvas.getUniqueId = function () {
if (typeof SVGCanvas.uniqueId === 'undefined') {
SVGCanvas.uniqueId = 0;
}
return SVGCanvas.uniqueId++;
};
/**
* Create new SVG element.
* Additional workaround is added to implement textarea element as a text element with a set of tspan subelements.
*
* @param {string} type Element type (SVG tag).
* @param {object} attributes Element attributes (SVG tag attributes) as key => value pairs.
* @param {SVGElement} parent Parent element if any (or null if none).
* @param {mixed} content Element textContent of a set of subelements.