Skip to content

Drawing Functions

Yegor Pelykh edited this page May 8, 2024 · 1 revision

Image Drawing Functions

These functions allow you to draw on images. Drawing functions can be applied using the static methods of the Draw abstract class.

compositeImage

Draw.compositeImage(opt: CompositeImageOptions): MemoryImage

Composite the image src onto the image dst.

In other words, compositeImage will take a rectangular area from src of width srcW and height srcH at position (srcX,srcY) and place it in a rectangular area of dst of width dstW and height dstH at position (dstX,dstY).

If the source and destination coordinates and width and heights differ, appropriate stretching or shrinking of the image fragment will be performed. The coordinates refer to the upper left corner. This function can be used to copy regions within the same image (if dst is the same as src) but if the regions overlap the results will be unpredictable.

Parameters:

  • dst: the MemoryImage into which the src image will be composited.
  • src: the MemoryImage to be composited into the dst image.
  • dstX (optional): the starting X coordinate in the dst image to copy to. Default is 0.
  • dstY (optional): the starting Y coordinate in the dst image to copy to. Default is 0.
  • dstW (optional): the width of the rectangle in the dst image to copy to. Default is min(dst.width, src.width).
  • dstH (optional): the height of the rectangle in the dst image to copy to. Default is min(dst.height, src.height).
  • srcX (optional): the starting X coordinate in the src image to copy from. Default is 0.
  • srcY (optional): the starting Y coordinate in the src image to copy from. Default is 0.
  • srcW (optional): the width of the rectangle in the src image to copy from. Default is src.width.
  • srcH (optional): the height of the rectangle in the src image to copy from. Default is src.height.
  • blend (optional): the blending mode. Default is BlendMode.alpha.
  • linearBlend (optional): if provided, the linear blending will be used. Default is false.
  • center (optional): if is true, the src will be centered in dst. Default is false.
  • mask (optional): sets he mask to apply to the object being drawn. Default is undefined.
  • maskChannel (optional): specifies which mask channel to consider when masking an object. Default is Channel.luminance.

Returns the modified dst MemoryImage.

drawCircle

Draw.drawCircle(opt: DrawCircleOptions): MemoryImage

Draw a circle into the image with a center of center and the given radius and color.

Parameters:

  • image: MemoryImage source.
  • center: center point of a circle.
  • radius: radius of a circle.
  • color: color of a circle.
  • antialias (optional): sets whether anti-aliasing should be applied to the drawing. Default is false.
  • mask (optional): sets he mask to apply to the object being drawn. Default is undefined.
  • maskChannel (optional): specifies which mask channel to consider when masking an object. Default is Channel.luminance.

Returns the modified MemoryImage.

fillCircle

Draw.fillCircle(opt: FillCircleOptions): MemoryImage

Draw and fill a circle into the image with a center and the given radius and color.

Parameters:

  • image: MemoryImage source.
  • center: center point of a circle.
  • radius: radius of a circle.
  • color: color of a circle.
  • antialias (optional): sets whether anti-aliasing should be applied to the drawing. Default is false.
  • mask (optional): sets he mask to apply to the object being drawn. Default is undefined.
  • maskChannel (optional): specifies which mask channel to consider when masking an object. Default is Channel.luminance.

Returns the modified MemoryImage.

drawLine

Draw.drawLine(opt: DrawLineOptions): MemoryImage

Draw a line into image.

Parameters:

  • image: MemoryImage source.
  • line: the Line object containing the line's coordinates.
  • color: line color.
  • antialias (optional): sets whether anti-aliasing should be applied to the drawing. Default is false.
  • thickness (optional): sets how thick the line should be drawn, in pixels. Default is 1.

Returns the modified MemoryImage.

drawPixel

Draw.drawPixel(opt: DrawPixelOptions): MemoryImage

Draw a single pixel into the image, applying alpha and opacity blending.

Parameters:

  • image: MemoryImage source.
  • pos: the Point object with coordinates of the point.
  • color: pixel color.
  • filter (optional): if provided, the color will be scaled by the filter color. Default is undefined.
  • alpha (optional): if provided, it will be used in place of the color alpha, as a normalized color value [0, 1]. Default is undefined.
  • blend (optional): the blending mode. Default is BlendMode.alpha.
  • linearBlend (optional): if provided, the linear blending will be used. Default is false.
  • mask (optional): sets he mask to apply to the object being drawn. Default is undefined.
  • maskChannel (optional): specifies which mask channel to consider when masking an object. Default is Channel.luminance.

Returns the modified MemoryImage.

drawPolygon

Draw.drawPolygon(opt: DrawPolygonOptions): MemoryImage

Draw a polygon defined by the given vertices.

Parameters:

  • image: MemoryImage source.
  • vertices: array of Point objects with coordinates of the points.
  • color: polygon color.
  • antialias (optional): sets whether anti-aliasing should be applied to the drawing. Default is false.
  • thickness (optional): sets how thick the line should be drawn, in pixels. Default is 1.
  • mask (optional): sets he mask to apply to the object being drawn. Default is undefined.
  • maskChannel (optional): specifies which mask channel to consider when masking an object. Default is Channel.luminance.

Returns the modified MemoryImage.

drawRect

Draw.drawRect(opt: DrawRectOptions): MemoryImage

Draw a rectangle in the image with the color.

Parameters:

  • image: MemoryImage source.
  • rect: Rectangle object with coordinates to draw.
  • color: the rectangle color.
  • thickness (optional): sets how thick the line should be drawn, in pixels. Default is 1.
  • radius (optional): the rectangle corner radius. Default is 0.
  • mask (optional): sets he mask to apply to the object being drawn. Default is undefined.
  • maskChannel (optional): specifies which mask channel to consider when masking an object. Default is Channel.luminance.

Returns the modified MemoryImage.

fillFlood

Draw.fillFlood(opt: FillFloodOptions): MemoryImage

Fill the 4-connected shape containing start in the image with the given color.

Parameters:

  • image: MemoryImage source.
  • start: the start point of type Point.
  • color: fill color.
  • threshold (optional): filling threshold. Default is 0.
  • compareAlpha (optional): whether to take into account the alpha channel. Default is false.
  • mask (optional): sets he mask to apply to the object being drawn. Default is undefined.
  • maskChannel (optional): specifies which mask channel to consider when masking an object. Default is Channel.luminance.

Returns the modified MemoryImage.

fillPolygon

Draw.fillPolygon(opt: FillPolygonOptions): MemoryImage

Fill a polygon defined by the given vertices.

Parameters:

  • image: MemoryImage source.
  • vertices: array of Point objects with coordinates of the points.
  • color: polygon fill color.
  • mask (optional): sets he mask to apply to the object being drawn. Default is undefined.
  • maskChannel (optional): specifies which mask channel to consider when masking an object. Default is Channel.luminance.

Returns the modified MemoryImage.

fillRect

Draw.fillRect(opt: FillRectOptions): MemoryImage

Fill a rectangle in the image with the given color.

Parameters:

  • image: MemoryImage source.
  • rect: rectangle coordinates of type Rectangle.
  • color: fill color.
  • radius: the rectangle corner radius. Default is 0.
  • alphaBlend (optional): sets whether alpha blending should be used. Default is true.
  • mask (optional): sets he mask to apply to the object being drawn. Default is undefined.
  • maskChannel (optional): specifies which mask channel to consider when masking an object. Default is Channel.luminance.

Returns the modified MemoryImage.

fill

Draw.fill(opt: FillOptions): MemoryImage

Set all of the pixels of an image to the given color.

Parameters:

  • image: MemoryImage source.
  • color: fill color.
  • mask (optional): sets he mask to apply to the object being drawn. Default is undefined.
  • maskChannel (optional): specifies which mask channel to consider when masking an object. Default is Channel.luminance.

Returns the modified MemoryImage.

maskFlood

Draw.maskFlood(opt: MaskFloodOptions): Uint8Array

Create a mask describing the 4-connected shape containing start in the image.

Parameters:

  • image: MemoryImage source.
  • start: the start point of type Point.
  • threshold (optional): threshold of filling. Default is 0.
  • compareAlpha (optional): whether to take into account the alpha channel. Default is false.
  • fillValue (optional): The value of the items that located in the filled area. Default is 255.

Returns the mask of type Uint8Array.