Class: Shape
shapes/shape.Shape
Represents an arbitrary shape in 2D world space.
A shape is self contained and includes functions to render itself.
Hierarchy
Constructors
constructor
โข new Shape()
Overrides
Defined in
src/shapes/shape.ts:30
Properties
cachedPoints
โข Private
cachedPoints: Object
Type declaration
Name | Type |
---|---|
points | vec2 [] |
pos | vec2 |
posSlop | number |
rotation | number |
rotationSlop | number |
Defined in
src/shapes/shape.ts:119
listeners
โข Protected
listeners: Object
= {}
Index signature
โช [index: string
]: Listener
[]
Inherited from
Defined in
src/object2d.ts:32
opacity
โข opacity: number
= 1
Opacity of the shape.
This is an opacity of the whole shape which is applied to the shape's entire texture equally.
Range is 0 (completely transparent) to 1 (completely opaque)
Defined in
src/shapes/shape.ts:28
posDiff
โข Private
posDiff: vec2
Defined in
src/shapes/shape.ts:127
texture
โข texture: Texture
= defaultTexture
Defined in
src/shapes/shape.ts:19
Methods
addEventListener
โธ addEventListener(event
, listener
): boolean
Executes a function when an event is fired.
Parameters
Name | Type | Description |
---|---|---|
event | string | The event to listen for |
listener | Listener | The function to execute when the event fired |
Returns
boolean
Wether or not the listener was added
Inherited from
Defined in
src/object2d.ts:209
fireEvent
โธ fireEvent(event
, ...e
): void
Executes all listeners attached to a given event.
Parameters
Name | Type | Description |
---|---|---|
event | string | The event to fire |
...e | any | The data to pass to each event listener |
Returns
void
Inherited from
Defined in
src/object2d.ts:185
getBaseVertices
โธ Abstract
getBaseVertices(): vec2
[]
Calculates the base vertices of the shape.
These are the vertices for the type of shape at unit size, scaled to the shape's dimensions.
Returns
vec2
[]
The shape's vertices
Defined in
src/shapes/shape.ts:109
getEventListeners
โธ getEventListeners(event
): Listener
[]
Gets the listeners attached to an event.
Parameters
Name | Type |
---|---|
event | string |
Returns
Listener
[]
An array of listeners attached to the given event
Inherited from
Defined in
src/object2d.ts:198
getEvents
โธ getEvents(): string
[]
Gets the events that can be listened to on the object.
Returns
string
[]
The events the object supports
Inherited from
Defined in
src/object2d.ts:175
getIndices
โธ Abstract
getIndices(offset?
): Uint16Array
Gets the shape's vertex indices for drawing using element arrays.
Parameters
Name | Type | Description |
---|---|---|
offset? | number | An offset to apply to each index |
Returns
Uint16Array
The shape's vertex indices with the given offset added to each index
Defined in
src/shapes/shape.ts:117
getPoints
โธ getPoints(): vec2
[]
Calculates the bounding points of the Shape instance.
NOTE: The shape's vertices are recalculated every time this function is called.
Returns
vec2
[]
The bounding points of the box
Defined in
src/shapes/shape.ts:136
getPosition
โธ getPosition(): vec2
Gets the object's position.
Returns
vec2
The object's position as a vec2
Inherited from
Defined in
src/object2d.ts:82
getRotation
โธ getRotation(): number
Gets the object's rotation as a number in radians relative to y+.
Returns
number
The object's rotation
Inherited from
Defined in
src/object2d.ts:139
getUVCoords
โธ Abstract
getUVCoords(): Float32Array
Calculates the shape's UV coords to be used for texture rendering.
Returns
Float32Array
the shape's UV coords
Defined in
src/shapes/shape.ts:50
getVertices
โธ getVertices(): number
[]
Calculates the shape's vertices in local space.
Returns
number
[]
The shape's vertices
Defined in
src/shapes/shape.ts:57
getVerticesClipSpace
โธ getVerticesClipSpace(origin
, scale
, rotation?
, camera?
): number
[]
Calculates the shape's vertices relative to the provided origin in world space.
Parameters
Name | Type | Description |
---|---|---|
origin | vec2 | The origin to calculate the vertices relative to, should be a world position |
scale | vec2 | The vector to scale the world space vertices by to obtain clip space values |
rotation? | number | An optional rotation to apply to the vertices |
camera? | Camera | An optional camera to get vertices relative to |
Returns
number
[]
The shape's vertices relative to the provided origin in clip space
Defined in
src/shapes/shape.ts:87
getVerticesWorld
โธ Abstract
getVerticesWorld(origin
, rotation?
, returnVecs?
): number
[] | vec2
[]
Calculates the shape's vertices relative to the provided origin in world space.
Parameters
Name | Type | Description |
---|---|---|
origin | vec2 | The origin to calculate the vertices relative to, should be a world position |
rotation? | number | An optional world space rotation to apply to the vertices |
returnVecs? | boolean | Wether or not to return the vertices as vec2s or raw numbers |
Returns
number
[] | vec2
[]
The shape's vertices relative to the provided origin in world space
Defined in
src/shapes/shape.ts:76
moveRight
โธ moveRight(dist
): void
Moves the object's position right (x+) relative to it's rotation.
Parameters
Name | Type | Description |
---|---|---|
dist | number | The distance to move right, can be + or - |
Returns
void
Inherited from
Defined in
src/object2d.ts:100
moveUp
โธ moveUp(dist
): void
Moves the object's position up (y+) relative to it's rotation.
Parameters
Name | Type | Description |
---|---|---|
dist | number | The distance to move up, can be + or - |
Returns
void
Inherited from
Defined in
src/object2d.ts:114
removeEventListener
โธ removeEventListener(event
, listener
): boolean
Removes a listener from an event.
Parameters
Name | Type | Description |
---|---|---|
event | string | The event the listener is attached to |
listener | Listener | The listener to remove |
Returns
boolean
Wether or not the listener was removed
Inherited from
Defined in
src/object2d.ts:223
render
โธ render(position?
, rotation?
, zIndex?
): void
Renders the shape using the Renderer.
Parameters
Name | Type | Description |
---|---|---|
position? | vec2 | The x and y position to render the shape at |
rotation? | number | The rotation to apply to the rendered shape |
zIndex? | number | The z position of the rendered shape |
Returns
void
Defined in
src/shapes/shape.ts:41
rotate
โธ rotate(angle
): void
Increments the object's rotation by an angle in radians.
Parameters
Name | Type | Description |
---|---|---|
angle | number | The angle to rotate by in radians |
Returns
void
Inherited from
Defined in
src/object2d.ts:148
setPosition
โธ setPosition(pos
): void
Sets the object's position.
Parameters
Name | Type | Description |
---|---|---|
pos | vec2 | The object's new position |
Returns
void
Inherited from
Defined in
src/object2d.ts:43
setPositionX
โธ setPositionX(x
): void
Sets the x component of the object's position.
Parameters
Name | Type | Description |
---|---|---|
x | number | The object's new x coordinate |
Returns
void
Inherited from
Defined in
src/object2d.ts:56
setPositionY
โธ setPositionY(y
): void
Sets the y component of the object's position.
Parameters
Name | Type | Description |
---|---|---|
y | number | The object's new y coordinate |
Returns
void
Inherited from
Defined in
src/object2d.ts:69
setRotation
โธ setRotation(angle
): void
Sets the object's rotation.
Parameters
Name | Type | Description |
---|---|---|
angle | number | The object's new rotation |
Returns
void
Inherited from
Defined in
src/object2d.ts:128
setupEvents
โธ Protected
setupEvents(): void
Sets all the events that can be used in listeners
.
Listeners can be added for an event using:
this.listeners.eventName = [];
example
Setup listeners for "position" and "rotate" events.
this.listeners.position = [];
this.listeners.rotate = [];
Returns
void
Inherited from
Defined in
src/object2d.ts:165
translate
โธ translate(v
): void
Translates the object by the provided vector.
Parameters
Name | Type | Description |
---|---|---|
v | vec2 | The vector to translate by |
Returns
void
Inherited from
Defined in
src/object2d.ts:91