title: CanvasContext

sidebar_label: CanvasContext

The drawing context of the canvas component.

Reference

Methods

Property Type Description
fillStyle string The fill color, used in the same way as [CanvasContext.setFillStyle()].
font string The current font style. It is a DOMString that conforms to the CSS font syntaxwith at least the font size and family passed. The default value is 10px sans-serif.
globalAlpha number The global brush transparency. The value range is 0-1. 0 indicates completely transparent and 1 indicates completely opaque.
globalCompositeOperation string The type of compositing operation applied when drawing a new shape. The Android version only supports merging fill blocks, and source-over is used to merge stroke line segments.

The following operations are supported:
- Android: xor, source-over, source-atop, destination-out, lighter, overlay, darken, lighten, hard-light
- iOS: xor, source-over, source-atop, destination-over, destination-out, lighter, multiply, overlay, darken, lighten, color-dodge, color-burn, hard-light, soft-light, difference, exclusion, saturation, luminosity
lineCap "butt" | "round" | "square" The endpoint style of the line, used in the same way as [CanvasContext.setLineCap()].
lineDashOffset number The dashed line offset, with an initial value of 0.
lineJoin "round" | "bevel" | "miter" The intersection style of the line, used in the same way as [CanvasContext.setLineJoin()]。
lineWidth number The width of the line, used in the same way as [CanvasContext.setLineWidth()]。
miterLimit number The maximum miter length, used in the same ways as [CanvasContext.setMiterLimit()]。
shadowBlur number The fuzziness level of the shadow.
shadowColor number The color of the shadow.
shadowOffsetX number The horizontal offset of the shadow relative to the shape.
shadowOffsetY number The vertical offset of the shadow relative to the shape.
strokeStyle string The border color, used in the same way as [CanvasContext.setFillStyle()]。

arc

Creates an arc.

  • To create a circle, specify the start radian as 0 and the end radian as 2 * Math.PI.
  • Use the stroke or fill method to draw an arc in canvas.

The three key coordinates for the arc (100, 75, 50, 0, 1.5 * Math.PI) are as follows:

  • Green: Center point (100, 75)
  • Red: Start radian (0)
  • Blue: End radian (1.5 * Math.PI)

Reference

  1. (x: number, y: number, r: number, sAngle: number, eAngle: number, counterclockwise?: boolean) => void
Property Type Description
x number The x-coordinate of the center point.
y number The y-coordinate of the center point.
r number The radius of the circle.
sAngle number The start radian (at the 3 o’clock position).
eAngle number The end radian.
counterclockwise boolean Indicates whether the direction of the arc is counterclockwise.

Sample Code

  1. const ctx = Taro.createCanvasContext('myCanvas')
  2. // Draw coordinates
  3. ctx.arc(100, 75, 50, 0, 2 * Math.PI)
  4. ctx.setFillStyle('#EEEEEE')
  5. ctx.fill()
  6. ctx.beginPath()
  7. ctx.moveTo(40, 75)
  8. ctx.lineTo(160, 75)
  9. ctx.moveTo(100, 15)
  10. ctx.lineTo(100, 135)
  11. ctx.setStrokeStyle('#AAAAAA')
  12. ctx.stroke()
  13. ctx.setFontSize(12)
  14. ctx.setFillStyle('black')
  15. ctx.fillText('0', 165, 78)
  16. ctx.fillText('0.5*PI', 83, 145)
  17. ctx.fillText('1*PI', 15, 78)
  18. ctx.fillText('1.5*PI', 83, 10)
  19. // Draw points
  20. ctx.beginPath()
  21. ctx.arc(100, 75, 2, 0, 2 * Math.PI)
  22. ctx.setFillStyle('lightgreen')
  23. ctx.fill()
  24. ctx.beginPath()
  25. ctx.arc(100, 25, 2, 0, 2 * Math.PI)
  26. ctx.setFillStyle('blue')
  27. ctx.fill()
  28. ctx.beginPath()
  29. ctx.arc(150, 75, 2, 0, 2 * Math.PI)
  30. ctx.setFillStyle('red')
  31. ctx.fill()
  32. // Draw arc
  33. ctx.beginPath()
  34. ctx.arc(100, 75, 50, 0, 1.5 * Math.PI)
  35. ctx.setStrokeStyle('#333333')
  36. ctx.stroke()
  37. ctx.draw()

API Support

API WeChat Mini-Program H5 React Native
CanvasContext.arc ✔️

arcTo

Draws an arc path based on control points and radius.

Reference

  1. (x1: number, y1: number, x2: number, y2: number, radius: number) => void
Property Type Description
x1 number The x-coordinate of the first control point.
y1 number The y-coordinate of the first control point.
x2 number The x-coordinate of the second control point.
y2 number The y-coordinate of the second control point.
radius number The radius of the arc.

API Support

API WeChat Mini-Program H5 React Native
CanvasContext.arcTo ✔️

beginPath

Starts to create a path. You must call fill or stroke to use the path for fill or stroke operations.

  • At the very beginning, it is equivalent to calling beginPath once.
  • If there are multiple settings of setFillStyle, setStrokeStyle and setLineWidth in a path, the last one set prevails.

Reference

  1. () => void

Sample Code

  1. const ctx = Taro.createCanvasContext('myCanvas')
  2. // begin path
  3. ctx.rect(10, 10, 100, 30)
  4. ctx.setFillStyle('yellow')
  5. ctx.fill()
  6. // begin another path
  7. ctx.beginPath()
  8. ctx.rect(10, 40, 100, 30)
  9. // only fill this rect, not in current path
  10. ctx.setFillStyle('blue')
  11. ctx.fillRect(10, 70, 100, 30)
  12. ctx.rect(10, 100, 100, 30)
  13. // it will fill current path
  14. ctx.setFillStyle('red')
  15. ctx.fill()
  16. ctx.draw()

API Support

API WeChat Mini-Program H5 React Native
CanvasContext.beginPath ✔️

bezierCurveTo

Creates a cubic Bézier curve path. The start point of the curve is the previous point in the path.

The three key coordinates for moveTo (20, 20) and bezierCurveTo (20, 100, 200, 100, 200, 20) are as follows:

  • Red: Start point (20, 20)
  • Blue: Two control points (20, 100) (200, 100)
  • Green: End point (200, 20)

Reference

  1. (cp1x: number, cp1y: number, cp2x: number, cp2y: number, x: number, y: number) => void
Property Type Description
cp1x number The x-coordinate of the first Bézier control point.
cp1y number The y-coordinate of the first Bézier control point.
cp2x number The x-coordinate of the second Bézier control point.
cp2y number The y-coordinate of the second Bézier control point.
x number The x-coordinate of the end point.
y number The y-coordinate of the end point.

Sample Code

  1. const ctx = Taro.createCanvasContext('myCanvas')
  2. // Draw points
  3. ctx.beginPath()
  4. ctx.arc(20, 20, 2, 0, 2 * Math.PI)
  5. ctx.setFillStyle('red')
  6. ctx.fill()
  7. ctx.beginPath()
  8. ctx.arc(200, 20, 2, 0, 2 * Math.PI)
  9. ctx.setFillStyle('lightgreen')
  10. ctx.fill()
  11. ctx.beginPath()
  12. ctx.arc(20, 100, 2, 0, 2 * Math.PI)
  13. ctx.arc(200, 100, 2, 0, 2 * Math.PI)
  14. ctx.setFillStyle('blue')
  15. ctx.fill()
  16. ctx.setFillStyle('black')
  17. ctx.setFontSize(12)
  18. // Draw guides
  19. ctx.beginPath()
  20. ctx.moveTo(20, 20)
  21. ctx.lineTo(20, 100)
  22. ctx.lineTo(150, 75)
  23. ctx.moveTo(200, 20)
  24. ctx.lineTo(200, 100)
  25. ctx.lineTo(70, 75)
  26. ctx.setStrokeStyle('#AAAAAA')
  27. ctx.stroke()
  28. // Draw quadratic curve
  29. ctx.beginPath()
  30. ctx.moveTo(20, 20)
  31. ctx.bezierCurveTo(20, 100, 200, 100, 200, 20)
  32. ctx.setStrokeStyle('black')
  33. ctx.stroke()
  34. ctx.draw()

API Support

API WeChat Mini-Program H5 React Native
CanvasContext.bezierCurveTo ✔️

clearRect

Clears the content in the specified rectangular area on the canvas.

Reference

  1. (x: number, y: number, width: number, height: number) => void
Property Type Description
x number The x-coordinate of the top-left corner of the rectangular path.
y number The y-coordinate of the top-left corner of the rectangular path.
width number The width of the rectangular path.
height number The height of the rectangular path.

Sample Code

Instead of drawing a white rectangle over the selected area, the clearRect operation clears the content in this rectangle. The following example adds a layer of background color to the canvas for demonstration purposes.

  1. <canvas canvas-id="myCanvas" style="border: 1px solid; background: #123456;"/>
  1. const ctx = Taro.createCanvasContext('myCanvas')
  2. ctx.setFillStyle('red')
  3. ctx.fillRect(0, 0, 150, 200)
  4. ctx.setFillStyle('blue')
  5. ctx.fillRect(150, 0, 150, 200)
  6. ctx.clearRect(10, 10, 150, 75)
  7. ctx.draw()

API Support

API WeChat Mini-Program H5 React Native
CanvasContext.clearRect ✔️

clip

Cuts an area of any size and shape from the original canvas. Once an area has been cut, all subsequent drawings are limited to the cut area (the other areas of the canvas cannot be accessed). You can use the save method to save the current canvas area before using the clip method. Then, you can use the restore method to restore the canvas at any later time.

Reference

  1. () => void

Sample Code

  1. const ctx = Taro.createCanvasContext('myCanvas')
  2. Taro.downloadFile({
  3. url: 'https://is5.mzstatic.com/image/thumb/Purple128/v4/75/3b/90/753b907c-b7fb-5877-215a-759bd73691a4/source/50x50bb.jpg',
  4. success: function(res) {
  5. ctx.save()
  6. ctx.beginPath()
  7. ctx.arc(50, 50, 25, 0, 2*Math.PI)
  8. ctx.clip()
  9. ctx.drawImage(res.tempFilePath, 25, 25)
  10. ctx.restore()
  11. ctx.draw()
  12. }
  13. })

API Support

API WeChat Mini-Program H5 React Native
CanvasContext.clip ✔️

closePath

Closes a path by connecting the start and end points. If you do not call fill or stroke and start a new path after closing the path, the previous path will not be rendered.

Reference

  1. () => void

Sample Code

Example 1
  1. const ctx = Taro.createCanvasContext('myCanvas')
  2. ctx.moveTo(10, 10)
  3. ctx.lineTo(100, 10)
  4. ctx.lineTo(100, 100)
  5. ctx.closePath()
  6. ctx.stroke()
  7. ctx.draw()
Example 2
  1. const ctx = Taro.createCanvasContext('myCanvas')
  2. // begin path
  3. ctx.rect(10, 10, 100, 30)
  4. ctx.closePath()
  5. // begin another path
  6. ctx.beginPath()
  7. ctx.rect(10, 40, 100, 30)
  8. // only fill this rect, not in current path
  9. ctx.setFillStyle('blue')
  10. ctx.fillRect(10, 70, 100, 30)
  11. ctx.rect(10, 100, 100, 30)
  12. // it will fill current path
  13. ctx.setFillStyle('red')
  14. ctx.fill()
  15. ctx.draw()

API Support

API WeChat Mini-Program H5 React Native
CanvasContext.closePath ✔️

createPattern

Creates a pattern for the specified image. It can repeat the image in the specified direction.

Reference

  1. (image: string, repetition: "repeat" | "repeat-x" | "repeat-y" | "no-repeat") => void
Property Type Description
image string The source of the repeated image. Only in-package paths and temporary paths are supported.
repetition "repeat" | "repeat-x" | "repeat-y" | "no-repeat" Indicates how to repeat the image.

API Support

API WeChat Mini-Program H5 React Native
CanvasContext.createPattern ✔️

draw

Draws the objects (path, transformation, and style) previously described in the drawing context to the canvas.

Reference

  1. (reserve?: boolean, callback?: (...args: any[]) => any) => void
Property Type Description
reserve boolean Indicates whether this drawing follows the previous drawing. If the reserve parameter is false (default), the native layer clears the canvas before drawing the content of this call. If the reserve parameter is true, the content on the current canvas is retained and the content of the drawCanvas call is drawn over it.
callback (…args: any[]) => any The callback function executed after the drawing is completed.

Sample Code

Example 1

If the reserve parameter for the second draw() is true, the content drawn by the previous operation is retained. In the context settings, the fillStyle value ‘red’ changes to the default value ‘black’.

  1. const ctx = Taro.createCanvasContext('myCanvas')
  2. ctx.setFillStyle('red')
  3. ctx.fillRect(10, 10, 150, 100)
  4. ctx.draw()
  5. ctx.fillRect(50, 50, 150, 100)
  6. ctx.draw(true)
Example 2

If the reserve parameter for the second draw() is false, the content drawn by the previous operation is not retained. In the context settings, the fillStyle value is still ‘red’.

  1. const ctx = Taro.createCanvasContext('myCanvas')
  2. ctx.setFillStyle('red')
  3. ctx.fillRect(10, 10, 150, 100)
  4. ctx.draw()
  5. ctx.fillRect(50, 50, 150, 100)
  6. ctx.draw()

API Support

API WeChat Mini-Program H5 React Native
CanvasContext.draw ✔️

drawImage

Draws an image to the canvas.

Reference

  1. { (imageResource: string, dx: number, dy: number): void; (imageResource: string, dx: number, dy: number, dWidth: number, dHeight: number): void; (imageResource: string, sx: number, sy: number, sWidth: number, sHeight: number, dx: number, dy: number, dWidth: number, dHeight: number): void; }
Property Type Description
imageResource string The source of the image to draw.
sx number The x-coordinate of the top-left corner of the source image’s rectangular selection box.
sy number The y-coordinate of the top-left corner of the source image’s rectangular selection box.
sWidth number The width of the source image’s rectangular selection box.
sHeight number The height of the source image’s rectangular selection box.
dx number The x-axis position of the image’s top-left corner on the destination canvas.
dy number The y-axis position of the image’s top-left corner on the destination canvas.
dWidth number The width of the image drawn on the canvas. The drawn image can be zoomed in/out.
dHeight number The height of the image drawn on the canvas. The drawn image can be zoomed in/out.

Sample Code

The code can be written in three ways:

  • drawImage(imageResource, dx, dy)
  • drawImage(imageResource, dx, dy, dWidth, dHeight)
  • drawImage(imageResource, sx, sy, sWidth, sHeight, dx, dy, dWidth, dHeight) (supported as of 1.9.0)
  1. const ctx = Taro.createCanvasContext('myCanvas')
  2. Taro.chooseImage({
  3. success: function(res){
  4. ctx.drawImage(res.tempFilePaths[0], 0, 0, 150, 100)
  5. ctx.draw()
  6. }
  7. })

API Support

API WeChat Mini-Program H5 React Native
CanvasContext.drawImage ✔️

fill

Fills the content in the current path. The default fill color is black.

Reference

  1. () => void

Sample Code

Example 1

If the current path is not closed, the fill() method connects the start and end points and then fills the path.

  1. const ctx = Taro.createCanvasContext('myCanvas')
  2. ctx.moveTo(10, 10)
  3. ctx.lineTo(100, 10)
  4. ctx.lineTo(100, 100)
  5. ctx.fill()
  6. ctx.draw()
Example 2

The path filled via fill() is calculated starting from beginPath(), and fillRect() is not included.

  1. const ctx = Taro.createCanvasContext('myCanvas')
  2. // begin path
  3. ctx.rect(10, 10, 100, 30)
  4. ctx.setFillStyle('yellow')
  5. ctx.fill()
  6. // begin another path
  7. ctx.beginPath()
  8. ctx.rect(10, 40, 100, 30)
  9. // only fill this rect, not in current path
  10. ctx.setFillStyle('blue')
  11. ctx.fillRect(10, 70, 100, 30)
  12. ctx.rect(10, 100, 100, 30)
  13. // it will fill current path
  14. ctx.setFillStyle('red')
  15. ctx.fill()
  16. ctx.draw()

API Support

API WeChat Mini-Program H5 React Native
CanvasContext.fill ✔️

fillRect

Fills a rectangle. Use setFillStyle to set the fill color, which is black by default.

Reference

  1. (x: number, y: number, width: number, height: number) => void
Property Type Description
x number The x-coordinate of the top-left corner of the rectangular path.
y number The y-coordinate of the top-left corner of the rectangular path.
width number The width of the rectangular path.
height number The height of the rectangular path.

Sample Code

  1. const ctx = Taro.createCanvasContext('myCanvas')
  2. ctx.setFillStyle('red')
  3. ctx.fillRect(10, 10, 150, 75)
  4. ctx.draw()

API Support

API WeChat Mini-Program H5 React Native
CanvasContext.fillRect ✔️

fillText

Draws filled text on the canvas.

Reference

  1. (text: string, x: number, y: number, maxWidth?: number) => void
Property Type Description
text string The text output on the canvas.
x number The x-coordinate of the top-left corner of the text to be drawn.
y number The y-coordinate of the top-left corner of the text to be drawn.
maxWidth number The maximum width of the drawing (optional).

Sample Code

  1. const ctx = Taro.createCanvasContext('myCanvas')
  2. ctx.setFontSize(20)
  3. ctx.fillText('Hello', 20, 20)
  4. ctx.fillText('MINA', 100, 100)
  5. ctx.draw()

API Support

API WeChat Mini-Program H5 React Native
CanvasContext.fillText ✔️

lineTo

Adds a new point and then creates a line from the last specified point to the target point. Use the stroke method to draw a line.

Reference

  1. (x: number, y: number) => void
Property Type Description
x number The x-coordinate of the destination location.
y number The y-coordinate of the destination location.

Sample Code

  1. const ctx = Taro.createCanvasContext('myCanvas')
  2. ctx.moveTo(10, 10)
  3. ctx.rect(10, 10, 100, 50)
  4. ctx.lineTo(110, 60)
  5. ctx.stroke()
  6. ctx.draw()

API Support

API WeChat Mini-Program H5 React Native
CanvasContext.lineTo ✔️

moveTo

Moves the path to the specified point on the canvas without creating lines. Use the stroke method to draw lines.

Reference

  1. (x: number, y: number) => void
Property Type Description
x number The x-coordinate of the destination location.
y number The y-coordinate of the destination location.

Sample Code

  1. const ctx = Taro.createCanvasContext('myCanvas')
  2. ctx.moveTo(10, 10)
  3. ctx.lineTo(100, 10)
  4. ctx.moveTo(10, 50)
  5. ctx.lineTo(100, 50)
  6. ctx.stroke()
  7. ctx.draw()

API Support

API WeChat Mini-Program H5 React Native
CanvasContext.moveTo ✔️

quadraticCurveTo

Creates a quadratic Bézier curve path. The start point of the curve is the previous point in the path.

The three key coordinates for moveTo(20, 20) and quadraticCurveTo(20, 100, 200, 20) are as follows:

  • Red: Start point (20, 20)
  • Blue: Control point (20, 100)
  • Green: End point (200, 20)

Reference

  1. (cpx: number, cpy: number, x: number, y: number) => void
Property Type Description
cpx number The x-coordinate of the Bézier control point.
cpy number The y-coordinate of the Bézier control point.
x number The x-coordinate of the end point.
y number The y-coordinate of the end point.

Sample Code

  1. const ctx = Taro.createCanvasContext('myCanvas')
  2. // Draw points
  3. ctx.beginPath()
  4. ctx.arc(20, 20, 2, 0, 2 * Math.PI)
  5. ctx.setFillStyle('red')
  6. ctx.fill()
  7. ctx.beginPath()
  8. ctx.arc(200, 20, 2, 0, 2 * Math.PI)
  9. ctx.setFillStyle('lightgreen')
  10. ctx.fill()
  11. ctx.beginPath()
  12. ctx.arc(20, 100, 2, 0, 2 * Math.PI)
  13. ctx.setFillStyle('blue')
  14. ctx.fill()
  15. ctx.setFillStyle('black')
  16. ctx.setFontSize(12)
  17. // Draw guides
  18. ctx.beginPath()
  19. ctx.moveTo(20, 20)
  20. ctx.lineTo(20, 100)
  21. ctx.lineTo(200, 20)
  22. ctx.setStrokeStyle('#AAAAAA')
  23. ctx.stroke()
  24. // Draw quadratic curve
  25. ctx.beginPath()
  26. ctx.moveTo(20, 20)
  27. ctx.quadraticCurveTo(20, 100, 200, 20)
  28. ctx.setStrokeStyle('black')
  29. ctx.stroke()
  30. ctx.draw()

API Support

API WeChat Mini-Program H5 React Native
CanvasContext.quadraticCurveTo ✔️

rect

Creates a rectangular path. You must use the fill or stroke method to actually draw the rectangle to the canvas.

Reference

  1. (x: number, y: number, width: number, height: number) => void
Property Type Description
x number The x-coordinate of the top-left corner of the rectangular path.
y number The y-coordinate of the top-left corner of the rectangular path.
width number The width of the rectangular path.
height number The height of the rectangular path.

Sample Code

  1. const ctx = Taro.createCanvasContext('myCanvas')
  2. ctx.rect(10, 10, 150, 75)
  3. ctx.setFillStyle('red')
  4. ctx.fill()
  5. ctx.draw()

API Support

API WeChat Mini-Program H5 React Native
CanvasContext.rect ✔️

restore

Restores a drawing context that is previously saved.

Reference

  1. () => void

Sample Code

  1. const ctx = Taro.createCanvasContext('myCanvas')
  2. // save the default fill style
  3. ctx.save()
  4. ctx.setFillStyle('red')
  5. ctx.fillRect(10, 10, 150, 100)
  6. // restore to the previous saved state
  7. ctx.restore()
  8. ctx.fillRect(50, 50, 150, 100)
  9. ctx.draw()

API Support

API WeChat Mini-Program H5 React Native
CanvasContext.restore ✔️

rotate

Rotates the current axis clockwise around the origin point. The rotation angles of multiple calls will be superimposed. The origin point can be modified using the translate method.

Reference

  1. (rotate: number) => void
Property Type Description
rotate number The rotation angle in radians (degrees * Math.PI/180; degrees: 0-360).

Sample Code

  1. const ctx = Taro.createCanvasContext('myCanvas')
  2. ctx.strokeRect(100, 10, 150, 100)
  3. ctx.rotate(20 * Math.PI / 180)
  4. ctx.strokeRect(100, 10, 150, 100)
  5. ctx.rotate(20 * Math.PI / 180)
  6. ctx.strokeRect(100, 10, 150, 100)
  7. ctx.draw()

API Support

API WeChat Mini-Program H5 React Native
CanvasContext.rotate ✔️

save

Saves the drawing context.

Reference

  1. () => void

Sample Code

  1. const ctx = Taro.createCanvasContext('myCanvas')
  2. // save the default fill style
  3. ctx.save()
  4. ctx.setFillStyle('red')
  5. ctx.fillRect(10, 10, 150, 100)
  6. // restore to the previous saved state
  7. ctx.restore()
  8. ctx.fillRect(50, 50, 150, 100)
  9. ctx.draw()

API Support

API WeChat Mini-Program H5 React Native
CanvasContext.save ✔️

scale

Zooms in/out the x- and y- coordinates of the path created after the call. The scales in multiple calls are multiplied.

Reference

  1. (scaleWidth: number, scaleHeight: number) => void
Property Type Description
scaleWidth number The scale for horizontal coordinate zoom (1 = 100%, 0.5 = 50%, 2 = 200%).
scaleHeight number The scale for vertical coordinate zoom (1 = 100%, 0.5 = 50%, 2 = 200%).

Sample Code

  1. const ctx = Taro.createCanvasContext('myCanvas')
  2. ctx.strokeRect(10, 10, 25, 15)
  3. ctx.scale(2, 2)
  4. ctx.strokeRect(10, 10, 25, 15)
  5. ctx.scale(2, 2)
  6. ctx.strokeRect(10, 10, 25, 15)
  7. ctx.draw()

API Support

API WeChat Mini-Program H5 React Native
CanvasContext.scale ✔️

setFillStyle

Sets the fill color.

Reference

  1. (color: string | CanvasGradient) => void
Property Type Description
color string | CanvasGradient The fill color. The default color is black.

Sample Code

  1. const ctx = Taro.createCanvasContext('myCanvas')
  2. ctx.setFillStyle('red')
  3. ctx.fillRect(10, 10, 150, 75)
  4. ctx.draw()

API Support

API WeChat Mini-Program H5 React Native
CanvasContext.setFillStyle ✔️

setFontSize

Sets the font size.

Reference

  1. (fontSize: number) => void
Property Type Description
fontSize number The font size.

Sample Code

  1. const ctx = Taro.createCanvasContext('myCanvas')
  2. ctx.setFontSize(20)
  3. ctx.fillText('20', 20, 20)
  4. ctx.setFontSize(30)
  5. ctx.fillText('30', 40, 40)
  6. ctx.setFontSize(40)
  7. ctx.fillText('40', 60, 60)
  8. ctx.setFontSize(50)
  9. ctx.fillText('50', 90, 90)
  10. ctx.draw()

API Support

API WeChat Mini-Program H5 React Native
CanvasContext.setFontSize ✔️

setGlobalAlpha

Sets the global brush transparency.

Reference

  1. (alpha: number) => void
Property Type Description
alpha number The transparency. The value range is 0-1. 0 indicates completely transparent and 1 indicates completely opaque.

Sample Code

  1. const ctx = Taro.createCanvasContext('myCanvas')
  2. ctx.setFillStyle('red')
  3. ctx.fillRect(10, 10, 150, 100)
  4. ctx.setGlobalAlpha(0.2)
  5. ctx.setFillStyle('blue')
  6. ctx.fillRect(50, 50, 150, 100)
  7. ctx.setFillStyle('yellow')
  8. ctx.fillRect(100, 100, 150, 100)
  9. ctx.draw()

API Support

API WeChat Mini-Program H5 React Native
CanvasContext.setGlobalAlpha ✔️

setLineCap

Sets the endpoint style of the line.

Reference

  1. (lineCap: "butt" | "round" | "square") => void
Property Type Description
lineCap "butt" | "round" | "square" The endpoint style of the line.

Sample Code

  1. const ctx = Taro.createCanvasContext('myCanvas')
  2. ctx.beginPath()
  3. ctx.moveTo(10, 10)
  4. ctx.lineTo(150, 10)
  5. ctx.stroke()
  6. ctx.beginPath()
  7. ctx.setLineCap('butt')
  8. ctx.setLineWidth(10)
  9. ctx.moveTo(10, 30)
  10. ctx.lineTo(150, 30)
  11. ctx.stroke()
  12. ctx.beginPath()
  13. ctx.setLineCap('round')
  14. ctx.setLineWidth(10)
  15. ctx.moveTo(10, 50)
  16. ctx.lineTo(150, 50)
  17. ctx.stroke()
  18. ctx.beginPath()
  19. ctx.setLineCap('square')
  20. ctx.setLineWidth(10)
  21. ctx.moveTo(10, 70)
  22. ctx.lineTo(150, 70)
  23. ctx.stroke()
  24. ctx.draw()

API Support

API WeChat Mini-Program H5 React Native
CanvasContext.setLineCap ✔️

setLineDash

Sets the dashed line style.

Reference

  1. (pattern: number[], offset: number) => void
Property Type Description
pattern number[] A set of numbers describing the length and spacing of the line segments (unit: coordinate space)
offset number The dashed line offset.

Sample Code

  1. const ctx = Taro.createCanvasContext('myCanvas')
  2. ctx.setLineDash([10, 20], 5);
  3. ctx.beginPath();
  4. ctx.moveTo(0,100);
  5. ctx.lineTo(400, 100);
  6. ctx.stroke();
  7. ctx.draw()

API Support

API WeChat Mini-Program H5 React Native
CanvasContext.setLineDash ✔️

setLineJoin

Sets the intersection style of the line.

Reference

  1. (lineJoin: "round" | "bevel" | "miter") => void
Property Type Description
lineJoin "round" | "bevel" | "miter" The intersection style of the line.

Sample Code

  1. const ctx = Taro.createCanvasContext('myCanvas')
  2. ctx.beginPath()
  3. ctx.moveTo(10, 10)
  4. ctx.lineTo(100, 50)
  5. ctx.lineTo(10, 90)
  6. ctx.stroke()
  7. ctx.beginPath()
  8. ctx.setLineJoin('bevel')
  9. ctx.setLineWidth(10)
  10. ctx.moveTo(50, 10)
  11. ctx.lineTo(140, 50)
  12. ctx.lineTo(50, 90)
  13. ctx.stroke()
  14. ctx.beginPath()
  15. ctx.setLineJoin('round')
  16. ctx.setLineWidth(10)
  17. ctx.moveTo(90, 10)
  18. ctx.lineTo(180, 50)
  19. ctx.lineTo(90, 90)
  20. ctx.stroke()
  21. ctx.beginPath()
  22. ctx.setLineJoin('miter')
  23. ctx.setLineWidth(10)
  24. ctx.moveTo(130, 10)
  25. ctx.lineTo(220, 50)
  26. ctx.lineTo(130, 90)
  27. ctx.stroke()
  28. ctx.draw()

API Support

API WeChat Mini-Program H5 React Native
CanvasContext.setLineJoin ✔️

setLineWidth

Sets the width of the line.

Reference

  1. (lineWidth: number) => void
Property Type Description
lineWidth number The witdth of the line in pixels.

Sample Code

  1. const ctx = Taro.createCanvasContext('myCanvas')
  2. ctx.beginPath()
  3. ctx.moveTo(10, 10)
  4. ctx.lineTo(150, 10)
  5. ctx.stroke()
  6. ctx.beginPath()
  7. ctx.setLineWidth(5)
  8. ctx.moveTo(10, 30)
  9. ctx.lineTo(150, 30)
  10. ctx.stroke()
  11. ctx.beginPath()
  12. ctx.setLineWidth(10)
  13. ctx.moveTo(10, 50)
  14. ctx.lineTo(150, 50)
  15. ctx.stroke()
  16. ctx.beginPath()
  17. ctx.setLineWidth(15)
  18. ctx.moveTo(10, 70)
  19. ctx.lineTo(150, 70)
  20. ctx.stroke()
  21. ctx.draw()

API Support

API WeChat Mini-Program H5 React Native
CanvasContext.setLineWidth ✔️

setMiterLimit

Sets the maximum miter length. The miter length is the distance between the inner corner and outer corner at the intersection of two lines. This value is valid when CanvasContext.setLineJoin() is set to miter. When the maximum miter length is exceeded, the lineJoin at the intersection takes the value of bevel.

Reference

  1. (miterLimit: number) => void
Property Type Description
miterLimit number The maximum miter length.

Sample Code

  1. const ctx = Taro.createCanvasContext('myCanvas')
  2. ctx.beginPath()
  3. ctx.setLineWidth(10)
  4. ctx.setLineJoin('miter')
  5. ctx.setMiterLimit(1)
  6. ctx.moveTo(10, 10)
  7. ctx.lineTo(100, 50)
  8. ctx.lineTo(10, 90)
  9. ctx.stroke()
  10. ctx.beginPath()
  11. ctx.setLineWidth(10)
  12. ctx.setLineJoin('miter')
  13. ctx.setMiterLimit(2)
  14. ctx.moveTo(50, 10)
  15. ctx.lineTo(140, 50)
  16. ctx.lineTo(50, 90)
  17. ctx.stroke()
  18. ctx.beginPath()
  19. ctx.setLineWidth(10)
  20. ctx.setLineJoin('miter')
  21. ctx.setMiterLimit(3)
  22. ctx.moveTo(90, 10)
  23. ctx.lineTo(180, 50)
  24. ctx.lineTo(90, 90)
  25. ctx.stroke()
  26. ctx.beginPath()
  27. ctx.setLineWidth(10)
  28. ctx.setLineJoin('miter')
  29. ctx.setMiterLimit(4)
  30. ctx.moveTo(130, 10)
  31. ctx.lineTo(220, 50)
  32. ctx.lineTo(130, 90)
  33. ctx.stroke()
  34. ctx.draw()

API Support

API WeChat Mini-Program H5 React Native
CanvasContext.setMiterLimit ✔️

setShadow

Sets the shadow style.

Reference

  1. (offsetX: number, offsetY: number, blur: number, color: string) => void
Property Type Description
offsetX number The horizontal offset of the shadow relative to the shape. The default value is 0.
offsetY number The vertical offset of the shadow relative to the shape. The default value is 0.
blur number The fuzziness level of the shadow. Higher values indicate greater fuzziness. The value range is 0-100 and the default value is 0.
color string The color of the shadow. The default value is black.

Sample Code

  1. const ctx = Taro.createCanvasContext('myCanvas')
  2. ctx.setFillStyle('red')
  3. ctx.setShadow(10, 50, 50, 'blue')
  4. ctx.fillRect(10, 10, 150, 75)
  5. ctx.draw()

API Support

API WeChat Mini-Program H5 React Native
CanvasContext.setShadow ✔️

setStrokeStyle

Sets the border color.

Reference

  1. (color: string | CanvasGradient) => void
Property Type Description
color string | CanvasGradient The border color. The default color is black.

Sample Code

  1. const ctx = Taro.createCanvasContext('myCanvas')
  2. ctx.setStrokeStyle('red')
  3. ctx.strokeRect(10, 10, 150, 75)
  4. ctx.draw()

API Support

API WeChat Mini-Program H5 React Native
CanvasContext.setStrokeStyle ✔️

setTextAlign

Sets the text alignment.

Reference

  1. (align: "left" | "center" | "right") => void
Property Type Description
align "left" | "center" | "right" The text alignment method.

Sample Code

  1. const ctx = Taro.createCanvasContext('myCanvas')
  2. ctx.setStrokeStyle('red')
  3. ctx.moveTo(150, 20)
  4. ctx.lineTo(150, 170)
  5. ctx.stroke()
  6. ctx.setFontSize(15)
  7. ctx.setTextAlign('left')
  8. ctx.fillText('textAlign=left', 150, 60)
  9. ctx.setTextAlign('center')
  10. ctx.fillText('textAlign=center', 150, 80)
  11. ctx.setTextAlign('right')
  12. ctx.fillText('textAlign=right', 150, 100)
  13. ctx.draw()

API Support

API WeChat Mini-Program H5 React Native
CanvasContext.setTextAlign ✔️

setTextBaseline

Sets the vertical text alignment.

Reference

  1. (textBaseline: "top" | "bottom" | "middle" | "normal") => void
Property Type Description
textBaseline "top" | "bottom" | "middle" | "normal" The vertical text alignment method.

Sample Code

  1. const ctx = Taro.createCanvasContext('myCanvas')
  2. ctx.setStrokeStyle('red')
  3. ctx.moveTo(5, 75)
  4. ctx.lineTo(295, 75)
  5. ctx.stroke()
  6. ctx.setFontSize(20)
  7. ctx.setTextBaseline('top')
  8. ctx.fillText('top', 5, 75)
  9. ctx.setTextBaseline('middle')
  10. ctx.fillText('middle', 50, 75)
  11. ctx.setTextBaseline('bottom')
  12. ctx.fillText('bottom', 120, 75)
  13. ctx.setTextBaseline('normal')
  14. ctx.fillText('normal', 200, 75)
  15. ctx.draw()

API Support

API WeChat Mini-Program H5 React Native
CanvasContext.setTextBaseline ✔️

setTransform

Resets (overrides) the current transformation with matrix.

Reference

  1. (scaleX: number, scaleY: number, skewX: number, skewY: number, translateX: number, translateY: number) => void
Property Type Description
scaleX number Horizontal zoom
scaleY number Vertical zoom
skewX number Horizontal skew
skewY number Vertical skew
translateX number Horizontal translation
translateY number Vertical translation

API Support

API WeChat Mini-Program H5 React Native
CanvasContext.setTransform ✔️

stroke

Draws the borders of the current path. The default color is black.

Reference

  1. () => void

Sample Code

Example 1
  1. const ctx = Taro.createCanvasContext('myCanvas')
  2. ctx.moveTo(10, 10)
  3. ctx.lineTo(100, 10)
  4. ctx.lineTo(100, 100)
  5. ctx.stroke()
  6. ctx.draw()
Example 2

The path described via stroke() is calculated starting from beginPath(), and strokeRect() is not included.

  1. const ctx = Taro.createCanvasContext('myCanvas')
  2. // begin path
  3. ctx.rect(10, 10, 100, 30)
  4. ctx.setStrokeStyle('yellow')
  5. ctx.stroke()
  6. // begin another path
  7. ctx.beginPath()
  8. ctx.rect(10, 40, 100, 30)
  9. // only stoke this rect, not in current path
  10. ctx.setStrokeStyle('blue')
  11. ctx.strokeRect(10, 70, 100, 30)
  12. ctx.rect(10, 100, 100, 30)
  13. // it will stroke current path
  14. ctx.setStrokeStyle('red')
  15. ctx.stroke()
  16. ctx.draw()

API Support

API WeChat Mini-Program H5 React Native
CanvasContext.stroke ✔️

strokeRect

Draws (but not fills) a rectangle. Use setStrokeStyle to set the line color for the rectangle. The default color is black.

Reference

  1. (x: number, y: number, width: number, height: number) => void
Property Type Description
x number The x-coordinate of the top-left corner of the rectangular path.
y number The y-coordinate of the top-left corner of the rectangular path.
width number The width of the rectangular path.
height number The height of the rectangular path.

Sample Code

  1. const ctx = Taro.createCanvasContext('myCanvas')
  2. ctx.setStrokeStyle('red')
  3. ctx.strokeRect(10, 10, 150, 75)
  4. ctx.draw()

API Support

API WeChat Mini-Program H5 React Native
CanvasContext.strokeRect ✔️

strokeText

Draws text strokes at a given position (x, y).

Reference

  1. (text: string, x: number, y: number, maxWidth?: number) => void
Property Type Description
text string The text to be drawn.
x number The x-coordinate of the text start point.
y number The y-coordinate of the text start point.
maxWidth number The maximum width of the drawing (optional).

API Support

API WeChat Mini-Program H5 React Native
CanvasContext.strokeText ✔️

transform

Multiplies the current transformation with matrix.

Reference

  1. (scaleX: number, scaleY: number, skewX: number, skewY: number, translateX: number, translateY: number) => void
Property Type Description
scaleX number Horizontal zoom
scaleY number Vertical zoom
skewX number Horizontal skew
skewY number Vertical skew
translateX number Horizontal translation
translateY number Vertical translation

API Support

API WeChat Mini-Program H5 React Native
CanvasContext.transform ✔️

translate

Translates the origin point (0, 0) of the current coordinate system. By default, the origin point of the coordinate system is the top-left corner of the page.

Reference

  1. (x: number, y: number) => void
Property Type Description
x number The horizontal coordinate offset.
y number The vertical coordinate offset.

Sample Code

  1. const ctx = Taro.createCanvasContext('myCanvas')
  2. ctx.strokeRect(10, 10, 150, 100)
  3. ctx.translate(20, 20)
  4. ctx.strokeRect(10, 10, 150, 100)
  5. ctx.translate(20, 20)
  6. ctx.strokeRect(10, 10, 150, 100)
  7. ctx.draw()

API Support

API WeChat Mini-Program H5 React Native
CanvasContext.translate ✔️

measureText

Measures the text size. This synchronous API only returns the text width.

Reference

  1. (text: string) => TextMetrics
Property Type Description
text string The text to be measured.

API Support

API WeChat Mini-Program H5 React Native
CanvasContext.measureText ✔️

createCircularGradient

Creates a circular color gradient. The start point is at the center of the circle and the end point is in the outer ring. The returned CanvasGradient object must use CanvasGradient.addColorStop() to specify at least two gradient points.

Reference

  1. (x: number, y: number, r: number) => CanvasGradient
Property Type Description
x number The x-coordinate of the center point.
y number The y-coordinate of the center point.
r number The radius of the circle.

Sample Code

  1. const ctx = Taro.createCanvasContext('myCanvas')
  2. // Create circular gradient
  3. const grd = ctx.createCircularGradient(75, 50, 50)
  4. grd.addColorStop(0, 'red')
  5. grd.addColorStop(1, 'white')
  6. // Fill with gradient
  7. ctx.setFillStyle(grd)
  8. ctx.fillRect(10, 10, 150, 80)
  9. ctx.draw()

API Support

API WeChat Mini-Program H5 React Native
CanvasContext.createCircularGradient ✔️

createLinearGradient

Creates a linear color gradient. The returned CanvasGradient object must use CanvasGradient.addColorStop() to specify at least two gradient points.

Reference

  1. (x0: number, y0: number, x1: number, y1: number) => CanvasGradient
Property Type Description
x0 number The x-coordinate of the start point.
y0 number The y-coordinate of the start point.
x1 number The x-coordinate of the end point.
y1 number The y-coordinate of the end point.

Sample Code

  1. const ctx = Taro.createCanvasContext('myCanvas')
  2. // Create linear gradient
  3. const grd = ctx.createLinearGradient(0, 0, 200, 0)
  4. grd.addColorStop(0, 'red')
  5. grd.addColorStop(1, 'white')
  6. // Fill with gradient
  7. ctx.setFillStyle(grd)
  8. ctx.fillRect(10, 10, 150, 80)
  9. ctx.draw()

API Support

API WeChat Mini-Program H5 React Native
CanvasContext.createLinearGradient ✔️

Parameters

repetition

Indicates how to repeat the image.

Property Description
repeat Repeat horizontally and vertically
repeat-x Repeat horizontally
repeat-y Repeat vertically
no-repeat Do not repeat

lineCap

参数 lineCap 可选值

Property Description
butt Adds a straight edge to each end of the line
round Adds a round line cap to each end of the line
square Adds a square line cap to each end of the line

lineJoin

Valid values of lineJoin

Property Description
bevel Bevel
round Round
miter Sharp

align

Valid values of align

Property Description
left Left aligned
center Center aligned
right Right aligned

textBaseline

Valid values of textBaseline

Property Description
top Top aligned
bottom Bottom aligned
middle Center aligned
normal

API Support

API WeChat Mini-Program H5 React Native
CanvasContext.arc ✔️
CanvasContext.arcTo ✔️
CanvasContext.beginPath ✔️
CanvasContext.bezierCurveTo ✔️
CanvasContext.clearRect ✔️
CanvasContext.clip ✔️
CanvasContext.closePath ✔️
CanvasContext.createPattern ✔️
CanvasContext.draw ✔️
CanvasContext.drawImage ✔️
CanvasContext.fill ✔️
CanvasContext.fillRect ✔️
CanvasContext.fillText ✔️
CanvasContext.lineTo ✔️
CanvasContext.moveTo ✔️
CanvasContext.quadraticCurveTo ✔️
CanvasContext.rect ✔️
CanvasContext.restore ✔️
CanvasContext.rotate ✔️
CanvasContext.save ✔️
CanvasContext.scale ✔️
CanvasContext.setFillStyle ✔️
CanvasContext.setFontSize ✔️
CanvasContext.setGlobalAlpha ✔️
CanvasContext.setLineCap ✔️
CanvasContext.setLineDash ✔️
CanvasContext.setLineJoin ✔️
CanvasContext.setLineWidth ✔️
CanvasContext.setMiterLimit ✔️
CanvasContext.setShadow ✔️
CanvasContext.setStrokeStyle ✔️
CanvasContext.setTextAlign ✔️
CanvasContext.setTextBaseline ✔️
CanvasContext.setTransform ✔️
CanvasContext.stroke ✔️
CanvasContext.strokeRect ✔️
CanvasContext.strokeText ✔️
CanvasContext.transform ✔️
CanvasContext.translate ✔️
CanvasContext.measureText ✔️
CanvasContext.createCircularGradient ✔️
CanvasContext.createLinearGradient ✔️