@didaoktv/didaoui-uniapp 2.12.0 → 2.14.0
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 +7 -7
- package/components/dd-album/dd-album.vue +280 -228
- package/components/dd-avatar/dd-avatar.vue +132 -75
- package/components/dd-avatar-group/dd-avatar-group.vue +52 -30
- package/components/dd-barcode/dd-barcode.vue +884 -947
- package/components/dd-button/dd-button.vue +47 -21
- package/components/dd-canvas/dd-canvas.vue +1156 -985
- package/components/dd-car-keyboard/dd-car-keyboard.vue +12 -12
- package/components/dd-champion-card/dd-champion-card.vue +49 -21
- package/components/dd-checkbox-group/dd-checkbox-group.vue +2 -0
- package/components/dd-copy/dd-copy.vue +51 -56
- package/components/dd-cropper/dd-cropper.vue +1224 -1135
- package/components/dd-divider/dd-divider.vue +9 -2
- package/components/dd-fold/dd-fold.vue +133 -0
- package/components/dd-form/dd-form.vue +2 -0
- package/components/dd-icon/dd-icon.vue +1 -1
- package/components/dd-keyboard/dd-keyboard.vue +95 -46
- package/components/dd-link/dd-link.vue +72 -49
- package/components/dd-markdown/dd-markdown.vue +154 -154
- package/components/dd-number-keyboard/dd-number-keyboard.vue +109 -94
- package/components/dd-parse/dd-parse.vue +473 -442
- package/components/dd-poster/dd-poster.vue +579 -567
- package/components/dd-pull-refresh/dd-pull-refresh.vue +1 -9
- package/components/dd-qrcode/dd-qrcode.vue +431 -405
- package/components/dd-radio-group/dd-radio-group.vue +1 -1
- package/components/dd-segmented-tab/dd-segmented-tab.vue +22 -14
- package/components/dd-signature/dd-signature.vue +377 -357
- package/components/dd-step/dd-step.vue +1 -1
- package/components/dd-sticky/dd-sticky.vue +5 -10
- package/components/dd-swipe-action/dd-swipe-action.vue +1 -9
- package/components/dd-swipe-item/dd-swipe-item.vue +2 -0
- package/components/dd-swipeable-tab/dd-swipeable-tab.vue +1 -5
- package/components/dd-switch/dd-switch.vue +1 -1
- package/components/dd-text/dd-text.vue +304 -82
- package/components/dd-waterfall/dd-waterfall.vue +89 -0
- package/index.ts +8 -6
- package/libs/function/index.d.ts +88 -0
- package/libs/function/index.js +14 -0
- package/libs/function/test.d.ts +65 -0
- package/package.json +1 -1
- package/scss/_mixins.scss +8 -0
- package/scss/_theme-tokens.scss +7 -0
- package/scss/_variables.scss +283 -156
- package/components/dd-album/album.js +0 -19
- package/components/dd-album/props.js +0 -89
- package/components/dd-avatar/avatar.js +0 -19
- package/components/dd-avatar/props.js +0 -84
- package/components/dd-avatar-group/avatarGroup.js +0 -14
- package/components/dd-avatar-group/props.js +0 -57
- package/components/dd-keyboard/keyboard.js +0 -19
- package/components/dd-keyboard/props.js +0 -79
- package/components/dd-link/link.js +0 -17
- package/components/dd-link/props.js +0 -44
- package/components/dd-number-keyboard/numberKeyboard.js +0 -8
- package/components/dd-number-keyboard/props.js +0 -24
- package/components/dd-parse/props.js +0 -57
- package/components/dd-text/props.js +0 -120
- package/components/dd-text/text.js +0 -30
- package/components/dd-text/value.js +0 -87
- package/scss/_dd-shared.scss +0 -40
|
@@ -48,1032 +48,1204 @@
|
|
|
48
48
|
</view>
|
|
49
49
|
</template>
|
|
50
50
|
|
|
51
|
-
<script>
|
|
51
|
+
<script setup lang="ts">
|
|
52
|
+
import { computed, getCurrentInstance, nextTick, onMounted, onUnmounted, ref, watch } from 'vue';
|
|
52
53
|
// #ifdef APP-NVUE
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
createWebViewCanvasContext
|
|
56
|
-
} from '../../libs/util/app-nvue-webview-canvas.js';
|
|
54
|
+
// @ts-ignore NVUE 画布桥接模块由宿主工程提供(仅条件编译进 APP-NVUE 包)
|
|
55
|
+
import { createWebViewCanvasBridge, createWebViewCanvasContext } from '../../libs/util/app-nvue-webview-canvas.js';
|
|
57
56
|
// #endif
|
|
58
57
|
|
|
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
|
-
|
|
58
|
+
interface Props {
|
|
59
|
+
canvasId?: string;
|
|
60
|
+
width?: string | number;
|
|
61
|
+
height?: string | number;
|
|
62
|
+
unit?: string;
|
|
63
|
+
useRootHeightAndWidth?: boolean;
|
|
64
|
+
bgColor?: string;
|
|
65
|
+
disableScroll?: boolean;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
const props = withDefaults(defineProps<Props>(), {
|
|
69
|
+
canvasId: () => `dd-canvas${Math.floor(Math.random() * 1000000)}`,
|
|
70
|
+
width: 300,
|
|
71
|
+
height: 300,
|
|
72
|
+
unit: 'px',
|
|
73
|
+
useRootHeightAndWidth: false,
|
|
74
|
+
bgColor: '#ffffff',
|
|
75
|
+
disableScroll: false
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
const emit = defineEmits<{
|
|
79
|
+
(e: 'ready', val: { width: number; height: number }): void;
|
|
80
|
+
(e: 'touchstart', val: any): void;
|
|
81
|
+
(e: 'touchmove', val: any): void;
|
|
82
|
+
(e: 'touchend', val: any): void;
|
|
83
|
+
}>();
|
|
84
|
+
|
|
85
|
+
const instance = getCurrentInstance();
|
|
86
|
+
const proxy = instance?.proxy;
|
|
87
|
+
|
|
88
|
+
const rootId = ref(`dd-canvas-root-${Math.floor(Math.random() * 1000000)}`);
|
|
89
|
+
const ctx = ref<any>(null);
|
|
90
|
+
const fontSize = ref(12);
|
|
91
|
+
const fontFamily = ref('sans-serif');
|
|
92
|
+
const fontWeight = ref('normal');
|
|
93
|
+
const dpr = ref(1);
|
|
94
|
+
// APP-NVUE 下的 web-view 画布桥接载体
|
|
95
|
+
const web = ref<any>(null);
|
|
96
|
+
|
|
97
|
+
// 以下为命令式画布状态,与 Options API 时期一致:不走响应式,避免高频重绘的代理开销
|
|
98
|
+
let _canvasNode: any = null;
|
|
99
|
+
let _selectorResult: any = null;
|
|
100
|
+
let _canvasElement: any = null;
|
|
101
|
+
const _imageCache: Record<string, any> = Object.create(null);
|
|
102
|
+
const _imageDataCache: Record<string, any> = Object.create(null);
|
|
103
|
+
let _isNvue = false;
|
|
104
|
+
let _harmonyMeasureUnavailable = false;
|
|
105
|
+
let _initPromise: any = null;
|
|
106
|
+
let _webViewBridge: any = null;
|
|
107
|
+
let _webViewContext: any = null;
|
|
108
|
+
let _webViewInitSignature: any = null;
|
|
109
|
+
let _webViewSessionId: any = null;
|
|
110
|
+
|
|
111
|
+
function parseSize(value: any): number {
|
|
112
|
+
if (typeof value === 'number') {
|
|
113
|
+
return value;
|
|
114
|
+
}
|
|
115
|
+
if (typeof value !== 'string') {
|
|
116
|
+
return 0;
|
|
117
|
+
}
|
|
118
|
+
if (value.endsWith('rpx') || value.endsWith('upx')) {
|
|
119
|
+
return uni.upx2px(parseFloat(value));
|
|
120
|
+
}
|
|
121
|
+
if (value.endsWith('px')) {
|
|
122
|
+
return parseFloat(value) || 0;
|
|
123
|
+
}
|
|
124
|
+
return parseFloat(value) || 0;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
const widthLocal = ref(parseSize(props.width));
|
|
128
|
+
const heightLocal = ref(parseSize(props.height));
|
|
129
|
+
|
|
130
|
+
const actualWidth = computed(() => {
|
|
131
|
+
return props.useRootHeightAndWidth ? widthLocal.value : parseSize(props.width);
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
const actualHeight = computed(() => {
|
|
135
|
+
return props.useRootHeightAndWidth ? heightLocal.value : parseSize(props.height);
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
watch(() => props.width, () => {
|
|
139
|
+
refresh();
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
watch(() => props.height, () => {
|
|
143
|
+
refresh();
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
watch(() => props.bgColor, () => {
|
|
147
|
+
clearCanvas();
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
onMounted(() => {
|
|
151
|
+
nextTick(() => {
|
|
152
|
+
initCanvas();
|
|
153
|
+
});
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
onUnmounted(() => {
|
|
157
|
+
if (_webViewBridge) {
|
|
158
|
+
_webViewBridge.destroy();
|
|
159
|
+
}
|
|
160
|
+
_webViewBridge = null;
|
|
161
|
+
_webViewContext = null;
|
|
162
|
+
ctx.value = null;
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
function onTouchStart(event: any) {
|
|
166
|
+
emit('touchstart', event);
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
function onTouchMove(event: any) {
|
|
170
|
+
emit('touchmove', event);
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
function onTouchEnd(event: any) {
|
|
174
|
+
emit('touchend', event);
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
function onWebViewMessage(event: any) {
|
|
178
|
+
if (_webViewBridge) {
|
|
179
|
+
_webViewBridge.handleMessage(event);
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
function onWebViewTouch(message: any) {
|
|
184
|
+
const eventType = message.eventType;
|
|
185
|
+
if (!['touchstart', 'touchmove', 'touchend'].includes(eventType)) return;
|
|
186
|
+
(emit as any)(eventType, {
|
|
187
|
+
type: eventType,
|
|
188
|
+
detail: {
|
|
189
|
+
x: message.x,
|
|
190
|
+
y: message.y
|
|
191
|
+
},
|
|
192
|
+
touches: message.touches || [],
|
|
193
|
+
changedTouches: message.changedTouches || [],
|
|
194
|
+
canvasWidth: message.canvasWidth,
|
|
195
|
+
canvasHeight: message.canvasHeight
|
|
196
|
+
});
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
function getCanvasNode(id = props.canvasId, isCanvas = true): Promise<any> {
|
|
200
|
+
return new Promise((resolve) => {
|
|
201
|
+
try {
|
|
202
|
+
uni.createSelectorQuery()
|
|
203
|
+
.in(proxy)
|
|
204
|
+
.select(`#${id}`)
|
|
205
|
+
.fields(
|
|
206
|
+
{
|
|
207
|
+
node: isCanvas,
|
|
208
|
+
size: true
|
|
209
|
+
},
|
|
210
|
+
(res: any) => {
|
|
211
|
+
// #ifdef APP-PLUS || APP-HARMONY
|
|
212
|
+
resolve(res || {
|
|
213
|
+
width: actualWidth.value,
|
|
214
|
+
height: actualHeight.value
|
|
215
|
+
});
|
|
216
|
+
return;
|
|
217
|
+
// #endif
|
|
218
|
+
|
|
219
|
+
resolve(res || false);
|
|
220
|
+
}
|
|
221
|
+
)
|
|
222
|
+
.exec();
|
|
223
|
+
} catch (error) {
|
|
224
|
+
console.error('获取画布节点失败:', error);
|
|
225
|
+
resolve(false);
|
|
90
226
|
}
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
227
|
+
});
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
function getCanvasElement() {
|
|
231
|
+
return _canvasElement || null;
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
function getRawContext() {
|
|
235
|
+
return ctx.value;
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
function getCanvasContext(): any {
|
|
239
|
+
// #ifdef APP-NVUE
|
|
240
|
+
return _webViewContext;
|
|
241
|
+
// #endif
|
|
242
|
+
|
|
243
|
+
// #ifdef APP-PLUS || APP-HARMONY
|
|
244
|
+
return uni.createCanvasContext(props.canvasId, proxy);
|
|
245
|
+
// #endif
|
|
246
|
+
|
|
247
|
+
// #ifdef MP || H5
|
|
248
|
+
return _canvasElement && typeof _canvasElement.getContext === 'function'
|
|
249
|
+
? _canvasElement.getContext('2d')
|
|
250
|
+
: null;
|
|
251
|
+
// #endif
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
async function setNewSize() {
|
|
255
|
+
const rootNode = await getCanvasNode(rootId.value, false);
|
|
256
|
+
if (!rootNode) {
|
|
257
|
+
return;
|
|
258
|
+
}
|
|
259
|
+
if (rootNode.width) {
|
|
260
|
+
widthLocal.value = rootNode.width;
|
|
261
|
+
}
|
|
262
|
+
if (rootNode.height) {
|
|
263
|
+
heightLocal.value = rootNode.height;
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
async function initCanvas(force = false) {
|
|
268
|
+
if (_initPromise) {
|
|
269
|
+
return _initPromise;
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
const initPromise = _initializeCanvas(force);
|
|
273
|
+
_initPromise = initPromise;
|
|
274
|
+
|
|
275
|
+
try {
|
|
276
|
+
return await initPromise;
|
|
277
|
+
} finally {
|
|
278
|
+
if (_initPromise === initPromise) {
|
|
279
|
+
_initPromise = null;
|
|
110
280
|
}
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
},
|
|
119
|
-
bgColor() {
|
|
120
|
-
this.clearCanvas();
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
async function _initializeCanvas(force = false) {
|
|
285
|
+
try {
|
|
286
|
+
if (props.useRootHeightAndWidth) {
|
|
287
|
+
await setNewSize();
|
|
121
288
|
}
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
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();
|
|
289
|
+
// #ifdef APP-NVUE
|
|
290
|
+
if (typeof createWebViewCanvasBridge === 'function') {
|
|
291
|
+
return await _initializeWebViewCanvas(force);
|
|
144
292
|
}
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
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 || APP-HARMONY
|
|
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
|
-
}
|
|
293
|
+
// #endif
|
|
294
|
+
|
|
295
|
+
if (ctx.value && !force) {
|
|
296
|
+
emit('ready', {
|
|
297
|
+
width: actualWidth.value,
|
|
298
|
+
height: actualHeight.value
|
|
222
299
|
});
|
|
223
|
-
|
|
224
|
-
|
|
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 || APP-HARMONY
|
|
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
|
-
}
|
|
300
|
+
return true;
|
|
301
|
+
}
|
|
261
302
|
|
|
262
|
-
|
|
263
|
-
|
|
303
|
+
_canvasNode = await getCanvasNode(props.canvasId);
|
|
304
|
+
if (!_canvasNode) {
|
|
305
|
+
return false;
|
|
306
|
+
}
|
|
264
307
|
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
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
|
-
}
|
|
308
|
+
_selectorResult = _canvasNode;
|
|
309
|
+
_canvasElement = _isNvue
|
|
310
|
+
? _canvasNode
|
|
311
|
+
: (_canvasNode.node || null);
|
|
312
|
+
const systemInfo: any = uni.getSystemInfoSync() || {};
|
|
313
|
+
const pixelRatio = Number(systemInfo.pixelRatio) || 1;
|
|
314
|
+
// 微信开发者工具的模拟器预览通常按高密度屏幕缩放,至少使用 2 倍 backing store。
|
|
315
|
+
dpr.value = systemInfo.platform === 'devtools'
|
|
316
|
+
? Math.max(pixelRatio, 2)
|
|
317
|
+
: pixelRatio;
|
|
291
318
|
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
319
|
+
// #ifdef MP
|
|
320
|
+
if (_canvasElement) {
|
|
321
|
+
_canvasElement.width = Math.ceil(actualWidth.value * dpr.value);
|
|
322
|
+
_canvasElement.height = Math.ceil(actualHeight.value * dpr.value);
|
|
323
|
+
}
|
|
324
|
+
// #endif
|
|
296
325
|
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
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
|
|
326
|
+
ctx.value = getCanvasContext();
|
|
327
|
+
if (!ctx.value) {
|
|
328
|
+
return false;
|
|
329
|
+
}
|
|
314
330
|
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
331
|
+
// #ifdef MP
|
|
332
|
+
if (typeof ctx.value.setTransform === 'function') {
|
|
333
|
+
ctx.value.setTransform(dpr.value, 0, 0, dpr.value, 0, 0);
|
|
334
|
+
} else if (typeof ctx.value.scale === 'function') {
|
|
335
|
+
ctx.value.scale(dpr.value, dpr.value);
|
|
336
|
+
}
|
|
337
|
+
// #endif
|
|
338
|
+
|
|
339
|
+
applyFont();
|
|
340
|
+
clearCanvas();
|
|
341
|
+
emit('ready', {
|
|
342
|
+
width: actualWidth.value,
|
|
343
|
+
height: actualHeight.value
|
|
344
|
+
});
|
|
345
|
+
return true;
|
|
346
|
+
} catch (error) {
|
|
347
|
+
console.error('初始化Canvas失败:', error);
|
|
348
|
+
return false;
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
async function _initializeWebViewCanvas(force = false) {
|
|
353
|
+
_isNvue = true;
|
|
354
|
+
const systemInfo: any = uni.getSystemInfoSync() || {};
|
|
355
|
+
dpr.value = Math.max(1, Number(systemInfo.pixelRatio) || 1);
|
|
319
356
|
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
357
|
+
if (!_webViewBridge) {
|
|
358
|
+
_webViewBridge = createWebViewCanvasBridge({
|
|
359
|
+
getWebView: () => web.value,
|
|
360
|
+
onTouch: (message: any) => onWebViewTouch(message),
|
|
361
|
+
onReady: (message: any) => {
|
|
362
|
+
if (
|
|
363
|
+
_webViewSessionId &&
|
|
364
|
+
message.sessionId &&
|
|
365
|
+
_webViewSessionId !== message.sessionId
|
|
366
|
+
) {
|
|
367
|
+
_webViewInitSignature = null;
|
|
325
368
|
}
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
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: this.estimateTextWidth(text, fontSize) };
|
|
368
|
-
}
|
|
369
|
-
});
|
|
370
|
-
this.ctx = this._webViewContext;
|
|
369
|
+
_webViewSessionId = message.sessionId || null;
|
|
370
|
+
},
|
|
371
|
+
onError: (error: any) => console.error('Canvas WebView错误:', error)
|
|
372
|
+
});
|
|
373
|
+
_webViewContext = createWebViewCanvasContext({
|
|
374
|
+
bridge: _webViewBridge,
|
|
375
|
+
resolveImage: (source: any) => resolveNvueImageSource(source),
|
|
376
|
+
measureText: (text: any, state: any) => {
|
|
377
|
+
const matched = String(state.font || '').match(/(\d+(?:\.\d+)?)px/);
|
|
378
|
+
const fontSizeValue = matched ? Number(matched[1]) : fontSize.value;
|
|
379
|
+
return { width: estimateTextWidth(text, fontSizeValue) };
|
|
371
380
|
}
|
|
381
|
+
});
|
|
382
|
+
ctx.value = _webViewContext;
|
|
383
|
+
}
|
|
372
384
|
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
385
|
+
await _webViewBridge.ready();
|
|
386
|
+
const signature = [
|
|
387
|
+
actualWidth.value,
|
|
388
|
+
actualHeight.value,
|
|
389
|
+
dpr.value,
|
|
390
|
+
props.disableScroll ? 1 : 0
|
|
391
|
+
].join(':');
|
|
392
|
+
if (force || _webViewInitSignature !== signature) {
|
|
393
|
+
await _webViewBridge.request('init', {
|
|
394
|
+
width: actualWidth.value,
|
|
395
|
+
height: actualHeight.value,
|
|
396
|
+
dpr: dpr.value,
|
|
397
|
+
disableScroll: props.disableScroll
|
|
398
|
+
});
|
|
399
|
+
_webViewInitSignature = signature;
|
|
400
|
+
applyFont();
|
|
401
|
+
await clearCanvas();
|
|
402
|
+
}
|
|
391
403
|
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
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
|
-
/**
|
|
650
|
-
* 按字形宽度估算文本宽度
|
|
651
|
-
* @description 当平台无法给出真实测量值时的兜底。必须区分全角/半角:
|
|
652
|
-
* 汉字、假名、全角标点约占一个字号,按半角的 0.6 估算会短 40%,
|
|
653
|
-
* 中文海报因此算出"整段都放得下"而不换行。
|
|
654
|
-
* @param {String} text 待测量文本
|
|
655
|
-
* @param {Number} [fontSize] 字号,默认取组件当前字号
|
|
656
|
-
* @returns {Number} 估算宽度(px)
|
|
657
|
-
* @author jry ijry@qq.com
|
|
658
|
-
*/
|
|
659
|
-
estimateTextWidth(text, fontSize) {
|
|
660
|
-
const size = Number(fontSize) || Number(this.fontSize) || 12;
|
|
661
|
-
const FULL_WIDTH = /[ᄀ-ᅟ⺀-〾ぁ-㏿㐀-䶿一-鿿ꀀ-가-힣豈-︰--⦆¢-₩]/;
|
|
662
|
-
return Array.from(String(text)).reduce((width, char) => {
|
|
663
|
-
if (FULL_WIDTH.test(char)) return width + size;
|
|
664
|
-
if (/\s/.test(char)) return width + size * 0.28;
|
|
665
|
-
return width + size * 0.56;
|
|
666
|
-
}, 0);
|
|
667
|
-
},
|
|
668
|
-
measureText(text) {
|
|
669
|
-
if (this.ctx && typeof this.ctx.measureText === 'function') {
|
|
670
|
-
const metrics = this.ctx.measureText(String(text));
|
|
671
|
-
const width = Number(metrics && metrics.width);
|
|
672
|
-
// 鸿蒙同步测量恒为 0(真实值只走 callback),上下文未就绪时其他
|
|
673
|
-
// 平台也可能返回 0。绝不能把 0 交给调用方——换行逻辑会把它当成
|
|
674
|
-
// "宽度足够",于是整段文本挤成一行。
|
|
675
|
-
if (width > 0) return metrics;
|
|
676
|
-
}
|
|
677
|
-
return {
|
|
678
|
-
width: this.estimateTextWidth(text)
|
|
679
|
-
};
|
|
680
|
-
},
|
|
681
|
-
measureTextAsync(text) {
|
|
682
|
-
if (this.ctx && typeof this.ctx.measureTextAsync === 'function') {
|
|
683
|
-
return this.ctx.measureTextAsync(String(text));
|
|
684
|
-
}
|
|
404
|
+
emit('ready', {
|
|
405
|
+
width: actualWidth.value,
|
|
406
|
+
height: actualHeight.value
|
|
407
|
+
});
|
|
408
|
+
return true;
|
|
409
|
+
}
|
|
685
410
|
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
if (this.ctx && typeof this.ctx.measureText === 'function' && !this._harmonyMeasureUnavailable) {
|
|
690
|
-
return new Promise((resolve) => {
|
|
691
|
-
let settled = false;
|
|
692
|
-
// evalJSAsync 并非所有鸿蒙版本都提供,回调可能永远不来,
|
|
693
|
-
// 必须兜底避免上层 await 永久挂起。且一旦超时就标记为不可用:
|
|
694
|
-
// 单次换行要二分测量十几次,逐次干等会把导出拖成十几秒。
|
|
695
|
-
const timer = setTimeout(() => {
|
|
696
|
-
if (settled) return;
|
|
697
|
-
settled = true;
|
|
698
|
-
this._harmonyMeasureUnavailable = true;
|
|
699
|
-
resolve({ width: this.estimateTextWidth(text) });
|
|
700
|
-
}, 300);
|
|
701
|
-
const done = (metrics) => {
|
|
702
|
-
if (settled) return;
|
|
703
|
-
settled = true;
|
|
704
|
-
clearTimeout(timer);
|
|
705
|
-
const width = Number(metrics && metrics.width);
|
|
706
|
-
resolve({ width: width > 0 ? width : this.estimateTextWidth(text) });
|
|
707
|
-
};
|
|
708
|
-
try {
|
|
709
|
-
const sync = this.ctx.measureText(String(text), done);
|
|
710
|
-
// 若该平台其实同步返回了有效值,直接采用
|
|
711
|
-
const syncWidth = Number(sync && sync.width);
|
|
712
|
-
if (syncWidth > 0) done(sync);
|
|
713
|
-
} catch (error) {
|
|
714
|
-
done(null);
|
|
715
|
-
}
|
|
716
|
-
});
|
|
717
|
-
}
|
|
718
|
-
// #endif
|
|
411
|
+
function refresh() {
|
|
412
|
+
return initCanvas(true);
|
|
413
|
+
}
|
|
719
414
|
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
if (this.ctx && typeof this.ctx.createLinearGradient === 'function') {
|
|
724
|
-
return this.ctx.createLinearGradient(x0, y0, x1, y1);
|
|
725
|
-
}
|
|
726
|
-
return null;
|
|
727
|
-
},
|
|
728
|
-
createRadialGradient(x0, y0, r0, x1, y1, r1) {
|
|
729
|
-
if (this.ctx && typeof this.ctx.createRadialGradient === 'function') {
|
|
730
|
-
return this.ctx.createRadialGradient(x0, y0, r0, x1, y1, r1);
|
|
731
|
-
}
|
|
732
|
-
return null;
|
|
733
|
-
},
|
|
734
|
-
createPattern(image, repetition = 'repeat') {
|
|
735
|
-
if (this.ctx && typeof this.ctx.createPattern === 'function') {
|
|
736
|
-
return this.ctx.createPattern(image, repetition);
|
|
737
|
-
}
|
|
738
|
-
return null;
|
|
739
|
-
},
|
|
740
|
-
async resolveNvueImageSource(src) {
|
|
741
|
-
if (typeof src !== 'string' || src.indexOf('data:image/') === 0) {
|
|
742
|
-
return src;
|
|
743
|
-
}
|
|
744
|
-
if (this._imageDataCache[src]) {
|
|
745
|
-
return this._imageDataCache[src];
|
|
746
|
-
}
|
|
415
|
+
function getWidth() {
|
|
416
|
+
return actualWidth.value;
|
|
417
|
+
}
|
|
747
418
|
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
419
|
+
function getHeight() {
|
|
420
|
+
return actualHeight.value;
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
function clearCanvas() {
|
|
424
|
+
if (!ctx.value) return;
|
|
425
|
+
clearRect(0, 0, actualWidth.value, actualHeight.value);
|
|
426
|
+
if (props.bgColor && props.bgColor !== 'transparent') {
|
|
427
|
+
beginPath();
|
|
428
|
+
rect(0, 0, actualWidth.value, actualHeight.value);
|
|
429
|
+
setFillStyle(props.bgColor);
|
|
430
|
+
fill();
|
|
431
|
+
}
|
|
432
|
+
return draw();
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
function callContext(method: string, ...args: any[]) {
|
|
436
|
+
if (ctx.value && typeof ctx.value[method] === 'function') {
|
|
437
|
+
return ctx.value[method](...args);
|
|
438
|
+
}
|
|
439
|
+
return undefined;
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
function rect(x: number, y: number, width: number, height: number) {
|
|
443
|
+
return callContext('rect', x, y, width, height);
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
function clearRect(x: number, y: number, width: number, height: number) {
|
|
447
|
+
return callContext('clearRect', x, y, width, height);
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
function fillRect(x: number, y: number, width: number, height: number) {
|
|
451
|
+
return callContext('fillRect', x, y, width, height);
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
function strokeRect(x: number, y: number, width: number, height: number) {
|
|
455
|
+
return callContext('strokeRect', x, y, width, height);
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
function fill() {
|
|
459
|
+
return callContext('fill');
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
function stroke() {
|
|
463
|
+
return callContext('stroke');
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
function beginPath() {
|
|
467
|
+
return callContext('beginPath');
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
function closePath() {
|
|
471
|
+
return callContext('closePath');
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
function moveTo(x: number, y: number) {
|
|
475
|
+
return callContext('moveTo', x, y);
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
function lineTo(x: number, y: number) {
|
|
479
|
+
return callContext('lineTo', x, y);
|
|
480
|
+
}
|
|
481
|
+
|
|
482
|
+
function arc(x: number, y: number, radius: number, startAngle: number, endAngle: number, anticlockwise = false) {
|
|
483
|
+
return callContext('arc', x, y, radius, startAngle, endAngle, anticlockwise);
|
|
484
|
+
}
|
|
485
|
+
|
|
486
|
+
function arcTo(x1: number, y1: number, x2: number, y2: number, radius: number) {
|
|
487
|
+
return callContext('arcTo', x1, y1, x2, y2, radius);
|
|
488
|
+
}
|
|
489
|
+
|
|
490
|
+
function bezierCurveTo(cp1x: number, cp1y: number, cp2x: number, cp2y: number, x: number, y: number) {
|
|
491
|
+
return callContext('bezierCurveTo', cp1x, cp1y, cp2x, cp2y, x, y);
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
function quadraticCurveTo(cpx: number, cpy: number, x: number, y: number) {
|
|
495
|
+
return callContext('quadraticCurveTo', cpx, cpy, x, y);
|
|
496
|
+
}
|
|
497
|
+
|
|
498
|
+
function ellipse(
|
|
499
|
+
x: number,
|
|
500
|
+
y: number,
|
|
501
|
+
radiusX: number,
|
|
502
|
+
radiusY: number,
|
|
503
|
+
rotation: number,
|
|
504
|
+
startAngle: number,
|
|
505
|
+
endAngle: number,
|
|
506
|
+
anticlockwise = false
|
|
507
|
+
) {
|
|
508
|
+
return callContext(
|
|
509
|
+
'ellipse',
|
|
510
|
+
x,
|
|
511
|
+
y,
|
|
512
|
+
radiusX,
|
|
513
|
+
radiusY,
|
|
514
|
+
rotation,
|
|
515
|
+
startAngle,
|
|
516
|
+
endAngle,
|
|
517
|
+
anticlockwise
|
|
518
|
+
);
|
|
519
|
+
}
|
|
520
|
+
|
|
521
|
+
function clip() {
|
|
522
|
+
return callContext('clip');
|
|
523
|
+
}
|
|
524
|
+
|
|
525
|
+
function save() {
|
|
526
|
+
return callContext('save');
|
|
527
|
+
}
|
|
528
|
+
|
|
529
|
+
function restore() {
|
|
530
|
+
return callContext('restore');
|
|
531
|
+
}
|
|
532
|
+
|
|
533
|
+
function translate(x: number, y: number) {
|
|
534
|
+
return callContext('translate', x, y);
|
|
535
|
+
}
|
|
536
|
+
|
|
537
|
+
function rotate(angle: number) {
|
|
538
|
+
return callContext('rotate', angle);
|
|
539
|
+
}
|
|
540
|
+
|
|
541
|
+
function scale(x: number, y: number) {
|
|
542
|
+
return callContext('scale', x, y);
|
|
543
|
+
}
|
|
544
|
+
|
|
545
|
+
function setTransform(a: number, b: number, c: number, d: number, e: number, f: number) {
|
|
546
|
+
return callContext('setTransform', a, b, c, d, e, f);
|
|
547
|
+
}
|
|
548
|
+
|
|
549
|
+
function transform(a: number, b: number, c: number, d: number, e: number, f: number) {
|
|
550
|
+
return callContext('transform', a, b, c, d, e, f);
|
|
551
|
+
}
|
|
552
|
+
|
|
553
|
+
function resetTransform() {
|
|
554
|
+
return callContext('resetTransform');
|
|
555
|
+
}
|
|
556
|
+
|
|
557
|
+
function setFillStyle(color: any) {
|
|
558
|
+
if (!ctx.value) return;
|
|
559
|
+
if (typeof ctx.value.setFillStyle === 'function') {
|
|
560
|
+
ctx.value.setFillStyle(color);
|
|
561
|
+
} else {
|
|
562
|
+
ctx.value.fillStyle = color;
|
|
563
|
+
}
|
|
564
|
+
}
|
|
565
|
+
|
|
566
|
+
function setStrokeStyle(color: any) {
|
|
567
|
+
if (!ctx.value) return;
|
|
568
|
+
if (typeof ctx.value.setStrokeStyle === 'function') {
|
|
569
|
+
ctx.value.setStrokeStyle(color);
|
|
570
|
+
} else {
|
|
571
|
+
ctx.value.strokeStyle = color;
|
|
572
|
+
}
|
|
573
|
+
}
|
|
574
|
+
|
|
575
|
+
function setLineWidth(width: number) {
|
|
576
|
+
if (!ctx.value) return;
|
|
577
|
+
if (typeof ctx.value.setLineWidth === 'function') {
|
|
578
|
+
ctx.value.setLineWidth(width);
|
|
579
|
+
} else {
|
|
580
|
+
ctx.value.lineWidth = width;
|
|
581
|
+
}
|
|
582
|
+
}
|
|
583
|
+
|
|
584
|
+
function setLineCap(lineCap: any = 'round') {
|
|
585
|
+
if (!ctx.value) return;
|
|
586
|
+
if (typeof ctx.value.setLineCap === 'function') {
|
|
587
|
+
ctx.value.setLineCap(lineCap);
|
|
588
|
+
} else {
|
|
589
|
+
ctx.value.lineCap = lineCap;
|
|
590
|
+
}
|
|
591
|
+
}
|
|
592
|
+
|
|
593
|
+
function setLineJoin(lineJoin: any = 'round') {
|
|
594
|
+
if (!ctx.value) return;
|
|
595
|
+
if (typeof ctx.value.setLineJoin === 'function') {
|
|
596
|
+
ctx.value.setLineJoin(lineJoin);
|
|
597
|
+
} else {
|
|
598
|
+
ctx.value.lineJoin = lineJoin;
|
|
599
|
+
}
|
|
600
|
+
}
|
|
601
|
+
|
|
602
|
+
function setMiterLimit(miterLimit: number) {
|
|
603
|
+
if (!ctx.value) return;
|
|
604
|
+
if (typeof ctx.value.setMiterLimit === 'function') {
|
|
605
|
+
ctx.value.setMiterLimit(miterLimit);
|
|
606
|
+
} else {
|
|
607
|
+
ctx.value.miterLimit = miterLimit;
|
|
608
|
+
}
|
|
609
|
+
}
|
|
610
|
+
|
|
611
|
+
function setLineDash(segments: any[] = []) {
|
|
612
|
+
return callContext('setLineDash', segments);
|
|
613
|
+
}
|
|
614
|
+
|
|
615
|
+
function getLineDash(): any[] {
|
|
616
|
+
const result = callContext('getLineDash');
|
|
617
|
+
return Array.isArray(result) ? result : [];
|
|
618
|
+
}
|
|
619
|
+
|
|
620
|
+
function setTextAlign(align: any = 'left') {
|
|
621
|
+
if (!ctx.value) return;
|
|
622
|
+
if (typeof ctx.value.setTextAlign === 'function') {
|
|
623
|
+
ctx.value.setTextAlign(align);
|
|
624
|
+
} else {
|
|
625
|
+
ctx.value.textAlign = align;
|
|
626
|
+
}
|
|
627
|
+
}
|
|
628
|
+
|
|
629
|
+
function setTextBaseline(baseline: any = 'alphabetic') {
|
|
630
|
+
if (!ctx.value) return;
|
|
631
|
+
if (typeof ctx.value.setTextBaseline === 'function') {
|
|
632
|
+
ctx.value.setTextBaseline(baseline);
|
|
633
|
+
} else {
|
|
634
|
+
ctx.value.textBaseline = baseline;
|
|
635
|
+
}
|
|
636
|
+
}
|
|
637
|
+
|
|
638
|
+
function setFontSize(fontSizeValue: any) {
|
|
639
|
+
fontSize.value = Number(fontSizeValue) || fontSize.value;
|
|
640
|
+
if (ctx.value && typeof ctx.value.setFontSize === 'function') {
|
|
641
|
+
ctx.value.setFontSize(fontSize.value);
|
|
642
|
+
}
|
|
643
|
+
applyFont();
|
|
644
|
+
}
|
|
645
|
+
|
|
646
|
+
function setFont(font: string) {
|
|
647
|
+
if (!ctx.value) return;
|
|
648
|
+
if ('font' in ctx.value) {
|
|
649
|
+
ctx.value.font = font;
|
|
650
|
+
return;
|
|
651
|
+
}
|
|
652
|
+
const matched = String(font).match(/(\d+(?:\.\d+)?)px/);
|
|
653
|
+
if (matched) {
|
|
654
|
+
fontSize.value = Number(matched[1]);
|
|
655
|
+
}
|
|
656
|
+
applyFont();
|
|
657
|
+
}
|
|
658
|
+
|
|
659
|
+
function setGlobalAlpha(alpha: number) {
|
|
660
|
+
if (!ctx.value) return;
|
|
661
|
+
if (typeof ctx.value.setGlobalAlpha === 'function') {
|
|
662
|
+
ctx.value.setGlobalAlpha(alpha);
|
|
663
|
+
} else {
|
|
664
|
+
ctx.value.globalAlpha = alpha;
|
|
665
|
+
}
|
|
666
|
+
}
|
|
667
|
+
|
|
668
|
+
function setGlobalCompositeOperation(operation: string) {
|
|
669
|
+
if (!ctx.value) return;
|
|
670
|
+
if (typeof ctx.value.setGlobalCompositeOperation === 'function') {
|
|
671
|
+
ctx.value.setGlobalCompositeOperation(operation);
|
|
672
|
+
} else {
|
|
673
|
+
ctx.value.globalCompositeOperation = operation;
|
|
674
|
+
}
|
|
675
|
+
}
|
|
676
|
+
|
|
677
|
+
function setShadow(offsetX = 0, offsetY = 0, blur = 0, color = 'rgba(0,0,0,0)') {
|
|
678
|
+
if (!ctx.value) return;
|
|
679
|
+
if (typeof ctx.value.setShadow === 'function') {
|
|
680
|
+
ctx.value.setShadow(offsetX, offsetY, blur, color);
|
|
681
|
+
return;
|
|
682
|
+
}
|
|
683
|
+
ctx.value.shadowOffsetX = offsetX;
|
|
684
|
+
ctx.value.shadowOffsetY = offsetY;
|
|
685
|
+
ctx.value.shadowBlur = blur;
|
|
686
|
+
ctx.value.shadowColor = color;
|
|
687
|
+
}
|
|
688
|
+
|
|
689
|
+
function setLineStyle(lineColor: any, lineWidth: number) {
|
|
690
|
+
setLineCap('round');
|
|
691
|
+
setLineJoin('round');
|
|
692
|
+
setStrokeStyle(lineColor);
|
|
693
|
+
setLineWidth(lineWidth);
|
|
694
|
+
}
|
|
695
|
+
|
|
696
|
+
function applyFont() {
|
|
697
|
+
if (!ctx.value) return;
|
|
698
|
+
const font = `${fontWeight.value === 'normal' ? '' : `${fontWeight.value} `}${fontSize.value}px ${fontFamily.value}`.trim();
|
|
699
|
+
if ('font' in ctx.value) {
|
|
700
|
+
ctx.value.font = font;
|
|
701
|
+
} else if (typeof ctx.value.setFont === 'function') {
|
|
702
|
+
ctx.value.setFont(font);
|
|
703
|
+
} else if (typeof ctx.value.setFontSize === 'function') {
|
|
704
|
+
ctx.value.setFontSize(fontSize.value);
|
|
705
|
+
}
|
|
706
|
+
}
|
|
707
|
+
|
|
708
|
+
function fillText(text: any, x: number, y: number) {
|
|
709
|
+
return callContext('fillText', String(text), x, y);
|
|
710
|
+
}
|
|
711
|
+
|
|
712
|
+
function strokeText(text: any, x: number, y: number, maxWidth?: number) {
|
|
713
|
+
if (maxWidth === undefined) {
|
|
714
|
+
return callContext('strokeText', String(text), x, y);
|
|
715
|
+
}
|
|
716
|
+
return callContext('strokeText', String(text), x, y, maxWidth);
|
|
717
|
+
}
|
|
718
|
+
|
|
719
|
+
/**
|
|
720
|
+
* 按字形宽度估算文本宽度
|
|
721
|
+
* @description 当平台无法给出真实测量值时的兜底。必须区分全角/半角:
|
|
722
|
+
* 汉字、假名、全角标点约占一个字号,按半角的 0.6 估算会短 40%,
|
|
723
|
+
* 中文海报因此算出"整段都放得下"而不换行。
|
|
724
|
+
* @param {String} text 待测量文本
|
|
725
|
+
* @param {Number} [fontSize] 字号,默认取组件当前字号
|
|
726
|
+
* @returns {Number} 估算宽度(px)
|
|
727
|
+
* @author jry ijry@qq.com
|
|
728
|
+
*/
|
|
729
|
+
function estimateTextWidth(text: any, fontSizePx?: any) {
|
|
730
|
+
const size = Number(fontSizePx) || Number(fontSize.value) || 12;
|
|
731
|
+
const FULL_WIDTH = /[ᄀ-ᅟ⺀-〾ぁ-㏿㐀-䶿一-鿿ꀀ-가-힣豈-︰--⦆¢-₩]/;
|
|
732
|
+
return Array.from(String(text)).reduce((width: number, char: string) => {
|
|
733
|
+
if (FULL_WIDTH.test(char)) return width + size;
|
|
734
|
+
if (/\s/.test(char)) return width + size * 0.28;
|
|
735
|
+
return width + size * 0.56;
|
|
736
|
+
}, 0);
|
|
737
|
+
}
|
|
738
|
+
|
|
739
|
+
function measureText(text: any) {
|
|
740
|
+
if (ctx.value && typeof ctx.value.measureText === 'function') {
|
|
741
|
+
const metrics = ctx.value.measureText(String(text));
|
|
742
|
+
const width = Number(metrics && metrics.width);
|
|
743
|
+
// 鸿蒙同步测量恒为 0(真实值只走 callback),上下文未就绪时其他
|
|
744
|
+
// 平台也可能返回 0。绝不能把 0 交给调用方——换行逻辑会把它当成
|
|
745
|
+
// "宽度足够",于是整段文本挤成一行。
|
|
746
|
+
if (width > 0) return metrics;
|
|
747
|
+
}
|
|
748
|
+
return {
|
|
749
|
+
width: estimateTextWidth(text)
|
|
750
|
+
};
|
|
751
|
+
}
|
|
752
|
+
|
|
753
|
+
function measureTextAsync(text: any): Promise<any> {
|
|
754
|
+
if (ctx.value && typeof ctx.value.measureTextAsync === 'function') {
|
|
755
|
+
return ctx.value.measureTextAsync(String(text));
|
|
756
|
+
}
|
|
757
|
+
|
|
758
|
+
// #ifdef APP-HARMONY
|
|
759
|
+
// 鸿蒙的 CanvasContext.measureText 同步返回值恒为 0,真实宽度只通过
|
|
760
|
+
// callback(webview.evalJSAsync) 回传,因此必须走回调形式。
|
|
761
|
+
if (ctx.value && typeof ctx.value.measureText === 'function' && !_harmonyMeasureUnavailable) {
|
|
762
|
+
return new Promise((resolve) => {
|
|
763
|
+
let settled = false;
|
|
764
|
+
// evalJSAsync 并非所有鸿蒙版本都提供,回调可能永远不来,
|
|
765
|
+
// 必须兜底避免上层 await 永久挂起。且一旦超时就标记为不可用:
|
|
766
|
+
// 单次换行要二分测量十几次,逐次干等会把导出拖成十几秒。
|
|
767
|
+
const timer = setTimeout(() => {
|
|
768
|
+
if (settled) return;
|
|
769
|
+
settled = true;
|
|
770
|
+
_harmonyMeasureUnavailable = true;
|
|
771
|
+
resolve({ width: estimateTextWidth(text) });
|
|
772
|
+
}, 300);
|
|
773
|
+
const done = (metrics: any) => {
|
|
774
|
+
if (settled) return;
|
|
775
|
+
settled = true;
|
|
776
|
+
clearTimeout(timer);
|
|
777
|
+
const width = Number(metrics && metrics.width);
|
|
778
|
+
resolve({ width: width > 0 ? width : estimateTextWidth(text) });
|
|
779
|
+
};
|
|
762
780
|
try {
|
|
763
|
-
const
|
|
764
|
-
|
|
765
|
-
|
|
781
|
+
const sync = ctx.value.measureText(String(text), done);
|
|
782
|
+
// 若该平台其实同步返回了有效值,直接采用
|
|
783
|
+
const syncWidth = Number(sync && sync.width);
|
|
784
|
+
if (syncWidth > 0) done(sync);
|
|
766
785
|
} catch (error) {
|
|
767
|
-
|
|
768
|
-
throw error;
|
|
786
|
+
done(null);
|
|
769
787
|
}
|
|
770
|
-
}
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
// #ifdef APP-NVUE
|
|
774
|
-
if (typeof plus === 'undefined' || !plus.io) {
|
|
775
|
-
reject(new Error('HTML5+ FileReader不可用'));
|
|
776
|
-
return;
|
|
777
|
-
}
|
|
778
|
-
plus.io.resolveLocalFileSystemURL(path, (entry) => {
|
|
779
|
-
entry.file((file) => {
|
|
780
|
-
const reader = new plus.io.FileReader();
|
|
781
|
-
reader.onloadend = event => resolve(event.target.result);
|
|
782
|
-
reader.onerror = reject;
|
|
783
|
-
reader.readAsDataURL(file);
|
|
784
|
-
}, reject);
|
|
785
|
-
}, reject);
|
|
786
|
-
return;
|
|
787
|
-
// #endif
|
|
788
|
+
});
|
|
789
|
+
}
|
|
790
|
+
// #endif
|
|
788
791
|
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
},
|
|
792
|
-
loadImage(src) {
|
|
793
|
-
// #ifdef APP-NVUE
|
|
794
|
-
return this.resolveNvueImageSource(src);
|
|
795
|
-
// #endif
|
|
792
|
+
return Promise.resolve(measureText(text));
|
|
793
|
+
}
|
|
796
794
|
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
795
|
+
function createLinearGradient(x0: number, y0: number, x1: number, y1: number) {
|
|
796
|
+
if (ctx.value && typeof ctx.value.createLinearGradient === 'function') {
|
|
797
|
+
return ctx.value.createLinearGradient(x0, y0, x1, y1);
|
|
798
|
+
}
|
|
799
|
+
return null;
|
|
800
|
+
}
|
|
802
801
|
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
802
|
+
function createRadialGradient(x0: number, y0: number, r0: number, x1: number, y1: number, r1: number) {
|
|
803
|
+
if (ctx.value && typeof ctx.value.createRadialGradient === 'function') {
|
|
804
|
+
return ctx.value.createRadialGradient(x0, y0, r0, x1, y1, r1);
|
|
805
|
+
}
|
|
806
|
+
return null;
|
|
807
|
+
}
|
|
809
808
|
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
809
|
+
function createPattern(image: any, repetition: any = 'repeat') {
|
|
810
|
+
if (ctx.value && typeof ctx.value.createPattern === 'function') {
|
|
811
|
+
return ctx.value.createPattern(image, repetition);
|
|
812
|
+
}
|
|
813
|
+
return null;
|
|
814
|
+
}
|
|
814
815
|
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
816
|
+
async function resolveNvueImageSource(src: any) {
|
|
817
|
+
if (typeof src !== 'string' || src.indexOf('data:image/') === 0) {
|
|
818
|
+
return src;
|
|
819
|
+
}
|
|
820
|
+
if (_imageDataCache[src]) {
|
|
821
|
+
return _imageDataCache[src];
|
|
822
|
+
}
|
|
819
823
|
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
824
|
+
const task = (async () => {
|
|
825
|
+
const localPath = await new Promise<any>((resolve, reject) => {
|
|
826
|
+
uni.getImageInfo({
|
|
827
|
+
src,
|
|
828
|
+
success: (res: any) => resolve(res.path || res.tempFilePath),
|
|
829
|
+
fail: reject
|
|
826
830
|
});
|
|
827
|
-
}
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
},
|
|
844
|
-
draw(isLastDraw = false, callback) {
|
|
845
|
-
if (this.ctx && typeof this.ctx.draw === 'function') {
|
|
846
|
-
return this.ctx.draw(isLastDraw, callback);
|
|
847
|
-
}
|
|
848
|
-
if (typeof callback === 'function') {
|
|
849
|
-
setTimeout(callback, 0);
|
|
850
|
-
}
|
|
851
|
-
return undefined;
|
|
852
|
-
},
|
|
853
|
-
toTempFilePath(options = {}) {
|
|
854
|
-
return new Promise((resolve, reject) => {
|
|
855
|
-
const width = options.width || this.actualWidth;
|
|
856
|
-
const height = options.height || this.actualHeight;
|
|
857
|
-
const hasDestWidth = options.destWidth !== undefined && options.destWidth !== null;
|
|
858
|
-
const hasDestHeight = options.destHeight !== undefined && options.destHeight !== null;
|
|
859
|
-
let destWidth = hasDestWidth ? options.destWidth : width;
|
|
860
|
-
let destHeight = hasDestHeight ? options.destHeight : height;
|
|
861
|
-
|
|
862
|
-
// MP、H5、APP-PLUS 和 APP-HARMONY 保持逻辑绘制坐标,只提升默认导出像素。
|
|
863
|
-
// #ifdef MP || H5 || APP-PLUS || APP-HARMONY
|
|
864
|
-
if (!hasDestWidth) {
|
|
865
|
-
destWidth = Math.round(width * this.dpr);
|
|
866
|
-
}
|
|
867
|
-
if (!hasDestHeight) {
|
|
868
|
-
destHeight = Math.round(height * this.dpr);
|
|
869
|
-
}
|
|
870
|
-
// #endif
|
|
871
|
-
|
|
872
|
-
const defaultFileType = 'png';
|
|
873
|
-
const request = {
|
|
874
|
-
x: options.x || 0,
|
|
875
|
-
y: options.y || 0,
|
|
876
|
-
width,
|
|
877
|
-
height,
|
|
878
|
-
destWidth,
|
|
879
|
-
destHeight,
|
|
880
|
-
fileType: options.fileType === undefined || options.fileType === null
|
|
881
|
-
? defaultFileType
|
|
882
|
-
: options.fileType,
|
|
883
|
-
quality: options.quality === undefined ? 1 : options.quality
|
|
884
|
-
};
|
|
885
|
-
const success = (res) => {
|
|
886
|
-
if (typeof options.success === 'function') options.success(res);
|
|
887
|
-
resolve(res);
|
|
888
|
-
};
|
|
889
|
-
const fail = (err) => {
|
|
890
|
-
if (typeof options.fail === 'function') options.fail(err);
|
|
891
|
-
reject(err);
|
|
892
|
-
};
|
|
893
|
-
const complete = (res) => {
|
|
894
|
-
if (typeof options.complete === 'function') options.complete(res);
|
|
895
|
-
};
|
|
831
|
+
});
|
|
832
|
+
if (!localPath) {
|
|
833
|
+
throw new Error(`Canvas图片未返回本地路径: ${src}`);
|
|
834
|
+
}
|
|
835
|
+
return readNvueFileAsDataURL(localPath);
|
|
836
|
+
})();
|
|
837
|
+
_imageDataCache[src] = task;
|
|
838
|
+
try {
|
|
839
|
+
const dataUrl = await task;
|
|
840
|
+
_imageDataCache[src] = dataUrl;
|
|
841
|
+
return dataUrl;
|
|
842
|
+
} catch (error) {
|
|
843
|
+
delete _imageDataCache[src];
|
|
844
|
+
throw error;
|
|
845
|
+
}
|
|
846
|
+
}
|
|
896
847
|
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
848
|
+
function readNvueFileAsDataURL(path: string): Promise<any> {
|
|
849
|
+
return new Promise((resolve, reject) => {
|
|
850
|
+
// #ifdef APP-NVUE
|
|
851
|
+
if (typeof plus === 'undefined' || !plus.io) {
|
|
852
|
+
reject(new Error('HTML5+ FileReader不可用'));
|
|
853
|
+
return;
|
|
854
|
+
}
|
|
855
|
+
plus.io.resolveLocalFileSystemURL(path, (entry: any) => {
|
|
856
|
+
entry.file((file: any) => {
|
|
857
|
+
const reader = new plus.io.FileReader();
|
|
858
|
+
reader.onloadend = (event: any) => resolve(event.target.result);
|
|
859
|
+
reader.onerror = reject;
|
|
860
|
+
reader.readAsDataURL(file);
|
|
861
|
+
}, reject);
|
|
862
|
+
}, reject);
|
|
863
|
+
return;
|
|
864
|
+
// #endif
|
|
865
|
+
|
|
866
|
+
resolve(path);
|
|
867
|
+
});
|
|
868
|
+
}
|
|
869
|
+
|
|
870
|
+
function loadImage(src: any): Promise<any> {
|
|
871
|
+
// #ifdef APP-NVUE
|
|
872
|
+
return resolveNvueImageSource(src);
|
|
873
|
+
// #endif
|
|
874
|
+
|
|
875
|
+
if (_imageCache[src]) {
|
|
876
|
+
return Promise.resolve(_imageCache[src]);
|
|
877
|
+
}
|
|
878
|
+
return new Promise((resolve, reject) => {
|
|
879
|
+
let image: any = null;
|
|
880
|
+
|
|
881
|
+
// #ifdef MP
|
|
882
|
+
const canvas = getCanvasElement();
|
|
883
|
+
if (canvas && typeof canvas.createImage === 'function') {
|
|
884
|
+
image = canvas.createImage();
|
|
885
|
+
}
|
|
886
|
+
// #endif
|
|
887
|
+
|
|
888
|
+
// #ifdef H5
|
|
889
|
+
image = new Image();
|
|
890
|
+
image.crossOrigin = 'anonymous';
|
|
891
|
+
// #endif
|
|
892
|
+
|
|
893
|
+
if (!image) {
|
|
894
|
+
resolve(src);
|
|
895
|
+
return;
|
|
896
|
+
}
|
|
897
|
+
|
|
898
|
+
image.onload = () => {
|
|
899
|
+
_imageCache[src] = image;
|
|
900
|
+
resolve(image);
|
|
901
|
+
};
|
|
902
|
+
image.onerror = reject;
|
|
903
|
+
image.src = src;
|
|
904
|
+
});
|
|
905
|
+
}
|
|
906
|
+
|
|
907
|
+
async function drawImage(source: any, ...args: any[]) {
|
|
908
|
+
if (!ctx.value || typeof ctx.value.drawImage !== 'function') {
|
|
909
|
+
return false;
|
|
910
|
+
}
|
|
911
|
+
if (typeof source !== 'string') {
|
|
912
|
+
ctx.value.drawImage(source, ...args);
|
|
913
|
+
return true;
|
|
914
|
+
}
|
|
915
|
+
if (typeof ctx.value.setFillStyle === 'function' && !_isNvue) {
|
|
916
|
+
ctx.value.drawImage(source, ...args);
|
|
917
|
+
return true;
|
|
918
|
+
}
|
|
919
|
+
const image = await loadImage(source);
|
|
920
|
+
ctx.value.drawImage(image, ...args);
|
|
921
|
+
return true;
|
|
922
|
+
}
|
|
923
|
+
|
|
924
|
+
function draw(isLastDraw = false, callback?: any) {
|
|
925
|
+
if (ctx.value && typeof ctx.value.draw === 'function') {
|
|
926
|
+
return ctx.value.draw(isLastDraw, callback);
|
|
927
|
+
}
|
|
928
|
+
if (typeof callback === 'function') {
|
|
929
|
+
setTimeout(callback, 0);
|
|
930
|
+
}
|
|
931
|
+
return undefined;
|
|
932
|
+
}
|
|
933
|
+
|
|
934
|
+
function toTempFilePath(options: any = {}): Promise<any> {
|
|
935
|
+
return new Promise((resolve, reject) => {
|
|
936
|
+
const width = options.width || actualWidth.value;
|
|
937
|
+
const height = options.height || actualHeight.value;
|
|
938
|
+
const hasDestWidth = options.destWidth !== undefined && options.destWidth !== null;
|
|
939
|
+
const hasDestHeight = options.destHeight !== undefined && options.destHeight !== null;
|
|
940
|
+
let destWidth = hasDestWidth ? options.destWidth : width;
|
|
941
|
+
let destHeight = hasDestHeight ? options.destHeight : height;
|
|
942
|
+
|
|
943
|
+
// MP、H5、APP-PLUS 和 APP-HARMONY 保持逻辑绘制坐标,只提升默认导出像素。
|
|
944
|
+
// #ifdef MP || H5 || APP-PLUS || APP-HARMONY
|
|
945
|
+
if (!hasDestWidth) {
|
|
946
|
+
destWidth = Math.round(width * dpr.value);
|
|
947
|
+
}
|
|
948
|
+
if (!hasDestHeight) {
|
|
949
|
+
destHeight = Math.round(height * dpr.value);
|
|
950
|
+
}
|
|
951
|
+
// #endif
|
|
952
|
+
|
|
953
|
+
const defaultFileType = 'png';
|
|
954
|
+
const request = {
|
|
955
|
+
x: options.x || 0,
|
|
956
|
+
y: options.y || 0,
|
|
957
|
+
width,
|
|
958
|
+
height,
|
|
959
|
+
destWidth,
|
|
960
|
+
destHeight,
|
|
961
|
+
fileType: options.fileType === undefined || options.fileType === null
|
|
962
|
+
? defaultFileType
|
|
963
|
+
: options.fileType,
|
|
964
|
+
quality: options.quality === undefined ? 1 : options.quality
|
|
965
|
+
};
|
|
966
|
+
const success = (res: any) => {
|
|
967
|
+
if (typeof options.success === 'function') options.success(res);
|
|
968
|
+
resolve(res);
|
|
969
|
+
};
|
|
970
|
+
const fail = (err: any) => {
|
|
971
|
+
if (typeof options.fail === 'function') options.fail(err);
|
|
972
|
+
reject(err);
|
|
973
|
+
};
|
|
974
|
+
const complete = (res: any) => {
|
|
975
|
+
if (typeof options.complete === 'function') options.complete(res);
|
|
976
|
+
};
|
|
977
|
+
|
|
978
|
+
// #ifdef H5
|
|
979
|
+
const canvas = getCanvasElement() || (ctx.value && ctx.value.canvas);
|
|
980
|
+
if (canvas && typeof canvas.toDataURL === 'function') {
|
|
981
|
+
try {
|
|
982
|
+
let exportCanvas = canvas;
|
|
983
|
+
if (
|
|
984
|
+
request.x !== 0 ||
|
|
985
|
+
request.y !== 0 ||
|
|
986
|
+
request.width !== actualWidth.value ||
|
|
987
|
+
request.height !== actualHeight.value ||
|
|
988
|
+
request.destWidth !== request.width ||
|
|
989
|
+
request.destHeight !== request.height
|
|
990
|
+
) {
|
|
991
|
+
exportCanvas = document.createElement('canvas');
|
|
992
|
+
exportCanvas.width = request.destWidth;
|
|
993
|
+
exportCanvas.height = request.destHeight;
|
|
994
|
+
const exportCtx = exportCanvas.getContext('2d') as CanvasRenderingContext2D;
|
|
995
|
+
exportCtx.drawImage(
|
|
996
|
+
canvas,
|
|
997
|
+
request.x * dpr.value,
|
|
998
|
+
request.y * dpr.value,
|
|
999
|
+
request.width * dpr.value,
|
|
1000
|
+
request.height * dpr.value,
|
|
1001
|
+
0,
|
|
1002
|
+
0,
|
|
1003
|
+
request.destWidth,
|
|
1004
|
+
request.destHeight
|
|
954
1005
|
);
|
|
955
|
-
return;
|
|
956
|
-
}
|
|
957
|
-
// #endif
|
|
958
|
-
|
|
959
|
-
const canvasNode = this.getCanvasElement();
|
|
960
|
-
const uniOptions = {
|
|
961
|
-
...request,
|
|
962
|
-
canvasId: this.canvasId,
|
|
963
|
-
success,
|
|
964
|
-
fail,
|
|
965
|
-
complete
|
|
966
|
-
};
|
|
967
|
-
if (canvasNode) {
|
|
968
|
-
uniOptions.canvas = canvasNode;
|
|
969
1006
|
}
|
|
970
|
-
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
|
|
975
|
-
fileType,
|
|
976
|
-
quality,
|
|
977
|
-
width: this.actualWidth,
|
|
978
|
-
height: this.actualHeight
|
|
979
|
-
});
|
|
980
|
-
return res.tempFilePath || res.apFilePath;
|
|
981
|
-
},
|
|
982
|
-
getImageData(options = {}) {
|
|
983
|
-
return new Promise((resolve, reject) => {
|
|
984
|
-
const success = (res) => {
|
|
985
|
-
if (typeof options.success === 'function') options.success(res);
|
|
986
|
-
resolve(res);
|
|
987
|
-
};
|
|
988
|
-
const fail = (err) => {
|
|
989
|
-
if (typeof options.fail === 'function') options.fail(err);
|
|
990
|
-
reject(err);
|
|
991
|
-
};
|
|
992
|
-
const complete = (res) => {
|
|
993
|
-
if (typeof options.complete === 'function') options.complete(res);
|
|
1007
|
+
const mime = request.fileType === 'jpg' || request.fileType === 'jpeg'
|
|
1008
|
+
? 'image/jpeg'
|
|
1009
|
+
: 'image/png';
|
|
1010
|
+
const res = {
|
|
1011
|
+
tempFilePath: exportCanvas.toDataURL(mime, request.quality)
|
|
994
1012
|
};
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
|
|
1013
|
+
success(res);
|
|
1014
|
+
complete(res);
|
|
1015
|
+
return;
|
|
1016
|
+
} catch (error) {
|
|
1017
|
+
fail(error);
|
|
1018
|
+
complete(error);
|
|
1019
|
+
return;
|
|
1020
|
+
}
|
|
1021
|
+
}
|
|
1022
|
+
// #endif
|
|
1023
|
+
|
|
1024
|
+
// #ifdef APP-NVUE
|
|
1025
|
+
if (ctx.value && typeof ctx.value.toTempFilePath === 'function') {
|
|
1026
|
+
Promise.resolve(ctx.value.toTempFilePath(request)).then(
|
|
1027
|
+
(res: any) => {
|
|
1028
|
+
success(res);
|
|
1029
|
+
complete(res);
|
|
1030
|
+
},
|
|
1031
|
+
(error: any) => {
|
|
1032
|
+
fail(error);
|
|
1033
|
+
complete(error);
|
|
1010
1034
|
}
|
|
1011
|
-
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
|
|
1017
|
-
|
|
1018
|
-
|
|
1019
|
-
|
|
1020
|
-
|
|
1021
|
-
|
|
1022
|
-
|
|
1023
|
-
|
|
1035
|
+
);
|
|
1036
|
+
return;
|
|
1037
|
+
}
|
|
1038
|
+
// #endif
|
|
1039
|
+
|
|
1040
|
+
const canvasNode = getCanvasElement();
|
|
1041
|
+
const uniOptions: any = {
|
|
1042
|
+
...request,
|
|
1043
|
+
canvasId: props.canvasId,
|
|
1044
|
+
success,
|
|
1045
|
+
fail,
|
|
1046
|
+
complete
|
|
1047
|
+
};
|
|
1048
|
+
if (canvasNode) {
|
|
1049
|
+
uniOptions.canvas = canvasNode;
|
|
1050
|
+
}
|
|
1051
|
+
uni.canvasToTempFilePath(uniOptions, proxy);
|
|
1052
|
+
});
|
|
1053
|
+
}
|
|
1054
|
+
|
|
1055
|
+
async function exportImage(fileType = 'png', quality = 1) {
|
|
1056
|
+
const res = await toTempFilePath({
|
|
1057
|
+
fileType,
|
|
1058
|
+
quality,
|
|
1059
|
+
width: actualWidth.value,
|
|
1060
|
+
height: actualHeight.value
|
|
1061
|
+
});
|
|
1062
|
+
return res.tempFilePath || res.apFilePath;
|
|
1063
|
+
}
|
|
1064
|
+
|
|
1065
|
+
function getImageData(options: any = {}): Promise<any> {
|
|
1066
|
+
return new Promise((resolve, reject) => {
|
|
1067
|
+
const success = (res: any) => {
|
|
1068
|
+
if (typeof options.success === 'function') options.success(res);
|
|
1069
|
+
resolve(res);
|
|
1070
|
+
};
|
|
1071
|
+
const fail = (err: any) => {
|
|
1072
|
+
if (typeof options.fail === 'function') options.fail(err);
|
|
1073
|
+
reject(err);
|
|
1074
|
+
};
|
|
1075
|
+
const complete = (res: any) => {
|
|
1076
|
+
if (typeof options.complete === 'function') options.complete(res);
|
|
1077
|
+
};
|
|
1078
|
+
// #ifdef APP-NVUE
|
|
1079
|
+
if (ctx.value && typeof ctx.value.getImageData === 'function') {
|
|
1080
|
+
Promise.resolve(ctx.value.getImageData({
|
|
1081
|
+
x: options.x || 0,
|
|
1082
|
+
y: options.y || 0,
|
|
1083
|
+
width: options.width || actualWidth.value,
|
|
1084
|
+
height: options.height || actualHeight.value
|
|
1085
|
+
})).then((res: any) => {
|
|
1086
|
+
success(res);
|
|
1087
|
+
complete(res);
|
|
1088
|
+
}, (error: any) => {
|
|
1089
|
+
fail(error);
|
|
1090
|
+
complete(error);
|
|
1024
1091
|
});
|
|
1025
|
-
|
|
1026
|
-
|
|
1027
|
-
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
|
|
1042
|
-
|
|
1043
|
-
|
|
1044
|
-
|
|
1045
|
-
|
|
1046
|
-
|
|
1047
|
-
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
|
|
1055
|
-
|
|
1056
|
-
|
|
1057
|
-
|
|
1058
|
-
|
|
1059
|
-
|
|
1060
|
-
|
|
1061
|
-
|
|
1062
|
-
|
|
1063
|
-
|
|
1064
|
-
|
|
1065
|
-
|
|
1066
|
-
|
|
1067
|
-
|
|
1068
|
-
|
|
1069
|
-
|
|
1092
|
+
return;
|
|
1093
|
+
}
|
|
1094
|
+
// #endif
|
|
1095
|
+
|
|
1096
|
+
const request = {
|
|
1097
|
+
canvasId: props.canvasId,
|
|
1098
|
+
x: options.x || 0,
|
|
1099
|
+
y: options.y || 0,
|
|
1100
|
+
width: options.width || actualWidth.value,
|
|
1101
|
+
height: options.height || actualHeight.value,
|
|
1102
|
+
success,
|
|
1103
|
+
fail,
|
|
1104
|
+
complete
|
|
1105
|
+
};
|
|
1106
|
+
uni.canvasGetImageData(request, proxy);
|
|
1107
|
+
});
|
|
1108
|
+
}
|
|
1109
|
+
|
|
1110
|
+
function putImageData(options: any = {}): Promise<any> {
|
|
1111
|
+
return new Promise((resolve, reject) => {
|
|
1112
|
+
const success = (res: any) => {
|
|
1113
|
+
if (typeof options.success === 'function') options.success(res);
|
|
1114
|
+
resolve(res);
|
|
1115
|
+
};
|
|
1116
|
+
const fail = (err: any) => {
|
|
1117
|
+
if (typeof options.fail === 'function') options.fail(err);
|
|
1118
|
+
reject(err);
|
|
1119
|
+
};
|
|
1120
|
+
const complete = (res: any) => {
|
|
1121
|
+
if (typeof options.complete === 'function') options.complete(res);
|
|
1122
|
+
};
|
|
1123
|
+
// #ifdef APP-NVUE
|
|
1124
|
+
if (ctx.value && typeof ctx.value.putImageData === 'function') {
|
|
1125
|
+
Promise.resolve(ctx.value.putImageData({
|
|
1126
|
+
x: options.x || 0,
|
|
1127
|
+
y: options.y || 0,
|
|
1128
|
+
width: options.width || (options.data && options.data.width) || actualWidth.value,
|
|
1129
|
+
height: options.height || (options.data && options.data.height) || actualHeight.value,
|
|
1130
|
+
data: options.data
|
|
1131
|
+
})).then((res: any) => {
|
|
1132
|
+
success(res);
|
|
1133
|
+
complete(res);
|
|
1134
|
+
}, (error: any) => {
|
|
1135
|
+
fail(error);
|
|
1136
|
+
complete(error);
|
|
1070
1137
|
});
|
|
1138
|
+
return;
|
|
1071
1139
|
}
|
|
1072
|
-
|
|
1073
|
-
|
|
1140
|
+
// #endif
|
|
1141
|
+
|
|
1142
|
+
const request = {
|
|
1143
|
+
canvasId: props.canvasId,
|
|
1144
|
+
x: options.x || 0,
|
|
1145
|
+
y: options.y || 0,
|
|
1146
|
+
width: options.width || (options.data && options.data.width) || actualWidth.value,
|
|
1147
|
+
height: options.height || (options.data && options.data.height) || actualHeight.value,
|
|
1148
|
+
data: options.data,
|
|
1149
|
+
success,
|
|
1150
|
+
fail,
|
|
1151
|
+
complete
|
|
1152
|
+
};
|
|
1153
|
+
uni.canvasPutImageData(request, proxy);
|
|
1154
|
+
});
|
|
1155
|
+
}
|
|
1156
|
+
|
|
1157
|
+
defineExpose({
|
|
1158
|
+
rootId,
|
|
1159
|
+
ctx,
|
|
1160
|
+
widthLocal,
|
|
1161
|
+
heightLocal,
|
|
1162
|
+
fontSize,
|
|
1163
|
+
fontFamily,
|
|
1164
|
+
fontWeight,
|
|
1165
|
+
dpr,
|
|
1166
|
+
parseSize,
|
|
1167
|
+
onTouchStart,
|
|
1168
|
+
onTouchMove,
|
|
1169
|
+
onTouchEnd,
|
|
1170
|
+
onWebViewMessage,
|
|
1171
|
+
onWebViewTouch,
|
|
1172
|
+
getCanvasNode,
|
|
1173
|
+
getCanvasElement,
|
|
1174
|
+
getRawContext,
|
|
1175
|
+
getCanvasContext,
|
|
1176
|
+
setNewSize,
|
|
1177
|
+
initCanvas,
|
|
1178
|
+
_initializeCanvas,
|
|
1179
|
+
_initializeWebViewCanvas,
|
|
1180
|
+
refresh,
|
|
1181
|
+
getWidth,
|
|
1182
|
+
getHeight,
|
|
1183
|
+
clearCanvas,
|
|
1184
|
+
callContext,
|
|
1185
|
+
rect,
|
|
1186
|
+
clearRect,
|
|
1187
|
+
fillRect,
|
|
1188
|
+
strokeRect,
|
|
1189
|
+
fill,
|
|
1190
|
+
stroke,
|
|
1191
|
+
beginPath,
|
|
1192
|
+
closePath,
|
|
1193
|
+
moveTo,
|
|
1194
|
+
lineTo,
|
|
1195
|
+
arc,
|
|
1196
|
+
arcTo,
|
|
1197
|
+
bezierCurveTo,
|
|
1198
|
+
quadraticCurveTo,
|
|
1199
|
+
ellipse,
|
|
1200
|
+
clip,
|
|
1201
|
+
save,
|
|
1202
|
+
restore,
|
|
1203
|
+
translate,
|
|
1204
|
+
rotate,
|
|
1205
|
+
scale,
|
|
1206
|
+
setTransform,
|
|
1207
|
+
transform,
|
|
1208
|
+
resetTransform,
|
|
1209
|
+
setFillStyle,
|
|
1210
|
+
setStrokeStyle,
|
|
1211
|
+
setLineWidth,
|
|
1212
|
+
setLineCap,
|
|
1213
|
+
setLineJoin,
|
|
1214
|
+
setMiterLimit,
|
|
1215
|
+
setLineDash,
|
|
1216
|
+
getLineDash,
|
|
1217
|
+
setTextAlign,
|
|
1218
|
+
setTextBaseline,
|
|
1219
|
+
setFontSize,
|
|
1220
|
+
setFont,
|
|
1221
|
+
setGlobalAlpha,
|
|
1222
|
+
setGlobalCompositeOperation,
|
|
1223
|
+
setShadow,
|
|
1224
|
+
setLineStyle,
|
|
1225
|
+
applyFont,
|
|
1226
|
+
fillText,
|
|
1227
|
+
strokeText,
|
|
1228
|
+
estimateTextWidth,
|
|
1229
|
+
measureText,
|
|
1230
|
+
measureTextAsync,
|
|
1231
|
+
createLinearGradient,
|
|
1232
|
+
createRadialGradient,
|
|
1233
|
+
createPattern,
|
|
1234
|
+
resolveNvueImageSource,
|
|
1235
|
+
readNvueFileAsDataURL,
|
|
1236
|
+
loadImage,
|
|
1237
|
+
drawImage,
|
|
1238
|
+
draw,
|
|
1239
|
+
toTempFilePath,
|
|
1240
|
+
exportImage,
|
|
1241
|
+
getImageData,
|
|
1242
|
+
putImageData
|
|
1243
|
+
});
|
|
1074
1244
|
</script>
|
|
1075
1245
|
|
|
1076
1246
|
<style lang="scss" scoped>
|
|
1247
|
+
@import '../../scss/variables';
|
|
1248
|
+
@import '../../scss/mixins';
|
|
1077
1249
|
.dd-canvas {
|
|
1078
1250
|
position: relative;
|
|
1079
1251
|
overflow: hidden;
|
|
@@ -1085,4 +1257,3 @@ export default {
|
|
|
1085
1257
|
height: 100%;
|
|
1086
1258
|
}
|
|
1087
1259
|
</style>
|
|
1088
|
-
|