Shape
Extends:
The Shape class is used to draw shapes manually onto the world. The class provides similar functionality to the methods found in the context on a canvas with key difference that all coordinates passed to the methods in the Shape class take as parameters real units. This makes it possible to draw shapes in the world with ease.
Constructor Summary
Public Constructor | ||
public |
constructor(onDraw: function, opts: object) |
Member Summary
Public Members | ||
public |
Sets the fill color for shapes. |
|
public |
Callback function where the shape will be drawn. |
|
public |
Sets the previous position used by Shape.lineTo or Shape.moveTo. |
|
public |
Sets the prerenderer for the Shape. |
|
public |
Sets if the coordinates are referenced to the previous coordinates given or are allways absolute. |
|
public |
Sets the currently active shape style. |
|
public |
Sets the stroke color for shapes. |
Method Summary
Public Methods | ||
public |
angleStyle(mode: number) Sets the active angle style found in ANGLE_STYLE. |
|
public |
Draws an arc or a circle on the world. |
|
public |
Draws an ellipse on the world. |
|
public |
end() Closes a drawing path and fills and/or strokes it depending on the settings of Shape.fillColor and Shape.strokeColor. |
|
public |
equilateralTriangle(x: number, y: number, s: number, r: number) Draws an equilateral triangle centered around the coordinates (x, y). |
|
public |
Sets a fill color. |
|
public |
Draws a line on the world. |
|
public |
Sets the line dash of all lines. |
|
public |
Draws a line to coordinates (x0, y0). |
|
public |
Sets the starting position of drawing path. |
|
public |
noFill() Disables filling the shapes drawn. |
|
public |
noStroke() Disables stroking the shapes drawn. |
|
public |
Draws a rectangle on the world. |
|
public |
restore() Restores the shape to the previously saved state. |
|
public |
Sets the rotation of the canvas. |
|
public |
save() Saves the current state of the shape. |
|
public |
Sets a stroke color. |
|
public |
strokeWeight(weight: number) Sets the stroke weight of all lines. |
|
public |
Writes text on the world. |
|
public |
Get the width of a text in units. |
|
public |
Translates the origin to a new point (x, y). |
|
public |
Draws a triangle given three sets of points. |
|
public |
Draws a vector between two points. |
|
public |
vectorFromMag(x0: number, y0: number, mag: number, angle: number, color: number, dashed: boolean): number[] Draws a vector given a starting point and a magnitude and angle. |
Private Methods | ||
private |
Starts a drawing path. |
|
private |
draw() Main draw function for the shape called by the World automatically. |
|
private |
Selects the currently active context for drawing the shape. |
Inherited Summary
From class WorldElement | ||
public |
Sets the color in HEX format of the element. |
|
public |
Sets the cursor style when the mouse is over the element. |
|
public |
Flag for enabling or diabling this element. |
|
public |
Flag set when the element is being dragged around. |
|
public |
Sets the font of the element. |
|
public |
Sets the current height of the element. |
|
public |
Flag for allowing the element to be dragged around. |
|
public |
Sets the move style of the element. |
|
public |
Flag set when the mouse is over the element. |
|
public |
Sets a callback function for when the element is dragged around. |
|
public |
Sets the position of the element. |
|
public |
Sets the renderer used for the element. |
|
public |
Sets the rotation of the element. |
|
public |
A copy of the z-index value is stored here. |
|
public |
Sets the drawing scale of the element. |
|
public |
Flag for determining if the element will be a top-most element when dragged around. |
|
public |
Flag used to determine if an object is a child of WorldElement. |
|
public |
Sets the current width of the element. |
|
public |
Sets the world where this element is found. |
|
public |
Sets the z-index of the element. |
|
public |
addPosition(x: number, y: number) Adds the provided coordinates to the position. |
|
public |
Sets if the element is draggable. |
|
public |
Function called when the mouse moves to check if the pointer is over the element. |
|
public |
Sets the color in HEX format of the element. |
|
public |
setPosition(x: number, y: number) Sets the position of the element. |
|
public |
Sets the scale of the element. |
|
public |
Sets the z-index of the element. |
|
private |
resize() Function called when the canvas gets resized. |
|
private |
Sets the world object where element will be drawn. |
|
private |
Sets the element as a topmost element. |
Public Constructors
public constructor(onDraw: function, opts: object) source
Override:
WorldElement#constructorPublic Members
public fillColor: string source
Sets the fill color for shapes. Default value is BOX_COLORS.GREEN.BACKGROUND.
public prevPosition: object source
Sets the previous position used by Shape.lineTo or Shape.moveTo. This is used if the Shape.shapeMode is set to SHAPE_STYLE.ACCOMULATIVE.
public renderer: Renderer source
Sets the prerenderer for the Shape. It is disabled by default.
Override:
WorldElement#rendererpublic shapeMode: number source
Sets if the coordinates are referenced to the previous coordinates given or are allways absolute. Default value is SHAPE_STYLE.NONE.
public shapeStyle: number source
Sets the currently active shape style. Default value is SHAPE_STYLE.NONE.
public strokeColor: string source
Sets the stroke color for shapes. Default value is BOX_COLORS.GREEN.BORDER.
Public Methods
public angleStyle(mode: number) source
Sets the active angle style found in ANGLE_STYLE.
Params:
Name | Type | Attribute | Description |
mode | number | Desired angle style. |
public arc(x0: number, y0: number, r: number, start: number, end: number) source
Draws an arc or a circle on the world.
public ellipse(x0: number, y0: number, w: number, h: number, start: number, end: number) source
Draws an ellipse on the world.
public end() source
Closes a drawing path and fills and/or strokes it depending on the settings of Shape.fillColor and Shape.strokeColor.
public equilateralTriangle(x: number, y: number, s: number, r: number) source
Draws an equilateral triangle centered around the coordinates (x, y).
public fill(fillColor: string) source
Sets a fill color.
Params:
Name | Type | Attribute | Description |
fillColor | string | Fill color in HEX format. |
public lineDash(i: number) source
Sets the line dash of all lines.
Params:
Name | Type | Attribute | Description |
i | number | Length of the dash in pixels. |
public lineTo(x0: number, y0: number) source
Draws a line to coordinates (x0, y0). Before using this function, the function Shape.moveTo must be used to set the starting point of the line. Multiple calls to this function can be used to create a shape.
public moveTo(x0: number, y0: number) source
Sets the starting position of drawing path. The path must be then continued by calling Shape.lineTo.
public rotate(angle: number) source
Sets the rotation of the canvas. Before doing a rotation the shape must be saved and then restored. See Shape.translate for an example.
Params:
Name | Type | Attribute | Description |
angle | number | Desired rotation angle. |
public stroke(strokeColor: string) source
Sets a stroke color.
Params:
Name | Type | Attribute | Description |
strokeColor | string | Stroke color in HEX format. |
public strokeWeight(weight: number) source
Sets the stroke weight of all lines.
Params:
Name | Type | Attribute | Description |
weight | number | Weight of the lines. |
public text(text: string, x0: number, y0: number) source
Writes text on the world. The style of the text is set by Shape.font.
public textWidth(text: string): number source
Get the width of a text in units.
Params:
Name | Type | Attribute | Description |
text | string | Text to measure. |
public translate(x: number, y: number) source
Translates the origin to a new point (x, y). Doing a translation requires to first save the context by calling Shape.save, and when finalizing the drawing by calling Shape.restore. This ensures that the translation doesn't mess with the origin of the axis. Translation is also required when doing a rotation around a point.
Example:
// The following example will draw a 5x5 box at coordinates (1, 1) with a rotation of 90°.
var s = new Shape();
s.save()
s.translate(1, 1)
s.rotate(90)
s.rect(0, 0, 5, 5);
s.restore();
public triangle(x0: number, y0: number, x1: number, y1: number, x2: number, y2: number) source
Draws a triangle given three sets of points.
Params:
Name | Type | Attribute | Description |
x0 | number | First -x coordinate of the triangle. |
|
y0 | number | First -y coordinate of the triangle. |
|
x1 | number | Second -x coordinate of the triangle. |
|
y1 | number | Second -y coordinate of the triangle. |
|
x2 | number | Third -x coordinate of the triangle. |
|
y2 | number | Third -y coordinate of the triangle. |
public vector(_x0: number, _y0: number, _x1: number, _y1: number, dashed: boolean) source
Draws a vector between two points. The vector is a line with an equilateral triangle at the tip of the vector.
public vectorFromMag(x0: number, y0: number, mag: number, angle: number, color: number, dashed: boolean): number[] source
Draws a vector given a starting point and a magnitude and angle.
Params:
Name | Type | Attribute | Description |
x0 | number | Start -x coordinate of the vector. |
|
y0 | number | Start -y coordinate of the vector. |
|
mag | number | Magnitude of the vector. |
|
angle | number | Angle of the vector. |
|
color | number | Color of the vector in HEX format. |
|
dashed | boolean | Flag for drawing the vector with a dashed line. |
Private Methods
private begin(s: number) source
Starts a drawing path.
Params:
Name | Type | Attribute | Description |
s | number |
|
Desired shape style. See SHAPE_STYLE. |