Class CanvasHelper

A wrapper for the CANVAS element that scales the canvas for high-DPI displays and helps with resizing.

const canvas = new CanvasHelper(`#my-canvas`, { resizeLogic: `both` });
const { ctx, width, height } = canvas.ctx; // Get drawing context, width & height

Draw whenever it is resized using the 'resize' event

canvas.addEventListener(`resize`, ({ctx, size}) => {
// Use ctx...
});

Or provide a function when initialising:

const onResize = (ctx, size) => {
// Do drawing
}
const canvas = new CanvasHelper(`#my-canvas`, { resizeLogic: `both`, onResize });

Automatically draw at animation speeds:

const draw = () => {
}
const canvas = new CanvasHelper(`#my-canvas`, { resizeLogic: `both`, draw });

Hierarchy (view full)

Constructors

  • Parameters

    • domQueryOrEl: Readonly<
          | undefined
          | null
          | string
          | HTMLCanvasElement>
    • opts: Partial<Readonly<{
          clearOnResize: boolean;
          colourSpace: PredefinedColorSpace;
          coordinateScale: ScaleBy;
          disablePointerEvents: boolean;
          draw?: ((ctx: CanvasRenderingContext2D, size: Rect, helper: CanvasHelper) => void);
          height: number;
          onResize?: ((ctx: CanvasRenderingContext2D, size: Rect, helper: CanvasHelper) => void);
          pixelZoom: number;
          resizeLogic?: ElementResizeLogic;
          skipCss: boolean;
          width: number;
          zIndex: number;
      }>> = {}

    Returns CanvasHelper

Properties

el: HTMLCanvasElement
opts: Readonly<{
    clearOnResize: boolean;
    colourSpace: PredefinedColorSpace;
    coordinateScale: ScaleBy;
    disablePointerEvents: boolean;
    draw?: ((ctx: CanvasRenderingContext2D, size: Rect, helper: CanvasHelper) => void);
    height: number;
    onResize?: ((ctx: CanvasRenderingContext2D, size: Rect, helper: CanvasHelper) => void);
    pixelZoom: number;
    resizeLogic?: ElementResizeLogic;
    skipCss: boolean;
    width: number;
    zIndex: number;
}>

Type declaration

  • clearOnResize: boolean

    If true (default) canvas is cleared when a resize happens

  • colourSpace: PredefinedColorSpace

    Colour space to use. Defaults to sRGB.

  • coordinateScale: ScaleBy
  • disablePointerEvents: boolean

    By default, the helper emits pointer events from the canvas. Set this to true to disable.

  • Optionaldraw?: ((ctx: CanvasRenderingContext2D, size: Rect, helper: CanvasHelper) => void)

    If specified, this function be called in an animation loop.

      • (ctx, size, helper): void
      • Parameters

        • ctx: CanvasRenderingContext2D

          Drawing context

        • size: Rect

          Viewport size

        • helper: CanvasHelper

          CanvasHelper instance

        Returns void

  • height: number

    Logical height of canvas. This is used for establishing the desired aspect ratio.

  • OptionalonResize?: ((ctx: CanvasRenderingContext2D, size: Rect, helper: CanvasHelper) => void)

    Callback when canvas is resized

      • (ctx, size, helper): void
      • Parameters

        Returns void

  • pixelZoom: number

    By default the display DPI is used for scaling. If this is set, this will override.

  • OptionalresizeLogic?: ElementResizeLogic

    Automatic canvas resizing logic.

  • skipCss: boolean

    If true, it won't add any position CSS

  • width: number

    Logical width of canvas. This is used for establishing the desired aspect ratio.

  • zIndex: number

    If set, the z-index for this canvas. By default, fullscreen canvas will be given -1

Accessors

  • get center(): {
        x: number;
        y: number;
    }
  • Gets the center coordinate of the canvas

    Returns {
        x: number;
        y: number;
    }

    • x: number
    • y: number
  • get ctx(): CanvasRenderingContext2D
  • Gets the drawing context

    Returns CanvasRenderingContext2D

  • get dimensionMax(): number
  • Returns the width or height, whichever is largest

    Returns number

  • get dimensionMin(): number
  • Returns the width or height, whichever is smallest

    Returns number

  • get height(): number
  • Gets the logical height of the canvas See also: width, size

    Returns number

  • get isDisposed(): boolean
  • Returns boolean

  • get logicalCenter(): {
        x: number;
        y: number;
    }
  • Returns {
        x: number;
        y: number;
    }

    • x: number
    • y: number
  • get ratio(): number
  • Gets the current scaling ratio being used to compensate for high-DPI display

    Returns number

  • get toAbsolute(): Scaler
  • Returns a Scaler that converts from relative to absolute coordinates. This is based on the canvas size.

    // Assuming a canvas of 800x600
    toAbsolute({ x: 1, y: 1 }); // { x: 800, y: 600}
    toAbsolute({ x: 0, y: 0 }); // { x: 0, y: 0}
    toAbsolute({ x: 0.5, y: 0.5 }); // { x: 400, y: 300}

    Returns Scaler

  • get toAbsoluteFixed(): Scaler
  • Returns a scaler for points based on width & height

    Returns Scaler

  • get toRelative(): Scaler
  • Returns a Scaler that converts from absolute to relative coordinates. This is based on the canvas size.

       * // Assuming a canvas of 800x500
    toRelative({ x: 800, y: 600 }); // { x: 1, y: 1 }
    toRelative({ x: 0, y: 0 }); // { x: 0, y: 0 }
    toRelative({ x: 400, y: 300 }); // { x: 0.5, y: 0.5 }

    Returns Scaler

  • get toRelativeFixed(): Scaler
  • Returns a scaler for points based on width & height

    Returns Scaler

  • get width(): number
  • Gets the logical width of the canvas See also: height, size

    Returns number

Methods

  • Clears the canvas.

    Shortcut for: ctx.clearRect(0, 0, this.width, this.height)

    Returns void

  • Parameters

    • strokeStyle: string = ...

    Returns void

  • Fills the canvas with a given colour.

    Shortcut for:

       * ctx.fillStyle = ``;
    ctx.fillRect(0, 0, this.width, this.height);

    Parameters

    • Optionalcolour: string

      Colour

    Returns void

  • Creates a drawing helper for the canvas. If one is already created it is reused.

    Returns void

  • Gets the image data for the canvas. Uses the 'physical' canvas size. Eg. A logical size of 400x400 might be 536x536 with a high-DPI display.

    Returns ImageData

  • Gets the drawable area of the canvas. This accounts for scaling due to high-DPI displays etc.

    Returns {
        height: number;
        width: number;
    }

    • height: number
    • width: number
  • Returns the canvas frame data as a writable grid. When editing, make as many edits as needed before calling flip, which writes buffer back to the canvas.

       * const g = helper.getWritableBuffer();
    // Get {r,g,b,opacity} of pixel 10,10
    const pixel = g.get({ x: 10, y: 10 });

    // Set a colour to pixel 10,10
    g.set({ r: 0.5, g: 1, b: 0, opacity: 0 }, { x: 10, y: 10 });

    // Write buffer to canvas
    g.flip();

    Uses 'physical' size of canvas. Eg with a high-DPI screen, this will mean a higher number of rows and columns compared to the logical size.

    Returns {
        flip: (() => void);
        get: GridCellAccessor<Rgb8Bit>;
        grid: Grid;
        set: GridCellSetter<Rgb>;
    }

  • Parameters

    Returns void