We often encounter the need to generate pictures in small programs. The pictures are usually added with the user's avatar and nickname. For the avatar, you only need to add the Tencent domain name to the request and download lists, and use wx.getImageInfo()
to cache it. To the local, the successful callback is added to the canvas to generate the image.
If you want the text to be centered at a certain position, how can the Chinese text in canvas be anchored at the center of the text according to the different lengths of the text?
var txtWidth=canvas.measureText(this.nickName).width
can get the width of the text in the canvas. Find the center x position of the text and subtract txtWidth/2 to achieve centering. If the parameter is a number type, convert it to a string type. Otherwise, it will be invalid on some iOS models and the text width cannot be obtained.
The above is the small program that the editor introduces to you to set the center anchor point of Chinese text in canvas. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time!