Class Draw

The Draw class provides methods for drawing shapes, text, and images on a canvas.

Constructors

Properties

game: Game

The game instance.

logo: HTMLImageElement = ...
pixelFont: HTMLImageElement = ...
pixelText: ((text: string, pos: Vector, scale?: number, textAlign?: CanvasTextAlign, textBaseline?: CanvasTextBaseline) => void) = ...

Methods

  • Copies the content of a canvas to the main context of the game.

    Parameters

    • canvas: HTMLCanvasElement

      The canvas element to copy from.

    • pos: Vector

      The position to place the copied content on the main context.

    • size: Vector

      The size of the copied content on the main context.

    Returns void

  • Creates a pixel font renderer function.

    Parameters

    • Optionalimage: HTMLImageElement

      The image containing the pixel font.

    • OptionalfontSize: number

      The size of each character in the pixel font.

    • Optionalcolor: string = 'white'

      The color to apply to the pixel font.

    Returns ((text: string, pos: Vector, scale?: number, textAlign?: CanvasTextAlign, textBaseline?: CanvasTextBaseline) => void)

    The pixel font renderer function.

      • (text, pos, scale?, textAlign?, textBaseline?): void
      • Parameters

        • text: string
        • pos: Vector
        • scale: number = 1
        • textAlign: CanvasTextAlign = 'left'
        • textBaseline: CanvasTextBaseline = 'top'

        Returns void

  • Draws a 2D image on the canvas.

    Parameters

    • image: HTMLImageElement | HTMLCanvasElement

      The HTMLImageElement representing the image to be drawn.

    • rect: Box

      The bounding box of the image.

    • scale: Vector = ...

      The scale factor to apply to the image (default: 1).

    • angle: number = 0

      The rotation angle of the image in radians (default: 0).

    • flipH: boolean = false

      Whether to flip the image horizontally (default: false).

    • flipV: boolean = false

      Whether to flip the image vertically (default: false).

    • Optionalclip: Vector

      The clipping region of the image (optional).

    • Optionalcontext: CanvasRenderingContext2D

      The CanvasRenderingContext2D to use for drawing (optional).

    Returns void

  • Fills a rectangle on the canvas with the specified color.

    Parameters

    • rect: Box

      The rectangle to be filled.

    • color: Color

      The color to fill the rectangle with.

    • angle: number = 0

      The angle (in radians) to rotate the rectangle (default: 0).

    Returns void

  • Draws an outlined rectangle on the canvas.

    Parameters

    • rect: Box

      The rectangle to outline.

    • color: Color

      The color of the outline.

    • lineWidth: number = 1

      The width of the outline line. Default is 1.

    • angle: number = 0

      The angle of rotation for the rectangle. Default is 0.

    Returns void

  • Displays a preloader with a progress bar on the canvas.

    Parameters

    • p: number

      The progress percent value.

    Returns void

  • Rotates the canvas context around a specified position and angle, and then executes a callback function.

    Parameters

    • rect: Box

      The box object containing the position and size.

    • angle: number = 0

      The angle of rotation in radians. Defaults to 0.

    • scale: Vector = ...

      The scale factor. Defaults to 0.

    Returns void

  • Sets the blending mode for the canvas rendering context.

    Parameters

    • blend: boolean

      A boolean indicating whether to enable blending.

    • context: CanvasRenderingContext2D = ...

      The canvas rendering context.

    Returns void

  • Draws a stroke using the provided points.

    Parameters

    • x: number

      The x-coordinate of the starting point.

    • y: number

      The y-coordinate of the starting point.

    • points: Vector[]

      An array of Vector objects representing the points to be connected.

    • lineWidth: number = 1

      The width of the stroke line (default: 1).

    Returns void

  • Renders text on the canvas.

    Parameters

    • text: string

      The text to render.

    • pos: Vector

      The position to render the text.

    • Optionalcolor: Color

      The color of the text. Defaults to white.

    • size: string = '1em'

      The font size of the text. Defaults to '1em'.

    • textAlign: CanvasTextAlign = 'left'

      The horizontal alignment of the text. Defaults to 'left'.

    • textBaseline: CanvasTextBaseline = 'top'

      The vertical alignment of the text. Defaults to 'top'.

    • stroke: boolean = false

      Whether to stroke the text. Defaults to false.

    Returns void