Skip to content

Canvas Overview

createOffscreenCanvas

TIP

The API usage is as follows: OffscreenCanvas wx.createOffscreenCanvas(object object, number width, number height, Object this)

  • Functional description: Create an off-screen canvas instance.
  • Parameters and descriptions:
    • number width: Canvas width.

    • number height: Canvas height.

    • object this: Under a custom component, this of the current component instance.

    • object object

      PropertiesTypeDefault valueRequiredDescription
      typestringwebglNoOff-screen canvas type created
      widthnumber-NoCanvas width.
      heightnumber-NoCanvas height.
      compInstComponent-NoUnder a custom component, this of the current component instance.
      • object.type
      Legal valueDescription
      webglwebgl type context
      2d2d type context

createCanvasContext

TIP

The API usage is as follows: CanvasContext wx.createCanvasContext(string canvasId, Object this)

  • Functional description: Creating a canvas drawing context canvasContext Object

  • Parameters and descriptions:

    • string canvasId To get the context's <canvas> component canvas-id attribute.
    • Object this: Under a custom component, this of the current component instance, which means to search for a <canvas> with canvas-id under this custom component. If omitted, it will not be searched in any custom component.
  • Return value: canvasContext

canvasToTempFilePath

TIP

The API usage is as follows: wx.canvasToTempFilePath(Object object, Object this)

  • Functional description: Export the content of the specified area of

  • Parameters and descriptions: Object object。

    PropertiesTypeDefault valueRequiredDescription
    xnumber0NoThe upper left corner horizontal coordinate of the specified canvas area
    ynumber0NoThe upper left corner vertical coordinate of the specified canvas area
    widthnumberCanvas width - xNoThe width of the specified canvas area
    heightnumberCanvas height - yNoThe height of the specified canvas area
    destWidthnumberwidth*screen pixel densityNoThe width of the output image
    destHeightnumberheight*screen pixel densityNoThe height of the output image
    canvasIdstring-YesCanvas ID, canvas-id passed to the <canvas> component
    fileTypestringpngNoThe type of the target file
    qualitynumber-YesThe quality of the image, currently only valid for jpg. The value range is (0, 1], and it is treated as 1.0 when it is not in the range.
    successFunction-NoCallback function for successful interface call
    failFunction-NoCallback function for failed interface call
    completeFunction-NoCallback function for interface call completion (executed regardless of success or failure)
    • Legal values

      ValueDescription
      jpgjpg image
      pngpng image
    • res callback result of Object.success

      ValueTypeDescription
      tempFilePathstringTemporary path for generated files (local path)
  • Sample code:

js
wx.canvasToTempFilePath({
  x: 100,
  y: 200,
  width: 50,
  height: 50,
  destWidth: 100,
  destHeight: 100,
  canvasId: 'myCanvas',
  success(res) {
    console.log(res.tempFilePath)
  }
})

canvasPutImageData

TIP

The API usage is as follows: wx.canvasPutImageData(Object object, Object this)

  • Functional description: Draw pixel data to the canvas. In custom components, the second parameter is passed to the custom component instance this to operate the canvas component in the component.

  • Parameter1 : Object object。

    PropertiesTypeDefault valueRequiredDescription
    canvasIdstring-YesCanvas ID, passed to the canvas-id attribute of the <canvas> component
    dataUint8ClampedArray-YesImage pixel data, one-dimensional array, each four items represent the rgba of a pixel
    xnumber-YesPosition offset of the source image data in the target canvas (offset in the x-axis direction)
    ynumber-YesPosition offset of the source image data in the target canvas (offset in the y-axis direction)
    widthnumber-YesWidth of the rectangular area of
    heightnumber-YesHeight of the rectangular area of
    successFunction-NoCallback function for successful interface call
    failFunction-NoCallback function for failed interface call
    completeFunction-NoCallback function for interface call completion (executed regardless of success or failure)
    • Parameter2 : Object this,In custom components, this of the current component instance is used to operate the <canvas> component in the component.。
  • Sample code:

js
const data = new Uint8ClampedArray([255, 0, 0, 1])
wx.canvasPutImageData({
  canvasId: 'myCanvas',
  x: 0,
  y: 0,
  width: 1,
  data,
  success(res) {}
})

canvasGetImageData

TIP

The API usage is as follows: wx.canvasGetImageData(Object object, Object this)

  • Functional description: Get the pixel data implicit in the canvas area.

  • Parameter1: Object object。

    PropertiesTypeDefault valueRequiredDescription
    canvasIdstring-YesCanvas ID, passed into the canvas-id attribute of the <canvas> component
    xnumber-YesThe upper left corner horizontal coordinate of the image data rectangle area to be extracted
    ynumber-YesThe upper left corner vertical coordinate of the image data rectangle area to be extracted
    widthnumber-YesThe width of the image data rectangle area to be extracted
    heightnumber-YesThe height of the image data rectangle area to be extracted
    successFunction-NoCallback function for successful interface call
    failFunction-NoCallback function for failed interface call
    completeFunction-NoCallback function for interface call completion (executed regardless of success or failure)
    • object.success callback function parameters: : Object res。

      PropertiesTypeDescription
      widthnumberThe width of the image data rectangle
      heightnumberThe height of the image data rectangle
      dataUint8ClampedArrayImage pixel data, one-dimensional array, each four items represent the rgba of a pixel
    • Parameter2 : Object this,In custom components, this of the current component instance is used to operate the <canvas> component in the component.。

  • Sample code:

js
wx.canvasGetImageData({
  canvasId: 'myCanvas',
  x: 0,
  y: 0,
  width: 100,
  height: 100,
  success(res) {
    console.log(res.width) // 100
    console.log(res.height) // 100
    console.log(res.data instanceof Uint8ClampedArray) // true
    console.log(res.data.length) // 100 * 100 * 4
  }
})

Image

  • Functional description: Picture objects

  • Parameters and descriptions:

    PropertiesTypeDescription
    srcstringThe URL of the image
    widthnumberThe actual width of the image
    heightnumberThe actual height of the image
    referrerPolicystringorigin: send a full referrer; no-referrer: do not send. The format is fixed as https://servicewechat.com/{appid}/{version}/page-frame.html, where {appid} is the appid of the mini program, and {version} is the version number of the mini program. Version number 0 indicates the development version, trial version, and audit version, version number devtools indicates the developer tools, and the rest are official versions;
    onloadfunctionCallback function triggered after the image is loaded
    onerrorfunctionCallback function triggered after an error occurs in image loading

ImageData

  • Functional description: ImageData object

  • Parameters and descriptions:

    PropertiesTypeDescription
    widthnumberUse pixels to describe the actual width of ImageData
    heightnumberUse pixels to describe the actual height of ImageData
    dataUint8ClampedArrayOne-dimensional array containing data in RGBA order, and the data is represented by integers from 0 to 255 (inclusive)

canvasGradient

  • Functional description: Gradient object

.addColorStop

TIP

The API usage is as follows: CanvasGradient.addColorStop(number stop, Color color)

  • Functional description: Add color gradient points. The part smaller than the minimum stop will be rendered according to the color of the minimum stop, and the part larger than the maximum stop will be rendered according to the color of the maximum stop, which is equivalent to CanvasGradient.addColorStop(number stop, Color color).
    Parameter1: number stop, Indicates the position between the start and end of the gradient, ranging from 0-1.
    Parameter2: Color color The color of the gradient point.

  • Sample code:

js
const ctx = wx.createCanvasContext('myCanvas')

// Create circular gradient
const grd = ctx.createLinearGradient(30, 10, 120, 10)
grd.addColorStop(0, 'red')
grd.addColorStop(0.16, 'orange')
grd.addColorStop(0.33, 'yellow')
grd.addColorStop(0.5, 'green')
grd.addColorStop(0.66, 'cyan')
grd.addColorStop(0.83, 'blue')
grd.addColorStop(1, 'purple')

// Fill with gradient
ctx.setFillStyle(grd)
ctx.fillRect(10, 10, 150, 80)
ctx.draw()

RenderingContext

  • Functional description: Canvas drawing context
    • The CanvasRenderingContext2D object can be obtained through the Canvas.getContext('2d') interface, which implements HTML Canvas 2D Context Defined properties and methods.
    • The WebGLRenderingContext object can be obtained through the Canvas.getContext('webgl') or OffscreenCanvas.getContext('webgl') interface, which implements WebGL 1.0 All properties, methods, and constants defined.

For more information, please see canvasContext

OffscreenCanvas

For more information, please see OffscreenCanvas

Path2D

For more information, please see Path2D

Canvas

For more information, please see Canvas

Color

For more information, please see Color