Home Reference Source
import Font from 'newton-lib/src/utils/Font.js'
private class | source

Font

Stores the font settings currently used by a World Element.

Static Method Summary

Static Private Methods
private static

Checks if the provided align value is valid for a canvas element.

private static

Checks if the provided baseline value is valid for a canvas element.

Constructor Summary

Public Constructor
public

Member Summary

Public Members
public

Sets the alignment value for the text.

public

Sets the baseline value for the text.

public

Sets the font color in HEX format.

public

Sets the font face used.

public

Sets the font size in pixels.

public

Sets the line spacing between text lines.

public

Sets the font weight used.

Method Summary

Public Methods
public

set(opts: object)

Given an object with valid properties, it assigns their values to the object.

Private Methods
private

toCtx(ctx: object)

Sets the current font settings to the context.

Static Private Methods

private static validateAlign(align: string): boolean source

Checks if the provided align value is valid for a canvas element. Valid align values: "left", "right", "center", "start", "end".

Params:

NameTypeAttributeDescription
align string

Align value to check.

Return:

boolean

True if align value is valid.

Throw:

error

Error when the align value is invalid.

private static validateBaseline(baseline: string): boolean source

Checks if the provided baseline value is valid for a canvas element. Valid baseline values include: "top", "hanging", "middle", "alphabetic", "ideographic", "bottom".

Params:

NameTypeAttributeDescription
baseline string

Baseline value to check.

Return:

boolean

True if baseline value is valid.

Throw:

error

Error when the baseline value is invalid.

Public Constructors

public constructor(opts: object) source

Params:

NameTypeAttributeDescription
opts object
  • optional

Object that contains valid Font properties with values. Their values will be assigned at the end of the constructor. If an invalid property is passed then the value will be ignored.

Public Members

public align: string source

Sets the alignment value for the text. Default value is center.

public baseline: string source

Sets the baseline value for the text. Default value is middle.

public color: string source

Sets the font color in HEX format. Default value is FONT_COLOR.

public face: string source

Sets the font face used. Default value is FONT.

public size: number | string source

Sets the font size in pixels. Default value is FONT_SIZE.

public spacing: number source

Sets the line spacing between text lines. The value is multiplied by the current font size. Default value is 1.

public weight: string source

Sets the font weight used. If the font face has different weights available, another value can be selected. Default value is normal.

Public Methods

public set(opts: object) source

Given an object with valid properties, it assigns their values to the object. This allows for a signle line to modify multiple properties of the font object.

Params:

NameTypeAttributeDescription
opts object

Object with font properties.

Private Methods

private toCtx(ctx: object) source

Sets the current font settings to the context.

Params:

NameTypeAttributeDescription
ctx object

Canvas context object.