1.0.16 • Published 3 years ago

super-poster v1.0.16

Weekly downloads
-
License
ISC
Repository
-
Last release
3 years ago

微信小程序海报帮助工具(原生、uniapp、taro)

安装

$ npm i super-poster

起步

首先需要再template中新增一个canvas组件

<canvas canvas-id="posterCanvas" id="posterCanvas" type="2d" ></canvas>

绘制 canvas 海报

import { getPoster } from 'super-poster';
let json = {
    width: 750, //画布宽度
    height: 1125, //画布高度
    displayWidth: 690, //展示宽度
    ratio: 1.5, // 画笔缩放比
    posterFileName: 'poster1',
    doms: [
        {
            type: 'text', //dom类型
            x: 0, //x坐标
            y: 56, //y坐标
            width: 750, //文本宽度
            value: '宇宙最强最强的小分队', //文本内容
            fontSize: 48, //字体大小
            color: '#212224', //文本颜色
            fontWeight: 'bold', //字重
            lineNum: 1, //行数
            textAlign: 'center', //左右对其方式
            borderColor: '#000', //文字描边颜色
            borderWidth: 0, //文字描边宽度
            rotate: 0, //旋转角度
            zIndex: 10,
        },
    ],
};
getPoster(json).then((res) => {
    console.log(res.img);
});

获取 renderdoms

import { getPreview } from 'super-poster';
let json = {
    width: 750, //画布宽度
    height: 1125, //画布高度
    displayWidth: 690, //展示宽度
    ratio: 1.5, // 画笔缩放比
    posterFileName: 'poster1',
    doms: [
        {
            type: 'text', //dom类型
            x: 0, //x坐标
            y: 56, //y坐标
            width: 750, //文本宽度
            value: '宇宙最强最强的小分队', //文本内容
            fontSize: 48, //字体大小
            color: '#212224', //文本颜色
            fontWeight: 'bold', //字重
            lineNum: 1, //行数
            textAlign: 'center', //左右对其方式
            borderColor: '#000', //文字描边颜色
            borderWidth: 0, //文字描边宽度
            rotate: 0, //旋转角度
            zIndex: 10,
        },
    ],
};
const domList = getPreview(json);
console.log(domList); //可遍历渲染domlist以dom方式展示海报

配置

config

参数名类型参数描述默认值是否必传
widthnumber画布宽度-
heightnumber画布高度-
displayWidthnumberdom 展示的宽度-
rationumber画布缩放比1
posterFileNamestring海报缓存的文件名Date.now()
domsArray海报元素列表Doms-

doms

dom 一共有:text、texts、image、block 这几种类型

text

文本

参数名类型参数描述默认值是否必传
typestringdom 类型固定值 text
xnumber起点位置 x 坐标0
ynumber起点位置 y 坐标0
valuestring文本内容-
widthnumber文本区宽度config.width
fontSizenumber文字大小-
colorstring文字颜色-
fontWeightstring | number字重normal
lineNumnumber行数1
textAlignstring对齐方式left
borderColorstring字体边框颜色-
borderWidthnumber字体边框宽度-
rotatenumber旋转角度0
zIndexnumber元素层级0

例:

{
    type: 'text', //dom类型
    x: 0, //x坐标
    y: 56, //y坐标
    width: 750, //文本宽度
    value: '宇宙最强最强的小分队', //文本内容
    fontSize: 48, //字体大小
    color: '#212224', //文本颜色
    fontWeight: 'bold', //字重
    lineNum: 1, //行数
    textAlign: 'center', //左右对其方式
    borderColor: '#000', //文字描边颜色
    borderWidth: 0, //文字描边宽度
    rotate: 0, //旋转角度
    zIndex: 10,
}

texts

多规格文本

参数名类型参数描述默认值是否必传
typestringdom 类型固定值 texts
xnumber起点位置 x 坐标0
ynumber起点位置 y 坐标0
widthnumber文本区宽度config.width
textAlignstring对齐方式left
textsTextsDom文本列表-

TextsDom

参数名类型参数描述默认值是否必传
valuestring文本内容-
fontSizenumber字体大小-
colorstring颜色#000
fontWeightnumber | string字重normal

例:

{
    type: 'texts',
    x: 352, //x坐标
    y: 1025, //y坐标
    width: 317,
    textAlign: 'center',
    texts: [
        {
            value: '-85.5', //文本内容
            fontSize: 48, //字体大小
            color: '#000', //文本颜色
            fontWeight: 'bold', //字重
        },
        {
            value: '斤', //文本内容
            fontSize: 36, //字体大小
            color: '#000', //文本颜色
            fontWeight: 'bold', //字重
        },
    ],
}

image

图片

参数名类型参数描述默认值是否必传
typestringdom 类型固定值 image
urlstring图片路径-
xnumber起点位置 x 坐标0
ynumber起点位置 y 坐标0
widthnumber图片宽度config.width
heightnumber图片高度config.height
borderRadiusnumber图片圆角半径0
borderColorstring图片边框颜色#000
borderWidthnumber图片边框宽度0
rotatenumber旋转角度0
clipnumber图片裁剪的顶点坐标集合 例如[0,0,0,100,100,200,200,0]-
zIndexnumber元素层级0

例:

{
    type: 'image',
    url: 'https://hellosanbao-1257196807.cos.ap-chengdu.myqcloud.com/wangyiyun/WechatIMG12.jpeg',
    x: 0,
    y: 0,
    width: 750,
    height: 1125,
    borderWidth: 6,
    borderColor: '#000',
    clip: [[0,0],[0,100],[200,150],[200,0]],
    zIndex: 2,
},

block

dom 集合块

参数名类型参数描述默认值是否必传
typestringdom 类型固定值 block
xnumber起点位置 x 坐标0
ynumber起点位置 y 坐标0
widthnumber宽度-
heightnumber高度-
rotatenumber旋转角度0
rotateOriginnumberx,y旋转中心店-
translatenumberx,y偏移量-
domsDomsdom 列表-

例:

{
    type: 'block',
    x: 0,
    y: 0,
    width: 750,
    height: 1125,
    rotate:10,
    rotateOrigin: [0,0],
    translate: [0, 0],
    doms: [
       {
            type: 'image',
            url: 'https://hellosanbao-1257196807.cos.ap-chengdu.myqcloud.com/wangyiyun/WechatIMG12.jpeg',
            x: 0,
            y: 0,
            width: 750,
            height: 1125,
            borderWidth: 6,
            borderColor: '#000',
            clip: [[0,0],[0,100],[200,150],[200,0]],
            zIndex: 2,
        },
        {
            type: 'text', //dom类型
            x: 0, //x坐标
            y: 56, //y坐标
            width: 750, //文本宽度
            value: '宇宙最强最强的小分队', //文本内容
            fontSize: 48, //字体大小
            color: '#212224', //文本颜色
            fontWeight: 'bold', //字重
            lineNum: 1, //行数
            textAlign: 'center', //左右对其方式
            borderColor: '#000', //文字描边颜色
            borderWidth: 0, //文字描边宽度
            rotate: 0, //旋转角度
            zIndex: 10,
        }
    ],
}
1.0.16

3 years ago

1.0.15

3 years ago

1.0.14

3 years ago

1.0.13

3 years ago

1.0.12

3 years ago

1.0.11

3 years ago

1.0.10

3 years ago

1.0.9

3 years ago

1.0.8

3 years ago

1.0.7

3 years ago

1.0.6

3 years ago

1.0.5

3 years ago

1.0.4

3 years ago

1.0.3

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago