Extends mxAbstractCanvas2D to implement a canvas for SVG. This canvas writes all calls as SVG output to the given SVG root node.
var svgDoc = mxUtils.createXmlDocument(); var root = (svgDoc.createElementNS != null) ? svgDoc.createElementNS(mxConstants.NS_SVG, 'svg') : svgDoc.createElement('svg'); if (svgDoc.createElementNS == null) { root.setAttribute('xmlns', mxConstants.NS_SVG); root.setAttribute('xmlns:xlink', mxConstants.NS_XLINK); } else { root.setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:xlink', mxConstants.NS_XLINK); } var bounds = graph.getGraphBounds(); root.setAttribute('width', (bounds.x + bounds.width + 4) + 'px'); root.setAttribute('height', (bounds.y + bounds.height + 4) + 'px'); root.setAttribute('version', '1.1'); svgDoc.appendChild(root); var svgCanvas = new mxSvgCanvas2D(root);
A description of the public API is available in mxXmlCanvas2D.
To disable anti-aliasing in the output, use the following code.
graph.view.canvas.ownerSVGElement.setAttribute('shape-rendering', 'crispEdges');
Or set the respective attribute in the SVG element directly.
mxSvgCanvas2D | Extends mxAbstractCanvas2D to implement a canvas for SVG. |
Functions | |
mxSvgCanvas2D | Constructs a new SVG canvas. |
Variables | |
root | Reference to the container for the SVG content. |
gradients | Local cache of gradients for quick lookups. |
defs | Reference to the defs section of the SVG document. |
styleEnabled | Stores the value of styleEnabled passed to the constructor. |
path | Holds the current DOM node. |
matchHtmlAlignment | Specifies if plain text output should match the vertical HTML alignment. |
textEnabled | Specifies if text output should be enabled. |
foEnabled | Specifies if use of foreignObject for HTML markup is allowed. |
foAltText | Specifies the fallback text for unsupported foreignObjects in exported documents. |
foOffset | Offset to be used for foreignObjects. |
textOffset | Offset to be used for text elements. |
imageOffset | Offset to be used for image elements. |
strokeTolerance | Adds transparent paths for strokes. |
minStrokeWidth | Minimum stroke width for output. |
refCount | Local counter for references in SVG export. |
lineHeightCorrection | Correction factor for mxConstants.LINE_HEIGHT in HTML output. |
pointerEventsValue | Default value for active pointer events. |
fontMetricsPadding | Padding to be added for text that is not wrapped to account for differences in font metrics on different platforms in pixels. |
cacheOffsetSize | Specifies if offsetWidth and offsetHeight should be cached. |
Functions | |
format | Rounds all numbers to 2 decimal points. |
getBaseUrl | Returns the URL of the page without the hash part. |
reset | Returns any offsets for rendering pixels. |
createStyle | Creates the optional style section. |
createElement | Private helper function to create SVG elements |
getAlternateText | Returns the alternate text string for the given foreignObject. |
getAlternateContent | Returns the alternate content for the given foreignObject. |
createGradientId | Private helper function to create SVG elements |
getSvgGradient | Private helper function to create SVG elements |
createSvgGradient | Creates the given SVG gradient. |
addNode | Private helper function to create SVG elements |
updateFill | Transfers the stroke attributes from <state> to <node>. |
getCurrentStrokeWidth | Returns the current stroke width (>= 1), ie. |
updateStroke | Transfers the stroke attributes from <state> to <node>. |
updateStrokeAttributes | Transfers the stroke attributes from <state> to <node>. |
createDashPattern | Creates the SVG dash pattern for the given state. |
createTolerance | Creates a hit detection tolerance shape for the given node. |
createShadow | Creates a shadow for the given node. |
setLink | Experimental implementation for hyperlinks. |
rotate | Sets the rotation of the canvas. |
begin | Extends superclass to create path. |
rect | Private helper function to create SVG elements |
roundrect | Private helper function to create SVG elements |
ellipse | Private helper function to create SVG elements |
image | Private helper function to create SVG elements |
convertHtml | Converts the given HTML string to XHTML. |
createDiv | Private helper function to create SVG elements |
addForeignObject | Creates a foreignObject for the given string and adds it to the given root. |
getTextCss | Private helper function to create SVG elements |
text | Paints the given text. |
createClip | Creates a clip for the given coordinates. |
plainText | Paints the given text. |
updateFont | Updates the text properties for the given node. |
addTextBackground | Background color and border |
stroke | Paints the outline of the current path. |
fill | Fills the current path. |
fillAndStroke | Fills and paints the outline of the current path. |
function mxSvgCanvas2D( root, styleEnabled )
Constructs a new SVG canvas.
root | SVG container for the output. |
styleEnabled | Optional boolean that specifies if a style section should be added. The style section sets the default font-size, font-family and stroke-miterlimit globally. Default is false. |
mxSvgCanvas2D.prototype.lineHeightCorrection
Correction factor for mxConstants.LINE_HEIGHT in HTML output. Default is 1.
mxSvgCanvas2D.prototype.getBaseUrl = function()
Returns the URL of the page without the hash part. This needs to use href to include any search part with no params (ie question mark alone). This is a workaround for the fact that window.location.search is empty if there is no search string behind the question mark.
mxSvgCanvas2D.prototype.text = function( x, y, w, h, str, align, valign, wrap, format, overflow, clip, rotation, dir )
Paints the given text. Possible values for format are empty string for plain text and html for HTML markup. Note that HTML markup is only supported if foreignObject is supported and foEnabled is true. (This means IE9 and later does currently not support HTML text as part of shapes.)
Constructs a new SVG canvas.
function mxSvgCanvas2D( root, styleEnabled )
Reference to the container for the SVG content.
this.root
Local cache of gradients for quick lookups.
this.gradients
Reference to the defs section of the SVG document.
this.defs
Stores the value of styleEnabled passed to the constructor.
this.styleEnabled
Specifies if plain text output should match the vertical HTML alignment.
mxSvgCanvas2D.prototype.matchHtmlAlignment
Specifies if text output should be enabled.
mxSvgCanvas2D.prototype.textEnabled
Specifies if use of foreignObject for HTML markup is allowed.
mxSvgCanvas2D.prototype.foEnabled
Specifies the fallback text for unsupported foreignObjects in exported documents.
mxSvgCanvas2D.prototype.foAltText
Offset to be used for foreignObjects.
mxSvgCanvas2D.prototype.foOffset
Offset to be used for text elements.
mxSvgCanvas2D.prototype.textOffset
Offset to be used for image elements.
mxSvgCanvas2D.prototype.imageOffset
Adds transparent paths for strokes.
mxSvgCanvas2D.prototype.strokeTolerance
Minimum stroke width for output.
mxSvgCanvas2D.prototype.minStrokeWidth
Local counter for references in SVG export.
mxSvgCanvas2D.prototype.refCount
Correction factor for mxConstants.LINE_HEIGHT in HTML output.
mxSvgCanvas2D.prototype.lineHeightCorrection
Defines the default line height for text labels.
LINE_HEIGHT: 1.2
Default value for active pointer events.
mxSvgCanvas2D.prototype.pointerEventsValue
Padding to be added for text that is not wrapped to account for differences in font metrics on different platforms in pixels.
mxSvgCanvas2D.prototype.fontMetricsPadding
Specifies if offsetWidth and offsetHeight should be cached.
mxSvgCanvas2D.prototype.cacheOffsetSize
Rounds all numbers to 2 decimal points.
mxSvgCanvas2D.prototype.format = function( value )
Returns the URL of the page without the hash part.
mxSvgCanvas2D.prototype.getBaseUrl = function()
Returns any offsets for rendering pixels.
mxSvgCanvas2D.prototype.reset = function()
Creates the optional style section.
mxSvgCanvas2D.prototype.createStyle = function( x )
Private helper function to create SVG elements
mxSvgCanvas2D.prototype.createElement = function( tagName, namespace )
Returns the alternate text string for the given foreignObject.
mxSvgCanvas2D.prototype.getAlternateText = function( fo, x, y, w, h, str, align, valign, wrap, format, overflow, clip, rotation )
Private helper function to create SVG elements
mxSvgCanvas2D.prototype.createGradientId = function( start, end, alpha1, alpha2, direction )
Private helper function to create SVG elements
mxSvgCanvas2D.prototype.getSvgGradient = function( start, end, alpha1, alpha2, direction )
Creates the given SVG gradient.
mxSvgCanvas2D.prototype.createSvgGradient = function( start, end, alpha1, alpha2, direction )
Private helper function to create SVG elements
mxSvgCanvas2D.prototype.addNode = function( filled, stroked )
Transfers the stroke attributes from state to node.
mxSvgCanvas2D.prototype.updateFill = function()
Returns the current stroke width (>= 1), ie.
mxSvgCanvas2D.prototype.getCurrentStrokeWidth = function()
Transfers the stroke attributes from state to node.
mxSvgCanvas2D.prototype.updateStroke = function()
Transfers the stroke attributes from state to node.
mxSvgCanvas2D.prototype.updateStrokeAttributes = function()
Creates the SVG dash pattern for the given state.
mxSvgCanvas2D.prototype.createDashPattern = function( scale )
Creates a hit detection tolerance shape for the given node.
mxSvgCanvas2D.prototype.createTolerance = function( node )
Creates a shadow for the given node.
mxSvgCanvas2D.prototype.createShadow = function( node )
Experimental implementation for hyperlinks.
mxSvgCanvas2D.prototype.setLink = function( link )
Sets the rotation of the canvas.
mxSvgCanvas2D.prototype.rotate = function( theta, flipH, flipV, cx, cy )
Extends superclass to create path.
mxSvgCanvas2D.prototype.begin = function()
Private helper function to create SVG elements
mxSvgCanvas2D.prototype.rect = function( x, y, w, h )
Private helper function to create SVG elements
mxSvgCanvas2D.prototype.roundrect = function( x, y, w, h, dx, dy )
Private helper function to create SVG elements
mxSvgCanvas2D.prototype.ellipse = function( x, y, w, h )
Private helper function to create SVG elements
mxSvgCanvas2D.prototype.image = function( x, y, w, h, src, aspect, flipH, flipV )
Converts the given HTML string to XHTML.
mxSvgCanvas2D.prototype.convertHtml = function( val )
Private helper function to create SVG elements
mxSvgCanvas2D.prototype.createDiv = function( str )
Creates a foreignObject for the given string and adds it to the given root.
mxSvgCanvas2D.prototype.addForeignObject = function( x, y, w, h, str, align, valign, wrap, format, overflow, clip, rotation, dir, div, root )
Private helper function to create SVG elements
mxSvgCanvas2D.prototype.getTextCss = function()
Paints the given text.
mxSvgCanvas2D.prototype.text = function( x, y, w, h, str, align, valign, wrap, format, overflow, clip, rotation, dir )
Creates a clip for the given coordinates.
mxSvgCanvas2D.prototype.createClip = function( x, y, w, h )
Paints the given text.
mxSvgCanvas2D.prototype.plainText = function( x, y, w, h, str, align, valign, wrap, overflow, clip, rotation, dir )
Updates the text properties for the given node.
mxSvgCanvas2D.prototype.updateFont = function( node )
Background color and border
mxSvgCanvas2D.prototype.addTextBackground = function( node, str, x, y, w, h, align, valign, overflow )
Paints the outline of the current path.
mxSvgCanvas2D.prototype.stroke = function()
Fills the current path.
mxSvgCanvas2D.prototype.fill = function()
Fills and paints the outline of the current path.
mxSvgCanvas2D.prototype.fillAndStroke = function()