Class: M2c2KitHelpers
Constructors
new M2c2KitHelpers()
new M2c2KitHelpers():
M2c2KitHelpers
Returns
Methods
arePointsOnSameSideOfLine()
static
arePointsOnSameSideOfLine(p1
,p2
,a
,b
):boolean
Checks if two points are on the same side of a line.
Parameters
• p1: Point
point to check
• p2: Point
point to check
• a: Point
point that defines one end of the line
• b: Point
point that defines the other end of the line
Returns
boolean
true if p1 and p2 are on the same side of the line, or false otherwise
Remarks
The line is defined by two points, a and b. The function uses the cross product to determine the relative position of the points.
Defined in
M2c2KitHelpers.ts:661
boundingBoxToPoints()
static
boundingBoxToPoints(boundingBox
):Point
[]
Converts a bounding box to an array of four points representing the vertices of the rectangle.
Parameters
• boundingBox: BoundingBox
Returns
Point
[]
An array of four points
Remarks
In m2c2kit, the y-axis is inverted: origin is in the upper-left. Vertices are returned in clockwise order starting from the upper-left.
Defined in
M2c2KitHelpers.ts:741
calculateNodeAbsoluteBoundingBox()
static
calculateNodeAbsoluteBoundingBox(node
):BoundingBox
Calculates the absolute bounding box of the node before any rotation is applied.
Parameters
• node: M2Node
Returns
BoundingBox
the bounding box of the node
Remarks
The absolute bounding box is the bounding box of the node relative to the scene's origin (0, 0).
Defined in
M2c2KitHelpers.ts:586
calculateRotatedPoints()
static
calculateRotatedPoints(drawableNode
):Point
[]
Calculates the four points of the bounding box of the node, taking into account the node's rotation (as well as the rotation of its ancestors).
Parameters
• drawableNode: IDrawable
& M2Node
Returns
Point
[]
array of points representing the rotated node
Remarks
This method is used to calculate the rotated bounding box of an node when in order to determine if a point is inside the node in response to DOM pointer events. This method is NOT used to prepare the CanvasKit canvas for drawing the node.
Defined in
M2c2KitHelpers.ts:510
calculateRotationTransforms()
static
calculateRotationTransforms(drawableNode
):RotationTransform
[]
Calculates the rotation transforms to apply to node, respecting any ancestor rotations.
Parameters
• drawableNode: IDrawable
& M2Node
node to calculate rotation transforms for
Returns
RotationTransform
[]
array of rotation transforms to apply
Defined in
M2c2KitHelpers.ts:812
convertValueToType()
static
convertValueToType(value
,type
):unknown
Converts a value to a JSON schema type or one of types.
Parameters
• value: null
| string
| number
| boolean
| object
| unknown
[]
the value to convert
• type: undefined
| JsonSchemaDataType
| JsonSchemaDataType
[]
A JSON Schema type or types to convert the value to, e.g., "string" or ["string", "null"]
Returns
unknown
the converted value
Remarks
The value can be of the target type, or a string that can be
parsed into the target type. For example, a string "3"
can be converted
to a number, and a string '{ "color" : "red" }'
can be converted to an
object. If the target type if an object or array, the value can be a
string parsable into the target type: this string can be the string
representation of the object or array, or the URI encoded string.
Throws an error if the value cannot be converted to the type or one of the
types. Converting an object, null, or array to a string is often not the
desired behavior, so a warning is logged if this occurs.
Defined in
M2c2KitHelpers.ts:70
createFrameUpdateTimestamps()
static
createFrameUpdateTimestamps():object
Creates timestamps based on when the current frame's update began.
Returns
object
object with timestamps
iso8601Timestamp
iso8601Timestamp:
string
timestamp
timestamp:
number
Remarks
When recording events related to node creation, node
parent-child relationships, and node properties, the timestamps should be
based on when current frame's update began -- not the current time. While
current time is most accurate for recording user interactions (use
M2c2KitHelpers.createTimestamps()
for user interactions), the frame's
update is the best way to ensure that node events that occurred in the same
frame are recorded with the same timestamp and thus are replayed in the
correct order. For example, a node may be created, added to a scene, and
have its hidden property set to true, all in the same frame. If the
current timestamps were used for all these events, it could happen that
the hidden property is set slightly after the node is added to the scene.
When replayed, this could cause the node to be visible for a single frame
if the queue of replay events pulls only the creation and addition events.
By using the frame's update time, we ensure that all events related to a
node are recorded with the same timestamp and are replayed in the same
frame.
If game has not yet begun to run (i.e., frame updates have not yet started),
the timestamps will be based on the current time.
Defined in
M2c2KitHelpers.ts:468
createTimestamps()
static
createTimestamps():object
Creates timestamps based on the current time.
Returns
object
object with timestamp
and iso8601Timestamp
properties
iso8601Timestamp
iso8601Timestamp:
string
timestamp
timestamp:
number
Remarks
Use M2c2KitHelpers.createFrameUpdateTimestamps()
when requesting
timestamps for events related to node creation, parent-child
relationships, and properties.
See () for explanation.
Defined in
M2c2KitHelpers.ts:490
findCentroid()
static
findCentroid(points
):Point
Finds the centroid of a rectangle.
Parameters
• points: Point
[]
An array of four points representing the vertices of the rectangle.
Returns
array of points representing the centroid of the rectangle.
Defined in
M2c2KitHelpers.ts:759
getUrlFromManifest()
static
getUrlFromManifest(game
,url
):string
Returns the URL as it appears in the game's manifest.json file.
Parameters
• game: Game
game object
• url: string
the URL
Returns
string
the hashed URL from the manifest, or the original URL if there is no manifest or the URL is not in the manifest.
Remarks
This is used to return the hashed URL.
Defined in
M2c2KitHelpers.ts:33
isPointInsideRectangle()
static
isPointInsideRectangle(point
,rect
):boolean
Checks if a point is inside a rectangle.
Parameters
• point: Point
The Point to check
• rect: Point
[]
An array of four Points representing the vertices of the rectangle in clockwise order
Returns
boolean
true if the Point is inside the rectangle
Remarks
The rectangle may have been rotated (sides might not be parallel to the axes).
Defined in
M2c2KitHelpers.ts:686
loadEruda()
static
loadEruda(pollingIntervalMs
,maxAttempts
):void
Loads eruda from a CDN and initializes it.
Parameters
• pollingIntervalMs: number
= 100
milliseconds between each attempt
• maxAttempts: number
= 50
how many attempts to make
Returns
void
Remarks
This is for debugging purposes only. If this is unwanted, it can be disabled on the server side with an appropriate Content Security Policy (CSP) header. eruda is a dev console overlay for mobile web browsers and web views. see https://github.com/liriliri/eruda
Defined in
M2c2KitHelpers.ts:381
loadScriptUrls()
static
loadScriptUrls(urls
):void
Load scripts from URLs.
Parameters
• urls: string
[]
URLs with scripts to load
Returns
void
Remarks
This is for debugging purposes only. If this is unwanted, it can be disabled on the server side with an appropriate Content Security Policy (CSP) header.
Defined in
M2c2KitHelpers.ts:348
nodeOrAncestorHasBeenRotated()
static
nodeOrAncestorHasBeenRotated(node
):boolean
Checks if the node or any of its ancestors have been rotated.
Parameters
• node: M2Node
node to check
Returns
boolean
true if the node or any of its ancestors have been rotated
Defined in
M2c2KitHelpers.ts:725
normalizeAngleRadians()
static
normalizeAngleRadians(radians
):number
Normalizes an angle in radians to the range [0, 2*Math.PI)
Parameters
• radians: number
angle in radians
Returns
number
normalized angle in radians
Defined in
M2c2KitHelpers.ts:638
radiansToDegrees()
static
radiansToDegrees(radians
):number
Converts an angle from radians to degrees.
Parameters
• radians: number
The angle in radians
Returns
number
The angle in degrees
Remarks
In m2c2kit, radians are counter clockwise from the positive x-axis, but the rotate method in CanvasKit uses degrees clockwise. Thus we negate after conversion from radians to degrees.
Defined in
M2c2KitHelpers.ts:628
registerM2NodeClass()
static
registerM2NodeClass(...nodeClass
):void
Registers a M2Node
class with the global class registry.
Parameters
• ...nodeClass: M2NodeConstructor
[]
class or classes to register.
Returns
void
Remarks
This is used to register a class so that it can be
instantiated by the M2NodeFactory
.
Defined in
M2c2KitHelpers.ts:431
rotateCanvasForDrawableNode()
static
rotateCanvasForDrawableNode(canvas
,drawableNode
):void
Rotates the canvas so the node appears rotated when drawn.
Parameters
• canvas: Canvas
CanvasKit canvas to rotate
• drawableNode: IDrawable
& M2Node
Node to rotate the canvas for
Returns
void
Remarks
Nodes inherit rotations from their ancestors. Each ancestor, however, rotates around its own anchor point. Thus, we must rotate the canvas around the anchor point of each ancestor as well as the node's anchor point.
Defined in
M2c2KitHelpers.ts:563
rotatePoint()
static
rotatePoint(point
,radians
,center
):Point
Rotates a point, counterclockwise, around another point by an angle in radians.
Parameters
• point: Point
Point to rotate
• radians: number
angle in radians
• center: Point
Point to rotate around
Returns
rotated point
Defined in
M2c2KitHelpers.ts:788
urlHasScheme()
static
urlHasScheme(url
):boolean
Does the URL have a scheme?
Parameters
• url: string
the URL to test
Returns
boolean
true if the url begins with a scheme (e.g., "http://", "https://", "file://", etc.)
Defined in
M2c2KitHelpers.ts:48