May 19, 2021 WeChat Mini Program Development Document
Drawing interfaces and methods
Gradients can be used to fill a rectangle, circle, line, text, etc. A fill color can be a color that is not fixed by a fixed bit.
We offer two ways to color gradients:
createLinearGradient(x, y, x1, y1)
- Create a linear gradient
createCircularGradient(x, y, r)
- Create a gradient that starts at the center of the circle
Once we have created a gradient object, we must add two color gradient points.
addColorStop(position, color)
position and color of the color gradient point, which must be between 0 and 1.
You
setFillStyle()
and
setStrokeStyle()
methods, and then paint a picture description.
const ctx = wx.createCanvasContext('myCanvas')
// Create linear gradient
const grd = ctx.createLinearGradient(0, 0, 200, 0)
grd.addColorStop(0, 'red')
grd.addColorStop(1, 'white')
// Fill with gradient
ctx.setFillStyle(grd)
ctx.fillRect(10, 10, 150, 80)
ctx.draw()
const ctx = wx.createCanvasContext('myCanvas')
// Create circular gradient
const grd = ctx.createCircularGradient(75, 50, 50)
grd.addColorStop(0, 'red')
grd.addColorStop(1, 'white')
// Fill with gradient
ctx.setFillStyle(grd)
ctx.fillRect(10, 10, 150, 80)
ctx.draw()