Home >WeChat Applet >Mini Program Development >Use the Canvas API in the WeChat applet to synthesize the poster generation component package
After each mini program is formed, it is usually chosen to generate a poster with a chrysanthemum code and share it to attract more traffic. Let’s introduce another implementation method
Principle: Mainly use the powerful Canvas API of WeChat applet to synthesize. After generation, you can use wx.canvasToTempFilePath() to export the image address, so that you can preview and save it to Mobile photo album
to open the project folder
1、 git clone https://github.com/WGinit/mini-poster.git
2. Register the component in the Json file of the page to be used
{ “usingComponents“: { “share-image“: “/components/share_image/share_image“ } }
3. Use this component in the page
<share–image drawDataList=“{{dataList}}“> </share–image>
dataList: { canvasData:{ type: 'image', url: '', top: 0, left: 0, width: 750, height: 1334, comment: '背景图', btnText: '保存至相册' }, content: [{ type: 'image', url: '', top: 136, left: 100, shape: 'square', width: 290, height: 186, comment: '头像' }, { type: 'text', content: '白山羊', top: 336, left: 100, fontSize: 40, lineHeight: 40, color: '#f00', textAlign: 'left', weight: 'bold', maxWidth: 287 }] }
canvasData------------canvas related parameters Configuration
Parameter | Type | Default value | Required | Description |
---|---|---|---|---|
String | image | is the | file type, here is the background image , the default image | |
String | '' | is | network image address | |
Number | 0 | No | The position of the upper left corner of the image on the Y-axis in the visual area , unit px | |
Number | 0 | No | The upper left corner of the image is visible The position of the X-axis on the area, unit px | |
Number | 750 | No | canvas The width of the canvas, in px | |
Number | 1334 | No | The height of the canvas, in units px | |
String | 'Background image' | No | Picture description | |
String | 'Save to album' | Yes | Generate button text |
Type | Default value | Required | Description | |
---|---|---|---|---|
String | '' | It is the type of | drawing, optional image and text | |
String | 'square' | No | Draw the shape of the picture, square, circle | |
String | '' | - | The network address of the image, type is required for image | |
String | '' | - | Text content, type is text required | |
Number | 0 | No | The position of the upper left corner of the image on the Y-axis on the target canvas, in px | |
NUmber | 0 | No | The position of the upper left corner of the image on the X-axis on the target canvas, unit px | |
Number | 100 | No | The width of the drawn picture, unit px | ##height |
100 | No | The height of the drawn picture, unit px | ##comment | |
' ' | No | Instructions for drawing pictures | fontSize | |
32 | No | Text font size, unit px | lineHeight | |
32 | No | Text line height, unit px | color | |
'#FFFFFF ' | No | Text font color | textAlign | |
'center' | No | Text level Alignment, optional left, center, right | weight | |
'normal' | No | Text font weight | maxWidth | |
600 | No | Maximum width of text limit , unit px | IV. Remarks |
The above is the detailed content of Use the Canvas API in the WeChat applet to synthesize the poster generation component package. For more information, please follow other related articles on the PHP Chinese website!