Constructors

Properties

Accessors

Methods

Constructors

  • Initialize canvas-constructor with canvas.

    Parameters

    • width: number

      The width of the canvas.

    • height: number

      The height of the canvas.

    • Optional type: "pdf" | "svg"

      The type of Canvas.

    Returns Canvas

Properties

canvas: Canvas

The constructed Canvas.

context: CanvasRenderingContext2D

The 2D context for the Canvas.

Accessors

  • get antialias(): "none" | "gray" | "default" | "subpixel"
  • Gets the anti-aliasing mode.

    Returns "none" | "gray" | "default" | "subpixel"

    Note

    This is a canvas extension.

  • get height(): number
  • The image height of this canvas

    Returns number

  • set height(value): void
  • Parameters

    • value: number

    Returns void

  • get lineDash(): number[]
  • A list of numbers that specifies distances to alternately draw a line and a gap (in coordinate space units). If the number, when setting the elements, was odd, the elements of the array get copied and concatenated. For example, setting the line dash to [5, 15, 25] will result in getting back [5, 15, 25, 5, 15, 25].

    Returns number[]

    See

    https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/getLineDash

    Example

    new Canvas(400, 300)
    .beginPath()
    .setLineDash([5, 15])
    .moveTo(0, 50)
    .lineTo(400, 50)
    .stroke()
    .png();
  • get patternQuality(): "fast" | "good" | "best" | "nearest" | "bilinear"
  • Gets the pattern quality.

    Returns "fast" | "good" | "best" | "nearest" | "bilinear"

    Note

    This is a canvas extension.

  • get textDrawingMode(): "path" | "glyph"
  • Gets the text drawing mode.

    Returns "path" | "glyph"

    Note

    This is a canvas extension.

  • get textFontHeight(): number
  • The font height

    Returns number

  • get width(): number
  • The image width of this canvas

    Returns number

  • set width(value): void
  • Parameters

    • value: number

    Returns void

Methods

  • For PDF canvases, adds another page.

    Parameters

    • Optional width: number

      The width of the new PDF page, defaults to the canvas's initial width.

    • Optional height: number

      The height of the new PDF page, defaults to the canvas's initial height.

    Returns this

    Note

    This is a canvas extension.

  • Adds an arc to the path which is centered at (X, Y) position with radius r starting at startAngle and ending at endAngle going in the given direction by anticlockwise (defaulting to clockwise).

    Parameters

    • x: number

      The X coordinate of the arc's center.

    • y: number

      The Y coordinate of the arc's center.

    • radius: number

      The arc's radius.

    • startAngle: number

      The angle at which the arc starts, measured clockwise from the positive x axis and expressed in radians.

    • endAngle: number

      The angle at which the arc ends, measured clockwise from the positive x axis and expressed in radians.

    • Optional anticlockwise: boolean

      An optional Boolean which, if true, causes the arc to be drawn counter-clockwise between the two angles. By default it is drawn clockwise.

    Returns this

  • Adds an arc to the path with the given control points and radius, connected to the previous point by a straight line.

    Parameters

    • x1: number

      The x axis of the coordinate for the first control point.

    • y1: number

      The y axis of the coordinate for the first control point.

    • x2: number

      The x axis of the coordinate for the second control point.

    • y2: number

      The y axis of the coordinate for the second control point.

    • radius: number

      The arc's radius.

    Returns this

  • Adds a cubic Bézier curve to the path. It requires three points. The first two points are control points and the third one is the end point. The starting point is the last point in the current path, which can be changed using moveTo() before creating the Bézier curve.

    Parameters

    • cp1x: number

      The x axis of the coordinate for the first control point.

    • cp1y: number

      The y axis of the coordinate for first control point.

    • cp2x: number

      The x axis of the coordinate for the second control point.

    • cp2y: number

      The y axis of the coordinate for the second control point.

    • x: number

      The x axis of the coordinate for the end point.

    • y: number

      The y axis of the coordinate for the end point.

    Returns this

  • Change the current canvas' height.

    Parameters

    • height: number

      The new height for the canvas.

    Returns this

  • Change the current canvas' size.

    Parameters

    • width: number

      The new width for the canvas.

    • height: number

      The new height for the canvas.

    Returns this

  • Change the current canvas' width.

    Parameters

    • width: number

      The new width for the canvas.

    Returns this

  • Clear a circle.

    Parameters

    • x: number

      The position x in the center of the clip's circle.

    • y: number

      The position y in the center of the clip's circle.

    • radius: number

      The radius for the clip.

    • start: number = 0

      The degree in radians to start drawing the circle.

    • angle: number = ...

      The degree in radians to finish drawing the circle, defaults to a full circle.

    • antiClockwise: boolean = false

      Whether or not the angle should be anti-clockwise.

    Returns this

  • Create a round clip.

    Parameters

    • dx: number

      The position x in the center of the clip's circle.

    • dy: number

      The position y in the center of the clip's circle.

    • radius: number

      The radius for the clip.

    • Optional start: number

      The degree in radians to start drawing the circle.

    • Optional angle: number

      The degree in radians to finish drawing the circle, defaults to a full circle.

    • Optional antiClockwise: boolean

      Whether the path should be anti-clockwise.

    Returns this

    See

    createRoundPath

  • Create a round path.

    Parameters

    • dx: number

      The position x in the center of the clip's circle.

    • dy: number

      The position y in the center of the clip's circle.

    • radius: number

      The radius for the clip.

    • start: number = 0

      The degree in radians to start drawing the circle.

    • angle: number = ...

      The degree in radians to finish drawing the circle, defaults to a full circle.

    • antiClockwise: boolean = false

      Whether the path should be anti-clockwise.

    Returns this

  • Creates an ellipse clip which is centered at (X, Y) position with the radius radiusX and radiusY starting at startAngle and ending at endAngle going in the given direction by anticlockwise (defaulting to clockwise).

    Parameters

    • x: number

      The x axis of the coordinate for the ellipse's center.

    • y: number

      The y axis of the coordinate for the ellipse's center.

    • radiusX: number

      The ellipse's major-axis radius.

    • radiusY: number

      The ellipse's minor-axis radius.

    • rotation: number

      The rotation for this ellipse, expressed in radians.

    • startAngle: number

      The starting point, measured from the x axis, from which it will be drawn, expressed in radians.

    • endAngle: number

      The end ellipse's angle to which it will be drawn, expressed in radians.

    • Optional anticlockwise: boolean

      An optional Boolean which, if true, draws the ellipse anticlockwise (counter-clockwise), otherwise in a clockwise direction.

    Returns this

  • Adds an ellipse to the path which is centered at (X, Y) position with the radius radiusX and radiusY starting at startAngle and ending at endAngle going in the given direction by anticlockwise (defaulting to clockwise).

    Parameters

    • x: number

      The x axis of the coordinate for the ellipse's center.

    • y: number

      The y axis of the coordinate for the ellipse's center.

    • radiusX: number

      The ellipse's major-axis radius.

    • radiusY: number

      The ellipse's minor-axis radius.

    • rotation: number

      The rotation for this ellipse, expressed in radians.

    • startAngle: number

      The starting point, measured from the x axis, from which it will be drawn, expressed in radians.

    • endAngle: number

      The end ellipse's angle to which it will be drawn, expressed in radians.

    • Optional anticlockwise: boolean

      An optional Boolean which, if true, draws the ellipse anticlockwise (counter-clockwise), otherwise in a clockwise direction.

    Returns this

  • Creates a new, blank ImageData object with the specified dimensions. All of the pixels in the new object are transparent black.

    Parameters

    • sw: number

      The width to give the new ImageData object. A negative value flips the rectangle around the vertical axis.

    • sh: number

      The height to give the new ImageData object. A negative value flips the rectangle around the horizontal axis.

    • Optional settings: ImageDataSettings

      The settings to be used.

    Returns ImageData

  • Creates a new, blank ImageData object with the dimensions of the specified object. All of the pixels in the new object are transparent black.

    Parameters

    • imageData: ImageData

      An existing ImageData object from which to copy the width and height. The image itself is not copied.

    Returns ImageData

  • Creates a gradient along the line given by the coordinates represented by the parameters. The coordinates are global, the second point does not rely on the position of the first and vice versa.

    Parameters

    • x0: number

      The x axis of the coordinate of the start point.

    • y0: number

      The y axis of the coordinate of the start point.

    • x1: number

      The x axis of the coordinate of the end point.

    • y1: number

      The y axis of the coordinate of the end point.

    • steps: readonly GradientStop[] = []

      The gradient steps.

    Returns CanvasGradient

  • Creates a radial gradient given by the coordinates of the two circles represented by the parameters.

    Parameters

    • x0: number

      The x axis of the coordinate of the start circle.

    • y0: number

      The y axis of the coordinate of the start circle.

    • r0: number

      The radius of the start circle.

    • x1: number

      The x axis of the coordinate of the end circle.

    • y1: number

      The y axis of the coordinate of the end circle.

    • r1: number

      The radius of the end circle.

    • steps: readonly GradientStop[] = []

      The gradient steps.

    Returns CanvasGradient

  • Create a rectangle clip.

    Parameters

    • x: number

      The position x in the left corner.

    • y: number

      The position y in the upper corner.

    • width: number

      The width of the rectangle.

    • height: number

      The height of the rectangle.

    Returns this

  • Create a rectangle path.

    Parameters

    • x: number

      The position x in the left corner.

    • y: number

      The position y in the upper corner.

    • width: number

      The width of the rectangle.

    • height: number

      The height of the rectangle.

    Returns this

  • Create a beveled clip.

    Parameters

    • x: number

      The position x to start drawing clip.

    • y: number

      The position y to start drawing clip.

    • width: number

      The width of clip.

    • height: number

      The height of clip.

    • radius: number | BeveledRadiusOptions

      The radius for clip's rounded borders.

    Returns this

    Example

    // Radius argument, fill the content
    new Canvas(200, 200)
    .createRoundedClip(0, 0, 200, 50, 35)
    .fill()
    .png();

    Example

    // Configured bevels
    new Canvas(200, 200)
    .createRoundedClip(0, 0, 200, 50, {
    // Top left border
    tl: 15,
    // Top right border
    tr: 20,
    // Bottom left border
    bl: 5,
    // Bottom right border
    br: 10
    })
    // Add an image with the shape of the beveled clip using different borders
    .printImage(buffer, 0, 0, 200, 50)
    .png();

    Example

    // Top bevels only
    new Canvas(200, 200)
    .createRoundedClip(0, 0, 200, 50, { tl: 20, tr: 20, bl: 0, br: 0 })
    .printImage(buffer, 0, 0, 200, 50)
    .png();
  • Create a beveled path.

    Parameters

    • x: number

      The position x to start drawing clip.

    • y: number

      The position y to start drawing clip.

    • width: number

      The width of clip.

    • height: number

      The height of clip.

    • radius: number | BeveledRadiusOptions

      The radius for clip's rounded borders.

    Returns this

  • Fills the current or given path with the current fill style using the non-zero or even-odd winding rule.

    Parameters

    • Optional fillRule: CanvasFillRule

      The algorithm by which to determine if a point is inside a path or outside a path.

    Returns this

  • Fills the current or given path with the current fill style using the non-zero or even-odd winding rule.

    Parameters

    • path: Path2D

      The path to fill.

    • Optional fillRule: CanvasFillRule

      The algorithm by which to determine if a point is inside a path or outside a path.

    Returns this

  • Returns an ImageData object representing the underlying pixel data for the area of the canvas denoted by the entire Canvas. This method is not affected by the canvas transformation matrix.

    Returns ImageData

  • Returns an ImageData object representing the underlying pixel data for the area of the canvas denoted by the rectangle which starts at (sx, sy) and has an sw width and sh height. This method is not affected by the canvas transformation matrix.

    Parameters

    • x: number

      The X coordinate of the upper left corner of the rectangle from which the ImageData will be extracted.

    • y: number

      The Y coordinate of the upper left corner of the rectangle from which the ImageData will be extracted.

    • width: number

      The width of the rectangle from which the ImageData will be extracted.

    • height: number

      The height of the rectangle from which the ImageData will be extracted.

    Returns ImageData

  • Reports whether or not the specified point is contained in the current path.

    Parameters

    • x: number

      The X coordinate of the point to check.

    • y: number

      The Y coordinate of the point to check.

    • Optional fillRule: CanvasFillRule

      The algorithm by which to determine if a point is inside a path or outside a path.

    Returns boolean

  • Reports whether or not the specified point is contained in the given path.

    Parameters

    • path: Path2D

      The Path2D to check against.

    • x: number

      The X coordinate of the point to check.

    • y: number

      The Y coordinate of the point to check.

    • Optional fillRule: CanvasFillRule

      The algorithm by which to determine if a point is inside a path or outside a path.

    Returns boolean

  • Gets a JPEG buffer.

    Parameters

    • Optional config: JpegConfig

      The render configuration.

    Returns Buffer

    A JPEG buffer.

    See

    jpegAsync for the async version.

  • Gets a JPEG buffer.

    Parameters

    • Optional config: JpegConfig

      The render configuration.

    Returns Promise<Buffer>

    A JPEG buffer.

    See

    jpeg for the sync version.

  • Creates a JPEG stream.

    Parameters

    • Optional config: JpegConfig

      The config to use.

    Returns JPEGStream

    Note

    This is a canvas extension.

  • Measure a text's width given a string.

    Parameters

    • text: string

      The text to measure.

    Returns TextMetrics

    See

    https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/measureText

    Example

    const size = new Canvas(500, 400)
    .setTextFont('40px Tahoma')
    .measureText('Hello World!'); // Returns a number

    const newSize = size.width < 500 ? 40 : (500 / size.width) * 40;

    new Canvas(500, 400)
    .setTextFont(`${newSize}px Tahoma`)
    .printText('Hello World!', 30, 50)
    .png(); // Returns a Buffer

    Example

    new Canvas(500, 400)
    .setTextFont('40px Tahoma')
    .process((canvas) => {
    const size = canvas.measureText('Hello World!');
    const newSize = size.width < 500 ? 40 : (500 / size.width) * 40;
    this.setTextFont(`${newSize}px Tahoma`);
    })
    .printText('Hello World!', 30, 50)
    .png(); // Returns a Buffer
  • Parameters

    • font: string

    Returns readonly [string] | readonly [string, number, string]

  • Gets a PDF buffer.

    Parameters

    • Optional config: PdfConfig

      The render configuration.

    Returns Buffer

    A PDF buffer.

    See

    pdfAsync for the async version.

  • Gets a PDF buffer.

    Parameters

    • Optional config: PdfConfig

      The render configuration.

    Returns Promise<Buffer>

    A PDF buffer.

    See

    pdf for the sync version.

  • Creates a PDF stream.

    Parameters

    • Optional config: PdfConfig

      The config to use.

    Returns PDFStream

    Note

    This is a canvas extension.

  • Gets a PNG buffer.

    Parameters

    • Optional config: PngConfig

      The render configuration.

    Returns Buffer

    A PNG buffer.

    See

    pngAsync for the async version.

  • Gets a PNG buffer.

    Parameters

    • Optional config: PngConfig

      The render configuration.

    Returns Promise<Buffer>

    A PNG buffer.

    See

    png for the sync version.

  • Creates a PNG stream.

    Parameters

    • Optional config: PngConfig

      The config to use.

    Returns PNGStream

    Note

    This is a canvas extension.

  • Add a circle or semi circle.

    Parameters

    • x: number

      The position x in the center of the circle.

    • y: number

      The position y in the center of the circle.

    • radius: number

      The radius for the clip.

    Returns this

  • Add a round image.

    Parameters

    • imageOrBuffer: ImageResolvable

      The image.

    • x: number

      The X coordinate in the destination canvas at which to place the top-left corner of the source image.

    • y: number

      The Y coordinate in the destination canvas at which to place the top-left corner of the source image.

    • radius: number

      The radius for the circle

    • Optional options: PrintCircularOptions

    Returns this

  • Add an image at a position (x, y) with the source image's width and height.

    Parameters

    • image: ImageResolvable

      The image.

    • dx: number

      The x-axis coordinate in the destination canvas at which to place the top-left corner of the source image.

    • dy: number

      The y-axis coordinate in the destination canvas at which to place the top-left corner of the source image.

    Returns this

  • Add an image at a position (x, y) with a given width and height.

    Parameters

    • image: ImageResolvable

      The image.

    • dx: number

      The x-axis coordinate in the destination canvas at which to place the top-left corner of the source image.

    • dy: number

      The y-axis coordinate in the destination canvas at which to place the top-left corner of the source image.

    • dw: number

      The width to draw the image in the destination canvas. This allows scaling of the drawn image.

    • dh: number

      The height to draw the image in the destination canvas. This allows scaling of the drawn image.

    Returns this

  • Add an image at a position (x, y) with a given width and height, from a specific source rectangle.

    Parameters

    • image: ImageResolvable

      The image.

    • sx: number

      The x-axis coordinate of the top left corner of the sub-rectangle of the source image to draw into the destination context.

    • sy: number

      The y-axis coordinate of the top left corner of the sub-rectangle of the source image to draw into the destination context.

    • sw: number

      The width of the sub-rectangle of the source image to draw into the destination context.

    • sh: number

      The height of the sub-rectangle of the source image to draw into the destination context.

    • dx: number

      The x-axis coordinate in the destination canvas at which to place the top-left corner of the source image.

    • dy: number

      The y-axis coordinate in the destination canvas at which to place the top-left corner of the source image.

    • dw: number

      The width to draw the image in the destination canvas. This allows scaling of the drawn image.

    • dh: number

      The height to draw the image in the destination canvas. This allows scaling of the drawn image.

    Returns this

  • Creates a gradient along the line given by the coordinates represented by the parameters. The coordinates are global, the second point does not rely on the position of the first and vice versa. This method is chainable and calls setColor after creating the gradient.

    Parameters

    • x0: number

      The x axis of the coordinate of the start point.

    • y0: number

      The y axis of the coordinate of the start point.

    • x1: number

      The x axis of the coordinate of the end point.

    • y1: number

      The y axis of the coordinate of the end point.

    • Optional steps: readonly GradientStop[]

      The gradient steps.

    Returns this

    See

    https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/createLinearGradient

    Example

    new Canvas(200, 200)
    .printLinearColorGradient(0, 0, 200, 50, [
    { position: 0, color: 'white' },
    { position: 0.25, color: 'red' },
    { position: 0.5, color: 'blue' }
    ])
    .printRectangle(10, 10, 200, 100)
  • Creates a gradient along the line given by the coordinates represented by the parameters. The coordinates are global, the second point does not rely on the position of the first and vice versa. This method is chainable and calls setStroke after creating the gradient.

    Parameters

    • x0: number

      The x axis of the coordinate of the start point.

    • y0: number

      The y axis of the coordinate of the start point.

    • x1: number

      The x axis of the coordinate of the end point.

    • y1: number

      The y axis of the coordinate of the end point.

    • Optional steps: readonly GradientStop[]

      The gradient steps.

    Returns this

    See

    https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/createLinearGradient

    Example

    new Canvas(200, 200)
    .printLinearStrokeGradient(0, 0, 200, 50, [
    { position: 0, color: 'white' },
    { position: 0.25, color: 'red' },
    { position: 0.5, color: 'blue' }
    ])
    .printRectangle(10, 10, 200, 100)
  • Add text with line breaks (node-canvas and web canvas compatible)

    Parameters

    • text: string

      The text to write.

    • x: number

      The position x to start drawing the element.

    • y: number

      The position y to start drawing the element.

    Returns this

    Example

    new Canvas(400, 300)
    .setTextFont('25px Tahoma')
    .printMultilineText('This is a really\nlong text!', 139, 360)
    .png();
  • Creates a radial gradient given by the coordinates of the two circles represented by the parameters. This method is chainable and calls setColor after creating the gradient.

    Parameters

    • x0: number

      The x axis of the coordinate of the start circle.

    • y0: number

      The y axis of the coordinate of the start circle.

    • r0: number

      The radius of the start circle.

    • x1: number

      The x axis of the coordinate of the end circle.

    • y1: number

      The y axis of the coordinate of the end circle.

    • r1: number

      The radius of the end circle.

    • Optional steps: readonly GradientStop[]

      The gradient steps.

    Returns this

  • Creates a radial gradient given by the coordinates of the two circles represented by the parameters. This method is chainable and calls setStroke after creating the gradient.

    Parameters

    • x0: number

      The x axis of the coordinate of the start circle.

    • y0: number

      The y axis of the coordinate of the start circle.

    • r0: number

      The radius of the start circle.

    • x1: number

      The x axis of the coordinate of the end circle.

    • y1: number

      The y axis of the coordinate of the end circle.

    • r1: number

      The radius of the end circle.

    • Optional steps: readonly GradientStop[]

      The gradient steps.

    Returns this

  • Add responsive text

    Parameters

    • text: string

      The text to write.

    • x: number

      The position x to start drawing the element.

    • y: number

      The position y to start drawing the element.

    • maxWidth: number

      The max length in pixels for the text.

    Returns this

    Example

    new Canvas(400, 300)
    .setTextFont('40px Tahoma')
    .printResponsiveText('Hello World', 30, 30, 50)
    .png();
  • Add a beveled image.

    Parameters

    • imageOrBuffer: ImageResolvable

      The image.

    • x: number

      The position x to start drawing the element.

    • y: number

      The position y to start drawing the element.

    • width: number

      The width of the element.

    • height: number

      The height of the element.

    • radius: number | BeveledRadiusOptions

      The radius for the new image.

    Returns this

  • Add a beveled rectangle.

    Parameters

    • x: number

      The position x to start drawing the element.

    • y: number

      The position y to start drawing the element.

    • width: number

      The width of the element.

    • height: number

      The height of the element.

    • radius: number | BeveledRadiusOptions

      The radius for the bevels.

    Returns this

    See

    https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/fillRect

    Example

    // Radius argument
    new Canvas(200, 200)
    .printRoundedRectangle(0, 0, 200, 50, 35)
    .png();

    Example

    // Configured bevels
    new Canvas(200, 200)
    .printRoundedRectangle(0, 0, 200, 50, {
    // Top left border
    tl: 15,
    // Top right border
    tr: 20,
    // Bottom left border
    bl: 5,
    // Bottom right border
    br: 10
    })
    .png();

    Example

    // Top bevels only
    new Canvas(200, 200)
    .printRoundedRectangle(0, 0, 200, 50, { tl: 20, tr: 20, bl: 0, br: 0 })
    .png();
  • Paints a rectangle which has a starting point at (X, Y) and has a w width and an h height onto the canvas, using the current stroke style.

    Parameters

    • x: number

      The x axis of the coordinate for the rectangle starting point.

    • y: number

      The y axis of the coordinate for the rectangle starting point.

    • width: number

      The rectangle's width.

    • height: number

      The rectangle's height.

    Returns this

  • Add stroked text.

    Parameters

    • text: string

      The text to write.

    • x: number

      The position x to start drawing the element.

    • y: number

      The position y to start drawing the element.

    • Optional maxWidth: number

      The maximum width to draw. If specified, and the string is computed to be wider than this width, the font is adjusted to use a more horizontally condensed font.

    Returns this

  • Add a text.

    Parameters

    • text: string

      The text to write.

    • x: number

      The position x to start drawing the element.

    • y: number

      The position y to start drawing the element.

    • Optional maxWidth: number

      The maximum width to draw. If specified, and the string is computed to be wider than this width, the font is adjusted to use a more horizontally condensed font.

    Returns this

  • Wrap the text in multiple lines and write it

    Parameters

    • text: string

      The text to wrap and write.

    • x: number

      The position x to start drawing the element.

    • y: number

      The position y to start drawing the element.

    • wrapWidth: number

      The width in pixels of the line wrap

    Returns this

    Example

    new Canvas(400, 300)
    .setTextFont('25px Tahoma')
    .printWrappedText('This is a really long text!', 139, 360)
    .png();
  • Process data with this as the context

    Type Parameters

    • Args extends readonly any[]

    Parameters

    • fn: ((this, canvas, ...args) => unknown)

      A callback function

        • (this, canvas, ...args): unknown
        • Parameters

          • this: this
          • canvas: this
          • Rest ...args: Args

          Returns unknown

    • Rest ...args: Args

      Extra arguments to pass to the function

    Returns this

  • The CanvasRenderingContext2D.putImageData() method of the Canvas 2D API paints data from the given ImageData object onto the bitmap. This method is not affected by the canvas transformation matrix.

    Parameters

    • imagedata: ImageData

      An ImageData object containing the array of pixel values.

    • dx: number

      Horizontal position (x-coordinate) at which to place the image data in the destination canvas.

    • dy: number

      Vertical position (y-coordinate) at which to place the image data in the destination canvas.

    Returns this

  • The CanvasRenderingContext2D.putImageData() method of the Canvas 2D API paints data from the given ImageData object onto the bitmap. Only the pixels from that rectangle are painted. This method is not affected by the canvas transformation matrix.

    Parameters

    • imagedata: ImageData

      An ImageData object containing the array of pixel values.

    • x: number

      Horizontal position (x-coordinate) at which to place the image data in the destination canvas.

    • y: number

      Vertical position (y-coordinate) at which to place the image data in the destination canvas.

    • dirtyX: number

      Horizontal position (x-coordinate). The X coordinate of the top left hand corner of your Image data. Defaults to 0.

    • dirtyY: number

      Vertical position (y-coordinate). The Y coordinate of the top left hand corner of your Image data. Defaults to 0.

    • dirtyWidth: number

      Width of the rectangle to be painted. Defaults to the width of the image data.

    • dirtyHeight: number

      Height of the rectangle to be painted. Defaults to the height of the image data.

    Returns this

  • Adds a quadratic Bézier curve to the path. It requires two points. The first point is a control point and the second one is the end point. The starting point is the last point in the current path, which can be changed using moveTo() before creating the quadratic Bézier curve.

    Parameters

    • cpx: number

      The x axis of the coordinate for the control point.

    • cpy: number

      The y axis of the coordinate for the control point.

    • x: number

      The x axis of the coordinate for the end point.

    • y: number

      The y axis of the coordinate for the end point.

    Returns this

  • Sets the shadow blur and offsets to zero, then sets the shadow color to transparent. If shadows are not longer used in a canvas and performance is critical, .setShadowColor('transparent') should be used instead, as of the note from Mozilla Developer Network.

    Returns this

    Example

    new Canvas(500, 500)
    // Set a shadow color and blur
    .setShadowColor('rgba(23, 23, 23, 0.2)')
    .setShadowBlur(5)
    // Render the text with a blow effect
    .printText('Hello', 30, 50)
    // Reset the shadows
    .resetShadows()
    // Render the text without shadows
    .printText('World!', 30, 100);
  • Parameters

    • imageOrBuffer: ImageResolvable
    • x: number
    • y: number
    • radius: number
    • fit: NonNullable<undefined | "fill" | "contain" | "cover" | "none">

    Returns ResolvedCircularCoordinates

  • Set antialias mode.

    Parameters

    • antialias: "none" | "gray" | "default" | "subpixel"

      The antialias mode.

    Returns this

    Note

    This is a canvas extension.

  • Sets the line dash pattern used when stroking lines, using an array of values which specify alternating lengths of lines and gaps which describe the pattern.

    Parameters

    • segments: number[]

      An Array of numbers which specify distances to alternately draw a line and a gap (in coordinate space units). If the number of elements in the array is odd, the elements of the array get copied and concatenated. For example, [5, 15, 25] will become [5, 15, 25, 5, 15, 25]. If the array is empty, the line dash list is cleared and line strokes return to being solid.

    Returns this

  • Determines how two connecting segments (of lines, arcs or curves) with non-zero lengths in a shape are joined together (degenerate segments with zero lengths, whose specified endpoints and control points are exactly at the same position, are skipped).

    Parameters

    • value: CanvasLineJoin

      The line join type.

    Returns this

  • Sets the miter limit ratio in space units. When getting, it returns the current value (10.0 by default). When setting, zero, negative, Infinity and NaN values are ignored; otherwise the current value is set to the new value.

    Parameters

    • value: number

      A number specifying the miter limit ratio in space units. Zero, negative, Infinity and NaN values are ignored.

    Returns this

  • Change the pattern quality

    Parameters

    • pattern: "fast" | "good" | "best" | "nearest" | "bilinear"

      The pattern quality.

    Returns this

    Note

    This is a canvas extension.

  • Parameters

    • width: number

    Returns this

  • Set the text drawing mode. Using glyph is much faster than path for drawing, and when using a PDF context will embed the text natively, so will be selectable and lower file size. The downside is that cairo does not have any subpixel precision for glyph, so this will be noticeably lower quality for text positioning in cases such as rotated text. Also, strokeText in glyph will act the same as fillText, except using the stroke style for the fill.

    Parameters

    • mode: "path" | "glyph"

      The drawing mode.

    Returns this

    Note

    This is a canvas extension.

  • Set the new font size, unlike setTextFont, this only requires the number.

    Parameters

    • size: number

      The new size to set

    Returns this

  • For image canvases, encodes the canvas as a PNG. For PDF canvases, encodes the canvas as a PDF. For SVG canvases, encodes the canvas as an SVG.

    Returns Buffer

  • Encodes the canvas as a PNG.

    Parameters

    • mimeType: "image/png"

      the standard MIME type for the image format to return.

    • Optional config: PngConfig

      The render configuration.

    Returns Buffer

  • Encodes the canvas as a JPG.

    Parameters

    • mimeType: "image/jpeg"

      the standard MIME type for the image format to return.

    • Optional config: JpegConfig

      The render configuration.

    Returns Buffer

  • Encodes the canvas as a PDF.

    Parameters

    • mimeType: "application/pdf"

      the standard MIME type for the image format to return.

    • Optional config: PdfConfig

      The render configuration.

    Returns Buffer

  • Returns the unencoded pixel data, top-to-bottom. On little-endian (most) systems, the array will be ordered BGRA; on big-endian systems, it will be ARGB.

    Parameters

    • mimeType: "raw"

      the standard MIME type for the image format to return.

    Returns Buffer

  • For image canvases, encodes the canvas as a PNG. For PDF canvases, encodes the canvas as a PDF. For SVG canvases, encodes the canvas as an SVG.

    Returns Promise<Buffer>

  • Encodes the canvas as a PNG.

    Parameters

    • mimeType: "image/png"

      the standard MIME type for the image format to return.

    • Optional config: PngConfig

      The render configuration.

    Returns Promise<Buffer>

  • Encodes the canvas as a JPG.

    Parameters

    • mimeType: "image/jpeg"

      the standard MIME type for the image format to return.

    • Optional config: JpegConfig

      The render configuration.

    Returns Promise<Buffer>

  • Encodes the canvas as a PDF.

    Parameters

    • mimeType: "application/pdf"

      the standard MIME type for the image format to return.

    • Optional config: PdfConfig

      The render configuration.

    Returns Promise<Buffer>

  • Wraps a text into a width-limited multi-line text.

    Parameters

    • text: string

      The text to wrap

    • wrapWidth: number

      The wrap width

    Returns string

    Example

    // Calculate the wrapped text and return it, which
    // is useful for storage to avoid re-calculating the
    // wrapped text
    const wrappedText = new Canvas(500, 300)
    .setTextFont('48px Verdana')
    .wrapText('Hello World, this is a quite\nlong text.', 300);

    Example

    // Wrap the text and add it
    const buffer = new Canvas(500, 300)
    .setTextFont('48px Verdana')
    .process((canvas) => {
    const wrappedText = canvas.wrapText('Hello World, this is a quite\nlong text.');
    return canvas
    .setTextAlign('center')
    .addMultilineText(wrappedText, 250, 50)
    })
    .png(); // Returns a Buffer

Generated using TypeDoc