-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathCanvasPainter.js
216 lines (198 loc) · 7.3 KB
/
CanvasPainter.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
const windowWidth = wx.getSystemInfoSync().windowWidth;
const WRITE_IMG = 'scope.writePhotosAlbum';
const imgCache = {};
class CanvasPainter {
constructor({canvasId, context, config, scaleBase = 750} = {}) {
this.canvasId = canvasId;
this.ctx = wx.createCanvasContext(canvasId, context);
this.context = context;
this.config = config;
this.picPath = null;
const scale = windowWidth / scaleBase;
this.ctx.scale(scale, scale);
this.downloadTask = [];
}
resetConfig(config){
this.config = config;
this.loadImg = false;
this.picPath = null;
}
loadImgInAdvance() {
this.config.forEach((config)=>{
if (config.type === 'image' && config.url) {
this.downloadTask.push(CanvasPainter.downloadSingleImage(config));
}
});
this.loadImg = true;
}
draw() {
const ctx = this.ctx;
if(!this.loadImg) { this.loadImgInAdvance(); }
return Promise.all(this.downloadTask).then(() => {
//按照数组顺序,从底层到顶层画图
this.config.forEach((item)=>{
switch (item.type) {
case 'image':
if(item.url) {
CanvasPainter.drawImage(ctx, item);
}
break;
case 'text':
CanvasPainter.drawSingleText(ctx, item);
break;
case 'multiline_text':
CanvasPainter.drawMultiText(ctx, item);
break;
case 'rect':
CanvasPainter.drawRect(ctx, item);
break;
default:
break;
}
});
}).then(()=>{
return new Promise((resolve) => {
ctx.draw(false, resolve);
});
})
}
toPic(){
const self = this;
return this.picPath ? Promise.resolve(this.picPath) :
new Promise((res, reject)=>{
wx.canvasToTempFilePath({
canvasId: self.canvasId,
success({tempFilePath}) {
self.picPath = tempFilePath;
res(tempFilePath);
},
fail:reject
}, self.context);
});
}
preview(){
const self = this;
return this.toPic().then(()=>{
return new Promise((resolve, reject)=>{
wx.previewImage({
current: self.picPath,
urls: [self.picPath],
success: resolve,
fail: reject
});
})
});
}
save(){
const self = this;
return this.toPic().then(()=>{
return new Promise((resolve, reject) => {
const saveImage = (resolve, reject)=>{
wx.saveImageToPhotosAlbum({
filePath: self.picPath,
fail: reject,
success: resolve
});
};
// 判断是否已授权下载图片到本地
wx.getSetting({
success ({authSetting = {}}={}) {
const authorizedSaveImg = authSetting[WRITE_IMG];
if(!authorizedSaveImg) {
wx.authorize({
scope: WRITE_IMG,
success() { saveImage(resolve, reject); },
fail() { reject({ errMsg: 'saveImageToPhotosAlbum:fail auth deny'}); }
});
} else { saveImage(resolve, reject); }
}
})
});
});
}
static downloadSingleImage(config) {
let url = config.url;
// 可加入url test
if(imgCache[url]) {
config.path = imgCache[url];
return Promise.resolve();
} else {
return new Promise((resolve, reject) => {
wx.getImageInfo({
src: url,
success: (res) => {
config.path = res.path;
imgCache[url] = res.path;
resolve(res);
},
fail: () => {
// 如有需要 可下载暂位图片
console.log(`下载图片失败:, ${url},即将下载占位图片`);
// CanvasPainter.downloadSingleImage({ url: '' })
// .then((path)=>{
// config.path = path;
// resolve();
// }).catch(reject);
}
});
});
}
}
static drawRect(ctx, config) {
const setColor = config.stroke ? 'setStrokeStyle' : 'setFillStyle';
const method = config.stroke ? 'strokeRect' : 'fillRect';
ctx[setColor](config.color || '#ffffff');
if(config.round) {
ctx.arc(config.x + config.width / 2, config.y + config.height / 2, config.width / 2, 0, 2 * Math.PI);
ctx.fill();
} else {
ctx[method](config.x, config.y, config.width, config.height);
}
}
static drawSingleText(ctx, config) {
ctx.setFontSize(config.size || 20);
ctx.setTextAlign(config.align || 'left');
ctx.setFillStyle(config.color || '#000000');
ctx.fillText(config.text, config.x, config.y);
//从中间画一条线
if(config.decoration === 'line-through'){
let {width} = ctx.measureText(config.text);
let y = config.y - config.size/2 + 2;
ctx.beginPath();
ctx.setLineWidth(1);
ctx.setStrokeStyle(config.color);
ctx.moveTo(config.x, y);
ctx.lineTo(config.x + width+2, y);
ctx.closePath();
ctx.stroke();
}
if(config.font) { ctx.font = config.font; }
}
static drawMultiText(ctx, config) {
//根据lineLimit拆成多个singleText,顺序往下排
if (config.text.length <= config.line_limit) {
this.drawSingleText(ctx, config);
} else {
let parts = parseInt(config.text.length / config.line_limit) + 1;
for (let idx = 0; idx < parts; idx++) {
//拼出n个single text
let subConfig = {};
Object.assign(subConfig, config);
subConfig.text = subConfig.text.substr(idx * config.line_limit, config.line_limit);
subConfig.y = config.y + idx * config.line_height;
this.drawSingleText(ctx, subConfig);
}
}
}
static drawImage(ctx, config) {
if (config.round) {
ctx.save();
ctx.beginPath();
ctx.arc(config.x + config.width / 2, config.y + config.height / 2, config.width / 2, 0, 2 * Math.PI);
ctx.clip();
ctx.closePath();
}
ctx.drawImage(config.path, config.x, config.y, config.width, config.height);
}
}
export default CanvasPainter;