Repository: baidu/mix-img
Branch: master
Commit: 176e32900a30
Files: 52
Total size: 71.8 KB
Directory structure:
gitextract_3wr5p3hn/
├── .editorconfig
├── .eslintrc.js
├── .gitignore
├── .stylelintrc
├── CHANGELOG.md
├── LICENSE
├── README.md
├── babel.config.js
├── ci.yml
├── docs/
│ ├── dev.md
│ ├── mixConfig.md
│ ├── mixImg.md
│ └── tool.md
├── jest.config.js
├── package.json
├── scripts/
│ ├── build.sh
│ ├── rollup.config.js
│ ├── rollup.e2e.config.js
│ ├── rollup.umd.config.js
│ └── tool.sh
├── src/
│ ├── config/
│ │ └── errorMap.js
│ ├── index.js
│ └── utils/
│ ├── canvasUtils.js
│ ├── createImg.js
│ ├── qrcode.js
│ └── tools.js
├── test/
│ ├── config/
│ │ ├── allConfig.js
│ │ └── index.js
│ ├── e2e/
│ │ ├── index.js
│ │ └── test.html
│ └── unit/
│ ├── __snapshots__/
│ │ └── canvasUtils.test.js.snap
│ ├── canvasUtils.test.js
│ ├── index.test.js
│ └── tools.test.js
└── toolkit/
├── .editorconfig
├── .eslintrc.js
├── .gitignore
├── .stylelintrc
├── babel.config.js
├── package.json
├── public/
│ └── index.html
├── src/
│ ├── App.vue
│ ├── components/
│ │ └── layout/
│ │ └── default/
│ │ ├── Layout.vue
│ │ ├── components/
│ │ │ └── header/
│ │ │ └── appHeader.vue
│ │ ├── index.js
│ │ └── layout.less
│ ├── main.js
│ ├── router/
│ │ ├── index.js
│ │ └── routers.js
│ └── template/
│ └── dynamicFe/
│ ├── index.js
│ └── index.vue
└── vue.config.js
================================================
FILE CONTENTS
================================================
================================================
FILE: .editorconfig
================================================
root = true
[*]
charset = utf-8
indent_style = space
indent_size = 4
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true
================================================
FILE: .eslintrc.js
================================================
module.exports = {
parser: 'babel-eslint',
extends: [
'@ecomfe/eslint-config'
],
parserOptions: {
babelOptions: {
configFile: './babel.config.js'
}
},
rules: {
'comma-dangle': ['error', {
objects: 'never'
}]
},
overrides: [
{
files: [
'**/*.test.js'
],
env: {
jest: true
}
}
]
};
================================================
FILE: .gitignore
================================================
# Referenced from https://github.com/github/gitignore/blob/master/Node.gitignore
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
# Runtime data
pids
*.pid
*.seed
*.pid.lock
# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov
# Coverage directory used by tools like istanbul
coverage
# nyc test coverage
.nyc_output
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt
# Bower dependency directory (https://bower.io/)
bower_components
# node-waf configuration
.lock-wscript
# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release
# Dependency directories
node_modules/
jspm_packages/
# Typescript v1 declaration files
typings/
# Optional npm cache directory
.npm
# Optional eslint cache
.eslintcache
# Optional REPL history
.node_repl_history
# Output of 'npm pack'
*.tgz
# Yarn Integrity file
.yarn-integrity
# dotenv environment variables file
.env
# next.js build output
.next
# other stuff
.DS_Store
Thumbs.db
# IDE configurations
.idea
.vscode
# build assets
output
dist
dll
mock
================================================
FILE: .stylelintrc
================================================
{
"extends": "@ecomfe/stylelint-config/baidu/default"
}
================================================
FILE: CHANGELOG.md
================================================
# 更新日志
### 1.0.6
[变更] 背景图backgroundImg可不传。若fileType为jpeg则背景为黑色,为png则背景透明
[修改] 绘制二维码时,需传入有效的宽高
### 1.0.5
[修复] 绘制多组文字样式不正确问题
### 1.0.4
[修改] 更新文档
### 1.0.3
[修改] 修改参数名称以表达正确意义
### 1.0.2
[修改] 配置文件整理
### 1.0.1
[修复] 修复notUseCache字段取值错误
### 1.0.0
[修改] 多个参数名称更新: baseConfig => base、dynamicConfig => dynamic、 imageTimeOut => loadImgTimeOut
[修改] 统一使用img表示图片,弃用image
### 0.0.7
[新增] 新增weight属性控制动态元素绘制层级
[新增] 支持文字设置fontFamily属性
### 0.0.6
[新增] 增加图片超时时间设置
[修改] 修改draw仅绘制逻辑
### 0.0.5
[修改] 修改合成成功返回数据格式
### 0.0.4
[修改] 输出方法名称变更为mix-image
### 0.0.3
[修改] 统一输出数据格式
### 0.0.2
[修复] 修改依赖配置
### 0.0.1
[发布] 图片合成工具库
================================================
FILE: LICENSE
================================================
MIT License
Copyright (c) 2021 Baidu
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
================================================
FILE: README.md
================================================
# mix-img
mix-img图片合成工具,通过调用canvas API实现包括图片和文字的合成并最终生成图片base64,合成成功后向用户展示和分享。

## Install
```js
npm install --save mix-img
```
## Quick Start
```js
import {mixImg} from 'mix-img';
import {mixConfig} from './mixConfig'; // 配置文件路径自定义
async function getImg() {
const res = await mixImg(mixConfig);
console.log('图片合成结束', res);
}
```
> mixConfig参数配置可参见参数说明文档;Lib库使用者可以通过调试工具在本地进行预览调试,生成配置。
## Document
- [Start](https://github.com/baidu/mix-img/blob/master/README.md)
- [Example](https://github.com/baidu/mix-img/blob/master/test/e2e/index.js)
- [mixImg方法使用说明](https://github.com/baidu/mix-img/blob/master/docs/mixImg.md)
- [mixConfig参数说明文档](https://github.com/baidu/mix-img/blob/master/docs/mixConfig.md)
- [参数调试工具](https://github.com/baidu/mix-img/blob/master/docs/tool.md)
- [本库开发者阅读](https://github.com/baidu/mix-img/blob/master/docs/dev.md)
## ChangeLog
Please visit document [ChangeLog](https://github.com/baidu/mix-img/blob/master/CHANGELOG.md)
================================================
FILE: babel.config.js
================================================
/**
* @file: babel配置文件
* @author: zhw
* @Date: 2021-01-09 14:16:42
* @Last Modified by: zhw
* @Last Modified time: 2021-05-31 22:08:36
*/
module.exports = function (api) {
api.cache(true);
const presets = [];
const plugins = [
'@babel/plugin-proposal-optional-chaining',
'@babel/plugin-proposal-async-generator-functions',
['@babel/plugin-proposal-pipeline-operator', {proposal: 'smart'}]
];
// 打包umd模块包含完备的polyfill
if (process.env.NODE_ENV === 'build:umd') {
presets.push([
'@babel/preset-env',
{
useBuiltIns: 'usage',
corejs: 3
}
]);
}
// 单测需要转一下es modules至commonjs
if (process.env.NODE_ENV === 'test') {
plugins.push('@babel/plugin-transform-modules-commonjs');
}
return {
presets,
plugins
};
};
================================================
FILE: ci.yml
================================================
================================================
FILE: docs/dev.md
================================================
## 开发
```
# 从本项目中fork代码后,将代码clone到本地。
# 安装依赖
npm i
# 运行项目
npm run dev
# 构建成功后会自动打开浏览器,访问本地IP:8899/test.html,进行调试。
```
## 测试
```
# 自动化单元测试
npm run test
# e2e测试, 同dev开发
npm run test:e2e
```
================================================
FILE: docs/mixConfig.md
================================================
## mixConfig 图片合成参数
### 参数说明
#### mixConfig 参数说明
|参数名|类型|必填|说明|备注|
|---|---|---|---|---|
|replaceText|Object|否|替换字段配置|需要替换的属性设置为 `{变量}`
支持替换的内容:动态配置中文本的 text、动态配置中图片的 imgUrl、二维码配置的 text|
|base|Object|是|基本配置|具体配置参见 base 参数说明|
|qrCode|Object|否|二维码配置|具体配置参见 qrCode 参数说明|
|dynamic|Array.<object>|否|动态配置|具体配置参见 dynamic 参数说明|
|dev|Object|否|开发配置|具体配置参见 dev 参数说明|
#### base 参数说明
|参数名|类型|必填|默认值|说明|备注|
|---|---|---|---|---|---|
|backgroundImg|String|否|-|合成图片的背景图,支持 url 或 base64
若不传,fileType 为 jpeg 时背景为黑色,为 png 时背景透明|图片过大影响性能,建议压缩至 100k 以下|
|width|Number|是|300|合成图片的宽度|单位 px|
|height|Number|是|300|合成图片的高度|单位 px|
|quality|Number|否|0.8|生成图片的质量,取值范围为 (0, 1]|建议设置为0.9及以下|
|fileType|String|否|jpeg|生成图片的数据类型,值为 jpeg 或 png|-|
|dataType|String|否|base64|最终返回的数据格式,值为 base64 或 canvas|默认返回 base64 字符;若指定为 canvas,则返回绘制完毕的 canvas 对象|
|loadingTimeout|Number|否|5000|加载图片响应超时时间|单位 ms,背景图、动态图片超过该时间未加载完毕则返回超时信息|
#### qrCode 参数说明
|参数名|类型|必填|默认值|说明|备注|
|---|---|---|---|---|---|
|width|Number|是|70|生成二维码宽度|单位 px|
|height|Number|是|70|生成二维码高度|单位 px|
|text|String|是|-|要转换成二维码的文本|支持变量,如:`{qrcodeUrl}`|
|x|Number|是|0|横坐标信息|起始点为背景图片左上角|
|y|Number|是|0|纵坐标信息|起始点为背景图片左上角|
|background|String|否|#ffffff|二维码背景色|-|
|foreground|String|否|#000000|二维码前景色|-|
|correctLevel|Number|否|1|容错级别,值为 1、0、3、2|对应容错率为:L (7%)、M (15%)、Q (25%)、H (30%)
级别越高,二维码图片允许遮挡的部分越多,二维码信息越复杂|
#### dynamic 参数说明
* 动态信息为图片时:type = 1
|参数名|类型|必填|默认值|说明|备注|
|---|---|---|---|---|---|
|type|Number|是|-|动态信息类型,1 为图片、2 为文字||
|imgUrl|String|是|-|图片地址,支持 url 或 base64|支持变量,如:`{avatarUrl}`|
|size|Object|是|-|-|-|
|size.dWidth|Number|是|图片实际宽度|绘制图片的宽度|单位 px|
|size.dHeight|Number|是|图片实际高度|绘制图片的高度|单位 px|
|position|Object|是|-|-|-|
|position.x|Number|是|0|横坐标信息|起始点为背景图片左上角|
|position.y|Number|是|0|纵坐标信息|起始点为背景图片左上角|
|isRound|Boolean|否|false|是否绘制成圆形|-|
|weight|Number|否|0|绘制权重|权重越大,绘制越晚,层级越高|
* 动态信息为文字时:type = 2
|参数名|类型|必填|默认值|说明|备注|
|---|---|---|---|---|---|
|type|Number|是|-|动态信息类型,1 为图片、2 为文字|-|
|text|String|是|-|文字内容|支持变量,如:`{userName}`|
|style|Object|是|-|-|-|
|style.color|String|是|#000000|文字颜色|-|
|style.fontSize|Number|是|20|文字大小|单位px|
|style.fontWeight|String|否|normal|文字粗细,值为 normal、bold、lighter|-|
|style.fontFamily|String|否|PingFang SC / Roboto|文字字体|IOS 为`PingFang SC`,Android 为`Roboto`|
|style.textAlign|String|是|left|文字水平方向对齐方式,值为 left、center、right 等|[textAlign 文档](https://developer.mozilla.org/zh-CN/docs/Web/API/CanvasRenderingContext2D/textAlign)|
|style.textBaseline|String|是|alphabetic|文字垂直方向对齐方式|[textBaseline 文档](https://developer.mozilla.org/zh-CN/docs/Web/API/CanvasRenderingContext2D/textBaseline)|
|position|Object|是|-|-|-|
|position.x|Number|是|0|横坐标信息|起始点为背景图片左上角|
|position.y|Number|是|0|纵坐标信息|起始点为背景图片左上角|
|weight|Number|否|0|绘制权重|权重越大,绘制越晚,层级越高|
#### dev 参数说明
|参数名|类型|必填|默认值|说明|备注|
|---|---|---|---|---|---|
|notUseCache|Boolean|否|false|是否禁用缓存|默认启用缓存策略|
> 缓存策略:当 dataType 为默认值返回 base64 时,会使用当前传入配置 mixConfig 的 md5 值做键名,缓存合成后的 base64 字符(仅缓存最新的两条)。当用户传入相同配置项时,会从缓存中直接读取 base64 字符。
### 注意事项
#### 1. 动态元素 weight 属性
weight 属性可以控制动态元素的绘制层级,当动态配置中两个元素存在覆盖关系时,上方元素的 weight 属性需要设置更大的值。若 weight 属性值相同,则绘制层级随机。
- 图片示例

#### 2. 文字使用自定义字体
>【注意】请确认使用的自定义字体已获得授权,增强法律意识,避免字体侵权行为。
a. 声明自定义字体
- css
```css
@font-face {
font-family: 'myFont';
src: url("https://efe-h2.cdn.bcebos.com/ceug/resource/res/2021-1/1611891166782/c4964f1209aa.ttf") format('truetype');
}
```
b. 预下载自定义字体
通常情况下,当页面元素用到了 font-face 中定义的字体,则会执行下载。
- css
```css
#font-loaded {
font-size: 0;
font-family: 'myFont', sans-serif;
}
```
- html
```html
.
```
c. 动态配置中 fontFamily 设置为自定义字体
- 动态配置
```
'dynamic': [
{
'type': 2,
'position': {
'x': 187,
'y': 353
},
'style': {
'fontFamily': 'myFont'
'fontSize': 22,
'color': '#ffebc0',
'textAlign': 'center'
},
'text': '『自定义字体abc123』'
}
]
```
> 在绘制文字前,使用了 [FontFaceSet.load()](https://developer.mozilla.org/en-US/docs/Web/API/FontFaceSet/load) 方法等待自定义字体下载完毕再进行后续操作。由于该API存在兼容性问题,若当前环境不可用,且绘制文字时找不到已加载的自定义字体,本次将使用默认字体进行绘制,同时触发自定义字体的加载。
================================================
FILE: docs/mixImg.md
================================================
## mixImg 图片合成方法
### 使用
```js
import {mixImg} from 'mix-img';
import {mixConfig} from './mixConfig'; // 配置文件路径自定义
async function getImg() {
const res = await mixImg(mixConfig);
console.log('图片合成结束', res);
}
```
### 参数示例
```js
export const mixConfig = {
'replaceText': {
'submitName': '朱雀号',
'userName': '百度网友123',
'avatarUrl': 'https://efe-h2.cdn.bcebos.com/ceug/resource/res/2020-07/1594717976441/idyexeq1u92w.png',
'qrCodeUrl': 'https://www.baidu.com'
},
'base': {
'backgroundImg': 'https://efe-h2.cdn.bcebos.com/ceug/resource/res/2020-07/1594797097021/ml9v716tnxoc.jpg',
'width': 375,
'height': 667,
'quality': 0.8,
'fileType': 'jpeg'
},
'qrCode': {
'width': 74,
'height': 74,
'text': '{qrCodeUrl}',
'x': 279,
'y': 576,
'correctLevel': 1
},
'dynamic': [
{
'type': 2,
'position': {
'x': 187,
'y': 353
},
'style': {
'fontSize': 22,
'color': '#ffebc0',
'textAlign': 'center',
'fontWeight': 'bold'
},
'text': '『{submitName}』'
},
{
'type': 1,
'position': {
'x': 169,
'y': 207
},
'size': {
'dWidth': 40,
'dHeight': 40
},
'imgUrl': '{avatarUrl}',
'isRound': true
},
{
'type': 2,
'position': {
'x': 187,
'y': 268
},
'style': {
'textAlign': 'center',
'fontSize': 16,
'color': '#ffebc0',
'fontWeight': 'normal'
},
'text': '{userName}'
}
]
};
```
> 参数含义可参见 [mixConfig参数说明文档](https://github.com/baidu/mix-img/blob/master/docs/mixConfig.md)
### 返回数据
#### 合成成功
1.dataType 为 base64
- 返回参数说明
| 参数 | 类型 | 说明 |
| ------ | ------ | ------ |
| errno | Number | 错误码,合成成功时为 0 |
| data | Object | 数据对象 |
| data.base64 | String | 图片的 base64 字符 |
- 返回示例
```json5
{
errno: 0,
data: {
base64: 'data:image/jpeg;base64,000'
}
}
```
2.dataType 为 draw
- 返回参数说明
| 参数 | 类型 | 说明 |
| ------ | ------ | ------ |
| errno | Number | 错误码,合成成功时为 0 |
| data | Object | 数据对象 |
| data.canvas | Object | 绘制完成的 canvas 对象 |
- 返回示例
```json5
{
errno: 0,
data: {
canvas: '' // 绘制完成的 canvas 对象
}
}
```
#### 合成失败
- 返回参数说明
| 参数 | 类型 | 说明 |
| ------ | ------ | ------ |
| errno | Number | 错误码 |
| errmsg | String | 错误描述 |
| err | Object / String | 错误信息 |
- 返回示例
```json5
{
errno: 90002,
errmsg: '[mix img err] 创建img标签超时!',
err: 'img response time more than 5000 ms'
}
```
================================================
FILE: docs/tool.md
================================================
## 调试工具
### 产生背景
图片合成的配置项包含 base(基本配置)、replaceText(替换字段配置)、qrCode(二维码配置)、dynamic(动态元素配置)四大项。 其中动态元素配置更是会有很多的情况,调试配置参数很困难。为了减少开发人员工作量,内置了参数调试工具。用户可以在平台内更改参数,预览合成图片效果。调试完毕后,复制最终配置到项目中使用。
### 如何启动
```
# 将本库代码clone到本地
# 安装依赖
npm i
# 启动配置调试工具
npm run tool
```
### 工具界面

### 使用步骤
1. 修改 JSON 配置
2. 点击「生成预览」按钮,进行预览
3. 参数调试完毕,点击「复制配置」按钮
================================================
FILE: jest.config.js
================================================
/**
* @file: 测试配置文件
* @author: zhw
* @Date: 2020-06-30 13:45:06
* @Last Modified by: zhw
* @Last Modified time: 2021-01-09 16:44:09
*/
module.exports = {
setupFiles: ['jest-canvas-mock'],
rootDir: __dirname,
testMatch: ['/test/unit/*.test.js']
};
================================================
FILE: package.json
================================================
{
"name": "mix-img",
"version": "1.0.6",
"description": "A fast mix image javascript tool libary",
"module": "dist/index.min.js",
"browser": "dist/umd/index.umd.js",
"files": [
"dist"
],
"scripts": {
"dev": "npm run test:e2e",
"lint": "eslint src/** --ignore-path .gitignore",
"build": "rollup -c scripts/rollup.config.js",
"build:umd": "cross-env NODE_ENV=build:umd rollup -c scripts/rollup.umd.config.js",
"test": "cross-env NODE_ENV=test jest --coverage",
"test:e2e": "rollup -c scripts/rollup.e2e.config.js -w",
"prepublishOnly": "npm run build && npm run build:umd",
"tool": "sh ./scripts/tool.sh"
},
"husky": {
"hooks": {
"pre-commit": "lint-staged"
}
},
"lint-staged": {
"linters": {
"*.js": [
"eslint"
]
}
},
"author": "zhenghaiwang",
"devDependencies": {
"@babel/core": "^7.11.1",
"@babel/eslint-parser": "^7.13.4",
"@babel/eslint-plugin": "^7.12.1",
"@babel/plugin-proposal-async-generator-functions": "^7.12.1",
"@babel/plugin-proposal-optional-chaining": "^7.12.7",
"@babel/plugin-proposal-pipeline-operator": "^7.5.0",
"@babel/plugin-transform-modules-commonjs": "^7.13.0",
"@babel/preset-env": "^7.11.0",
"@ecomfe/eslint-config": "^7.0.0",
"@ecomfe/stylelint-config": "^1.1.1",
"babel-eslint": "^11.0.0-beta.0",
"core-js": "^3.6.5",
"cross-env": "^5.2.0",
"eslint": "^7.17.0",
"husky": "^4.3.7",
"internal-ip": "^6.2.0",
"jest": "^26.6.3",
"jest-canvas-mock": "^2.3.0",
"lint-staged": "^8.1.0",
"rollup": "^2.39.1",
"rollup-plugin-babel": "^4.4.0",
"rollup-plugin-commonjs": "^10.1.0",
"rollup-plugin-node-resolve": "^5.2.0",
"rollup-plugin-serve": "^1.1.0",
"rollup-plugin-terser": "^7.0.0",
"san": "^3.10.1",
"stylelint": "^13.13.1"
},
"engine": {
"node": ">= 8"
},
"dependencies": {
"md5": "^2.3.0",
"qrcodejs2-fixes": "0.0.2"
},
"license": "MIT",
"repository": {
"type": "git",
"url": "https://github.com/baidu/mix-img.git"
}
}
================================================
FILE: scripts/build.sh
================================================
export PATH=$NODEJS_BIN_LATEST:$PATH
echo "node: $(node -v)"
echo "npm: $(npm -v)"
out_dir="output"
# 安装依赖
export NODE_ENV=development
npm install
# 跑一遍build
export NODE_ENV=production
npm run build
mkdir -p $out_dir
mv ./dist $out_dir
if [ $? -eq 0 ]; then
echo '[publish] done'
exit 0
else
echo '[publish] fail'
exit 1
fi
================================================
FILE: scripts/rollup.config.js
================================================
/**
* @file: rollup配置文件
* @author: zhw
* @Date: 2020-07-06 16:16:49
* @Last Modified by: zhw
* @Last Modified time: 2021-01-09 16:27:16
*/
// rollup.config.js
import babel from 'rollup-plugin-babel';
import {terser} from 'rollup-plugin-terser';
export default [
{
input: 'src/index.js',
output: {
file: './dist/index.min.js',
format: 'es'
},
plugins: [
babel({
runtimeHelpers: true,
extensions: ['.js'],
exclude: 'node_modules/**'
}),
terser()
],
watch: {
include: 'src/**'
}
},
{
input: 'src/index.js',
output: {
file: './dist/index.js',
format: 'es'
},
plugins: [
babel({
runtimeHelpers: true,
extensions: ['.js'],
exclude: 'node_modules/**'
})
],
watch: {
include: 'src/**'
}
}
];
================================================
FILE: scripts/rollup.e2e.config.js
================================================
import babel from 'rollup-plugin-babel';
import serve from 'rollup-plugin-serve';
import resolve from 'rollup-plugin-node-resolve';
import commonjs from 'rollup-plugin-commonjs';
const internalIp = require('internal-ip');
const devHost = internalIp.v4.sync();
export default {
input: 'test/e2e/index.js',
output: {
file: 'dist/e2e/test.js',
format: 'iife'
},
plugins: [
babel({
include: ['src/**', 'test/**']
}),
resolve(),
commonjs(),
serve({
host: devHost,
open: true,
openPage: '/test.html',
contentBase: ['test/e2e', 'dist'],
port: 8899
})
]
};
================================================
FILE: scripts/rollup.umd.config.js
================================================
/**
* @file: rollup umd配置文件
* @author: haoxin
*/
// rollup.umd.config.js
import babel from 'rollup-plugin-babel';
import resolve from 'rollup-plugin-node-resolve';
import commonjs from 'rollup-plugin-commonjs';
import {terser} from 'rollup-plugin-terser';
export default [
{
input: 'src/index.js',
output: {
file: './dist/umd/index.umd.js',
format: 'umd',
name: 'mixImg'
},
plugins: [
babel({
runtimeHelpers: true,
extensions: ['.js'],
exclude: 'node_modules/**'
}),
resolve(),
commonjs(),
terser()
],
watch: {
include: 'src/**'
}
}
];
================================================
FILE: scripts/tool.sh
================================================
cd toolkit || exit
npm i
npm run start
================================================
FILE: src/config/errorMap.js
================================================
/**
* @file errorMap
* @author haoxin
*/
export const errorMap = {
CREATE_CANVAS_ERROR: {errno: 10001, errmsg: '[mix img err] 创建canvas标签出错!'},
ADD_BG_ERROR: {errno: 20001, errmsg: '[mix img err] 绘制背景图出错!'},
ADD_DYNAMIC_ERROR: {errno: 30001, errmsg: '[mix img err] 添加动态元素错误!'},
ADD_TEXT_ERROR: {errno: 300011, errmsg: '[mix img err] 添加文字错误!'},
ADD_IMG_ERROR: {errno: 300012, errmsg: '[mix img err] 添加图片错误!'},
ADD_QRCODE_ERROR: {errno: 30002, errmsg: '[mix img err] 添加二维码错误!'},
TO_BASE64_ERROR: {errno: 40001, errmsg: '[mix img err] canvas转base64出错!'},
CREATE_IMG_ERROR: {errno: 90001, errmsg: '[mix img err] 创建img标签出错!请排查该图片是否跨域'},
CREATE_IMG_TIMEOUT: {errno: 90002, errmsg: '[mix img err] 创建img标签超时!'}
};
================================================
FILE: src/index.js
================================================
/**
* @file: 图片合成方法
* @author: haoxin
*/
import {
addImgToCanvas,
addTextToCanvas,
addQrCodeToCanvas,
createCanvas,
canvasToBase64
} from './utils/canvasUtils';
import {createImg} from './utils/createImg';
import {errorMap} from './config/errorMap';
import {splitArr} from './utils/tools';
import md5 from 'md5';
let hash = '';
/**
* 绘制背景图
* @param {Object} config 总配置项
* @return {Promise