utils.js
4.8 KB
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
// 填充文本
export const fillText = (ctx, {
color,
size,
text,
x,
y,
align = 'left',
}) => {
if (!text) {
return;
}
ctx.setFillStyle(color);
ctx.setFontSize(size);
ctx.setTextAlign(align);
ctx.fillText(text, x, y);
};
// 画线条图案
export const drawLine = (ctx, {
x,
y,
width,
height,
color,
}) => {
ctx.setStrokeStyle(color);
ctx.strokeRect(x, y, width, height);
};
// 绘图
export const drawImage = (ctx, {
image,
x,
y,
width,
height,
radius = 0, // 次radius表示是否为圆形,并不是半径
}) => {
if (!image) {
return;
}
if (radius) {
const rr = width / 2;
ctx.save();
ctx.beginPath();
ctx.arc(x + rr, y + rr, rr, 0, 2 * Math.PI);
ctx.clip();
}
ctx.drawImage(image, x, y, width, height);
ctx.restore();
};
// 画区块
export const fillRect = (ctx, {
color,
x,
y,
width,
height,
}) => {
ctx.setFillStyle(color);
ctx.fillRect(x, y, width, height);
};
// 圆角矩形区域裁剪函数
const clipRect = (ctx, {
x,
y,
w,
h,
r,
}) => {
// 左上角
ctx.arc(x + r, y + r, r, Math.PI, Math.PI * 1.5);
// 顶部线条
ctx.moveTo(x + r, y);
ctx.lineTo(x + w - r, y);
ctx.lineTo(x + w, y + r);
// 右上角
ctx.arc(x + w - r, y + r, r, Math.PI * 1.5, Math.PI * 2);
// 右部线条
ctx.lineTo(x + w, y + h - r);
ctx.lineTo(x + w - r, y + h);
// 右下角
ctx.arc(x + w - r, y + h - r, r, 0, Math.PI * 0.5);
// 底部线条
ctx.lineTo(x + r, y + h);
ctx.lineTo(x, y + h - r);
// 左下角
ctx.arc(x + r, y + h - r, r, Math.PI * 0.5, Math.PI);
// 左部线条
ctx.lineTo(x, y + r);
ctx.lineTo(x + r, y);
};
// 各类矩形绘制,可以绘制填充色圆角矩形/填充色普通矩形(radius = 0),圆角图片/普通图片(radius = 0)
const drawRadiusBox = (ctx, {
x,
y,
radius = 0,
width,
height,
color = 'transparent',
image,
}) => {
ctx.setFillStyle(color);
// 开始绘制
ctx.save();
ctx.beginPath();
clipRect(ctx, {
x,
y,
w: width,
h: height,
r: radius,
});
ctx.fill();
// 剪切
ctx.clip();
if (image) {
drawImage(ctx, {
width,
height,
x,
y,
image,
});
}
ctx.restore();
};
// 测量文本长度
export const measureText = (ctx, text, size) => {
ctx.setFontSize(size);
return ctx.measureText(text).width;
};
// 绘制类型type与处理对象的映射表
const HandleTypeMap = {
text: {
handle: fillText,
},
rect: {
handle: fillRect,
},
image: {
handle: drawImage,
},
line: {
handle: drawLine,
},
box: {
handle: drawRadiusBox,
},
};
// canvas 转图片
export const canvasToTempFilePath = (
{
canvasId,
quality = 1,
fileType = 'jpg',
},
context
) => new Promise((resolve, reject) => {
wx.canvasToTempFilePath({
canvasId,
quality,
fileType,
success: resolve,
fail: reject,
}, context);
});
// 保存文件
export const saveFile = (tempFilePath) => new Promise((resolve, reject) => {
wx.saveFile({
tempFilePath,
success: resolve,
fail: reject,
});
});
// url转本地图片
export const onlineImageToLocalImage = (imgMap) => {
const tasks = [];
const resultMap = {};
Object.keys(imgMap).forEach((key) => {
let imgSrc = imgMap[key];
tasks.push(new Promise((resolveSub) => {
console.log(imgSrc.replace(/^http(?=:)/i, 'https'))
wx.getImageInfo({
src: imgSrc.replace(/^http(?=:)/i, 'https'),
success(res) {
resultMap[key] = res.path;
resolveSub(res.path);
},
fail(err) {
resultMap[key] = '';
resolveSub('');
},
});
}));
});
return Promise.all(tasks).then(() => resultMap);
};
// 画布开始
export const drawPoster = ({ctx, data} = {}) => new Promise((resolve) => {
ctx.setTextBaseline('top');
// 遍历所有笔画
data.forEach((item) => {
if (item && item.type) {
let handleObj = HandleTypeMap[item.type];
if (!handleObj) {
return;
}
let {handle} = handleObj;
handle(ctx, item);
}
});
// 绘制画布
// 延迟200ms后再resolve,否则会出现输出图片白屏
ctx.draw(true, () => {
setTimeout(() => {
resolve();
}, 200);
});
});
export const getUUID = () => (Date.now()).toString(16);