The Canvas provides a drawing interface on which you can draw anything you want. 1. Basic use
Step 1:Create a Canvas drawing context
<canvas id="myCanvas" type="2d" style="border: 1px solid; width: 300px; height: 150px;" />
First you need to add the Canvas to the WXML. Specify id="myCanvas"
to uniquely identify a canvas, which will be used to get the Canvas object later.
Specify type
to define the type of the canvas, for this example, type="2d"
is used.
Step 2: Use the Canvas drawing context to describe the drawing
wx.createSelectorQuery()
.select('#myCanvas')
.fields({ node: true, size: true })
.exec((res) => {
const canvas = res[0].node
const ctx = canvas.getContext('2d')
})
Subsequent canvas operations and rendering operations need to be implemented through these two objects.
Step 3: Initialise the Canvas
wx.createSelectorQuery()
.select('#myCanvas')
.fields({ node: true, size: true })
.exec((res) => {
const canvas = res[0].node
const ctx = canvas.getContext('2d')
const width = res[0].width
const height = res[0].height
const dpr = wx.getWindowInfo().pixelRatio
canvas.width = width * dpr
canvas.height = height * dpr
ctx.scale(dpr, dpr)
})
The canvas width is divided into a rendered width and a logical width:
Rendered aspect is the actual width and height of the canvas canvas on the page, i.e. the size obtained by boundingClientRect requests to the nodes. The logical width is the size of the logical width of the canvas during the rendering process, e.g. if you draw a rectangle with the same logical width, the rectangle will eventually fill the entire canvas. The logical width defaults to 300 * 150.
On different devices, there are cases where the physical pixels and logical pixels are not equal, so generally we need to use wx.getWindowInfo to get the pixel ratio of the device, multiply it by the rendered size of the canvas, and use it as the logical size of the canvas. Step 4: Drawing
ctx.clearRect(0, 0, width, height)
ctx.fillStyle = 'rgb(200, 0, 0)';
ctx.fillRect(10, 10, 50, 50);
ctx.fillStyle = 'rgba(0, 0, 200, 0.5)';
ctx.fillRect(30, 30, 50, 50);
The drawing api on the render context allows us to do arbitrary drawing on the canvas. 2. Advanced use
2.1 Drawing pictures
const image = canvas.createImage()
image.onload = () => {
ctx.drawImage(image, 0, 0)
}
image.src = 'https://open.weixin.qq.com/zh_CN/htmledition/res/assets/res-design-download/icon64_wx_logo.png'
With Canvas.createImage we can create an image object and load the image. Once the image is loaded and the onload callback is triggered, use ctx.drawImage to draw the image to the canvas. 2.2 Generate Image
ctx.fillStyle = 'rgb(200, 0, 0)';
ctx.fillRect(10, 10, 50, 50);
ctx.fillStyle = 'rgba(0, 0, 200, 0.5)';
ctx.fillRect(30, 30, 50, 50);
wx.canvasToTempFilePath({
canvas,
success: res => {
const tempFilePath = res.tempFilePath
},
})
2.3 Frame animation
const startTime = Date.now()
const draw = () => {
const time = Date.now()
const elapsed = time - startTime
const n = Math.floor(elapsed / 3000)
const m = elapsed % 3000
const dx = (n % 2 ? 0 : 1) + (n % 2 ? 1 : -1) * (m < 2500 ? easeOutBounce(m / 2500) : 1)
const x = (width - 50) * dx
ctx.clearRect(0, 0, width, height)
ctx.fillStyle = 'rgb(200, 0, 0)';
ctx.fillRect(x, height / 2 - 25, 50, 50);
canvas.requestAnimationFrame(draw)
}
draw()
2.4 Custom Fonts
Was this page helpful?