Optional
massObject: null | MassObjectthe MassObject to display
Optional
proto: null | DisplayShapethe prototype DisplayShape to inherit properties from
Private
borderLine dash array used when drawing the border. Corresponds to lengths of dashes
and spaces, in screen coordinates. For example, [3, 5]
alternates dashes of
length 3 with spaces of length 5. Empty array indicates solid line.
Private
dragable_Whether the MassObject is dragable.
Private
drawWhether to draw the location of the center of mass; it is drawn as two small crossed lines.
Private
drawWhether to draw the 'drag points' on the object; these are the places that a spring can be attached to for dragging the object.
Private
fillThe color or gradient used when drawing (filling) the massObject. It can be a CSS3 color value (possibly including transparency) or a ColorGradient. Set this to the empty string to not fill the massObject.
Private
imageAT_AffineTransform to use when drawing image. The image is drawn in coordinates
that are oriented like body coordinates, but are like screen coordinates in that
the origin is at top left of the DisplayShape bounding box, Y increases downwards,
and units are equal to a screen pixel.
The imageAT
AffineTransform is applied to further modify the coordinate system
before the image is drawn using the command context.drawImage(this.image_, 0, 0)
.
Private
imageWhether to clip the image with the shape of the MassObject.
Private
Optional
imageFunction to draw an image, it is called after the massObject is filled, the border is drawn and the image_ is drawn. The AffineTransform imageAT_ is applied first, and the image is clipped if imageClip_ is set. The current path is the outline of the massObject.
Private
image_Image to draw, after the massObject is filled and border is drawn. The AffineTransform imageAT_ is applied first, and the image is clipped if imageClip_ is set. This disables the name being drawn.
Private
lastRemember the last color drawn, to keep isDarkColor_ in sync with fillStyle.
Private
massThe MassObject to display.
Private
nameColor for drawing name of the object; a CSS3 color value.
Private
nameFont for drawing name of the object, or the empty string to not draw the name. It should be a CSS3 font value such as '16pt sans-serif'.
Private
nameAngle of rotation for drawing name, in radians. Rotation is relative to the position in body coordinates.
Private
strokeThe color to use for drawing the border, or the empty string to not draw the border. It should be a CSS3 color value (possibly including transparency). The thickness of the border is set by thickness_.
Private
thickness_Thickness of border drawn, see strokeStyle_, in screen coordinates. A value of 1 corresponds to a single pixel thickness.
Whether the DisplayObject contains the given world coordinates point.
the point in world coordinates
true
if this DisplayObject contains the given point
Draws this DisplayObject using the given CoordMap.
the canvas's context to draw this object into
the mapping to use for translating between simulation and screen coordinates
Image to draw, after the massObject is filled and border is drawn. The AffineTransform imageAT_ is applied first, and the image is clipped if imageClip_ is set. This disables the name being drawn.
AffineTransform to use when drawing image. The image is drawn in coordinates
that are oriented like body coordinates, but are like screen coordinates in that
the origin is at top left of the DisplayShape bounding box, Y increases downwards,
and units are equal to a screen pixel.
The imageAT
AffineTransform is applied to further modify the coordinate system
before the image is drawn using the command context.drawImage(this.image_, 0, 0)
.
Function to draw an image, it is called after the massObject is filled, the border is drawn and the image_ is drawn. The AffineTransform imageAT_ is applied first, and the image is clipped if imageClip_ is set. The current path is the outline of the massObject.
Returns the set of MassObjects that this DisplayObject represents. Returns an empty list if this DisplayObject doesn't represent a MassObject.
the set of MassObjects that this DisplayObject represents
Set the prototype DisplayShape for this object. Display parameters are inherited from the prototype unless the parameter is explicitly set for this object.
The color to use for drawing the border, or the empty string to not draw the border. It should be a CSS3 color value (possibly including transparency). The thickness of the border is set by getThickness.
Thickness of border drawn, see getStrokeStyle, in screen coordinates. A value of 1 corresponds to a single pixel thickness.
Image to draw, after the massObject is filled and border is drawn. The AffineTransform imageAT_ is applied first, and the image is clipped if imageClip_ is set. This disables the name being drawn.
AffineTransform to use when drawing image. The image is drawn in coordinates
that are oriented like body coordinates, but are like screen coordinates in that
the origin is at top left of the DisplayShape bounding box, Y increases downwards,
and units are equal to a screen pixel.
The imageAT
AffineTransform is applied to further modify the coordinate system
before the image is drawn using the command context.drawImage(this.image_, 0, 0)
.
Function to draw an image, it is called after the massObject is filled, the border is drawn and the image_ is drawn. The AffineTransform imageAT_ is applied first, and the image is clipped if imageClip_ is set. The current path is the outline of the massObject.
Sets this DisplayObject's position in simulation coordinates of the containing SimView. Each type of DisplayObject has a different policy regarding whether this will have an effect. Generally the policies are:
If the DisplayObject does not represent a SimObject, then the position can be set. Examples are DisplayClock, EnergyBarGraph.
If the SimObject's position is dependent on other objects, then the position cannot be set. Examples are DisplayConnector, DisplayRope, DisplaySpring.
If the SimObject can be moved independently and isDragable
is true
, then the position of the SimObject is modified. Example: DisplayShape.
this DisplayObject's position, in simulation coordinates.
Set the prototype DisplayShape for this object. Display parameters are inherited from the prototype unless the parameter is explicitly set for this object.
The color to use for drawing the border, or the empty string to not draw the border. It should be a CSS3 color value (possibly including transparency). The thickness of the border is set by getThickness.
Thickness of border drawn, see getStrokeStyle, in screen coordinates. A value of 1 corresponds to a single pixel thickness.
Returns a minimal string representation of this object, usually giving just identity information like the class name and name of the object.
For an object whose main purpose is to represent another Printable object, it is
recommended to include the result of calling toStringShort
on that other object.
For example, calling toStringShort()
on a DisplayShape might return something like
this:
DisplayShape{polygon:Polygon{'chain3'}}
a minimal string representation of this object.
Generated using TypeDoc
Displays a MassObject with specified style such as color, border, etc.
Displays the MassObject by filling the shape, drawing the border, and optionally drawing an image. Lastly ornaments are drawn such as a center of gravity marker, drag handles, and name of the MassObject.
Setting Display Attributes
DisplayShape has attributes to determine fill color, border color, border thickness, whether to draw a center of mass symbol, etc. There are two ways to specify these attributes:
1. Modify the style directly
Modify the DisplayShape's style directly after it has been created. Here is a typical example where the DisplayShape is created by RigidBodyObserver.
2. Modify the prototype
DisplayShape allows specifying a prototype DisplayShape. When a display property is
undefined
, then the property is fetched from the prototype. If it is alsoundefined
on the prototype then a default value is used.Keep in mind that all objects with a given prototype will be affected by any changes made to the prototype.
Here is an example where we make a prototype that causes the names to be drawn.
See RigidBodyObserver which sets up several prototype objects.
Drawing Name of MassObject
The name of the MassObject is drawn when the setNameFont property has a valid font specifier string. There are also setNameColor and setNameRotate properties that affect how the name is drawn. Example code:
Here is a script that can be executed in Terminal to show names on all DisplayShapes:
Or, if the objects of interest have the same prototype, then you can just change the prototype object.
Drawing an Image
You can draw an image in the DisplayShape by getting an HTMLImageElement that is on the HTML page and using setImage. For example in the HTML page you would have
Then in the JavaScript application after making the DisplayShape, you can assign the image to the DisplayShape:
You can further translate, scale, and rotate the image within in DisplayShape by making an AffineTransform and using setImageAT. You can clip the image to the boundary of the MassObject by using setImageClip.
You can assign a function to setImageDraw and do the drawing within that function. The same coordinate system modifications are done in that case. The current path is the shape of the MassObject, so you can for example fill with a pattern.
Both
setImage
andsetImageDraw
can be used together. Theimage
is drawn first, then theimageDraw
function is called.The application DoublePendulum2App has examples of
Coordinate System When Drawing An Image
When drawing an image it is important to understand the several coordinate systems involved:
simulation coordinates uses the Y increases up convention. The origin can be anywhere, and the units can be any size.
JavaScript canvas screen coordinates uses the Y increases down convention, with the origin at the top-left corner of the canvas, and with each unit corresponding to a pixel. The transformation between simulation and screen coordinates is handled by a CoordMap which can be set for the SimView.
body coordinates rotates and translates along with a MassObject. Like simulation coordinates it uses the Y increases up convention, and has the same unit size as simulation coordinates. See Body Coordinates, and MassObject.
For drawing the MassObject, we set the canvas to be in body coordinates. But when drawing an image or text, the difference of whether the Y coordinate increases up or down can cause the image or text to be drawn mirrored upside down.
Therefore, before the image is drawn, we make additional transformations to return to something like screen coordinates with
The setImageAT AffineTransform can then be specified to cause the image to be appear with any combination of rotation, scaling, and positioning. See the diagram below.
There are also differences in how angles are specified in the different coordinate systems; see 'About Coordinates and Angles' in CircularEdge for more information.
The above examples of using an AffineTransform on an image were generated by altering the application DoublePendulum2App.
TO DO provide an AffineTransform for drawing the name, instead of nameRotate.