Shape
A Shape is a Solid graphic which has a particular shape, either one of the built-in shapes, or a custom bezier shape.
Getting Shapes
Here is a script for creating an array of references to all shapes on the current canvas:
Get All Shapes on Current Canvas | ||
01 | cnvs = document.windows[0].selection.canvas | |
02 | var shapes = new Array() | |
03 | cnvs.graphics.forEach(function(graphic){ | |
04 | if(graphic instanceof Shape){shapes.push(graphic)} | |
05 | }) |
And here’s a script for creating an array of references to all shapes in the document:
Get All Shapes in Document | ||
01 | shapes = new Array() | |
02 | canvases.forEach(function(cnvs){ | |
03 | cnvs.graphics.forEach(function(graphic){ | |
04 | if(graphic instanceof Shape){shapes.push(graphic)} | |
05 | }) | |
06 | }) |
Using the previous script, this version replaces the text in all shapes whose text matches a specific text string:
Find/Change Graphic Text | ||
01 | shapes = new Array() | |
02 | canvases.forEach(function(cnvs){ | |
03 | cnvs.graphics.forEach(function(graphic){ | |
04 | if(graphic instanceof Shape){shapes.push(graphic)} | |
05 | }) | |
06 | }) | |
07 | ||
08 | textToMatch = 'How Now Brown Cow' | |
09 | replacementText = 'Cow Brown Now How' | |
10 | shapes.forEach(function(solid){ | |
11 | if(solid.text === textToMatch){solid.text = replacementText} | |
12 | }) |
Information about styled text in graphics can be found in the Text section.
And here's a variation on the previous scripts for locating all of the circles on the current canvas:
All Circles on Current Canvas | ||
01 | cnvs = document.windows[0].selection.canvas | |
02 | var circles = new Array() | |
03 | cnvs.graphics.forEach(function(graphic){ | |
04 | if(graphic instanceof Shape && graphic.shape === 'Circle'){ | |
05 | circles.push(graphic) | |
06 | } | |
07 | }) |
And a variation for locating all stars in a document:
All Stars in Document | ||
01 | var stars = new Array() | |
02 | canvases.forEach(function(cnvs){ | |
03 | cnvs.graphics.forEach(function(graphic){ | |
04 | if(graphic instanceof Shape && graphic.shape === 'Star'){ | |
05 | stars.push(graphic) | |
06 | } | |
07 | }) | |
08 | }) |
Creating Shapes
There are two methods of the Canvas class that can be used to create shapes:
addShape(shapeNameString, boundsRect) (Shape) • Create a new graphic of a given shape and place it on the first visible layer.
newShape() (Shape) • Create a zero-sized rectangle (presumably to be modified further) and place it on the first visible layer.
New Shape using newShape() | ||
01 | cnvs = document.windows[0].selection.canvas | |
02 | solid = cnvs.newShape() | |
03 | solid.shape = 'Rectangle' | |
04 | solid.geometry = new Rect(0, 0, 200, 200) |
New Shape using addShape() | ||
01 | cnvs = document.windows[0].selection.canvas | |
02 | cnvs.addShape('Circle',new Rect(200, 200, 400, 400)) |
In addition to inheriting the properties of a graphic, each instance of a shape has the following properties:
shape (String or nil) • Name of the shape for this graphic. The names of the currently available shapes are: AndGate, Bolt, Circle, Cloud, Cross, Cube, Cylinder, Delete, Diamond, DisplayShape, DocumentShape, DoubleHorizontalArrow, Heart, Hexagon, HorizontalArrow, HorizontalBrackets, HorizontalTriangle, House, Lightning Bolt, NoteShape, Octagon, OrGate, ParallelLines, Parallelogram, Patch, Pentagon, Puzzle Piece, QuarterArc, QuarterCircle, Rectangle, RightTriangle, RoundRect, RoundedStack, SemiCircle, SingleLine, SlopedRect, Speech Bubble, Star, Sticky, Subprocess, Think Bubble, Trapazoid, VerticalArrow, VerticalBrackets, VerticalParallelLines, VerticalTriangle, iOSAppIcon
shapeControlPoints (array of Points) • The vertices and controlPoint1 & controlPoint2 of each bezier segment. For straight line segments, both control points will be identical to the vertex point.
shapeVertices (array of Points) • Array of vertices for this shape.
Shape Vertices and Shape Control Points
Shape Vertices are the points, or corners, in geometrical and mathematical shapes where two or more lines meet but do not cross. Vertices are used to define the outline of a shape. For example, here are the four vertices of a circle:
4 Shape Vertices of a Circle | ||
01 | var cnvs = document.windows[0].selection.canvas; | |
02 | var g1 = cnvs.newShape() | |
03 | g1.shape = "Circle" | |
04 | g1.shadowColor = null | |
05 | g1.geometry = new Rect(0.00, 0.00, 100.0, 100.0) | |
06 | g1.shapeVertices | |
07 | ==> [object Point: (85.3553, 14.6447)],[object Point: (85.3553, 85.3553)],[object Point: (14.6447, 85.3553)],[object Point: (14.6447, 14.6447)] |
Shape Control Points are a collection of points defining the postion of the adjustment handles of the shape vertices:
8 Shape Control Points of a Circle | ||
01 | var cnvs = document.windows[0].selection.canvas; | |
02 | var g1 = cnvs.newShape() | |
03 | g1.shape = "Circle" | |
04 | g1.shadowColor = null | |
05 | g1.geometry = new Rect(0.00, 0.00, 100.0, 100.0) | |
06 | g1.shapeControlPoints | |
07 | ==> [object Point: (85.3553, 14.6447)],[object Point: (104.8816, 34.1709)],[object Point: (104.8816, 65.8291)],[object Point: (85.3553, 85.3553)],[object Point: (65.8291, 104.8816)],[object Point: (34.1709, 104.8816)],[object Point: (14.6447, 85.3553)],[object Point: (-4.8816, 65.8291)],[object Point: (-4.8816, 34.1709)],[object Point: (14.6447, 14.6447)],[object Point: (34.1709, -4.8816)],[object Point: (65.8291, -4.8816)] |
Shape Vertices 1 and Shape Control Points 2
Using addShape() to Create a Shape
The easiest way to create a standard shape of a specific size and position is to use addShape() method, passing in the name of the shape (see list above), and a Rect defining the object’s position and size:
Using addShape() to Create a Shape | ||
01 | var cnvs = document.windows[0].selection.canvas | |
02 | graphic = cnvs.addShape('Circle', new Rect(0, 0, 144, 144)) |
A circular shape will be added to the current canvas.
Thought Bubble | ||
01 | var cnvs = document.windows[0].selection.canvas | |
02 | graphic = cnvs.addShape('Think Bubble', new Rect(0, 0, 144, 144)) | |
03 | graphic.text = "What was I thinking?" |
The resulting thought-bubble:
Using newShape() to Create a Shape
The newShape() method is used to create an zero-sized shape, which is then defined by following script statements. For example, the following script demonstrates how to create a standard shape using this method:
Using newShape() to Create a Shape | ||
01 | var cnvs = document.windows[0].selection.canvas | |
02 | graphic = cnvs.newShape() | |
03 | graphic.shape = 'DoubleHorizontalArrow' | |
04 | graphic.geometry = new Rect(0, 0, 300, 150) |
The newShape() method is often used to create graphics with custom shapes and designs by providing an array of the shape control points or shape vertices:
Create Custom Shape: 4-Pointed Star | ||
01 | var cnvs = document.windows[0].selection.canvas | |
02 | var g1 = cnvs.newShape() | |
03 | g1.shapeControlPoints = [new Point(103.08, 53.02), new Point(94.59, 56.53), new Point(87.83, 59.70), new Point(82.79, 62.51), new Point(77.75, 65.32), new Point(73.83, 68.11), new Point(71.02, 70.88), new Point(68.25, 73.64), new Point(65.46, 77.53), new Point(62.65, 82.55), new Point(59.84, 87.56), new Point(56.63, 94.43), new Point(53.02, 103.15), new Point(53.02, 103.15), new Point(50.13, 103.15), new Point(50.13, 103.15), new Point(46.48, 94.43), new Point(43.24, 87.56), new Point(40.43, 82.55), new Point(37.62, 77.53), new Point(34.85, 73.64), new Point(32.13, 70.88), new Point(29.32, 68.11), new Point(25.39, 65.32), new Point(20.36, 62.51), new Point(15.32, 59.70), new Point(8.53, 56.53), new Point(0.00, 53.02), new Point(0.00, 53.02), new Point(0.00, 50.13), new Point(0.00, 50.13), new Point(8.58, 46.62), new Point(15.39, 43.45), new Point(20.43, 40.64), new Point(25.46, 37.83), new Point(29.37, 35.04), new Point(32.13, 32.27), new Point(34.85, 29.51), new Point(37.62, 25.62), new Point(40.43, 20.60), new Point(43.24, 15.59), new Point(46.48, 8.72), new Point(50.13, 0.00), new Point(50.13, 0.00), new Point(53.02, 0.00), new Point(53.02, 0.00), new Point(56.63, 8.72), new Point(59.84, 15.59), new Point(62.65, 20.60), new Point(65.46, 25.62), new Point(68.25, 29.51), new Point(71.02, 32.27), new Point(73.73, 35.04), new Point(77.61, 37.83), new Point(82.65, 40.64), new Point(87.69, 43.45), new Point(94.50, 46.62), new Point(103.08, 50.13), new Point(103.08, 50.13), new Point(103.08, 53.02)] | |
04 | g1.strokeType = null | |
05 | g1.geometry = new Rect(0.00, 0.00, 103.15, 103.15) | |
06 | g1.shadowColor = null | |
07 | g1.strokeColor = null | |
08 | g1.fillColor = Color.RGB(0.0, 0.0, 0.0) | |
09 | g1.name = "Four-Pointed Star" |
The resulting four-pointed star shape:
Solid
A solid graphic is one that potentially has a fill, image, and text - as opposed to a “Line,” which has only a stroke. Almost all solid graphics will actually be the subclass “Shape,” but a canvas background is a “Solid” without being a “Shape.”
TIP: Detailed examples of creating and manipulating Solids containing text, is available on the Text page.
Solid Properties
autosizing (TextAutosizing) • Autosizing behavior of the graphic when the text size changes.
blendColor (Color or nil) • The middle color of a three color gradient fill, if the fill style includes such.
blendFraction (Number) • The middle fraction of a three color gradient fill, if the fill style includes such.
fillColor (Color or nil) • Color of the fill for this graphic.
fillType (FillType or nil) • Style of fill for this graphic.
fontName (String) • Font of text in this graphic. This is the Font’s “Postscript name”, as displayed in the Font Book application’s Font Info pane on the Mac. Where there are multiple fonts, this returns the first character’s font. Setting this value sets it for all text in the graphic.
gradientAngle (Number) • For linear gradients, the angle at which the gradient is drawn.
gradientCenter (Point) • For radial gradients, the position of the center of the gradient.
gradientColor (Color or nil) • For gradient fills, the second color (along with the fill color) that the fill goes between.
image (ImageReference or nil) • Image fill for this graphic, if any.
imageOffset (Point) • Positioning offset of the image fill. This is the difference between the image origin and the graphic origin.
imageOpacity (Number) • Opacity percentage for the image fill for this graphic.
imagePage (Number) • Page number to display for the given image, if relevant. Mainly useful for PDF images, which are potentially multiple pages.
imageScale (Size) • Scaling of the image fill. This is a multiplier between the displayed size and original image size.
imageSizing (ImageSizing) • Type of sizing behavior for the image, if any. ImageSizing.Manual, ImageSizing.Stretched, ImageSizing.Tiled
magnets (Array of Array) • Array of connection magnets for this graphic.
rotation (Number) • Rotation of this graphic.
shape (String) • Inherited from the solid shape class, this property represents the type of shape of the graphic. It applies only to graphics that are solids, not lines. Here are the standard string values: AndGate, Bolt, Circle, Cloud, Cross, Cube, Cylinder, Delete, Diamond, DisplayShape, DocumentShape, DoubleHorizontalArrow, Heart, Hexagon, HorizontalArrow, HorizontalBrackets, HorizontalTriangle, House, Lightning Bolt, NoteShape, Octagon, OrGate, ParallelLines, Parallelogram, Patch, Pentagon, Puzzle Piece, QuarterArc, QuarterCircle, Rectangle, RightTriangle, RoundRect, RoundedStack, SemiCircle, SingleLine, SlopedRect, Speech Bubble, Star, Sticky, Subprocess, Think Bubble, Trapazoid, VerticalArrow, VerticalBrackets, VerticalParallelLines, VerticalTriangle, iOSAppIcon
text (String) • Text contents of this graphic.
textAlongPathGlyphAnchor (Number) • text-on-path glyph placement anchor; 0 = glyph anchored at bottom center (this is how text-on-path works in OmniGraffle 7.10 and later), 1 = glyph anchored at bottom left (this is how text-on-path works in OmniGraffle 7.9.3 and previous)
textColor (Color) • Color of the text in this graphic. Where there are multiple colors, this returns the first character’s color. Setting this value sets it for all text in the graphic.
textFlow (TextFlow) • (new in 7.11) Text flow behavior of the graphic.
textGeometry (Rect r/o) • Drawing bounds of the text in canvas coordinates.
textHorizontalAlignment (HorizontalTextAlignment) • Alignment of the text in this graphic horizontally. HorizontalTextAlignment.Center, HorizontalTextAlignment.Justify, HorizontalTextAlignment.Left, HorizontalTextAlignment.Right
textHorizontalPadding (Number) • Horizontal padding between the edge of the graphic’s bounds and the edge of the text area where text is drawn.
textRotation (Number) • Rotation of the text.
textRotationIsRelative (boolean) • Whether the text rotation is relative to the existing rotation of the graphic itself, or whether it is constant compared to the canvas.
textSize (Number) • Font size of text in this graphic. Where there are multiple fonts, this returns the first character’s font size. Setting this value sets it for all text in the graphic.
textUnitRect (Rect) • Size and position of the graphics’ text area as a unit square. I.e. The x and y are in terms of proportion of graphic size from the graphic bounds (so 0,0 is the graphic origin, (1,1) originates at the graphic’s lower-righthand corner, and the width and height are the scale of the text area in proportion to the graphic, so (1,1) is the same size as the graphic bounds, (2,2) would be twice as large, etc.
textVerticalPadding (Number) • Vertical padding between the edge of the graphic’s bounds and the edge of the text area where text is drawn.
textVerticalPlacement (VerticalTextPlacement) • Alignment of the text in this graphic vertically. VerticalAlignment.Bottom, VerticalAlignment.Center, VerticalAlignment.Top
textWraps (Boolean) • Whether the text wraps to the graphic’s bounds, or can go as wide as it wishes outside the graphic bounds.
tripleBlend (boolean) • Whether the fill includes three colors in a gradient.
Checking for Single Selected Solid
It it is common to create scripts that require the user to have already selected a single solid. Here is the outline of such a script:
Check for Single Selected Solid | ||
01 | if (document.windows[0].selection.solids.length != 1){ | |
02 | title = "SELECTION ERROR" | |
03 | message = "Please select a single solid." | |
04 | new Alert(title, message).show(function(result){}) | |
05 | } else { | |
06 | cnvs = document.windows[0].selection.canvas | |
07 | solid = document.windows[0].selection.solids[0] | |
08 | // processing code goes here | |
09 | } |
And here is a variation of the previous script that requires the single selected solid to contain an image:
Check for Single Selected Image | ||
01 | if (document.windows[0].selection.solids.length != 1){ | |
02 | title = "SELECTION ERROR" | |
03 | message = "Please select a single solid." | |
04 | new Alert(title, message).show(function(result){}) | |
05 | } else { | |
06 | cnvs = document.windows[0].selection.canvas | |
07 | solid = document.windows[0].selection.solids[0] | |
08 | if (solid.image instanceof ImageReference){ | |
09 | // processing code goes here | |
10 | } else { | |
11 | title = "NO IMAGE" | |
12 | message = "The solid does not contain an image." | |
13 | new Alert(title, message).show(function(result){}) | |
14 | } | |
15 | } |
Text Autosizing
The TextAutosizing properties are the value for the autosizing property of the Solid class:
TextAutosizing | ||
01 | graphic = document.windows[0].selection.graphics[0] | |
02 | graphic.autosizing = TextAutosizing.Vertical |
Clip (TextAutosizing 3) • Clip.
Full (TextAutosizing 1) • Full.
Overflow (TextAutosizing 0) • Overflow.
Vertical (TextAutosizing 2) • Vertical.
Fill Type
The FillType properties are the value for the fillType property (note lowercase f) of the Solid class:
Fill Type | ||
01 | graphic = document.windows[0].selection.graphics[0] | |
02 | graphic.fillType = FillType.Squiggle |
Linear (FillType 2) • Linear Gradient.
Marker (FillType 7) • Marker.
Plastic (FillType 6) • Plastic.
Radial (FillType 3) • Radial Gradient.
Solid (FillType 1) • Solid.
Squiggle (FillType 5) • Squiggle.
Stipple (FillType 4) • Stipple.
Image Sizing
The ImageSizing properties are the value for the imageSizing property (note lowercase i) of the Solid class:
Image Sizing | ||
01 | graphic = document.windows[0].selection.graphics[0] | |
02 | graphic.imageSizing = ImageSizing.Manual |
Manual (ImageSizing 0) • Manual.
Stretched (ImageSizing 1) • Stretched.
Tiled (ImageSizing 2) • Tiled.
Horizontal Text Alignment
The HorizontalTextAlignment props are the value for the textHorizontalAlignment property of the Solid class:
Horizontal Text Alignment | ||
01 | graphic = document.windows[0].selection.graphics[0] | |
02 | graphic.textHorizontalAlignment = HorizontalTextAlignment.Left |
Center (HorizontalTextAlignment 1) • Centered horizontally.
Justify (HorizontalTextAlignment 3) • Spacing adjusted to fill available horizontal space.
Left (HorizontalTextAlignment 0) • Aligned left.
Right (HorizontalTextAlignment 2) • Aligned right.
Vertical Text Placement
The VerticalTextPlacement props are the value for the textVerticalPlacement property of the Solid class:
Vertical Text Placement | ||
01 | graphic = document.windows[0].selection.graphics[0] | |
02 | graphic.textVerticalPlacement = VerticalTextPlacement.Bottom |
Middle (VerticalTextPlacement 1) • Centered in the vertical middle of the shape.
Bottom (VerticalTextPlacement 2) • Aligned to the bottom of the shape.
Top (VerticalTextPlacement 0) • Aligned to the top of the shape.
Text Flow Options (v7.11)
The TextFlow enumeration contains the values for the textFlow property of the Solid class:
Text Flow Options | ||
01 | graphic = document.windows[0].selection.graphics[0] | |
02 | graphic.textVerticalPlacement = VerticalTextPlacement.Bottom |
Clip • Using this setting will cause any overflowed text contents to not be displayed.
FillsShape • Use this property to cause the text contents to fill the shape. Note that this property is often set along with the textVerticalPlacement and textHorizontalAlignment shape properties.
FollowsPath • Using this property will cause the text contents to wrap to the exterior boundary of the shape.
Overflow • Using this property will cause the text contents to overflow the lower side of the containing shape.
Resize • Using this property will cause the vertical measurement of the shape to expand or contract to display all of the text contents.
all • An array of all items of this enumeration.
Here is an example script using the FollowsPath property for creating a circle with text on the outside of the shape:
Text on a Path | ||
01 | // SETTINGS | |
02 | shapeDiameter = 200 | |
03 | textOnPath = " HOW NOW BROWN COW ·" | |
04 | typefaceName = 'Arial Black' | |
05 | typefaceFactor = 4.4 | |
06 | fontSize = (shapeDiameter / textOnPath.length) * typefaceFactor | |
07 | // CREATE SHAPE WITH TEXT | |
08 | cnvs = document.windows[0].selection.canvas | |
09 | solid = cnvs.newShape() | |
10 | solid.shape = 'Circle' | |
11 | solid.geometry = new Rect(100, 100, shapeDiameter, shapeDiameter) | |
12 | solid.strokeThickness = 0 | |
13 | solid.shadowColor = null | |
14 | solid.fillColor = null | |
15 | solid.textFlow = TextFlow.FollowsPath | |
16 | solid.text = textOnPath | |
17 | solid.textSize = fontSize | |
18 | solid.fontName = typefaceName |
Example script used twice with different shape diameters:
A script example demonstrating the use of the FillsShape property of the TextFlow options:
Text Fills Shape | ||
01 | // SETTINGS | |
02 | shapeDiameter = 200 | |
03 | textContent = "Fusce dapibus, tellus ac cursus commodo, tortor mauris condimentum nibh, ut fermentum massa justo sit amet risus. Cras justo odio, dapibus ac facilisis in, egestas eget quam. Praesent commodo cursus magna, vel scelerisque nisl consectetur et. Sed posuere consectetur est at lobortis. Cras mattis consectetur purus sit amet fermentum. Etiam porta sem malesuada magna mollis euismod. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec ullamcorper nulla non metus auctor fringilla." | |
04 | typefaceName = 'Helvetica' | |
05 | fontSize = 9 | |
06 | // CREATE SHAPE WITH TEXT | |
07 | cnvs = document.windows[0].selection.canvas | |
08 | solid = cnvs.newShape() | |
09 | solid.shape = 'Circle' | |
10 | solid.geometry = new Rect(100, 100, shapeDiameter, shapeDiameter) | |
11 | solid.strokeThickness = 2 | |
12 | solid.shadowColor = null | |
13 | solid.fillColor = null | |
14 | solid.textFlow = TextFlow.FillsShape | |
15 | solid.text = textContent | |
16 | solid.textSize = fontSize | |
17 | solid.fontName = typefaceName | |
18 | solid.strokeColor = Color.lightGray | |
19 | solid.textHorizontalAlignment = HorizontalTextAlignment.Justify | |
20 | solid.textVerticalPlacement = VerticalTextPlacement.Middle |
Shape Properties Example Script
Create a Rotated Star | ||
01 | var canvas = document.windows[0].selection.canvas | |
02 | var g1 = canvas.newShape() | |
03 | g1.textUnitRect = new Rect(0.33, 0.33, 0.34, 0.34) | |
04 | g1.rotation = 90 | |
05 | g1.shape = "AdjustableStar" | |
06 | g1.fillType = FillType.Radial | |
07 | g1.text = "STAR" | |
08 | g1.textSize = 36 | |
09 | g1.textRotationIsRelative = false | |
10 | g1.shadowVector = new Point(13.00, 13.00) | |
11 | g1.geometry = new Rect(260.00, 89.00, 356.00, 356.00) | |
12 | g1.shadowColor = Color.RGB(0.0, 0.0, 0.0, 0.382143621575342) | |
13 | g1.strokeColor = Color.RGB(0.0, 0.0, 1.0) | |
14 | g1.gradientColor = Color.RGB(0.0, 0.0, 1.0) | |
15 | g1.shadowFuzziness = 12 | |
16 | g1.fillColor = Color.RGB(1.0, 1.0, 1.0) | |
17 | g1.strokeThickness = 5 | |
18 | g1.strokePattern = StrokeDash.Dash4on4off |
This webpage is in the process of being developed. Any content may change and may not be accurate or complete at this time.