/***** * * SVGUtil.es * * Author: Antoine Quint * *****/ SVGUtil.VERSION = 0.1; function SVGUtil () {}; /***** Namespaces *****/ SVGUtil.NS = { SVG: 'http://www.w3.org/2000/svg', XLink: 'http://www.w3.org/1999/xlink', XMLEvents: 'http://www.w3.org/2001/xml-events', XForms: 'www.w3.org/2002/01/xforms', FUI: 'http://xmlns.fuchsia-design.com/ui/' }; /***** SVG DOM routines *****/ SVGUtil.CreateElement = function (name, attributes) { var str; if (arguments.length == 1) { str = '<' + name + ' xmlns="http://www.w3.org/2000/svg" />'; } else if (arguments.length == 2) { var xmlStr = ''; if (attributes.xml) { for (var i=0; i'; } var fragment = parseXML(str, document); return fragment; }; SVGUtil.CreateSVGPoint = function (x,y) { var p = document.documentElement.createSVGPoint(); p.x = x; p.y = y; return p; }; SVGUtil.CreateSVGRect = function (x,y,w,h) { return { x: x, y: y, width: w, height: h }; }; SVGUtil.InsertAfter = function (newChild, refChild) { refChild.parentNode.insertBefore( newChild, (refChild?refChild.nextSibling:parent.firstChild) ); }; /***** Mouse Methods *****/ SVGUtil.GetClientMouseCoords = function (e) { return this.CreateSVGPoint(e.clientX, e.clientY); }; SVGUtil.GetMouseCoordsInContext = function (e, context) { var c = this.GetClientMouseCoords(e); var m = context.getScreenCTM().inverse(); return c.matrixTransform(m); }; /***** Bounding Box Methods *****/ SVGUtil.GetBBox = function (elem) { var m = elem.getScreenCTM(); return this._ComputeBBox(m, elem); }; SVGUtil.GetBBoxInContext = function (elem, context) { var m = elem.getTransformToElement(context); return this._ComputeBBox(m, elem); }; SVGUtil._ComputeBBox = function (m, elem) { var box; if (elem.shadowTree) { box = elem.shadowTree.getBBox(); } else { box = elem.getBBox(); } var corners = []; var point = this.CreateSVGPoint(box.x, box.y); corners.push( point.matrixTransform(m) ); point.x = box.x + box.width; point.y = box.y; corners.push( point.matrixTransform(m) ); point.x = box.x + box.width; point.y = box.y + box.height; corners.push( point.matrixTransform(m) ); point.x = box.x; point.y = box.y + box.height; corners.push( point.matrixTransform(m) ); var max = this.CreateSVGPoint(corners[0].x, corners[0].y); var min = this.CreateSVGPoint(corners[0].x, corners[0].y); for (var i = 1; i < corners.length; i++) { var x = corners[i].x; var y = corners[i].y; if (x < min.x) { min.x = x; } else if (x > max.x) { max.x = x; } if (y < min.y) { min.y = y; } else if (y > max.y) { max.y = y; } } return this.CreateSVGRect(min.x, min.y, max.x - min.x, max.y - min.y); };