摘要: 简单的开源工具。html
Fundebug经受权转载,版权归原做者全部。git
在项目里写过几个canvas生成分享海报页面后,以为这是个重复且冗余的工做.因而就想有没有能经过相似json直接生成海报的库.github
而后就在github找到到两个项目:web
而后就想着能不能本身再造个轮子.因而就有了这个项目 json2canvas,你能够简单的理解为是mp_canvas_drawer的加强版吧.npm
项目的canvas绘制是基于cax实现的.因此自然的带来一个好处,json2canvas同时支持小程序和webjson
git clone https://github.com/willnewii/json2canvas.git
微信开发者工具导入项目 example/weapp/
复制代码
npm i json2canvas
微信开发者工具->工具->构建npm
复制代码
在须要使用的界面引入Componentcanvas
{
"usingComponents": {
"json2canvas":"/miniprogram_npm/json2canvas/index"
}
}
复制代码
想要生成一个这样的海报,须要怎么作?(红框是图片元素,蓝框是文字元素,其他的是一张背景图。)小程序
一个json就搞定.具体支持的元素和参数,请查看项目readmesegmentfault
{
"width": 750,
"height": 1334,
"scale": 0.5,
"children": [
{
"type": "image",
"url": "http://res.mayday5.me/wxapp/wxavatar/tmp/bg_concerts_1.jpg",
"width": 750,
"height": 1334
}, {
"type": "image",
"url": "http://res.mayday5.me/wxapp/wxavatar/tmp/wxapp_code.jpg",
"width": 100,
"x": 48,
"y": 44,
"isCircular": true,
}, {
"type": "circle",
"r": 50,
"lineWidth": 5,
"strokeStyle": "#CCCCCC",
"x": 48,
"y": 44,
}, {
"type": "text",
"text": "歌词本",
"font": "30px Arial",
"color": "#FFFFFF",
"x": 168,
"y": 75,
"shadow": {
"color": "#000",
"offsetX": 2,
"offsetY": 2,
"blur": 2
}
}, {
"type": "image",
"url": "http://res.mayday5.me/wxapp/wxavatar/tmp/medal_concerts_1.png",
"width": 300,
"x": "center",
"y": 361
}, {
"type": "text",
"text": "一辈子活一场 五月天",
"font": "38px Arial",
"color": "#FFFFFF",
"x": "center",
"y": 838,
"shadow": {
"color": "#000",
"offsetX": 2,
"offsetY": 2,
"blur": 2
}
}, {
"type": "text",
"text": "北京6场,郑州2场,登船,上班,听到你想听的歌了吗?",
"font": "24px Arial",
"color": "#FFFFFF",
"x": "center",
"y": 888,
"shadow": {
"color": "#000",
"offsetX": 2,
"offsetY": 2,
"blur": 2
}
}, {
"type": "rect",
"width": 750,
"height": 193,
"fillStyle": "#FFFFFF",
"x": 0,
"y": "bottom"
}, {
"type": "image",
"url": "http://res.mayday5.me/wxapp/wxavatar/tmp/wxapp_code.jpg",
"width": 117,
"height": 117,
"x": 47,
"y": 1180
}, {
"type": "text",
"text": "长按识别小程序二维码",
"font": "26px Arial",
"color": "#858687",
"x": 192,
"y": 1202
}, {
"type": "text",
"text": "加入五月天 永远不会太迟",
"font": "18px Arial",
"color": "#A4A5A6",
"x": 192,
"y": 1249
}]
}
复制代码
有什么问题能够直接提issuebash