Skip to main content

Class: M2c2KitHelpers

Constructors​

new M2c2KitHelpers()​

new M2c2KitHelpers(): M2c2KitHelpers

Returns​

M2c2KitHelpers

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​

m2c2kit/packages/core/src/M2c2KitHelpers.ts:696


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​

m2c2kit/packages/core/src/M2c2KitHelpers.ts:776


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​

m2c2kit/packages/core/src/M2c2KitHelpers.ts:621


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​

m2c2kit/packages/core/src/M2c2KitHelpers.ts:545


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​

m2c2kit/packages/core/src/M2c2KitHelpers.ts:847


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​

m2c2kit/packages/core/src/M2c2KitHelpers.ts:105


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​

m2c2kit/packages/core/src/M2c2KitHelpers.ts:503


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​

m2c2kit/packages/core/src/M2c2KitHelpers.ts:525


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​

Point

array of points representing the centroid of the rectangle.

Defined in​

m2c2kit/packages/core/src/M2c2KitHelpers.ts:794


flat()​

static flat<A, D>(arr, depthArg?): FlatArray<A, D>[]

Flattens an array up to the specified depth.

This method is an implementation of the Array.prototype.flat() method, which is not present in environments prior to ES2019.

Type Parameters​

• A extends readonly unknown[]

• D extends number = 1

Parameters​

• arr: A

The array to flatten.

• depthArg?: D

The depth level specifying how deep a nested array structure should be flattened. Defaults to 1.

Returns​

FlatArray<A, D>[]

A new array with the sub-array elements concatenated into it.

Defined in​

m2c2kit/packages/core/src/M2c2KitHelpers.ts:905


flatMap()​

static flatMap<T, U>(arr, callback, thisArg?): U[]

Maps each element using a transformation function and flattens the result by one level.

This provides a spec-compliant implementation of Array.prototype.flatMap for environments that lack native support. It handles sparse arrays correctly by skipping "holes" and respects Symbol.species for array construction.

Type Parameters​

• T

The type of elements in the input array.

• U

The type of elements in the resulting flattened array.

Parameters​

• arr: undefined | null | readonly T[]

The array-like object to map and flatten.

• callback

Function that produces an element (or array of elements) of the new Array.

• thisArg?: unknown

Value to use as this when executing callback.

Returns​

U[]

A new array with each element being the result of the callback and flattened by a depth of 1.

Throws​

If arr is null or undefined.

Defined in​

m2c2kit/packages/core/src/M2c2KitHelpers.ts:946


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​

m2c2kit/packages/core/src/M2c2KitHelpers.ts:34


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​

m2c2kit/packages/core/src/M2c2KitHelpers.ts:721


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​

m2c2kit/packages/core/src/M2c2KitHelpers.ts:416


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​

m2c2kit/packages/core/src/M2c2KitHelpers.ts:383


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​

m2c2kit/packages/core/src/M2c2KitHelpers.ts:760


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​

m2c2kit/packages/core/src/M2c2KitHelpers.ts:673


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​

m2c2kit/packages/core/src/M2c2KitHelpers.ts:663


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​

m2c2kit/packages/core/src/M2c2KitHelpers.ts:466


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​

m2c2kit/packages/core/src/M2c2KitHelpers.ts:598


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​

Point

rotated point

Defined in​

m2c2kit/packages/core/src/M2c2KitHelpers.ts:823


sanitizeParameters()​

static sanitizeParameters(parameters): any

Sanitizes parameters object to prevent prototype pollution.

Parameters​

• parameters: unknown

parameters object to be sanitized

Returns​

any

sanitized parameters or undefined if parameters is undefined

Defined in​

m2c2kit/packages/core/src/M2c2KitHelpers.ts:59


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​

m2c2kit/packages/core/src/M2c2KitHelpers.ts:49