GeezerVison is showing maxgraph.github.io — SvgCanvas2D | @maxgraph/core
Extends AbstractCanvas2D to implement a canvas for SVG.
This canvas writes all calls as SVG output to the given SVG root node.
const svgDoc = xmlUtils.createXmlDocument(); const root = svgDoc.createElementNS ? svgDoc.createElementNS(constants.NS_SVG, 'svg') : svgDoc.createElement('svg');
if (!svgDoc.createElementNS) { root.setAttribute('xmlns', constants.NS_SVG); root.setAttribute('xmlns:xlink', constants.NS_XLINK); } else { root.setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:xlink', constants.NS_XLINK); }
const 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);
const svgCanvas = new mxSvgCanvas2D(root);
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.
Constructors
constructor
-
new SvgCanvas2D(root: SVGElement, styleEnabled: boolean): SvgCanvas2D
Parameters
- root: SVGElement
- styleEnabled: boolean
Returns SvgCanvas2D
Properties
cacheOffsetSize
cacheOffsetSize: boolean = true
Specifies if offsetWidth and offsetHeight should be cached. This is used to speed up repaint of text in updateText.
closeOp
closeOp: string = 'Z'
Holds the operator for closing curves. Default is 'Z'.
converter
Holds the to convert image URLs.
curveOp
curveOp: string = 'C'
Contains the string used for bezier curves. Default is 'C'.
defs
defs: SVGDefsElement | null = null
foAltText
foAltText: string = '[Object]'
Specifies the fallback text for unsupported foreignObjects in exported documents.
If this is set to null then no fallback text is added to the exported document.
foEnabled
foEnabled: boolean = true
Specifies if use of foreignObject for HTML markup is allowed.
fontMetricsPadding
fontMetricsPadding: number = 10
Padding to be added for text that is not wrapped to account for differences in font metrics on different platforms in pixels.
foOffset
foOffset: number = 0
Offset to be used for foreignObjects.
imageOffset
imageOffset: number = 0
Offset to be used for image elements.
lastX
lastX: number = 0
Holds the last x coordinate.
lastY
lastY: number = 0
Holds the last y coordinate.
lineHeightCorrection
lineHeightCorrection: number = 1
lineOp
lineOp: string = 'L'
Contains the string used for moving in paths. Default is 'L'.
matchHtmlAlignment
matchHtmlAlignment: boolean = true
Specifies if plain text output should match the vertical HTML alignment.
minStrokeWidth
minStrokeWidth: number = 1
Minimum stroke width for output.
moveOp
moveOp: string = 'M'
Contains the string used for moving in paths. Default is 'M'.
node
node: SVGElement | null = null
Holds the current DOM node.
originalRoot
originalRoot: SVGElement | null = null
path
path: (string | number)[] = []
Holds the current path as an array.
pointerEvents
pointerEvents: boolean = false
Boolean value that specifies if events should be handled. Default is false.
pointerEventsValue
pointerEventsValue: string = 'all'
Default value for active pointer events.
quadOp
quadOp: string = 'Q'
Contains the string used for quadratic paths. Default is 'Q'.
refCount
refCount: number = 0
Local counter for references in SVG export.
root
root: SVGElement | null
rotateHtml
rotateHtml: boolean = true
Switch for rotation of HTML. Default is false.
strokeTolerance
strokeTolerance: number = 0
Adds transparent paths for strokes.
styleEnabled
styleEnabled: boolean = true
textEnabled
textEnabled: boolean = true
Specifies if text output should be enabled.
textOffset
textOffset: number = 0
Offset to be used for text elements.
Methods
addForeignObject
-
addForeignObject( x: number, y: number, w: number, h: number, str: string, align: AlignValue, valign: VAlignValue, wrap: boolean, format: string, overflow: OverflowValue, clip: boolean, rotation: number, dir: TextDirectionValue, div: HTMLElement, root: SVGElement,): void
Creates a foreignObject for the given string and adds it to the given root.
Returns void
addNode
-
addNode(filled: boolean, stroked: boolean): void
Private helper function to create SVG elements
Parameters
- filled: boolean
- stroked: boolean
Returns void
addOp
-
addOp(op: string, ...args: number[]): void
Adds the given operation to the path.
Parameters
- op: string
- ...args: number[]
Returns void
addTextBackground
-
Background color and border
Returns void
arcTo
-
arcTo( rx: number, ry: number, angle: number, largeArcFlag: boolean, sweepFlag: boolean, x: number, y: number,): void
Adds the given arc to the current path. This is a synthetic operation that
is broken down into curves.
Parameters
- rx: number
- ry: number
- angle: number
- largeArcFlag: boolean
- sweepFlag: boolean
- x: number
- y: number
Returns void
close
-
close( x1?: number, y1?: number, x2?: number, y2?: number, x3?: number, y3?: number,): void
Parameters
Optionalx1: number
Optionaly1: number
Optionalx2: number
Optionaly2: number
Optionalx3: number
Optionaly3: number
Returns void
convertHtml
-
convertHtml(val: string): string
Converts the given HTML string to XHTML.
Returns string
createAlternateContent
-
createAlternateContent( fo: SVGForeignObjectElement, x: number, y: number, w: number, h: number, str: string, align: AlignValue, valign: VAlignValue, wrap: boolean, format: string, overflow: OverflowValue, clip: boolean, rotation: number,): SVGElement | null
Returns the alternate content for the given foreignObject.
Parameters
- fo: SVGForeignObjectElement
- x: number
- y: number
- w: number
- h: number
- str: string
- align: AlignValue
- valign: VAlignValue
- wrap: boolean
- format: string
- overflow: OverflowValue
- clip: boolean
- rotation: number
Returns SVGElement | null
createClip
-
createClip(x: number, y: number, w: number, h: number): SVGElement
Creates a clip for the given coordinates.
Parameters
- x: number
- y: number
- w: number
- h: number
Returns SVGElement
createDashPattern
-
createDashPattern(scale: number): string
Creates the SVG dash pattern for the given state.
Returns string
createDiv
-
createDiv(str: string | HTMLElement): HTMLElement
Private helper function to create SVG elements
Note: signature changed in mxgraph 4.1.0
Parameters
- str: string | HTMLElement
Returns HTMLElement
createElement
-
createElement(tagName: string, namespace?: string): SVGElement
Private helper function to create SVG elements
Parameters
- tagName: string
Optionalnamespace: string
Returns SVGElement
createGradientId
-
createGradientId( start: string, end: string, alpha1: number, alpha2: number, direction: DirectionValue,): string
Private helper function to create SVG elements
Parameters
- start: string
- end: string
- alpha1: number
- alpha2: number
- direction: DirectionValue
Returns string
createShadow
-
createShadow(node: SVGElement): SVGElement
Creates a shadow for the given node.
Returns SVGElement
createState
-
Creates the state of the this canvas.
createStyle
-
createStyle(): SVGElement
Creates the optional style section.
Returns SVGElement
createSvgGradient
-
createSvgGradient( start: string, end: string, alpha1: number, alpha2: number, direction: DirectionValue,): Gradient
Creates the given SVG gradient.
Parameters
- start: string
- end: string
- alpha1: number
- alpha2: number
- direction: DirectionValue
createTolerance
-
createTolerance(node: SVGElement): SVGElement
Creates a hit detection tolerance shape for the given node.
Returns SVGElement
createUrlConverter
-
Create a new and returns it.
curveTo
-
curveTo( x1: number, y1: number, x2: number, y2: number, x3: number, y3: number,): void
Adds a bezier curve to the current path.
Parameters
- x1: number
- y1: number
- x2: number
- y2: number
- x3: number
- y3: number
Returns void
ellipse
-
ellipse(x: number, y: number, w: number, h: number): void
Private helper function to create SVG elements
Parameters
- x: number
- y: number
- w: number
- h: number
Returns void
end
-
Empty implementation for backwards compatibility. This will be removed.
Returns void
fillAndStroke
-
Fills and paints the outline of the current path.
Returns void
format
-
format(value: number): number
Rounds all numbers to 2 decimal points.
Returns number
getAlternateText
-
getAlternateText( fo: SVGForeignObjectElement, x: number, y: number, w: number, h: number, str: string | Element, align: AlignValue, valign: VAlignValue, wrap: boolean, format: string, overflow: OverflowValue, clip: boolean, rotation: number,): string | null
Returns the alternate text string for the given foreignObject.
Parameters
- fo: SVGForeignObjectElement
- x: number
- y: number
- w: number
- h: number
- str: string | Element
- align: AlignValue
- valign: VAlignValue
- wrap: boolean
- format: string
- overflow: OverflowValue
- clip: boolean
- rotation: number
Returns string | null
getBaseUrl
-
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.
Returns string
getCurrentStrokeWidth
-
getCurrentStrokeWidth(): number
Returns the current stroke width (>= 1), ie. max(1, this.format(this.state.strokeWidth * this.state.scale)).
Returns number
getSvgGradient
-
getSvgGradient( start: string, end: string, alpha1: number, alpha2: number, direction: DirectionValue,): string | null
Private helper function to create SVG elements
Parameters
- start: string
- end: string
- alpha1: number
- alpha2: number
- direction: DirectionValue
Returns string | null
getTextCss
-
Private helper function to create SVG elements
Returns string
image
-
image( x: number, y: number, w: number, h: number, src: string, aspect?: boolean, flipH?: boolean, flipV?: boolean,): void
Private helper function to create SVG elements
Parameters
- x: number
- y: number
- w: number
- h: number
- src: string
- aspect: boolean = true
- flipH: boolean = false
- flipV: boolean = false
Returns void
lineTo
-
lineTo(x: number, y: number): void
Draws a line to the given coordinates. Uses moveTo with the op argument.
Returns void
moveTo
-
moveTo(x: number, y: number): void
Moves the current path the given coordinates.
Returns void
plainText
-
plainText( x: number, y: number, w: number, h: number, str: string, align: AlignValue, valign: VAlignValue, wrap: boolean, overflow: OverflowValue, clip: boolean, rotation?: number, dir: TextDirectionValue,): void
Paints the given text. Possible values for format are empty string for
plain text and html for HTML markup.
Returns void
quadTo
-
quadTo(x1: number, y1: number, x2: number, y2: number): void
Adds a quadratic curve to the current path.
Parameters
- x1: number
- y1: number
- x2: number
- y2: number
Returns void
rect
-
rect(x: number, y: number, w: number, h: number): void
Private helper function to create SVG elements
Parameters
- x: number
- y: number
- w: number
- h: number
Returns void
reset
-
Returns any offsets for rendering pixels.
Returns void
restore
-
Restores the current state.
Returns void
rotate
-
rotate( theta: number, flipH: boolean, flipV: boolean, cx: number, cy: number,): void
Sets the rotation of the canvas. Note that rotation cannot be concatenated.
Parameters
- theta: number
- flipH: boolean
- flipV: boolean
- cx: number
- cy: number
Returns void
rotatePoint
-
rotatePoint(x: number, y: number, theta: number, cx: number, cy: number): Point
Rotates the given point and returns the result as an Point.
Parameters
- x: number
- y: number
- theta: number
- cx: number
- cy: number
roundrect
-
roundrect( x: number, y: number, w: number, h: number, dx: number, dy: number,): void
Private helper function to create SVG elements
Parameters
- x: number
- y: number
- w: number
- h: number
- dx: number
- dy: number
Returns void
scale
-
scale(value: number): void
Scales the current state.
Returns void
setAlpha
-
setAlpha(value: number): void
setDashed
-
setDashed(value: boolean, fixDash?: boolean): void
Enables or disables dashed lines.
Parameters
- value: boolean
- fixDash: boolean = false
Returns void
setDashPattern
-
setDashPattern(value: string): void
Sets the current dash pattern.
Returns void
setFillAlpha
-
setFillAlpha(value: number): void
Sets the current solid fill alpha.
Returns void
setFillColor
-
setFillColor(value: string | null): void
Sets the current fill color.
Returns void
setFontBackgroundColor
-
setFontBackgroundColor(value: string | null): void
Sets the current font background color.
Returns void
setFontBorderColor
-
setFontBorderColor(value: string | null): void
Sets the current font border color.
Returns void
setFontColor
-
setFontColor(value: string | null): void
Sets the current font color.
Returns void
setFontFamily
-
setFontFamily(value: string): void
Sets the current font family.
Returns void
setFontSize
-
setFontSize(value: number): void
Sets the current font size.
Returns void
setFontStyle
-
setFontStyle(value: number): void
Sets the current font style.
Returns void
setGradient
-
setGradient( color1: string, color2: string, x: number, y: number, w: number, h: number, direction: DirectionValue, alpha1?: number, alpha2?: number,): void
Sets the current gradient.
Parameters
- color1: string
- color2: string
- x: number
- y: number
- w: number
- h: number
- direction: DirectionValue
- alpha1: number = 1
- alpha2: number = 1
Returns void
setLineCap
-
setLineCap(value: string): void
Sets the current line cap.
Returns void
setLineJoin
-
setLineJoin(value: string): void
Sets the current line join.
Returns void
setLink
-
setLink(link: string): void
Experimental implementation for hyperlinks.
Returns void
setMiterLimit
-
setMiterLimit(value: number): void
Sets the current miter limit.
Returns void
setShadow
-
setShadow(enabled: boolean): void
Enables or disables and configures the current shadow.
Returns void
setShadowAlpha
-
setShadowAlpha(value: number): void
Sets the current shadow alpha.
Parameters
- value: number
Number that represents the new alpha. Possible values are between 1 (opaque) and 0 (transparent).
Returns void
setShadowColor
-
setShadowColor(value: string | null): void
Sets the current shadow color.
Parameters
- value: string | null
Hexadecimal representation of the color or none.
Returns void
setShadowOffset
-
setShadowOffset(dx: number, dy: number): void
Sets the current shadow offset.
Parameters
- dx: number
Number that represents the horizontal offset of the shadow.
- dy: number
Number that represents the vertical offset of the shadow.
Returns void
setStrokeAlpha
-
setStrokeAlpha(value: number): void
Sets the current stroke alpha.
Returns void
setStrokeColor
-
setStrokeColor(value: string | null): void
Sets the current stroke color.
Returns void
setStrokeWidth
-
setStrokeWidth(value: number): void
Sets the current stroke width.
Returns void
stroke
-
Paints the outline of the current path.
Returns void
text
-
text( x: number, y: number, w: number, h: number, str: string, align: AlignValue, valign: VAlignValue, wrap: boolean, format: string, overflow: OverflowValue, clip: boolean, rotation?: number, dir: TextDirectionValue,): void
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.
Returns void
translate
-
translate(dx: number, dy: number): void
Translates the current state.
Returns void
updateFill
-
Transfers the stroke attributes from to .
Returns void
updateFont
-
updateFont(node: SVGElement): void
Updates the text properties for the given node. (NOTE: For this to work in
IE, the given node must be a text or tspan element.)
Returns void
updateStrokeAttributes
-
updateStrokeAttributes(): void
updateText
-
updateText( x: number, y: number, w: number, h: number, align: AlignValue, valign: VAlignValue, wrap: boolean, overflow: OverflowValue, clip: boolean, rotation: number, node: SVGElement,): void
Updates existing DOM nodes for text rendering. LATER: Merge common parts with text function below.
Returns void
updateTextNodes
-
updateTextNodes( x: number, y: number, w: number, h: number, align: AlignValue, valign: VAlignValue, wrap: boolean, overflow: OverflowValue, clip: boolean, rotation: number, g: SVGElement,): void
Updates existing DOM nodes for text rendering.
Returns void
StaticcreateCss
-
createCss( w: number, h: number, align: AlignValue, valign: VAlignValue, wrap: boolean, overflow: string, clip: boolean, bg: string | null, border: string | null, flex: string, block: string, scale: number, callback: ( dx: number, dy: number, flex: string, item: string, block: string, ofl: string, ) => void,): void
Updates existing DOM nodes for text rendering.
Parameters
- w: number
- h: number
- align: AlignValue
- valign: VAlignValue
- wrap: boolean
- overflow: string
- clip: boolean
- bg: string | null
- border: string | null
- flex: string
- block: string
- scale: number
- callback: (
dx: number, dy: number, flex: string, item: string, block: string, ofl: string, ) => void
Returns void
On This Page
Constructors
Properties
Methods
|