/* coding: utf-8 */ var SvgBbox = function (svg) { this.svg = svg; this.width = document.body.clientWidth; this.height = null; this.fringe = 0; this.scale1px = null; this.vaspOption = 1; }; SvgBbox.prototype.calc = function (id) { var chd = this.svg.getElementById(id).childNodes, aryX = Array(), aryY = Array(); for (var i = 0, len = chd.length; i < len; i++) { var ele = chd[i], tgn = ele.tagName; if (tgn === 'ellipse') { aryX.push(Number(ele.getAttribute('cx'))); aryY.push(Number(ele.getAttribute('cy'))); } else if (tgn === 'text') { aryX.push(Number(ele.getAttribute('x'))); aryY.push(Number(ele.getAttribute('y'))); } else if (tgn === 'path') { var seg = ele.pathSegList; for (var j = 0, len2 = seg.length; j < len2; j++) { var s = seg[j]; if (s.pathSegType === 2 || s.pathSegType === 4) { // only SVGPathSegMovetoAbs or SVGPathSegLinetoAbs aryX.push(s.x); aryY.push(s.y); } } } } var x1 = Math.min.apply(null, aryX), y1 = Math.min.apply(null, aryY), x2 = Math.max.apply(null, aryX), y2 = Math.max.apply(null, aryY), hh = y2 - y1, ww = x2 - x1; this.scale1px = ww / this.width; this.height = hh / this.scale1px; var fgx = this.fringe * this.scale1px, fgy = this.fringe * this.scale1px / this.vaspOption; fg2x = this.fringe * 2, fg2y = this.fringe * 2 / this.vaspOption; this.svg.setAttribute('preserveAspectRatio', 'none'); this.svg.setAttribute('height', this.height * this.vaspOption + fg2y); this.svg.setAttribute('width', this.width + fg2x); this.svg.setAttribute('viewBox', Array( x1 - fgx, y1 - fgy, x2 - x1 + fgx * 2, y2 - y1 + fgy * 2).join(' ')); };