Skip to content

Commit dbe5769

Browse files
author
bjningbo
committed
修复两点bug,替换一个api
1 parent 68036eb commit dbe5769

File tree

3 files changed

+26
-16
lines changed

3 files changed

+26
-16
lines changed

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import CreatePicture from 'create-picture';
2020
const cp = new CreatePicture();
2121

2222
// 绘制图片,参数1为图片路径,其他参数与 CanvasRenderingContext2D.drawImage() 参数相同
23-
cp.drawPicture(require('../assets/save_bg.jpg'),0,0);
23+
cp.drawImage(require('../assets/save_bg.jpg'),0,0);
2424

2525
// 绘制文本
2626
cp.drawText({content:'文本'});
@@ -34,8 +34,8 @@ cp.getPicture().then((picture)=>{
3434
## 方法
3535

3636
- `new CreatePicture();` - 初始化,可接受一个对象参数
37-
- `drawPicture()` - 绘制图片,第一个参数为图片路径,其他参数与 [CanvasRenderingContext2D.drawImage()](https://developer.mozilla.org/zh-CN/docs/Web/API/CanvasRenderingContext2D/drawImage) 参数相同
38-
- `drawCirclePicture()` - 绘制头像等圆形图片,与 `drawPicture` 参数相同
37+
- `drawImage()` - 绘制图片,第一个参数为图片路径,其他参数与 [CanvasRenderingContext2D.drawImage()](https://developer.mozilla.org/zh-CN/docs/Web/API/CanvasRenderingContext2D/drawImage) 参数相同
38+
- `drawCirclePicture()` - 绘制头像等圆形图片,与 `drawImage` 参数相同
3939
- `drawText()` - 绘制文本,返回文字宽度(可选)
4040
- `getPicture().then((picture)=>{});` - 最终合成的图片
4141

@@ -46,7 +46,7 @@ cp.getPicture().then((picture)=>{
4646
- `width` - 画布宽度,默认值 `750`
4747
- `height` - 画布高度,默认值 `1448`
4848

49-
### drawPicture() 可选参数
49+
### drawImage() 可选参数
5050

5151
- 第一个参数为图片路径
5252
- 其他参数与 [CanvasRenderingContext2D.drawImage()](https://developer.mozilla.org/zh-CN/docs/Web/API/CanvasRenderingContext2D/drawImage) 参数相同
@@ -104,7 +104,7 @@ const cp:CreatePicture = new CreatePicture();
104104
const cp:CreatePicture = new CreatePicture({width:750,height:1448});
105105

106106
// 绘制图片,参数1为图片路径,其他参数与 CanvasRenderingContext2D.drawImage() 参数相同
107-
cp.drawPicture(require('../assets/save_bg.jpg'),0,0);
107+
cp.drawImage(require('../assets/save_bg.jpg'),0,0);
108108

109109
// 绘制文本
110110
cp.drawText({content:'文本'});

lib/create-picture.js

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
/* 说明 V 0.2.4
1+
/* 说明 V 0.3.0
22
* 该组件提供了 canvas 的图片绘制与文本绘制功能,使用同步的语法完成异步绘制,简化原生 canvas 绘制语法。
33
* API:https://github.com/NalvyBoo/create-picture
44
* npm:https://www.npmjs.com/package/create-picture
55
* */
66

77
let saidHello = false;
8-
const VERSION = '0.2.4';
8+
const VERSION = '0.3.0';
99
const defaultProp = {
1010
width: 750,
1111
height: 1448,
@@ -28,18 +28,23 @@ const defaultFontStyle = {
2828
};
2929
export default class CreatePicture{
3030
constructor({...prop}={}){
31-
prop = Object.assign(prop,defaultProp);
31+
prop = Object.assign(defaultProp,prop);
3232
this.canvas = document.createElement('canvas');
3333
this.ctx = this.canvas.getContext('2d');
3434
[this.canvas.width,this.canvas.height] = [Reflect.get(prop,'width'),Reflect.get(prop,'height')];
3535
this.map = new Map();
3636
this.pictureMap = new Map();
3737
this.sayHello();
3838
}
39+
// V 0.2.4 即将废弃
3940
drawPicture(...prop){
4041
this.map.set(prop,'picture');
4142
this.pictureMap.set(prop,prop[0]);
4243
}
44+
drawImage(...prop){
45+
this.map.set(prop,'picture');
46+
this.pictureMap.set(prop,prop[0]);
47+
}
4348
drawCirclePicture(...prop){
4449
this.map.set(prop,'circlePicture');
4550
this.pictureMap.set(prop,prop[0]);
@@ -108,13 +113,18 @@ export default class CreatePicture{
108113
const promiseArr = [];
109114
assetsArr.forEach((item,index)=>{
110115
promiseArr.push(new Promise((resolve)=>{
111-
const image = new Image();
112-
imageArr.push(image);
113-
image.onload = ()=>{
114-
resolve(imageArr)
115-
};
116-
image.crossOrigin = 'anonymous';
117-
image.src = assetsArr[index]+'?corp';
116+
if(item instanceof HTMLCanvasElement){
117+
imageArr.push(item);
118+
resolve(imageArr);
119+
}else{
120+
const image = new Image();
121+
imageArr.push(image);
122+
image.onload = ()=>{
123+
resolve(imageArr)
124+
};
125+
image.crossOrigin = 'anonymous';
126+
image.src = assetsArr[index]+'?corp';
127+
}
118128
}));
119129
});
120130
return Promise.all(promiseArr);

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "create-picture",
3-
"version": "0.2.4",
3+
"version": "0.3.0",
44
"description": "该组件提供了 Canvas 的图片绘制与文本绘制功能,使用同步的语法完成异步绘制,简化原生 canvas 绘制语法。",
55
"main": "lib/create-picture.js",
66
"scripts": {

0 commit comments

Comments
 (0)