uview-plus 3.8.55 → 3.8.106
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/changelog.md +2147 -1828
- package/components/u-action-sheet/props.js +3 -1
- package/components/u-action-sheet/u-action-sheet.vue +22 -2
- package/components/u-album/props.js +3 -1
- package/components/u-alert/props.js +4 -2
- package/components/u-avatar/props.js +4 -1
- package/components/u-avatar-group/props.js +4 -1
- package/components/u-back-top/props.js +4 -1
- package/components/u-badge/props.js +4 -1
- package/components/u-barcode/u-barcode.vue +44 -15
- package/components/u-box/props.js +5 -2
- package/components/u-button/props.js +4 -1
- package/components/u-button/u-button.vue +2 -5
- package/components/u-button/vue.scss +3 -3
- package/components/u-calendar/calendar.js +8 -0
- package/components/u-calendar/props.js +43 -1
- package/components/u-calendar/u-calendar.vue +16 -0
- package/components/u-calendar-strip/props.js +3 -1
- package/components/u-canvas/u-canvas.vue +1028 -423
- package/components/u-car-keyboard/props.js +3 -1
- package/components/u-card/props.js +3 -1
- package/components/u-cell/props.js +4 -1
- package/components/u-cell/u-cell.vue +9 -1
- package/components/u-cell-group/props.js +4 -1
- package/components/u-checkbox/props.js +4 -1
- package/components/u-checkbox/u-checkbox.vue +7 -3
- package/components/u-checkbox-group/props.js +3 -1
- package/components/u-circle-progress/props.js +4 -1
- package/components/u-code/props.js +4 -1
- package/components/u-code-input/props.js +4 -1
- package/components/u-col/props.js +4 -1
- package/components/u-collapse/props.js +4 -1
- package/components/u-collapse-item/props.js +4 -1
- package/components/u-color-picker/u-color-picker.vue +1 -0
- package/components/u-column-notice/props.js +4 -1
- package/components/u-count-down/props.js +4 -1
- package/components/u-count-to/props.js +4 -1
- package/components/u-cropper/u-cropper.vue +95 -51
- package/components/u-datetime-picker/props.js +4 -1
- package/components/u-datetime-picker/u-datetime-picker.vue +53 -4
- package/components/u-divider/props.js +4 -1
- package/components/u-dragsort/u-dragsort.vue +57 -32
- package/components/u-dropdown/props.js +3 -1
- package/components/u-dropdown-item/props.js +3 -1
- package/components/u-empty/props.js +4 -1
- package/components/u-form/props.js +4 -1
- package/components/u-form-item/props.js +4 -1
- package/components/u-gap/props.js +4 -1
- package/components/u-grid/props.js +4 -1
- package/components/u-grid-item/props.js +4 -1
- package/components/u-guide/props.js +3 -1
- package/components/u-icon/props.js +4 -1
- package/components/u-icon/u-icon.vue +11 -2
- package/components/u-icon/upicon.ttf +0 -0
- package/components/u-icon/util.js +103 -10
- package/components/u-image/props.js +4 -1
- package/components/u-image/u-image.vue +280 -267
- package/components/u-index-anchor/props.js +4 -1
- package/components/u-index-item/props.js +2 -1
- package/components/u-index-item/u-index-item.vue +1 -1
- package/components/u-index-list/props.js +4 -1
- package/components/u-index-list/u-index-list.vue +1 -1
- package/components/u-input/props.js +3 -1
- package/components/u-keyboard/props.js +4 -1
- package/components/u-line/props.js +4 -1
- package/components/u-line-progress/props.js +6 -3
- package/components/u-link/props.js +4 -1
- package/components/u-list/props.js +4 -1
- package/components/u-list-item/props.js +4 -1
- package/components/u-loading-icon/props.js +4 -1
- package/components/u-loading-page/props.js +3 -1
- package/components/u-loadmore/props.js +4 -1
- package/components/u-markdown/u-markdown.vue +45 -3
- package/components/u-modal/props.js +4 -1
- package/components/u-navbar/props.js +4 -2
- package/components/u-navbar/u-navbar.vue +15 -9
- package/components/u-no-network/props.js +4 -1
- package/components/u-notice-bar/props.js +4 -1
- package/components/u-notify/props.js +4 -1
- package/components/u-novel-reader/content-normalizer.js +80 -0
- package/components/u-novel-reader/layout-engine.js +225 -0
- package/components/u-novel-reader/measure-adapter.js +69 -0
- package/components/u-novel-reader/novelReader.js +35 -0
- package/components/u-novel-reader/persistence.js +144 -0
- package/components/u-novel-reader/props.js +100 -0
- package/components/u-novel-reader/reader-catalog.vue +234 -0
- package/components/u-novel-reader/reader-content.vue +226 -0
- package/components/u-novel-reader/reader-core.js +151 -0
- package/components/u-novel-reader/reader-settings.vue +325 -0
- package/components/u-novel-reader/reader-toolbar.vue +313 -0
- package/components/u-novel-reader/theme-vars.scss +49 -0
- package/components/u-novel-reader/u-novel-reader.vue +836 -0
- package/components/u-number-box/props.js +4 -1
- package/components/u-number-box/u-number-box.vue +2 -2
- package/components/u-number-keyboard/props.js +4 -1
- package/components/u-overlay/props.js +4 -1
- package/components/u-pagination/u-pagination.vue +60 -39
- package/components/u-parse/props.js +4 -1
- package/components/u-picker/props.js +4 -1
- package/components/u-picker-column/props.js +3 -1
- package/components/u-popover/props.js +2 -1
- package/components/u-popup/props.js +4 -1
- package/components/u-poster/u-poster.vue +246 -214
- package/components/u-qrcode/qrcode.js +105 -97
- package/components/u-qrcode/u-qrcode.vue +242 -289
- package/components/u-radio/props.js +4 -1
- package/components/u-radio/u-radio.vue +7 -3
- package/components/u-radio-group/props.js +4 -1
- package/components/u-rate/props.js +4 -1
- package/components/u-read-more/props.js +4 -1
- package/components/u-row/props.js +4 -1
- package/components/u-row-notice/props.js +4 -1
- package/components/u-safe-bottom/props.js +2 -1
- package/components/u-scroll-list/props.js +4 -1
- package/components/u-scroll-list/scrollWxs.wxs +1 -1
- package/components/u-search/props.js +3 -1
- package/components/u-skeleton/props.js +4 -1
- package/components/u-slider/props.js +3 -1
- package/components/u-slider/u-slider.vue +71 -29
- package/components/u-status-bar/props.js +4 -1
- package/components/u-steps/props.js +4 -1
- package/components/u-steps-item/props.js +4 -1
- package/components/u-sticky/props.js +4 -1
- package/components/u-sticky/u-sticky.vue +11 -0
- package/components/u-subsection/props.js +3 -1
- package/components/u-swipe-action/props.js +4 -1
- package/components/u-swipe-action-item/index.wxs +43 -25
- package/components/u-swipe-action-item/nvue.js +9 -0
- package/components/u-swipe-action-item/other.js +32 -21
- package/components/u-swipe-action-item/props.js +9 -1
- package/components/u-swipe-action-item/swipeActionItem.js +1 -0
- package/components/u-swipe-action-item/u-swipe-action-item.vue +42 -4
- package/components/u-swiper/props.js +9 -1
- package/components/u-swiper/swiper.js +1 -0
- package/components/u-swiper/u-swiper.vue +2 -0
- package/components/u-swiper-indicator/props.js +4 -1
- package/components/u-switch/props.js +4 -1
- package/components/u-switch/u-switch.vue +1 -1
- package/components/u-tabbar/props.js +3 -1
- package/components/u-tabbar/u-tabbar.vue +2 -1
- package/components/u-tabbar-item/props.js +4 -1
- package/components/u-tabbar-item/u-tabbar-item.vue +64 -21
- package/components/u-table/props.js +3 -1
- package/components/u-table2/tableRow.vue +155 -69
- package/components/u-table2/u-table2.vue +327 -137
- package/components/u-tabs/props.js +4 -1
- package/components/u-tabs/u-tabs.vue +11 -8
- package/components/u-tabs-item/props.js +3 -1
- package/components/u-tabs-pro/u-tabs-pro.vue +212 -0
- package/components/u-tag/props.js +4 -1
- package/components/u-td/props.js +3 -1
- package/components/u-text/props.js +4 -1
- package/components/u-text/text.js +1 -1
- package/components/u-text/u-text.vue +18 -6
- package/components/u-textarea/props.js +3 -1
- package/components/u-textarea/u-textarea.vue +16 -5
- package/components/u-th/props.js +3 -1
- package/components/u-toolbar/props.js +4 -1
- package/components/u-tooltip/props.js +10 -2
- package/components/u-tooltip/tooltip.js +2 -1
- package/components/u-tooltip/u-tooltip.vue +31 -3
- package/components/u-tr/props.js +3 -1
- package/components/u-transition/nvue-ani-map.js +75 -68
- package/components/u-transition/props.js +4 -1
- package/components/u-transition/transitionMixin.js +164 -163
- package/components/u-upload/props.js +4 -1
- package/components/u-upload/u-upload.vue +48 -41
- package/components/u-waterfall/u-waterfall.vue +162 -110
- package/index.js +65 -8
- package/libs/config/config.js +1 -1
- package/libs/config/props.js +180 -204
- package/libs/css/components.scss +3 -0
- package/libs/function/index.js +66 -0
- package/libs/function/uni-api-shims.js +29 -0
- package/libs/i18n/index.js +71 -36
- package/libs/i18n/locale-packs.js +11 -0
- package/libs/i18n/locales/all.js +23 -0
- package/libs/i18n/locales/{de.json → de.js} +1 -1
- package/libs/i18n/locales/{en.json → en.js} +2 -2
- package/libs/i18n/locales/{es.json → es.js} +1 -1
- package/libs/i18n/locales/{fr.json → fr.js} +2 -2
- package/libs/i18n/locales/{ja.json → ja.js} +1 -1
- package/libs/i18n/locales/{ko.json → ko.js} +1 -1
- package/libs/i18n/locales/{ru.json → ru.js} +1 -1
- package/libs/i18n/locales/{th.json → th.js} +1 -1
- package/libs/i18n/locales/{zh-Hans.json → zh-Hans.js} +1 -1
- package/libs/i18n/locales/{zh-Hant.json → zh-Hant.js} +1 -1
- package/libs/mixin/mixin.js +18 -54
- package/libs/root/index.js +78 -5
- package/libs/root/page.js +7 -7
- package/libs/root/root.js +73 -3
- package/libs/util/app-nvue-webview-canvas.js +486 -0
- package/libs/util/gcanvas/bridge/bridge-weex.js +10 -7
- package/libs/util/gcanvas/context-2d/FillStyleLinearGradient.js +0 -0
- package/libs/util/gcanvas/context-2d/FillStylePattern.js +0 -0
- package/libs/util/gcanvas/context-2d/FillStyleRadialGradient.js +0 -0
- package/libs/util/gcanvas/context-2d/RenderingContext.js +0 -0
- package/libs/util/gcanvas/context-webgl/ActiveInfo.js +0 -0
- package/libs/util/gcanvas/context-webgl/Buffer.js +0 -0
- package/libs/util/gcanvas/context-webgl/Framebuffer.js +0 -0
- package/libs/util/gcanvas/context-webgl/GLenum.js +0 -0
- package/libs/util/gcanvas/context-webgl/GLmethod.js +0 -0
- package/libs/util/gcanvas/context-webgl/GLtype.js +0 -0
- package/libs/util/gcanvas/context-webgl/Program.js +0 -0
- package/libs/util/gcanvas/context-webgl/Renderbuffer.js +0 -0
- package/libs/util/gcanvas/context-webgl/RenderingContext.js +0 -0
- package/libs/util/gcanvas/context-webgl/Shader.js +0 -0
- package/libs/util/gcanvas/context-webgl/ShaderPrecisionFormat.js +0 -0
- package/libs/util/gcanvas/context-webgl/Texture.js +0 -0
- package/libs/util/gcanvas/context-webgl/UniformLocation.js +0 -0
- package/libs/util/gcanvas/context-webgl/classUtils.js +0 -0
- package/libs/util/gcanvas/env/canvas.js +0 -0
- package/libs/util/gcanvas/env/image.js +0 -0
- package/libs/util/gcanvas/env/tool.js +0 -0
- package/libs/util/gcanvas/index.js +3 -3
- package/package.json +4 -5
- package/tsconfig.json +18 -0
- package/types/comps/calendar.d.ts +38 -0
- package/types/comps/checkbox.d.ts +12 -0
- package/types/comps/lineProgress.d.ts +5 -0
- package/types/comps/navbar.d.ts +3 -3
- package/types/comps/swipeActionItem.d.ts +13 -0
- package/types/comps/swiper.d.ts +5 -0
- package/types/comps/tabs.d.ts +2 -1
- package/types/comps/text.d.ts +1 -1
- package/types/comps/tooltip.d.ts +36 -1
- package/types/func.d.ts +23 -0
- package/types/index.d.ts +1 -0
- /package/components/u-markdown/{marked.esm.js → marked.esm.mjs} +0 -0
|
@@ -1,423 +1,1028 @@
|
|
|
1
|
-
<template>
|
|
2
|
-
<view
|
|
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
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
// #
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
}
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
}
|
|
368
|
-
}
|
|
369
|
-
}
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
}
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
1
|
+
<template>
|
|
2
|
+
<view
|
|
3
|
+
class="u-canvas"
|
|
4
|
+
:id="rootId"
|
|
5
|
+
:style="{
|
|
6
|
+
width: useRootHeightAndWidth ? '100%' : actualWidth + unit,
|
|
7
|
+
height: useRootHeightAndWidth ? '100%' : actualHeight + unit
|
|
8
|
+
}"
|
|
9
|
+
>
|
|
10
|
+
<!-- #ifdef MP || H5 -->
|
|
11
|
+
<canvas
|
|
12
|
+
class="u-canvas__canvas"
|
|
13
|
+
:id="canvasId"
|
|
14
|
+
:canvas-id="canvasId"
|
|
15
|
+
type="2d"
|
|
16
|
+
:disable-scroll="disableScroll"
|
|
17
|
+
:style="{ width: actualWidth + unit, height: actualHeight + unit }"
|
|
18
|
+
@touchstart="onTouchStart"
|
|
19
|
+
@touchmove="onTouchMove"
|
|
20
|
+
@touchend="onTouchEnd"
|
|
21
|
+
/>
|
|
22
|
+
<!-- #endif -->
|
|
23
|
+
|
|
24
|
+
<!-- #ifdef APP-PLUS -->
|
|
25
|
+
<!-- #ifndef APP-NVUE -->
|
|
26
|
+
<canvas
|
|
27
|
+
class="u-canvas__canvas"
|
|
28
|
+
:id="canvasId"
|
|
29
|
+
:canvas-id="canvasId"
|
|
30
|
+
:disable-scroll="disableScroll"
|
|
31
|
+
:style="{ width: actualWidth + unit, height: actualHeight + unit }"
|
|
32
|
+
@touchstart="onTouchStart"
|
|
33
|
+
@touchmove="onTouchMove"
|
|
34
|
+
@touchend="onTouchEnd"
|
|
35
|
+
/>
|
|
36
|
+
<!-- #endif -->
|
|
37
|
+
<!-- #endif -->
|
|
38
|
+
|
|
39
|
+
<!-- #ifdef APP-NVUE -->
|
|
40
|
+
<web-view
|
|
41
|
+
class="u-canvas__canvas"
|
|
42
|
+
ref="web"
|
|
43
|
+
src="/static/app-plus/up-canvas/local.html"
|
|
44
|
+
:style="{ width: actualWidth + unit, height: actualHeight + unit }"
|
|
45
|
+
@onPostMessage="onWebViewMessage"
|
|
46
|
+
/>
|
|
47
|
+
<!-- #endif -->
|
|
48
|
+
</view>
|
|
49
|
+
</template>
|
|
50
|
+
|
|
51
|
+
<script>
|
|
52
|
+
// #ifdef APP-NVUE
|
|
53
|
+
import {
|
|
54
|
+
createWebViewCanvasBridge,
|
|
55
|
+
createWebViewCanvasContext
|
|
56
|
+
} from '../../libs/util/app-nvue-webview-canvas.js';
|
|
57
|
+
// #endif
|
|
58
|
+
|
|
59
|
+
export default {
|
|
60
|
+
name: 'u-canvas',
|
|
61
|
+
emits: ['ready', 'touchstart', 'touchmove', 'touchend'],
|
|
62
|
+
props: {
|
|
63
|
+
canvasId: {
|
|
64
|
+
type: String,
|
|
65
|
+
default: () => `u-canvas${Math.floor(Math.random() * 1000000)}`
|
|
66
|
+
},
|
|
67
|
+
width: {
|
|
68
|
+
type: [String, Number],
|
|
69
|
+
default: 300
|
|
70
|
+
},
|
|
71
|
+
height: {
|
|
72
|
+
type: [String, Number],
|
|
73
|
+
default: 300
|
|
74
|
+
},
|
|
75
|
+
unit: {
|
|
76
|
+
type: String,
|
|
77
|
+
default: 'px'
|
|
78
|
+
},
|
|
79
|
+
useRootHeightAndWidth: {
|
|
80
|
+
type: Boolean,
|
|
81
|
+
default: false
|
|
82
|
+
},
|
|
83
|
+
bgColor: {
|
|
84
|
+
type: String,
|
|
85
|
+
default: '#ffffff'
|
|
86
|
+
},
|
|
87
|
+
disableScroll: {
|
|
88
|
+
type: Boolean,
|
|
89
|
+
default: false
|
|
90
|
+
}
|
|
91
|
+
},
|
|
92
|
+
data() {
|
|
93
|
+
return {
|
|
94
|
+
rootId: `u-canvas-root-${Math.floor(Math.random() * 1000000)}`,
|
|
95
|
+
ctx: null,
|
|
96
|
+
widthLocal: this.parseSize(this.width),
|
|
97
|
+
heightLocal: this.parseSize(this.height),
|
|
98
|
+
fontSize: 12,
|
|
99
|
+
fontFamily: 'sans-serif',
|
|
100
|
+
fontWeight: 'normal',
|
|
101
|
+
dpr: 1
|
|
102
|
+
};
|
|
103
|
+
},
|
|
104
|
+
computed: {
|
|
105
|
+
actualWidth() {
|
|
106
|
+
return this.useRootHeightAndWidth ? this.widthLocal : this.parseSize(this.width);
|
|
107
|
+
},
|
|
108
|
+
actualHeight() {
|
|
109
|
+
return this.useRootHeightAndWidth ? this.heightLocal : this.parseSize(this.height);
|
|
110
|
+
}
|
|
111
|
+
},
|
|
112
|
+
watch: {
|
|
113
|
+
width() {
|
|
114
|
+
this.refresh();
|
|
115
|
+
},
|
|
116
|
+
height() {
|
|
117
|
+
this.refresh();
|
|
118
|
+
},
|
|
119
|
+
bgColor() {
|
|
120
|
+
this.clearCanvas();
|
|
121
|
+
}
|
|
122
|
+
},
|
|
123
|
+
created() {
|
|
124
|
+
this._canvasNode = null;
|
|
125
|
+
this._selectorResult = null;
|
|
126
|
+
this._canvasElement = null;
|
|
127
|
+
this._imageCache = Object.create(null);
|
|
128
|
+
this._imageDataCache = Object.create(null);
|
|
129
|
+
this._isNvue = false;
|
|
130
|
+
this._initPromise = null;
|
|
131
|
+
this._webViewBridge = null;
|
|
132
|
+
this._webViewContext = null;
|
|
133
|
+
this._webViewInitSignature = null;
|
|
134
|
+
this._webViewSessionId = null;
|
|
135
|
+
},
|
|
136
|
+
mounted() {
|
|
137
|
+
this.$nextTick(() => {
|
|
138
|
+
this.initCanvas();
|
|
139
|
+
});
|
|
140
|
+
},
|
|
141
|
+
beforeUnmount() {
|
|
142
|
+
if (this._webViewBridge) {
|
|
143
|
+
this._webViewBridge.destroy();
|
|
144
|
+
}
|
|
145
|
+
this._webViewBridge = null;
|
|
146
|
+
this._webViewContext = null;
|
|
147
|
+
this.ctx = null;
|
|
148
|
+
},
|
|
149
|
+
methods: {
|
|
150
|
+
parseSize(value) {
|
|
151
|
+
if (typeof value === 'number') {
|
|
152
|
+
return value;
|
|
153
|
+
}
|
|
154
|
+
if (typeof value !== 'string') {
|
|
155
|
+
return 0;
|
|
156
|
+
}
|
|
157
|
+
if (value.endsWith('rpx') || value.endsWith('upx')) {
|
|
158
|
+
return uni.upx2px(parseFloat(value));
|
|
159
|
+
}
|
|
160
|
+
if (value.endsWith('px')) {
|
|
161
|
+
return parseFloat(value) || 0;
|
|
162
|
+
}
|
|
163
|
+
return parseFloat(value) || 0;
|
|
164
|
+
},
|
|
165
|
+
onTouchStart(event) {
|
|
166
|
+
this.$emit('touchstart', event);
|
|
167
|
+
},
|
|
168
|
+
onTouchMove(event) {
|
|
169
|
+
this.$emit('touchmove', event);
|
|
170
|
+
},
|
|
171
|
+
onTouchEnd(event) {
|
|
172
|
+
this.$emit('touchend', event);
|
|
173
|
+
},
|
|
174
|
+
onWebViewMessage(event) {
|
|
175
|
+
if (this._webViewBridge) {
|
|
176
|
+
this._webViewBridge.handleMessage(event);
|
|
177
|
+
}
|
|
178
|
+
},
|
|
179
|
+
onWebViewTouch(message) {
|
|
180
|
+
const eventType = message.eventType;
|
|
181
|
+
if (!['touchstart', 'touchmove', 'touchend'].includes(eventType)) return;
|
|
182
|
+
this.$emit(eventType, {
|
|
183
|
+
type: eventType,
|
|
184
|
+
detail: {
|
|
185
|
+
x: message.x,
|
|
186
|
+
y: message.y
|
|
187
|
+
},
|
|
188
|
+
touches: message.touches || [],
|
|
189
|
+
changedTouches: message.changedTouches || [],
|
|
190
|
+
canvasWidth: message.canvasWidth,
|
|
191
|
+
canvasHeight: message.canvasHeight
|
|
192
|
+
});
|
|
193
|
+
},
|
|
194
|
+
async getCanvasNode(id = this.canvasId, isCanvas = true) {
|
|
195
|
+
return new Promise((resolve) => {
|
|
196
|
+
try {
|
|
197
|
+
uni.createSelectorQuery()
|
|
198
|
+
.in(this)
|
|
199
|
+
.select(`#${id}`)
|
|
200
|
+
.fields(
|
|
201
|
+
{
|
|
202
|
+
node: isCanvas,
|
|
203
|
+
size: true
|
|
204
|
+
},
|
|
205
|
+
(res) => {
|
|
206
|
+
// #ifdef APP-PLUS
|
|
207
|
+
resolve(res || {
|
|
208
|
+
width: this.actualWidth,
|
|
209
|
+
height: this.actualHeight
|
|
210
|
+
});
|
|
211
|
+
return;
|
|
212
|
+
// #endif
|
|
213
|
+
|
|
214
|
+
resolve(res || false);
|
|
215
|
+
}
|
|
216
|
+
)
|
|
217
|
+
.exec();
|
|
218
|
+
} catch (error) {
|
|
219
|
+
console.error('获取画布节点失败:', error);
|
|
220
|
+
resolve(false);
|
|
221
|
+
}
|
|
222
|
+
});
|
|
223
|
+
},
|
|
224
|
+
getCanvasElement() {
|
|
225
|
+
return this._canvasElement || null;
|
|
226
|
+
},
|
|
227
|
+
getRawContext() {
|
|
228
|
+
return this.ctx;
|
|
229
|
+
},
|
|
230
|
+
getCanvasContext() {
|
|
231
|
+
// #ifdef APP-NVUE
|
|
232
|
+
return this._webViewContext;
|
|
233
|
+
// #endif
|
|
234
|
+
|
|
235
|
+
// #ifdef APP-PLUS
|
|
236
|
+
return uni.createCanvasContext(this.canvasId, this);
|
|
237
|
+
// #endif
|
|
238
|
+
|
|
239
|
+
// #ifdef MP || H5
|
|
240
|
+
return this._canvasElement && typeof this._canvasElement.getContext === 'function'
|
|
241
|
+
? this._canvasElement.getContext('2d')
|
|
242
|
+
: null;
|
|
243
|
+
// #endif
|
|
244
|
+
},
|
|
245
|
+
async setNewSize() {
|
|
246
|
+
const rootNode = await this.getCanvasNode(this.rootId, false);
|
|
247
|
+
if (!rootNode) {
|
|
248
|
+
return;
|
|
249
|
+
}
|
|
250
|
+
if (rootNode.width) {
|
|
251
|
+
this.widthLocal = rootNode.width;
|
|
252
|
+
}
|
|
253
|
+
if (rootNode.height) {
|
|
254
|
+
this.heightLocal = rootNode.height;
|
|
255
|
+
}
|
|
256
|
+
},
|
|
257
|
+
async initCanvas(force = false) {
|
|
258
|
+
if (this._initPromise) {
|
|
259
|
+
return this._initPromise;
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
const initPromise = this._initializeCanvas(force);
|
|
263
|
+
this._initPromise = initPromise;
|
|
264
|
+
|
|
265
|
+
try {
|
|
266
|
+
return await initPromise;
|
|
267
|
+
} finally {
|
|
268
|
+
if (this._initPromise === initPromise) {
|
|
269
|
+
this._initPromise = null;
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
},
|
|
273
|
+
async _initializeCanvas(force = false) {
|
|
274
|
+
try {
|
|
275
|
+
if (this.useRootHeightAndWidth) {
|
|
276
|
+
await this.setNewSize();
|
|
277
|
+
}
|
|
278
|
+
// #ifdef APP-NVUE
|
|
279
|
+
if (typeof createWebViewCanvasBridge === 'function') {
|
|
280
|
+
return await this._initializeWebViewCanvas(force);
|
|
281
|
+
}
|
|
282
|
+
// #endif
|
|
283
|
+
|
|
284
|
+
if (this.ctx && !force) {
|
|
285
|
+
this.$emit('ready', {
|
|
286
|
+
width: this.actualWidth,
|
|
287
|
+
height: this.actualHeight
|
|
288
|
+
});
|
|
289
|
+
return true;
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
this._canvasNode = await this.getCanvasNode(this.canvasId);
|
|
293
|
+
if (!this._canvasNode) {
|
|
294
|
+
return false;
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
this._selectorResult = this._canvasNode;
|
|
298
|
+
this._canvasElement = this._isNvue
|
|
299
|
+
? this._canvasNode
|
|
300
|
+
: (this._canvasNode.node || null);
|
|
301
|
+
const systemInfo = uni.getSystemInfoSync() || {};
|
|
302
|
+
const pixelRatio = Number(systemInfo.pixelRatio) || 1;
|
|
303
|
+
// 微信开发者工具的模拟器预览通常按高密度屏幕缩放,至少使用 2 倍 backing store。
|
|
304
|
+
this.dpr = systemInfo.platform === 'devtools'
|
|
305
|
+
? Math.max(pixelRatio, 2)
|
|
306
|
+
: pixelRatio;
|
|
307
|
+
|
|
308
|
+
// #ifdef MP
|
|
309
|
+
if (this._canvasElement) {
|
|
310
|
+
this._canvasElement.width = Math.ceil(this.actualWidth * this.dpr);
|
|
311
|
+
this._canvasElement.height = Math.ceil(this.actualHeight * this.dpr);
|
|
312
|
+
}
|
|
313
|
+
// #endif
|
|
314
|
+
|
|
315
|
+
this.ctx = this.getCanvasContext();
|
|
316
|
+
if (!this.ctx) {
|
|
317
|
+
return false;
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
// #ifdef MP
|
|
321
|
+
if (typeof this.ctx.setTransform === 'function') {
|
|
322
|
+
this.ctx.setTransform(this.dpr, 0, 0, this.dpr, 0, 0);
|
|
323
|
+
} else if (typeof this.ctx.scale === 'function') {
|
|
324
|
+
this.ctx.scale(this.dpr, this.dpr);
|
|
325
|
+
}
|
|
326
|
+
// #endif
|
|
327
|
+
|
|
328
|
+
this.applyFont();
|
|
329
|
+
this.clearCanvas();
|
|
330
|
+
this.$emit('ready', {
|
|
331
|
+
width: this.actualWidth,
|
|
332
|
+
height: this.actualHeight
|
|
333
|
+
});
|
|
334
|
+
return true;
|
|
335
|
+
} catch (error) {
|
|
336
|
+
console.error('初始化Canvas失败:', error);
|
|
337
|
+
return false;
|
|
338
|
+
}
|
|
339
|
+
},
|
|
340
|
+
async _initializeWebViewCanvas(force = false) {
|
|
341
|
+
this._isNvue = true;
|
|
342
|
+
const systemInfo = uni.getSystemInfoSync() || {};
|
|
343
|
+
this.dpr = Math.max(1, Number(systemInfo.pixelRatio) || 1);
|
|
344
|
+
|
|
345
|
+
if (!this._webViewBridge) {
|
|
346
|
+
this._webViewBridge = createWebViewCanvasBridge({
|
|
347
|
+
getWebView: () => this.$refs.web,
|
|
348
|
+
onTouch: message => this.onWebViewTouch(message),
|
|
349
|
+
onReady: (message) => {
|
|
350
|
+
if (
|
|
351
|
+
this._webViewSessionId &&
|
|
352
|
+
message.sessionId &&
|
|
353
|
+
this._webViewSessionId !== message.sessionId
|
|
354
|
+
) {
|
|
355
|
+
this._webViewInitSignature = null;
|
|
356
|
+
}
|
|
357
|
+
this._webViewSessionId = message.sessionId || null;
|
|
358
|
+
},
|
|
359
|
+
onError: error => console.error('Canvas WebView错误:', error)
|
|
360
|
+
});
|
|
361
|
+
this._webViewContext = createWebViewCanvasContext({
|
|
362
|
+
bridge: this._webViewBridge,
|
|
363
|
+
resolveImage: source => this.resolveNvueImageSource(source),
|
|
364
|
+
measureText: (text, state) => {
|
|
365
|
+
const matched = String(state.font || '').match(/(\d+(?:\.\d+)?)px/);
|
|
366
|
+
const fontSize = matched ? Number(matched[1]) : this.fontSize;
|
|
367
|
+
return { width: String(text).length * fontSize * 0.6 };
|
|
368
|
+
}
|
|
369
|
+
});
|
|
370
|
+
this.ctx = this._webViewContext;
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
await this._webViewBridge.ready();
|
|
374
|
+
const signature = [
|
|
375
|
+
this.actualWidth,
|
|
376
|
+
this.actualHeight,
|
|
377
|
+
this.dpr,
|
|
378
|
+
this.disableScroll ? 1 : 0
|
|
379
|
+
].join(':');
|
|
380
|
+
if (force || this._webViewInitSignature !== signature) {
|
|
381
|
+
await this._webViewBridge.request('init', {
|
|
382
|
+
width: this.actualWidth,
|
|
383
|
+
height: this.actualHeight,
|
|
384
|
+
dpr: this.dpr,
|
|
385
|
+
disableScroll: this.disableScroll
|
|
386
|
+
});
|
|
387
|
+
this._webViewInitSignature = signature;
|
|
388
|
+
this.applyFont();
|
|
389
|
+
await this.clearCanvas();
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
this.$emit('ready', {
|
|
393
|
+
width: this.actualWidth,
|
|
394
|
+
height: this.actualHeight
|
|
395
|
+
});
|
|
396
|
+
return true;
|
|
397
|
+
},
|
|
398
|
+
refresh() {
|
|
399
|
+
return this.initCanvas(true);
|
|
400
|
+
},
|
|
401
|
+
getWidth() {
|
|
402
|
+
return this.actualWidth;
|
|
403
|
+
},
|
|
404
|
+
getHeight() {
|
|
405
|
+
return this.actualHeight;
|
|
406
|
+
},
|
|
407
|
+
clearCanvas() {
|
|
408
|
+
if (!this.ctx) return;
|
|
409
|
+
this.clearRect(0, 0, this.actualWidth, this.actualHeight);
|
|
410
|
+
if (this.bgColor && this.bgColor !== 'transparent') {
|
|
411
|
+
this.beginPath();
|
|
412
|
+
this.rect(0, 0, this.actualWidth, this.actualHeight);
|
|
413
|
+
this.setFillStyle(this.bgColor);
|
|
414
|
+
this.fill();
|
|
415
|
+
}
|
|
416
|
+
return this.draw();
|
|
417
|
+
},
|
|
418
|
+
callContext(method, ...args) {
|
|
419
|
+
if (this.ctx && typeof this.ctx[method] === 'function') {
|
|
420
|
+
return this.ctx[method](...args);
|
|
421
|
+
}
|
|
422
|
+
return undefined;
|
|
423
|
+
},
|
|
424
|
+
rect(x, y, width, height) {
|
|
425
|
+
return this.callContext('rect', x, y, width, height);
|
|
426
|
+
},
|
|
427
|
+
clearRect(x, y, width, height) {
|
|
428
|
+
return this.callContext('clearRect', x, y, width, height);
|
|
429
|
+
},
|
|
430
|
+
fillRect(x, y, width, height) {
|
|
431
|
+
return this.callContext('fillRect', x, y, width, height);
|
|
432
|
+
},
|
|
433
|
+
strokeRect(x, y, width, height) {
|
|
434
|
+
return this.callContext('strokeRect', x, y, width, height);
|
|
435
|
+
},
|
|
436
|
+
fill() {
|
|
437
|
+
return this.callContext('fill');
|
|
438
|
+
},
|
|
439
|
+
stroke() {
|
|
440
|
+
return this.callContext('stroke');
|
|
441
|
+
},
|
|
442
|
+
beginPath() {
|
|
443
|
+
return this.callContext('beginPath');
|
|
444
|
+
},
|
|
445
|
+
closePath() {
|
|
446
|
+
return this.callContext('closePath');
|
|
447
|
+
},
|
|
448
|
+
moveTo(x, y) {
|
|
449
|
+
return this.callContext('moveTo', x, y);
|
|
450
|
+
},
|
|
451
|
+
lineTo(x, y) {
|
|
452
|
+
return this.callContext('lineTo', x, y);
|
|
453
|
+
},
|
|
454
|
+
arc(x, y, radius, startAngle, endAngle, anticlockwise = false) {
|
|
455
|
+
return this.callContext('arc', x, y, radius, startAngle, endAngle, anticlockwise);
|
|
456
|
+
},
|
|
457
|
+
arcTo(x1, y1, x2, y2, radius) {
|
|
458
|
+
return this.callContext('arcTo', x1, y1, x2, y2, radius);
|
|
459
|
+
},
|
|
460
|
+
bezierCurveTo(cp1x, cp1y, cp2x, cp2y, x, y) {
|
|
461
|
+
return this.callContext('bezierCurveTo', cp1x, cp1y, cp2x, cp2y, x, y);
|
|
462
|
+
},
|
|
463
|
+
quadraticCurveTo(cpx, cpy, x, y) {
|
|
464
|
+
return this.callContext('quadraticCurveTo', cpx, cpy, x, y);
|
|
465
|
+
},
|
|
466
|
+
ellipse(x, y, radiusX, radiusY, rotation, startAngle, endAngle, anticlockwise = false) {
|
|
467
|
+
return this.callContext(
|
|
468
|
+
'ellipse',
|
|
469
|
+
x,
|
|
470
|
+
y,
|
|
471
|
+
radiusX,
|
|
472
|
+
radiusY,
|
|
473
|
+
rotation,
|
|
474
|
+
startAngle,
|
|
475
|
+
endAngle,
|
|
476
|
+
anticlockwise
|
|
477
|
+
);
|
|
478
|
+
},
|
|
479
|
+
clip() {
|
|
480
|
+
return this.callContext('clip');
|
|
481
|
+
},
|
|
482
|
+
save() {
|
|
483
|
+
return this.callContext('save');
|
|
484
|
+
},
|
|
485
|
+
restore() {
|
|
486
|
+
return this.callContext('restore');
|
|
487
|
+
},
|
|
488
|
+
translate(x, y) {
|
|
489
|
+
return this.callContext('translate', x, y);
|
|
490
|
+
},
|
|
491
|
+
rotate(angle) {
|
|
492
|
+
return this.callContext('rotate', angle);
|
|
493
|
+
},
|
|
494
|
+
scale(x, y) {
|
|
495
|
+
return this.callContext('scale', x, y);
|
|
496
|
+
},
|
|
497
|
+
setTransform(a, b, c, d, e, f) {
|
|
498
|
+
return this.callContext('setTransform', a, b, c, d, e, f);
|
|
499
|
+
},
|
|
500
|
+
transform(a, b, c, d, e, f) {
|
|
501
|
+
return this.callContext('transform', a, b, c, d, e, f);
|
|
502
|
+
},
|
|
503
|
+
resetTransform() {
|
|
504
|
+
return this.callContext('resetTransform');
|
|
505
|
+
},
|
|
506
|
+
setFillStyle(color) {
|
|
507
|
+
if (!this.ctx) return;
|
|
508
|
+
if (typeof this.ctx.setFillStyle === 'function') {
|
|
509
|
+
this.ctx.setFillStyle(color);
|
|
510
|
+
} else {
|
|
511
|
+
this.ctx.fillStyle = color;
|
|
512
|
+
}
|
|
513
|
+
},
|
|
514
|
+
setStrokeStyle(color) {
|
|
515
|
+
if (!this.ctx) return;
|
|
516
|
+
if (typeof this.ctx.setStrokeStyle === 'function') {
|
|
517
|
+
this.ctx.setStrokeStyle(color);
|
|
518
|
+
} else {
|
|
519
|
+
this.ctx.strokeStyle = color;
|
|
520
|
+
}
|
|
521
|
+
},
|
|
522
|
+
setLineWidth(width) {
|
|
523
|
+
if (!this.ctx) return;
|
|
524
|
+
if (typeof this.ctx.setLineWidth === 'function') {
|
|
525
|
+
this.ctx.setLineWidth(width);
|
|
526
|
+
} else {
|
|
527
|
+
this.ctx.lineWidth = width;
|
|
528
|
+
}
|
|
529
|
+
},
|
|
530
|
+
setLineCap(lineCap = 'round') {
|
|
531
|
+
if (!this.ctx) return;
|
|
532
|
+
if (typeof this.ctx.setLineCap === 'function') {
|
|
533
|
+
this.ctx.setLineCap(lineCap);
|
|
534
|
+
} else {
|
|
535
|
+
this.ctx.lineCap = lineCap;
|
|
536
|
+
}
|
|
537
|
+
},
|
|
538
|
+
setLineJoin(lineJoin = 'round') {
|
|
539
|
+
if (!this.ctx) return;
|
|
540
|
+
if (typeof this.ctx.setLineJoin === 'function') {
|
|
541
|
+
this.ctx.setLineJoin(lineJoin);
|
|
542
|
+
} else {
|
|
543
|
+
this.ctx.lineJoin = lineJoin;
|
|
544
|
+
}
|
|
545
|
+
},
|
|
546
|
+
setMiterLimit(miterLimit) {
|
|
547
|
+
if (!this.ctx) return;
|
|
548
|
+
if (typeof this.ctx.setMiterLimit === 'function') {
|
|
549
|
+
this.ctx.setMiterLimit(miterLimit);
|
|
550
|
+
} else {
|
|
551
|
+
this.ctx.miterLimit = miterLimit;
|
|
552
|
+
}
|
|
553
|
+
},
|
|
554
|
+
setLineDash(segments = []) {
|
|
555
|
+
return this.callContext('setLineDash', segments);
|
|
556
|
+
},
|
|
557
|
+
getLineDash() {
|
|
558
|
+
const result = this.callContext('getLineDash');
|
|
559
|
+
return Array.isArray(result) ? result : [];
|
|
560
|
+
},
|
|
561
|
+
setTextAlign(align = 'left') {
|
|
562
|
+
if (!this.ctx) return;
|
|
563
|
+
if (typeof this.ctx.setTextAlign === 'function') {
|
|
564
|
+
this.ctx.setTextAlign(align);
|
|
565
|
+
} else {
|
|
566
|
+
this.ctx.textAlign = align;
|
|
567
|
+
}
|
|
568
|
+
},
|
|
569
|
+
setTextBaseline(baseline = 'alphabetic') {
|
|
570
|
+
if (!this.ctx) return;
|
|
571
|
+
if (typeof this.ctx.setTextBaseline === 'function') {
|
|
572
|
+
this.ctx.setTextBaseline(baseline);
|
|
573
|
+
} else {
|
|
574
|
+
this.ctx.textBaseline = baseline;
|
|
575
|
+
}
|
|
576
|
+
},
|
|
577
|
+
setFontSize(fontSize) {
|
|
578
|
+
this.fontSize = Number(fontSize) || this.fontSize;
|
|
579
|
+
if (this.ctx && typeof this.ctx.setFontSize === 'function') {
|
|
580
|
+
this.ctx.setFontSize(this.fontSize);
|
|
581
|
+
}
|
|
582
|
+
this.applyFont();
|
|
583
|
+
},
|
|
584
|
+
setFont(font) {
|
|
585
|
+
if (!this.ctx) return;
|
|
586
|
+
if ('font' in this.ctx) {
|
|
587
|
+
this.ctx.font = font;
|
|
588
|
+
return;
|
|
589
|
+
}
|
|
590
|
+
const matched = String(font).match(/(\d+(?:\.\d+)?)px/);
|
|
591
|
+
if (matched) {
|
|
592
|
+
this.fontSize = Number(matched[1]);
|
|
593
|
+
}
|
|
594
|
+
this.applyFont();
|
|
595
|
+
},
|
|
596
|
+
setGlobalAlpha(alpha) {
|
|
597
|
+
if (!this.ctx) return;
|
|
598
|
+
if (typeof this.ctx.setGlobalAlpha === 'function') {
|
|
599
|
+
this.ctx.setGlobalAlpha(alpha);
|
|
600
|
+
} else {
|
|
601
|
+
this.ctx.globalAlpha = alpha;
|
|
602
|
+
}
|
|
603
|
+
},
|
|
604
|
+
setGlobalCompositeOperation(operation) {
|
|
605
|
+
if (!this.ctx) return;
|
|
606
|
+
if (typeof this.ctx.setGlobalCompositeOperation === 'function') {
|
|
607
|
+
this.ctx.setGlobalCompositeOperation(operation);
|
|
608
|
+
} else {
|
|
609
|
+
this.ctx.globalCompositeOperation = operation;
|
|
610
|
+
}
|
|
611
|
+
},
|
|
612
|
+
setShadow(offsetX = 0, offsetY = 0, blur = 0, color = 'rgba(0,0,0,0)') {
|
|
613
|
+
if (!this.ctx) return;
|
|
614
|
+
if (typeof this.ctx.setShadow === 'function') {
|
|
615
|
+
this.ctx.setShadow(offsetX, offsetY, blur, color);
|
|
616
|
+
return;
|
|
617
|
+
}
|
|
618
|
+
this.ctx.shadowOffsetX = offsetX;
|
|
619
|
+
this.ctx.shadowOffsetY = offsetY;
|
|
620
|
+
this.ctx.shadowBlur = blur;
|
|
621
|
+
this.ctx.shadowColor = color;
|
|
622
|
+
},
|
|
623
|
+
setLineStyle(lineColor, lineWidth) {
|
|
624
|
+
this.setLineCap('round');
|
|
625
|
+
this.setLineJoin('round');
|
|
626
|
+
this.setStrokeStyle(lineColor);
|
|
627
|
+
this.setLineWidth(lineWidth);
|
|
628
|
+
},
|
|
629
|
+
applyFont() {
|
|
630
|
+
if (!this.ctx) return;
|
|
631
|
+
const font = `${this.fontWeight === 'normal' ? '' : `${this.fontWeight} `}${this.fontSize}px ${this.fontFamily}`.trim();
|
|
632
|
+
if ('font' in this.ctx) {
|
|
633
|
+
this.ctx.font = font;
|
|
634
|
+
} else if (typeof this.ctx.setFont === 'function') {
|
|
635
|
+
this.ctx.setFont(font);
|
|
636
|
+
} else if (typeof this.ctx.setFontSize === 'function') {
|
|
637
|
+
this.ctx.setFontSize(this.fontSize);
|
|
638
|
+
}
|
|
639
|
+
},
|
|
640
|
+
fillText(text, x, y) {
|
|
641
|
+
return this.callContext('fillText', String(text), x, y);
|
|
642
|
+
},
|
|
643
|
+
strokeText(text, x, y, maxWidth) {
|
|
644
|
+
if (maxWidth === undefined) {
|
|
645
|
+
return this.callContext('strokeText', String(text), x, y);
|
|
646
|
+
}
|
|
647
|
+
return this.callContext('strokeText', String(text), x, y, maxWidth);
|
|
648
|
+
},
|
|
649
|
+
measureText(text) {
|
|
650
|
+
if (this.ctx && typeof this.ctx.measureText === 'function') {
|
|
651
|
+
return this.ctx.measureText(String(text));
|
|
652
|
+
}
|
|
653
|
+
return {
|
|
654
|
+
width: String(text).length * this.fontSize * 0.6
|
|
655
|
+
};
|
|
656
|
+
},
|
|
657
|
+
measureTextAsync(text) {
|
|
658
|
+
if (this.ctx && typeof this.ctx.measureTextAsync === 'function') {
|
|
659
|
+
return this.ctx.measureTextAsync(String(text));
|
|
660
|
+
}
|
|
661
|
+
return Promise.resolve(this.measureText(text));
|
|
662
|
+
},
|
|
663
|
+
createLinearGradient(x0, y0, x1, y1) {
|
|
664
|
+
if (this.ctx && typeof this.ctx.createLinearGradient === 'function') {
|
|
665
|
+
return this.ctx.createLinearGradient(x0, y0, x1, y1);
|
|
666
|
+
}
|
|
667
|
+
return null;
|
|
668
|
+
},
|
|
669
|
+
createRadialGradient(x0, y0, r0, x1, y1, r1) {
|
|
670
|
+
if (this.ctx && typeof this.ctx.createRadialGradient === 'function') {
|
|
671
|
+
return this.ctx.createRadialGradient(x0, y0, r0, x1, y1, r1);
|
|
672
|
+
}
|
|
673
|
+
return null;
|
|
674
|
+
},
|
|
675
|
+
createPattern(image, repetition = 'repeat') {
|
|
676
|
+
if (this.ctx && typeof this.ctx.createPattern === 'function') {
|
|
677
|
+
return this.ctx.createPattern(image, repetition);
|
|
678
|
+
}
|
|
679
|
+
return null;
|
|
680
|
+
},
|
|
681
|
+
async resolveNvueImageSource(src) {
|
|
682
|
+
if (typeof src !== 'string' || src.indexOf('data:image/') === 0) {
|
|
683
|
+
return src;
|
|
684
|
+
}
|
|
685
|
+
if (this._imageDataCache[src]) {
|
|
686
|
+
return this._imageDataCache[src];
|
|
687
|
+
}
|
|
688
|
+
|
|
689
|
+
const task = (async () => {
|
|
690
|
+
const localPath = await new Promise((resolve, reject) => {
|
|
691
|
+
uni.getImageInfo({
|
|
692
|
+
src,
|
|
693
|
+
success: res => resolve(res.path || res.tempFilePath),
|
|
694
|
+
fail: reject
|
|
695
|
+
});
|
|
696
|
+
});
|
|
697
|
+
if (!localPath) {
|
|
698
|
+
throw new Error(`Canvas图片未返回本地路径: ${src}`);
|
|
699
|
+
}
|
|
700
|
+
return this.readNvueFileAsDataURL(localPath);
|
|
701
|
+
})();
|
|
702
|
+
this._imageDataCache[src] = task;
|
|
703
|
+
try {
|
|
704
|
+
const dataUrl = await task;
|
|
705
|
+
this._imageDataCache[src] = dataUrl;
|
|
706
|
+
return dataUrl;
|
|
707
|
+
} catch (error) {
|
|
708
|
+
delete this._imageDataCache[src];
|
|
709
|
+
throw error;
|
|
710
|
+
}
|
|
711
|
+
},
|
|
712
|
+
readNvueFileAsDataURL(path) {
|
|
713
|
+
return new Promise((resolve, reject) => {
|
|
714
|
+
// #ifdef APP-NVUE
|
|
715
|
+
if (typeof plus === 'undefined' || !plus.io) {
|
|
716
|
+
reject(new Error('HTML5+ FileReader不可用'));
|
|
717
|
+
return;
|
|
718
|
+
}
|
|
719
|
+
plus.io.resolveLocalFileSystemURL(path, (entry) => {
|
|
720
|
+
entry.file((file) => {
|
|
721
|
+
const reader = new plus.io.FileReader();
|
|
722
|
+
reader.onloadend = event => resolve(event.target.result);
|
|
723
|
+
reader.onerror = reject;
|
|
724
|
+
reader.readAsDataURL(file);
|
|
725
|
+
}, reject);
|
|
726
|
+
}, reject);
|
|
727
|
+
return;
|
|
728
|
+
// #endif
|
|
729
|
+
|
|
730
|
+
resolve(path);
|
|
731
|
+
});
|
|
732
|
+
},
|
|
733
|
+
loadImage(src) {
|
|
734
|
+
// #ifdef APP-NVUE
|
|
735
|
+
return this.resolveNvueImageSource(src);
|
|
736
|
+
// #endif
|
|
737
|
+
|
|
738
|
+
if (this._imageCache[src]) {
|
|
739
|
+
return Promise.resolve(this._imageCache[src]);
|
|
740
|
+
}
|
|
741
|
+
return new Promise((resolve, reject) => {
|
|
742
|
+
let image = null;
|
|
743
|
+
|
|
744
|
+
// #ifdef MP
|
|
745
|
+
const canvas = this.getCanvasElement();
|
|
746
|
+
if (canvas && typeof canvas.createImage === 'function') {
|
|
747
|
+
image = canvas.createImage();
|
|
748
|
+
}
|
|
749
|
+
// #endif
|
|
750
|
+
|
|
751
|
+
// #ifdef H5
|
|
752
|
+
image = new Image();
|
|
753
|
+
image.crossOrigin = 'anonymous';
|
|
754
|
+
// #endif
|
|
755
|
+
|
|
756
|
+
if (!image) {
|
|
757
|
+
resolve(src);
|
|
758
|
+
return;
|
|
759
|
+
}
|
|
760
|
+
|
|
761
|
+
image.onload = () => {
|
|
762
|
+
this._imageCache[src] = image;
|
|
763
|
+
resolve(image);
|
|
764
|
+
};
|
|
765
|
+
image.onerror = reject;
|
|
766
|
+
image.src = src;
|
|
767
|
+
});
|
|
768
|
+
},
|
|
769
|
+
async drawImage(source, ...args) {
|
|
770
|
+
if (!this.ctx || typeof this.ctx.drawImage !== 'function') {
|
|
771
|
+
return false;
|
|
772
|
+
}
|
|
773
|
+
if (typeof source !== 'string') {
|
|
774
|
+
this.ctx.drawImage(source, ...args);
|
|
775
|
+
return true;
|
|
776
|
+
}
|
|
777
|
+
if (typeof this.ctx.setFillStyle === 'function' && !this._isNvue) {
|
|
778
|
+
this.ctx.drawImage(source, ...args);
|
|
779
|
+
return true;
|
|
780
|
+
}
|
|
781
|
+
const image = await this.loadImage(source);
|
|
782
|
+
this.ctx.drawImage(image, ...args);
|
|
783
|
+
return true;
|
|
784
|
+
},
|
|
785
|
+
draw(isLastDraw = false, callback) {
|
|
786
|
+
if (this.ctx && typeof this.ctx.draw === 'function') {
|
|
787
|
+
return this.ctx.draw(isLastDraw, callback);
|
|
788
|
+
}
|
|
789
|
+
if (typeof callback === 'function') {
|
|
790
|
+
setTimeout(callback, 0);
|
|
791
|
+
}
|
|
792
|
+
return undefined;
|
|
793
|
+
},
|
|
794
|
+
toTempFilePath(options = {}) {
|
|
795
|
+
return new Promise((resolve, reject) => {
|
|
796
|
+
const width = options.width || this.actualWidth;
|
|
797
|
+
const height = options.height || this.actualHeight;
|
|
798
|
+
const hasDestWidth = options.destWidth !== undefined && options.destWidth !== null;
|
|
799
|
+
const hasDestHeight = options.destHeight !== undefined && options.destHeight !== null;
|
|
800
|
+
let destWidth = hasDestWidth ? options.destWidth : width;
|
|
801
|
+
let destHeight = hasDestHeight ? options.destHeight : height;
|
|
802
|
+
|
|
803
|
+
// MP、H5 和 APP-PLUS 保持逻辑绘制坐标,只提升默认导出像素。
|
|
804
|
+
// #ifdef MP || H5 || APP-PLUS
|
|
805
|
+
if (!hasDestWidth) {
|
|
806
|
+
destWidth = Math.round(width * this.dpr);
|
|
807
|
+
}
|
|
808
|
+
if (!hasDestHeight) {
|
|
809
|
+
destHeight = Math.round(height * this.dpr);
|
|
810
|
+
}
|
|
811
|
+
// #endif
|
|
812
|
+
|
|
813
|
+
const defaultFileType = 'png';
|
|
814
|
+
const request = {
|
|
815
|
+
x: options.x || 0,
|
|
816
|
+
y: options.y || 0,
|
|
817
|
+
width,
|
|
818
|
+
height,
|
|
819
|
+
destWidth,
|
|
820
|
+
destHeight,
|
|
821
|
+
fileType: options.fileType === undefined || options.fileType === null
|
|
822
|
+
? defaultFileType
|
|
823
|
+
: options.fileType,
|
|
824
|
+
quality: options.quality === undefined ? 1 : options.quality
|
|
825
|
+
};
|
|
826
|
+
const success = (res) => {
|
|
827
|
+
if (typeof options.success === 'function') options.success(res);
|
|
828
|
+
resolve(res);
|
|
829
|
+
};
|
|
830
|
+
const fail = (err) => {
|
|
831
|
+
if (typeof options.fail === 'function') options.fail(err);
|
|
832
|
+
reject(err);
|
|
833
|
+
};
|
|
834
|
+
const complete = (res) => {
|
|
835
|
+
if (typeof options.complete === 'function') options.complete(res);
|
|
836
|
+
};
|
|
837
|
+
|
|
838
|
+
// #ifdef H5
|
|
839
|
+
const canvas = this.getCanvasElement() || (this.ctx && this.ctx.canvas);
|
|
840
|
+
if (canvas && typeof canvas.toDataURL === 'function') {
|
|
841
|
+
try {
|
|
842
|
+
let exportCanvas = canvas;
|
|
843
|
+
if (
|
|
844
|
+
request.x !== 0 ||
|
|
845
|
+
request.y !== 0 ||
|
|
846
|
+
request.width !== this.actualWidth ||
|
|
847
|
+
request.height !== this.actualHeight ||
|
|
848
|
+
request.destWidth !== request.width ||
|
|
849
|
+
request.destHeight !== request.height
|
|
850
|
+
) {
|
|
851
|
+
exportCanvas = document.createElement('canvas');
|
|
852
|
+
exportCanvas.width = request.destWidth;
|
|
853
|
+
exportCanvas.height = request.destHeight;
|
|
854
|
+
const exportCtx = exportCanvas.getContext('2d');
|
|
855
|
+
exportCtx.drawImage(
|
|
856
|
+
canvas,
|
|
857
|
+
request.x * this.dpr,
|
|
858
|
+
request.y * this.dpr,
|
|
859
|
+
request.width * this.dpr,
|
|
860
|
+
request.height * this.dpr,
|
|
861
|
+
0,
|
|
862
|
+
0,
|
|
863
|
+
request.destWidth,
|
|
864
|
+
request.destHeight
|
|
865
|
+
);
|
|
866
|
+
}
|
|
867
|
+
const mime = request.fileType === 'jpg' || request.fileType === 'jpeg'
|
|
868
|
+
? 'image/jpeg'
|
|
869
|
+
: 'image/png';
|
|
870
|
+
const res = {
|
|
871
|
+
tempFilePath: exportCanvas.toDataURL(mime, request.quality)
|
|
872
|
+
};
|
|
873
|
+
success(res);
|
|
874
|
+
complete(res);
|
|
875
|
+
return;
|
|
876
|
+
} catch (error) {
|
|
877
|
+
fail(error);
|
|
878
|
+
complete(error);
|
|
879
|
+
return;
|
|
880
|
+
}
|
|
881
|
+
}
|
|
882
|
+
// #endif
|
|
883
|
+
|
|
884
|
+
// #ifdef APP-NVUE
|
|
885
|
+
if (this.ctx && typeof this.ctx.toTempFilePath === 'function') {
|
|
886
|
+
Promise.resolve(this.ctx.toTempFilePath(request)).then(
|
|
887
|
+
(res) => {
|
|
888
|
+
success(res);
|
|
889
|
+
complete(res);
|
|
890
|
+
},
|
|
891
|
+
(error) => {
|
|
892
|
+
fail(error);
|
|
893
|
+
complete(error);
|
|
894
|
+
}
|
|
895
|
+
);
|
|
896
|
+
return;
|
|
897
|
+
}
|
|
898
|
+
// #endif
|
|
899
|
+
|
|
900
|
+
const canvasNode = this.getCanvasElement();
|
|
901
|
+
const uniOptions = {
|
|
902
|
+
...request,
|
|
903
|
+
canvasId: this.canvasId,
|
|
904
|
+
success,
|
|
905
|
+
fail,
|
|
906
|
+
complete
|
|
907
|
+
};
|
|
908
|
+
if (canvasNode) {
|
|
909
|
+
uniOptions.canvas = canvasNode;
|
|
910
|
+
}
|
|
911
|
+
uni.canvasToTempFilePath(uniOptions, this);
|
|
912
|
+
});
|
|
913
|
+
},
|
|
914
|
+
async exportImage(fileType = 'png', quality = 1) {
|
|
915
|
+
const res = await this.toTempFilePath({
|
|
916
|
+
fileType,
|
|
917
|
+
quality,
|
|
918
|
+
width: this.actualWidth,
|
|
919
|
+
height: this.actualHeight
|
|
920
|
+
});
|
|
921
|
+
return res.tempFilePath || res.apFilePath;
|
|
922
|
+
},
|
|
923
|
+
getImageData(options = {}) {
|
|
924
|
+
return new Promise((resolve, reject) => {
|
|
925
|
+
const success = (res) => {
|
|
926
|
+
if (typeof options.success === 'function') options.success(res);
|
|
927
|
+
resolve(res);
|
|
928
|
+
};
|
|
929
|
+
const fail = (err) => {
|
|
930
|
+
if (typeof options.fail === 'function') options.fail(err);
|
|
931
|
+
reject(err);
|
|
932
|
+
};
|
|
933
|
+
const complete = (res) => {
|
|
934
|
+
if (typeof options.complete === 'function') options.complete(res);
|
|
935
|
+
};
|
|
936
|
+
// #ifdef APP-NVUE
|
|
937
|
+
if (this.ctx && typeof this.ctx.getImageData === 'function') {
|
|
938
|
+
Promise.resolve(this.ctx.getImageData({
|
|
939
|
+
x: options.x || 0,
|
|
940
|
+
y: options.y || 0,
|
|
941
|
+
width: options.width || this.actualWidth,
|
|
942
|
+
height: options.height || this.actualHeight
|
|
943
|
+
})).then((res) => {
|
|
944
|
+
success(res);
|
|
945
|
+
complete(res);
|
|
946
|
+
}, (error) => {
|
|
947
|
+
fail(error);
|
|
948
|
+
complete(error);
|
|
949
|
+
});
|
|
950
|
+
return;
|
|
951
|
+
}
|
|
952
|
+
// #endif
|
|
953
|
+
|
|
954
|
+
const request = {
|
|
955
|
+
canvasId: this.canvasId,
|
|
956
|
+
x: options.x || 0,
|
|
957
|
+
y: options.y || 0,
|
|
958
|
+
width: options.width || this.actualWidth,
|
|
959
|
+
height: options.height || this.actualHeight,
|
|
960
|
+
success,
|
|
961
|
+
fail,
|
|
962
|
+
complete
|
|
963
|
+
};
|
|
964
|
+
uni.canvasGetImageData(request, this);
|
|
965
|
+
});
|
|
966
|
+
},
|
|
967
|
+
putImageData(options = {}) {
|
|
968
|
+
return new Promise((resolve, reject) => {
|
|
969
|
+
const success = (res) => {
|
|
970
|
+
if (typeof options.success === 'function') options.success(res);
|
|
971
|
+
resolve(res);
|
|
972
|
+
};
|
|
973
|
+
const fail = (err) => {
|
|
974
|
+
if (typeof options.fail === 'function') options.fail(err);
|
|
975
|
+
reject(err);
|
|
976
|
+
};
|
|
977
|
+
const complete = (res) => {
|
|
978
|
+
if (typeof options.complete === 'function') options.complete(res);
|
|
979
|
+
};
|
|
980
|
+
// #ifdef APP-NVUE
|
|
981
|
+
if (this.ctx && typeof this.ctx.putImageData === 'function') {
|
|
982
|
+
Promise.resolve(this.ctx.putImageData({
|
|
983
|
+
x: options.x || 0,
|
|
984
|
+
y: options.y || 0,
|
|
985
|
+
width: options.width || (options.data && options.data.width) || this.actualWidth,
|
|
986
|
+
height: options.height || (options.data && options.data.height) || this.actualHeight,
|
|
987
|
+
data: options.data
|
|
988
|
+
})).then((res) => {
|
|
989
|
+
success(res);
|
|
990
|
+
complete(res);
|
|
991
|
+
}, (error) => {
|
|
992
|
+
fail(error);
|
|
993
|
+
complete(error);
|
|
994
|
+
});
|
|
995
|
+
return;
|
|
996
|
+
}
|
|
997
|
+
// #endif
|
|
998
|
+
|
|
999
|
+
const request = {
|
|
1000
|
+
canvasId: this.canvasId,
|
|
1001
|
+
x: options.x || 0,
|
|
1002
|
+
y: options.y || 0,
|
|
1003
|
+
width: options.width || (options.data && options.data.width) || this.actualWidth,
|
|
1004
|
+
height: options.height || (options.data && options.data.height) || this.actualHeight,
|
|
1005
|
+
data: options.data,
|
|
1006
|
+
success,
|
|
1007
|
+
fail,
|
|
1008
|
+
complete
|
|
1009
|
+
};
|
|
1010
|
+
uni.canvasPutImageData(request, this);
|
|
1011
|
+
});
|
|
1012
|
+
}
|
|
1013
|
+
}
|
|
1014
|
+
};
|
|
1015
|
+
</script>
|
|
1016
|
+
|
|
1017
|
+
<style lang="scss" scoped>
|
|
1018
|
+
.u-canvas {
|
|
1019
|
+
position: relative;
|
|
1020
|
+
overflow: hidden;
|
|
1021
|
+
}
|
|
1022
|
+
|
|
1023
|
+
.u-canvas__canvas {
|
|
1024
|
+
display: block;
|
|
1025
|
+
width: 100%;
|
|
1026
|
+
height: 100%;
|
|
1027
|
+
}
|
|
1028
|
+
</style>
|