Geometry path strings
The GoJS Geometry class controls the "shape" of a Shape, whereas the Shape.fill and Shape.stroke and other shape properties control the colors and appearance of the shape. For common shape figures, there are predefined geometries that can be used by setting Shape.figure. However, you can also define custom geometries.
One can construct any Geometry by allocating and initializing a Geometry of at least one PathFigure holding some PathSegments. However, you may find that using the string representation of a Geometry is easier to write and save in a database. Use the static method Geometry.parse or the Shape.geometryString property to transform a geometry path string into a Geometry object.
See samples that make use of Geometries in the samples index.
Geometry path string syntax
The syntax for a geometry path string is an extension of the SVG path string syntax. The string consists of a number of commands, each a single letter followed by some command-specific numeric parameters.
Below are the possible commands along with the parameters they take. The parameter notation (x y)+ means that the command requires exactly two
parameters, but there can be 1 or more sets of parameters for each command. For instance, the L (x y)+ command can be written as
L 10 10 20 20 to denote two straight line segments, one from the previous point on the grid to (10, 10), and the next from (10, 10) to (20, 20).
Commands written with an uppercase letter indicate absolute coordinates; lowercase commands specify coordinates relative to the last command. Some commands do not care about case because they do not take coordinates as arguments.
Move commands, as described above, begin a new subpath in a PathFigure. A move command is needed to begin a PathFigure, and therefore must be the first segment type in the path
string, with the exception of a Fill command (F) that can precede it.
Additional sets of parameters for a move command are automatically considered Line commands, so M 10 10 20 20 is identical to
M 10 10 L 20 20.
L: Line command adds a straight line segment from the previous point to the new point.
H, V: Horizontal line command extends a straight horizontal line to a given x value.
Vertical line command extends a straight vertical line to a given y value.
Q: Quadratic Bezier Curves, x1, y1 is the control point, x, y is the destination.
T: Short-hand Quadratic Bezier Curves, the control point is based on SVG's path rules.
See SVG Quadratic Bezier command for more details.
C: Cubic Bezier Curves. x1, y1 and x2, y2 are the control points. See
SVG Cubic Bezier command for more details.
S: Short-hand Cubic Bezier Curves. The two control points are calculated based on
SVG's path rules.
A: Elliptical Arcs. These follow the SVG arc conventions.
Z: Denotes that the current segment is closed. This is placed after the last segment of a subpath.
There are no parameters, and case does not matter with this command.
Additionally there are some tokens specific to GoJS:
B: Arcs following GoJS canvas arc convention. These arcs create a new line from the last point in the subpath to the first point of an arc defined by
the five arguments. Unlike all other commands with parameters, multiple sets of parameters are not allowed for B-arcs.
X: Used before M commands to denote separate PathFigures instead of a subpath. There are no parameters, and case does not matter with this
command. Separate PathFigures are important when different fills are desired per figure component.
F, F0: This command specifies whether the current PathFigure is filled (true if F is present). F0 specifies a fill with
the even-odd rule (setting PathFigure.isEvenOdd) instead of the default non-zero winding number rule. F1 is a synonym of F.
This is placed at the beginning of a figure. Case does not matter with this command.
U: This command specifies whether the current PathFigure is shadowed (false if U is present. A shadowed
PathFigure is the default). Shadows on shapes (and therefore PathFigures) only exist if Part.isShadowed is set to true on the containing part. This
is placed at the beginning of a figure. Case does not matter with this command.
Geometry path string examples
Here is a simple usage of a geometry path string when initializing a Shape without setting Shape.figure:
Here is a geometry path string that uses quadratic Bezier curves:
This geometry uses GoJS arcs:
The following example fits an auto panel around a custom geometry shape. Because the default value of Shape.geometryStretch causes the Geometry to be stretched around the TextBlock, the custom geometry is also stretched to fit around the text.
In the following Diagram we use a path string that contains three PathFigures. Note the X commands separating the figures and the
F commands denoting fill.
The first two PathFigures are open; the first and third figures are filled. The Z command only closes the PathFigure that it ends.
In the following Diagram we use a path string that contains four PathFigures, two of which have a shadow. Note that figures are shadowed by default if the
containing Part has Part.isShadowed set to true. To un-shadow specific path figures we use the U command.
Even-odd
Use F to fill with the (default) non-zero winding number rule, or F0 to fill with the even-odd
fill rule.
Geometry.parse
Use the static function Geometry.parse to convert a GoJS syntax path string into a Geometry.
Note that even though a Shape.fill is specified, the shape does not appear filled. This is because the geometry's one PathFigure is not declared
to be filled -- there is no F command. Importing SVG path strings that are filled also requires declaring that the geometry is filled. There are
several ways to do that:
- Call Geometry.fillPath for converting the SVG path string to GoJS syntax before calling Geometry.parse. For literal SVG path strings it is often easiest just to prefix it with "F ".
- Call Geometry.parse with a second argument that is true.
- Modify the Geometry returned by Geometry.parse, by setting PathFigure.isFilled to true on the desired PathFigures.
Here is the same example, but using a filled geometry path string.
All Geometry objects have bounds that contain the origin, so a geometry created with no points at x==0 or y==0 will have extra space to the left of it or above it. Note how there is extra space in the node on the left in the example below. To get rid of this extra space, we can call Geometry.normalize, which modifies the Geometry's points in-place and returns a Point describing the amount they were offset.
Shape.geometryString
The Shape.geometryString property setter parses a given GoJS path string as a Geometry, normalizes it, sets the Shape.geometry to this new Geometry, and offsets the Shape's position by the amount it was shifted in normalization. The positioning is useful when the shape is inside a Panel.Position panel. But when the shape is used in any other kind of panel (thus ignoring the GraphObject.position), it is still useful to remove the extra space so that the shape fits in well with the other objects in the panel.
The example below adds three Nodes with Shapes to the diagram. The first shape uses Geometry.parse to set the Shape's Geometry, the second one uses Geometry.parse and Geometry.normalize. The third uses Shape.geometryString. Note the difference in size between the first Node and the other two.
Flipping geometries horizontally and vertically
GoJS Geometries have several methods for modifying the geometry's points by a transformation matrix. We can use these methods to flip or mirror the geometries if needed.
geometry.scale(-1, 1) will flip a geometry horizontally. geometry.scale(1, -1) will flip a geometry vertically.
Converting path strings
The static method Geometry.stringify can be used to output a Geometry as a string. This string will have the GoJS path string syntax. You can use Geometry.stringify and Geometry.parse to data bind custom shape geometries.
Geometry.parse(Geometry.stringify(myGeometry)) will return a geometry equal to myGeometry, though if myGeometry was created from a
string, the string itself is not guaranteed to be the same. If you merely want to copy a Geometry you should use Geometry.copy.
Because of the additional non-SVG commands, a string generated from Geometry.stringify will not necessarily be a valid SVG path.
The static method Geometry.fillPath takes a path string of either syntax and adds F tokens before each PathFigure that does not have them.
Because SVG path strings are not considered to be "filled" by themselves, if you are converting an SVG Path shape to GoJS you will want to call
Geometry.fillPath on the SVG string.
Parameterized Geometries
Although individual Geometry objects cannot be dynamically parameterized based on the intended size or other properties, the Shape class does support such parameterization via Shape.defineFigureGenerator. When you set or bind the Shape.figure property, the shape will call the named figure generator to generate a Geometry appropriate for the desired width and height and other Shape properties.
You can see the definitions of all of the predefined figures in the extensions file: Figures.js.
The example below defines a custom figure named "MyPlus" that draws a plus/cross shape. The generator function takes the Shape along
with the desired width w and height h, and uses Shape.parameter1 to control the arm thickness as a fraction of the smaller
dimension, with the default being .3. The same figure name is then used by several shapes at different sizes and different parameters. The generator
function is called each time to parametrize a new geometry.