nvue3 1.1.1 → 1.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -7,7 +7,7 @@ import App from './App';
7
7
 
8
8
  import { createSSRApp } from 'vue';
9
9
 
10
- import kernel from '@/kernel/kernel';
10
+ import kernel from 'nvue3';
11
11
 
12
12
  import config from '@/common/config';
13
13
  import variables from '@/common/variables';
@@ -0,0 +1,398 @@
1
+ export default {
2
+ methods: {
3
+ drawStar(data) {
4
+ if (this.drawIng) return;
5
+ this.drawIng = true;
6
+ console.log('drawData:', data || this.data);
7
+ this.$emit('speed', '开始绘图');
8
+ let canvasData = JSON.parse(JSON.stringify(data || this.data));
9
+ //按index进行排序,值大的在后面,最后绘
10
+ canvasData.sort(function(a, b) {
11
+ if (!a.index) a.index = 0;
12
+ if (!b.index) b.index = 0;
13
+ return String(a.index * 1).localeCompare(b.index * 1)
14
+ });
15
+ this.total = canvasData.length;
16
+ let PromiseTask = [];
17
+ canvasData.forEach((item, p) => {
18
+ if (item.type !== 'img' && item.type !== 'image') return;
19
+ PromiseTask.push(new Promise((resolve, reject) => {
20
+ let img = item.src;
21
+ this.$emit('speed', `(${p})加载图片`);
22
+ uni.downloadFile({
23
+ url: item.src,
24
+ success: res => {
25
+ PromiseTask.push(new Promise((resolve2, reject2) => {
26
+ uni.getImageInfo({
27
+ src: res.tempFilePath,
28
+ success: (image) => {
29
+ item.src = res
30
+ .tempFilePath;
31
+ resolve(res)
32
+ },
33
+ fail: (err) => {
34
+ reject({
35
+ errMsg: `(${p})图片格式错误,无法读取,本次任务失败`
36
+ });
37
+ console.error(
38
+ `(${p})图片格式错误,无法读取,本次任务失败`,
39
+ item, err)
40
+ }
41
+ });
42
+ }))
43
+ },
44
+ fail: err => {
45
+ reject({
46
+ errMsg: `(${p})图片无法下载,本次任务失败`
47
+ })
48
+ console.error(`(${p})图片无法下载,本次任务失败`, img, err)
49
+ }
50
+ })
51
+ }));
52
+ });
53
+ if (PromiseTask.length > 0) {
54
+ Promise.all(PromiseTask).then(
55
+ res => {
56
+ console.log('Promise.all success', res)
57
+ this.drawData(canvasData);
58
+ },
59
+ err => {
60
+ console.log('Promise.all fail', err)
61
+ this.$emit('error', err.errMsg);
62
+ }
63
+ );
64
+ }
65
+ else {
66
+ this.drawData(canvasData);
67
+ }
68
+ },
69
+ drawData(data) {
70
+ console.warn('drawData:', data);
71
+ const ctx = uni.createCanvasContext(this.canvasId, this);
72
+ ctx.save();
73
+ ctx.clearRect(0, 0, this.width, this.height);
74
+ ctx.setFillStyle(this.background)
75
+ ctx.fillRect(0, 0, this.width, this.height)
76
+ ctx.draw();
77
+ data.forEach((item, i) => {
78
+ console.log('speed', `绘元素(${i}).${item.type}`)
79
+ this.$emit('speed', `绘元素(${i}).${item.type}`);
80
+ ctx.restore(); //每个项目开始前先恢复初始设置
81
+ if (item.x < 0) item.x = this.width + item.x;
82
+ if (item.y < 0) item.y = this.height + item.y;
83
+ if (!item.alpha) item.alpha = 1;
84
+ if (item.alpha) ctx.setGlobalAlpha(item.alpha)
85
+ item.finish = 0;
86
+ switch (item.type) {
87
+ case 'rect':
88
+ this.drawRect(ctx, item);
89
+ break;
90
+ case 'text':
91
+ this.drawText(ctx, item);
92
+ break;
93
+ case 'img':
94
+ case 'image':
95
+ this.drawImg(ctx, item);
96
+ break;
97
+ case 'circular':
98
+ this.drawCircular(ctx, item);
99
+ break;
100
+ case 'line':
101
+ this.drawLine(ctx, item);
102
+ break;
103
+ case 'clear':
104
+ this.drawLine(ctx, item);
105
+ break;
106
+ }
107
+ this.draw(ctx, item);
108
+ });
109
+
110
+ let count = 0;
111
+ let tm = setInterval(() => {
112
+ let i = 0;
113
+ data.forEach(obj => {
114
+ if (!obj.finish) i++;
115
+ });
116
+
117
+ if (i === 0 || count++ > 5) {
118
+ clearInterval(tm)
119
+ uni.canvasToTempFilePath({
120
+ x: 0,
121
+ y: 0,
122
+ width: this.width,
123
+ height: this.height,
124
+ fileType: 'png',
125
+ canvasId: this.canvasId,
126
+ success: (res) => {
127
+ //这里获取的是base64码,可以直接用于image
128
+ this.$emit('save', res.tempFilePath);
129
+ this.drawIng = false;
130
+ },
131
+ fail: (err) => {
132
+ console.error('canvasToTempFilePath', err);
133
+ this.drawIng = false;
134
+ this.$emit('error', err.errMsg);
135
+ }
136
+ }, this);
137
+ }
138
+
139
+ }, 200);
140
+
141
+ },
142
+ drawCircular(ctx, item) { //画圆
143
+ ctx.arc(item.x, item.y, item.radius, 0, 2 * Math.PI)
144
+ ctx.setFillStyle(item.color)
145
+ ctx.fill()
146
+ },
147
+ drawClear(ctx, item) { //清除某个区域
148
+ ctx.clearRect(item.x, item.y, item.width, item.height)
149
+ },
150
+ drawLine(ctx, item) { //画线
151
+ let {
152
+ color,
153
+ width,
154
+ height,
155
+ cap,
156
+ join,
157
+ dash,
158
+ x,
159
+ y,
160
+ point,
161
+ relative
162
+ } = item;
163
+ ctx.setStrokeStyle(color)
164
+ ctx.setLineWidth(width || 10)
165
+ ctx.setLineCap(cap || 'round') //'butt'平角、'round'圆角、'square'
166
+ ctx.setLineJoin(join || 'miter') //'bevel'斜面、'round'圆角,'miter'斜角
167
+ if (dash) ctx.setLineDash(dash.pattern, dash.offset);
168
+ ctx.moveTo(x, y)
169
+ if (point) {
170
+ point.forEach(p => {
171
+ ctx.lineTo(p[0], p[1])
172
+ })
173
+ }
174
+ else {
175
+ relative.forEach(p => {
176
+ x += p[0], y += p[1];
177
+ ctx.lineTo(x, y)
178
+ })
179
+ }
180
+
181
+ ctx.stroke()
182
+ },
183
+ drawText(ctx, item) {
184
+ if (!item.line || item.line === 0) item.line = 1;
185
+ if (!item.lineHeight || item.lineHeight === 0) item.lineHeight = 2; //行距
186
+ if (!item.size) item.size = 12;
187
+ if (!item.padding) item.padding = 0;
188
+ if (typeof item.padding === 'number') { //上下,左右
189
+ item.padding = [item.padding, item.padding];
190
+ }
191
+ ctx.setFontSize(item.size);
192
+ if (item.align) {
193
+ if (!item.x) item.x = 0;
194
+ if (!item.width) item.width = this.width;
195
+ if (item.align === 'center') {
196
+ let mea = ctx.measureText(item.text);
197
+ item.x += (item.width - mea.width) / 2;
198
+ item.width = mea.width;
199
+ item.padding[1] = 0;
200
+ }
201
+ else if (item.align === 'right') {
202
+ let mea = ctx.measureText(item.text);
203
+ item.x += (item.width - mea.width);
204
+ item.width = mea.width;
205
+ item.padding[1] = 0;
206
+ }
207
+ }
208
+
209
+ if (!item.width) {
210
+ let mea = ctx.measureText(item.text)
211
+ item.width = mea.width + item.padding[1] * 2;
212
+ if (item.width > this.width) item.width = this.width;
213
+ }
214
+ if (!item.height) {
215
+ item.height = ((item.size + item.lineHeight) * item.line - item.lineHeight + item.padding[0] * 2);
216
+ }
217
+ let xy = [item.x, item.y];
218
+
219
+ console.log('text', JSON.stringify(item))
220
+ if (item.background) {
221
+ let bg = {
222
+ type: 'extend',
223
+ color: item.background,
224
+ x: item.x,
225
+ y: item.y,
226
+ width: item.width,
227
+ height: item.height,
228
+ alpha: 1,
229
+ };
230
+ this.drawRect(ctx, bg)
231
+ }
232
+ item.y += item.size;
233
+ let width = item.width - item.padding[1] * 2;
234
+ let height = item.height - item.padding[0] * 2;
235
+ item.x += item.padding[1];
236
+ item.y += item.padding[0];
237
+
238
+ if (item.alpha) ctx.setGlobalAlpha(item.alpha)
239
+ ctx.setFillStyle(item.color);
240
+ ctx.setTextAlign('left');
241
+ // ctx.setTextBaseline('bottom')
242
+ if (!item.width || item.line === 1) {
243
+ //不指定宽,直接显示整行
244
+ if (item.shade) {
245
+ // ctx.setShadow(item.shade.x, item.shade.y, 50, item.shade.color)
246
+ ctx.setFillStyle(item.shade.color);
247
+ ctx.fillText(item.text, item.x + item.shade.x, item.y + item.shade.y);
248
+ }
249
+ ctx.setFillStyle(item.color);
250
+ ctx.fillText(item.text, item.x, item.y);
251
+ // ctx.strokeText(item.text, item.x, item.y, 300)
252
+ return;
253
+ }
254
+ let metrics;
255
+ let stop = 0;
256
+ let star = 0; //每次开始截取的字符串的索引
257
+
258
+ for (let i = 0; i < item.text.length; i++) {
259
+ metrics = ctx.measureText(item.text.substring(star, i))
260
+ if (metrics.width >= width) {
261
+ stop = i;
262
+ if (metrics.width > width) stop -= 1;
263
+ ctx.fillText(item.text.substring(star, stop), item.x, item.y); //绘制截取部分
264
+ item.y += (item.size + item.lineHeight);
265
+ star = stop;
266
+ item.line--;
267
+ if (item.line <= 1) break;
268
+ }
269
+ }
270
+ let len = Math.floor(width / item.size);
271
+ // console.log(item.line, star, len, len);
272
+ if (item.line > 0 && star < item.text.length) {
273
+ ctx.fillText(item.text.substring(star, len + star), item.x, item.y);
274
+ }
275
+ if (item.radius) {
276
+ item.x = xy[0];
277
+ item.y = xy[1];
278
+ this.drawRadius(ctx, item);
279
+ }
280
+ },
281
+
282
+ drawRect(ctx, item) {
283
+ console.log('rect', JSON.stringify(item))
284
+ ctx.setFillStyle(item.color)
285
+ if (item.rotate) ctx.rotate(item.rotate * Math.PI / 180)
286
+ if (item.alpha) ctx.globalAlpha = item.alpha;
287
+ ctx.fillRect(item.x, item.y, item.width, item.height)
288
+ if (item.rotate) ctx.rotate(360 - item.rotate * Math.PI / 180)
289
+ if (item.radius) this.drawRadius(ctx, item);
290
+ },
291
+ drawImg(ctx, item) {
292
+ if (item.download === 0) {
293
+ console.log('img Not Download', JSON.stringify(item))
294
+ return;
295
+ }
296
+ console.log('img', JSON.stringify(item))
297
+ if (!item.padding) item.padding = 0;
298
+ if (item.padding > 0) {
299
+ let bg = {
300
+ type: 'extend',
301
+ color: item.background || '#fff',
302
+ x: item.x,
303
+ y: item.y,
304
+ width: item.width,
305
+ height: item.height,
306
+ alpha: 1,
307
+ };
308
+ this.drawRect(ctx, bg)
309
+ }
310
+ if (item.alpha) ctx.globalAlpha = item.alpha;
311
+ let opt = [item.src];
312
+ if (item.area) {
313
+ opt.push(...item.area, item.x + item.padding, item.y + item.padding,
314
+ item.width - item.padding * 2, item.height - item.padding * 2);
315
+ }
316
+ else {
317
+ opt.push(item.x + item.padding, item.y + item.padding,
318
+ item.width - item.padding * 2, item.height - item.padding * 2);
319
+ }
320
+ ctx.drawImage(...opt);
321
+
322
+ if (item.radius) this.drawRadius(ctx, item);
323
+ },
324
+ drawRadius(ctx, item) {
325
+ ctx.setFillStyle(item.radiusBackground || this.background)
326
+
327
+ let {
328
+ width,
329
+ height,
330
+ x,
331
+ y,
332
+ padding,
333
+ radius
334
+ } = item;
335
+
336
+ if (typeof radius === 'number' || typeof radius === 'string') {
337
+ radius = Array.from(Array(4), (v, k) => radius);
338
+ }
339
+ radius.forEach((r, i) => {
340
+ if (String(r).indexOf('%') > 0) {
341
+ radius[i] = ((width + height) / 2 + padding * 2) * (parseFloat(r) / 100);
342
+ }
343
+ else {
344
+ radius[i] = parseInt(r);
345
+ }
346
+ });
347
+
348
+ //pi从3点钟方向开始,这里分别是12/3/6/9点钟的pi值
349
+ let pi = [Math.PI * 3 / 2, 0, Math.PI / 2, Math.PI];
350
+
351
+ //右上角
352
+ if (radius[0]) {
353
+ ctx.beginPath();
354
+ ctx.arc(x + width - radius[0], y + radius[0], radius[0], pi[0], pi[1])
355
+ ctx.lineTo(x + width, y);
356
+ ctx.closePath();
357
+ ctx.fill()
358
+ }
359
+
360
+ //右下角
361
+ if (radius[1]) {
362
+ ctx.beginPath();
363
+ ctx.arc(x + width - radius[1], y + height - radius[1], radius[1], pi[1], pi[2])
364
+ ctx.lineTo(x + width, y + height);
365
+ ctx.closePath();
366
+ ctx.fill()
367
+ }
368
+
369
+ //左下角
370
+ if (radius[2]) {
371
+ ctx.beginPath();
372
+ ctx.arc(x + radius[2], y + height - radius[2], radius[2], pi[2], pi[3])
373
+ ctx.lineTo(x, y + height);
374
+ ctx.closePath();
375
+ ctx.fill()
376
+ }
377
+
378
+
379
+ //左上角
380
+ if (radius[3]) {
381
+ ctx.beginPath();
382
+ ctx.arc(x + radius[3], y + radius[3], radius[3], pi[3], pi[0])
383
+ ctx.lineTo(x, y);
384
+ ctx.closePath();
385
+ ctx.fill()
386
+ }
387
+
388
+ },
389
+ draw(ctx, item) {
390
+ ctx.draw(true, setTimeout(() => {
391
+ item.finish = 1;
392
+ if (item.type !== 'extend') this.finish++;
393
+ this.$emit('speed', Math.ceil((this.finish / this.total) * 100) + '%');
394
+ }, 50));
395
+ },
396
+ },
397
+
398
+ }