May 18, 2021 WeChat Mini Program Development Document
All drawings in canvas must be done with JavaScript:
WXML: (We'll use this WXML as a template in the next example if there's no special declaration, don't repeat it)
<canvas canvas-id="myCanvas" style="border: 1px solid;"/>
JS: (We'll put JS in onLoad in the next example)
const ctx = wx.createCanvasContext('myCanvas')
ctx.setFillStyle('red')
ctx.fillRect(10, 10, 150, 75)
ctx.draw()
Step 1: Create a Canvas drawing context
First, we need to create a Canvas drawing context for CanvasContext.
CanvasContext is an object built into a small program and has some drawing methods:
const ctx = wx.createCanvasContext('myCanvas')
Step 2: Use the Canvas drawing context to describe the drawing
Next, let's describe what to draw in Canvas.
Set the fill color of the drawing context to red:
ctx.setFillStyle('red')
Draw a rectangle with the fillRect (x, y, width, height) method, filled with the red just set:
ctx.fillRect(10, 10, 150, 75)
Step 3: Draw
Tell the canvas component that you want to draw the description you just made:
ctx.draw()
Results:
canvas is in a two-dimensional grid. The coordinates in the upper left corner are (0, 0).
In the last section, we used this method fillRect (0, 0, 150, 75).
It means to draw a 150 x 75px rectangle starting at the upper left corner (0, 0).
Code example
We can add some events to canvas to observe its coordinate system
<canvas canvas-id="myCanvas"
style="margin: 5px; border:1px solid #d3d3d3;"
bindtouchstart="start"
bindtouchmove="move"
bindtouchend="end"/>
<view hidden="{{hidden}}">
Coordinates: ({{x}}, {{y}})
</view>
Page({
data: {
x: 0,
y: 0,
hidden: true
},
start (e) {
this.setData({
hidden: false,
x: e.touches[0].x,
y: e.touches[0].y
})
},
move (e) {
this.setData({
x: e.touches[0].x,
y: e.touches[0].y
})
},
end (e) {
this.setData({
hidden: true
})
}
})
When you put your finger in canvas, the coordinates of the touch point are displayed between the lower parts:
Gradients can be used to fill a rectangle, circle, line, text, etc. Fill colors can be not fixed to a fixed color.
We offer two ways to color gradients:
Once we have created a gradient object, we must add two color gradient points.
The addColorStop (position, color) method specifies the position and color of the color gradient point, which must be between 0 and 1.
You can set the gradient using the setFillStyle and setStyle methods, and then paint a picture description.
Use createLinearGradient()
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()
Use createCircularGradient()
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()