@didaoktv/didaoui-uniapp 2.13.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 +6 -6
- 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-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 +4 -3
- 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
|
@@ -30,7 +30,7 @@
|
|
|
30
30
|
</view>
|
|
31
31
|
</template>
|
|
32
32
|
|
|
33
|
-
<script>
|
|
33
|
+
<script setup lang="ts">
|
|
34
34
|
/**
|
|
35
35
|
* Poster 海报组件
|
|
36
36
|
* @description 用于生成海报的组件,支持文本、图片、二维码等元素
|
|
@@ -46,605 +46,617 @@
|
|
|
46
46
|
* @example <dd-poster :json="posterJson"></dd-poster>
|
|
47
47
|
*/
|
|
48
48
|
|
|
49
|
-
import {
|
|
50
|
-
rpx2px
|
|
51
|
-
} from '../../libs/function/index.js';
|
|
49
|
+
import { rpx2px } from '../../libs/function/index.js';
|
|
52
50
|
// z-index 统一取 libs/config/zIndex.js(离屏隐藏容器归 base 档,Options API 经 data 暴露给模板)
|
|
53
51
|
import zIndexConfig from '../../libs/config/zIndex.js';
|
|
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
|
-
|
|
52
|
+
import { nextTick, ref } from 'vue'
|
|
53
|
+
|
|
54
|
+
interface Props {
|
|
55
|
+
json?: Record<string, any>
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const props = withDefaults(defineProps<Props>(), {
|
|
59
|
+
json: () => ({})
|
|
60
|
+
})
|
|
61
|
+
|
|
62
|
+
// 离屏隐藏容器层级(base 档)
|
|
63
|
+
const zcBase = zIndexConfig.base
|
|
64
|
+
const canvasId = 'dd-poster-canvas-' + Date.now()
|
|
65
|
+
const showCanvas = ref(false)
|
|
66
|
+
const canvasWidth = ref(0)
|
|
67
|
+
const canvasHeight = ref(0)
|
|
68
|
+
// 二维码相关数据
|
|
69
|
+
const qrCodeValue = ref('')
|
|
70
|
+
const qrCodeSize = ref(200)
|
|
71
|
+
const qrCodeShow = ref(false)
|
|
72
|
+
// 存储多个二维码的数据
|
|
73
|
+
const qrCodeMap = new Map<string, string>()
|
|
74
|
+
|
|
75
|
+
const posterCanvas = ref<any>(null)
|
|
76
|
+
const qrCode = ref<any>(null)
|
|
77
|
+
|
|
78
|
+
// 根据传入的css生成文本样式
|
|
79
|
+
function getTextStyle(css: any) {
|
|
80
|
+
const style: any = {};
|
|
81
|
+
if (css.color) style.color = css.color;
|
|
82
|
+
if (css.fontSize) style.fontSize = css.fontSize;
|
|
83
|
+
if (css.fontWeight) style.fontWeight = css.fontWeight;
|
|
84
|
+
if (css.lineHeight) style.lineHeight = css.lineHeight;
|
|
85
|
+
if (css.textAlign) style.textAlign = css.textAlign;
|
|
86
|
+
return style;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* 导出海报图片
|
|
91
|
+
* @description 根据json配置生成海报并导出为临时图片路径
|
|
92
|
+
* @returns {Promise<Object>} 返回包含图片信息的对象
|
|
93
|
+
* @author jry ijry@qq.com
|
|
94
|
+
*/
|
|
95
|
+
async function exportImage() {
|
|
96
|
+
let timeoutId: any = null;
|
|
97
|
+
try {
|
|
98
|
+
const timeoutPromise = new Promise<any>((resolve, reject) => {
|
|
99
|
+
timeoutId = setTimeout(() => reject(new Error('导出图片超时')), 20000);
|
|
100
|
+
});
|
|
101
|
+
const exportPromise = (async () => {
|
|
102
|
+
// 获取海报尺寸信息
|
|
103
|
+
const posterSize = props.json.css || {};
|
|
104
|
+
const width = convertRpxToPx(posterSize.width || '750rpx');
|
|
105
|
+
const height = convertRpxToPx(posterSize.height || '1114rpx');
|
|
106
|
+
|
|
107
|
+
canvasWidth.value = width;
|
|
108
|
+
canvasHeight.value = height;
|
|
109
|
+
showCanvas.value = true;
|
|
110
|
+
await nextTick();
|
|
111
|
+
|
|
112
|
+
const posterCanvasInstance = await getPosterCanvas();
|
|
113
|
+
const ctx = posterCanvasInstance;
|
|
114
|
+
if (posterSize.background) {
|
|
115
|
+
if (posterSize.background.includes('linear-gradient') || posterSize.background.includes('radial-gradient')) {
|
|
116
|
+
drawGradientBackground(ctx, posterSize, 0, 0, width, height);
|
|
117
|
+
} else {
|
|
118
|
+
ctx.setFillStyle(posterSize.background);
|
|
119
|
+
ctx.fillRect(0, 0, width, height);
|
|
120
|
+
}
|
|
89
121
|
}
|
|
90
|
-
}
|
|
91
|
-
},
|
|
92
|
-
methods: {
|
|
93
|
-
/**
|
|
94
|
-
* 导出海报图片
|
|
95
|
-
* @description 根据json配置生成海报并导出为临时图片路径
|
|
96
|
-
* @returns {Promise<Object>} 返回包含图片信息的对象
|
|
97
|
-
* @author jry ijry@qq.com
|
|
98
|
-
*/
|
|
99
|
-
async exportImage() {
|
|
100
|
-
let timeoutId = null;
|
|
101
|
-
try {
|
|
102
|
-
const timeoutPromise = new Promise((resolve, reject) => {
|
|
103
|
-
timeoutId = setTimeout(() => reject(new Error('导出图片超时')), 20000);
|
|
104
|
-
});
|
|
105
|
-
const exportPromise = (async () => {
|
|
106
|
-
// 获取海报尺寸信息
|
|
107
|
-
const posterSize = this.json.css || {};
|
|
108
|
-
const width = this.convertRpxToPx(posterSize.width || '750rpx');
|
|
109
|
-
const height = this.convertRpxToPx(posterSize.height || '1114rpx');
|
|
110
|
-
|
|
111
|
-
this.canvasWidth = width;
|
|
112
|
-
this.canvasHeight = height;
|
|
113
|
-
this.showCanvas = true;
|
|
114
|
-
await this.$nextTick();
|
|
115
|
-
|
|
116
|
-
const posterCanvas = await this.getPosterCanvas();
|
|
117
|
-
const ctx = posterCanvas;
|
|
118
|
-
if (posterSize.background) {
|
|
119
|
-
if (posterSize.background.includes('linear-gradient') || posterSize.background.includes('radial-gradient')) {
|
|
120
|
-
this.drawGradientBackground(ctx, posterSize, 0, 0, width, height);
|
|
121
|
-
} else {
|
|
122
|
-
ctx.setFillStyle(posterSize.background);
|
|
123
|
-
ctx.fillRect(0, 0, width, height);
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
122
|
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
123
|
+
for (const item of props.json.views || []) {
|
|
124
|
+
await drawItem(ctx, item, width, height);
|
|
125
|
+
}
|
|
130
126
|
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
}
|
|
137
|
-
return {
|
|
138
|
-
width,
|
|
139
|
-
height,
|
|
140
|
-
path,
|
|
141
|
-
blob: this.dataURLToBlob(path)
|
|
142
|
-
};
|
|
143
|
-
})();
|
|
144
|
-
return await Promise.race([exportPromise, timeoutPromise]);
|
|
145
|
-
} finally {
|
|
146
|
-
if (timeoutId) clearTimeout(timeoutId);
|
|
147
|
-
this.showCanvas = false;
|
|
127
|
+
await flushPosterCanvas(posterCanvasInstance);
|
|
128
|
+
const res = await posterCanvasInstance.toTempFilePath({ width, height });
|
|
129
|
+
const path = res && (res.tempFilePath || res.apFilePath);
|
|
130
|
+
if (!path) {
|
|
131
|
+
throw new Error('导出图片未返回文件路径');
|
|
148
132
|
}
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
133
|
+
return {
|
|
134
|
+
width,
|
|
135
|
+
height,
|
|
136
|
+
path,
|
|
137
|
+
blob: dataURLToBlob(path)
|
|
138
|
+
};
|
|
139
|
+
})();
|
|
140
|
+
return await Promise.race([exportPromise, timeoutPromise]);
|
|
141
|
+
} finally {
|
|
142
|
+
if (timeoutId) clearTimeout(timeoutId);
|
|
143
|
+
showCanvas.value = false;
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
function flushPosterCanvas(posterCanvasInstance: any) {
|
|
148
|
+
return new Promise<void>((resolve, reject) => {
|
|
149
|
+
let settled = false;
|
|
150
|
+
const finish = (error?: any) => {
|
|
151
|
+
if (settled) return;
|
|
152
|
+
settled = true;
|
|
153
|
+
if (error) reject(error);
|
|
154
|
+
else resolve();
|
|
155
|
+
};
|
|
156
|
+
try {
|
|
157
|
+
const result = posterCanvasInstance.draw(false, () => finish());
|
|
158
|
+
if (result && typeof result.then === 'function') {
|
|
159
|
+
result.then(() => finish(), finish);
|
|
160
|
+
}
|
|
161
|
+
} catch (error) {
|
|
162
|
+
finish(error);
|
|
163
|
+
}
|
|
164
|
+
});
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
async function getPosterCanvas() {
|
|
168
|
+
await nextTick();
|
|
169
|
+
const instance = posterCanvas.value;
|
|
170
|
+
if (!instance) {
|
|
171
|
+
throw new Error('无法获取海报画布实例');
|
|
172
|
+
}
|
|
173
|
+
const initialized = await instance.initCanvas();
|
|
174
|
+
if (!initialized || !instance.getRawContext()) {
|
|
175
|
+
throw new Error('无法初始化海报画布');
|
|
176
|
+
}
|
|
177
|
+
return instance;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* 绘制单个元素
|
|
182
|
+
* @description 根据元素类型绘制文本、图片、矩形或二维码到canvas
|
|
183
|
+
* @param {Object} ctx canvas上下文
|
|
184
|
+
* @param {Object} item 元素配置信息
|
|
185
|
+
* @param {Number} canvasWidth canvas宽度
|
|
186
|
+
* @param {Number} canvasHeight canvas高度
|
|
187
|
+
* @returns {Promise} 绘制完成的Promise
|
|
188
|
+
* @author jry ijry@qq.com
|
|
189
|
+
*/
|
|
190
|
+
async function drawItem(ctx: any, item: any, canvasWidth: number, canvasHeight: number) {
|
|
191
|
+
const css = item.css || {};
|
|
192
|
+
const left = convertRpxToPx(css.left || '0rpx');
|
|
193
|
+
const top = convertRpxToPx(css.top || '0rpx');
|
|
194
|
+
const width = convertRpxToPx(css.width || '0rpx');
|
|
195
|
+
const height = convertRpxToPx(css.height || '0rpx');
|
|
196
|
+
|
|
197
|
+
switch (item.type) {
|
|
198
|
+
case 'view':
|
|
199
|
+
// 绘制矩形背景
|
|
200
|
+
if (css.background) {
|
|
201
|
+
// 支持渐变背景色
|
|
202
|
+
if (css.background.includes('linear-gradient') || css.background.includes('radial-gradient')) {
|
|
203
|
+
drawGradientBackground(ctx, css, left, top, width, height);
|
|
204
|
+
} else {
|
|
205
|
+
ctx.setFillStyle(css.background);
|
|
206
|
+
// 处理圆角
|
|
207
|
+
if (css.radius) {
|
|
208
|
+
const radius = convertRpxToPx(css.radius);
|
|
209
|
+
drawRoundRect(ctx, left, top, width, height, radius, css.background);
|
|
210
|
+
} else {
|
|
211
|
+
ctx.fillRect(left, top, width, height);
|
|
164
212
|
}
|
|
165
|
-
} catch (error) {
|
|
166
|
-
finish(error);
|
|
167
213
|
}
|
|
168
|
-
});
|
|
169
|
-
},
|
|
170
|
-
|
|
171
|
-
async getPosterCanvas() {
|
|
172
|
-
await this.$nextTick();
|
|
173
|
-
const posterCanvas = this.$refs.posterCanvas;
|
|
174
|
-
if (!posterCanvas) {
|
|
175
|
-
throw new Error('无法获取海报画布实例');
|
|
176
214
|
}
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
215
|
+
break;
|
|
216
|
+
|
|
217
|
+
case 'text':
|
|
218
|
+
// 设置文本样式
|
|
219
|
+
ctx.setFillStyle(css.color || '#000000');
|
|
220
|
+
if (css.fontSize) {
|
|
221
|
+
const fontSize = convertRpxToPx(css.fontSize);
|
|
222
|
+
ctx.setFontSize(fontSize);
|
|
180
223
|
}
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
async drawItem(ctx, item, canvasWidth, canvasHeight) {
|
|
195
|
-
const css = item.css || {};
|
|
196
|
-
const left = this.convertRpxToPx(css.left || '0rpx');
|
|
197
|
-
const top = this.convertRpxToPx(css.top || '0rpx');
|
|
198
|
-
const width = this.convertRpxToPx(css.width || '0rpx');
|
|
199
|
-
const height = this.convertRpxToPx(css.height || '0rpx');
|
|
224
|
+
if (css.fontWeight) {
|
|
225
|
+
ctx.setLineWidth(css.fontWeight === 'bold' ? 2 : 1);
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
// 处理文本换行
|
|
229
|
+
if (css.lineClamp) {
|
|
230
|
+
await drawTextWithLineClamp(ctx, item.text, left, top, width, css);
|
|
231
|
+
} else {
|
|
232
|
+
// 修复:文本垂直居中对齐问题
|
|
233
|
+
const textBaseLine = css.fontSize ? convertRpxToPx(css.fontSize) / 2 : 10;
|
|
234
|
+
ctx.fillText(item.text, left, top + textBaseLine);
|
|
235
|
+
}
|
|
236
|
+
break;
|
|
200
237
|
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
ctx.setFillStyle(css.background);
|
|
210
|
-
// 处理圆角
|
|
238
|
+
case 'image':
|
|
239
|
+
// 绘制图片
|
|
240
|
+
return new Promise<void>((resolve, reject) => {
|
|
241
|
+
uni.getImageInfo({
|
|
242
|
+
src: item.src,
|
|
243
|
+
success: async (res) => {
|
|
244
|
+
let clipped = false;
|
|
245
|
+
try {
|
|
211
246
|
if (css.radius) {
|
|
212
|
-
const radius =
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
ctx.fillRect(left, top, width, height);
|
|
247
|
+
const radius = convertRpxToPx(css.radius);
|
|
248
|
+
clipRoundRect(ctx, left, top, width, height, radius);
|
|
249
|
+
clipped = true;
|
|
216
250
|
}
|
|
251
|
+
await ctx.drawImage(res.path, left, top, width, height);
|
|
252
|
+
resolve();
|
|
253
|
+
} catch (error: any) {
|
|
254
|
+
reject(new Error(`海报图片绘制失败: ${error && error.message ? error.message : error}`));
|
|
255
|
+
} finally {
|
|
256
|
+
if (clipped) ctx.restore();
|
|
217
257
|
}
|
|
258
|
+
},
|
|
259
|
+
fail: (e: any) => {
|
|
260
|
+
reject(new Error(`海报图片加载失败: ${e && e.errMsg ? e.errMsg : e}`));
|
|
218
261
|
}
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
}
|
|
231
|
-
|
|
232
|
-
// 处理文本换行
|
|
233
|
-
if (css.lineClamp) {
|
|
234
|
-
await this.drawTextWithLineClamp(ctx, item.text, left, top, width, css);
|
|
235
|
-
} else {
|
|
236
|
-
// 修复:文本垂直居中对齐问题
|
|
237
|
-
const textBaseLine = css.fontSize ? this.convertRpxToPx(css.fontSize) / 2 : 10;
|
|
238
|
-
ctx.fillText(item.text, left, top + textBaseLine);
|
|
239
|
-
}
|
|
240
|
-
break;
|
|
241
|
-
|
|
242
|
-
case 'image':
|
|
243
|
-
// 绘制图片
|
|
244
|
-
return new Promise((resolve, reject) => {
|
|
245
|
-
uni.getImageInfo({
|
|
246
|
-
src: item.src,
|
|
247
|
-
success: async (res) => {
|
|
248
|
-
let clipped = false;
|
|
249
|
-
try {
|
|
250
|
-
if (css.radius) {
|
|
251
|
-
const radius = this.convertRpxToPx(css.radius);
|
|
252
|
-
this.clipRoundRect(ctx, left, top, width, height, radius);
|
|
253
|
-
clipped = true;
|
|
254
|
-
}
|
|
255
|
-
await ctx.drawImage(res.path, left, top, width, height);
|
|
256
|
-
resolve();
|
|
257
|
-
} catch (error) {
|
|
258
|
-
reject(new Error(`海报图片绘制失败: ${error && error.message ? error.message : error}`));
|
|
259
|
-
} finally {
|
|
260
|
-
if (clipped) ctx.restore();
|
|
261
|
-
}
|
|
262
|
-
},
|
|
263
|
-
fail: (e) => {
|
|
264
|
-
reject(new Error(`海报图片加载失败: ${e && e.errMsg ? e.errMsg : e}`));
|
|
265
|
-
}
|
|
266
|
-
});
|
|
267
|
-
});
|
|
268
|
-
|
|
269
|
-
case 'qrcode':
|
|
270
|
-
// 绘制二维码
|
|
271
|
-
if (item.text) {
|
|
272
|
-
// 使用 dd-qrcode 生成二维码图片
|
|
273
|
-
const qrCodeImageUrl = await this.generateQRCode(item.text, width, height);
|
|
274
|
-
const drawn = await ctx.drawImage(qrCodeImageUrl, left, top, width, height);
|
|
275
|
-
if (drawn === false) {
|
|
276
|
-
throw new Error('二维码图片无法绘制到海报画布');
|
|
277
|
-
}
|
|
278
|
-
} else {
|
|
279
|
-
// 绘制二维码占位符
|
|
280
|
-
ctx.setFillStyle('#f5f5f5');
|
|
281
|
-
ctx.fillRect(left, top, width, height);
|
|
282
|
-
ctx.setFillStyle('#999');
|
|
283
|
-
ctx.setFontSize(12);
|
|
284
|
-
ctx.setTextAlign('center');
|
|
285
|
-
ctx.fillText('QR', left + width/2, top + height/2);
|
|
286
|
-
ctx.setTextAlign('left');
|
|
287
|
-
}
|
|
288
|
-
break;
|
|
289
|
-
}
|
|
290
|
-
},
|
|
291
|
-
|
|
292
|
-
/**
|
|
293
|
-
* 绘制圆角矩形
|
|
294
|
-
* @description 绘制指定位置和尺寸的圆角矩形
|
|
295
|
-
* @param {Object} ctx canvas上下文
|
|
296
|
-
* @param {Number} x x坐标
|
|
297
|
-
* @param {Number} y y坐标
|
|
298
|
-
* @param {Number} width 宽度
|
|
299
|
-
* @param {Number} height 高度
|
|
300
|
-
* @param {Number} radius 圆角半径
|
|
301
|
-
* @param {String} fillColor 填充颜色
|
|
302
|
-
* @author jry ijry@qq.com
|
|
303
|
-
*/
|
|
304
|
-
drawRoundRect(ctx, x, y, width, height, radius, fillColor) {
|
|
305
|
-
ctx.save();
|
|
306
|
-
ctx.beginPath();
|
|
307
|
-
ctx.moveTo(x + radius, y);
|
|
308
|
-
ctx.lineTo(x + width - radius, y);
|
|
309
|
-
ctx.quadraticCurveTo(x + width, y, x + width, y + radius);
|
|
310
|
-
ctx.lineTo(x + width, y + height - radius);
|
|
311
|
-
ctx.quadraticCurveTo(x + width, y + height, x + width - radius, y + height);
|
|
312
|
-
ctx.lineTo(x + radius, y + height);
|
|
313
|
-
ctx.quadraticCurveTo(x, y + height, x, y + height - radius);
|
|
314
|
-
ctx.lineTo(x, y + radius);
|
|
315
|
-
ctx.quadraticCurveTo(x, y, x + radius, y);
|
|
316
|
-
ctx.closePath();
|
|
317
|
-
if (fillColor) {
|
|
318
|
-
ctx.setFillStyle(fillColor);
|
|
319
|
-
ctx.fill();
|
|
320
|
-
}
|
|
321
|
-
ctx.restore();
|
|
322
|
-
},
|
|
323
|
-
|
|
324
|
-
/**
|
|
325
|
-
* 裁剪圆角矩形区域
|
|
326
|
-
* @description 在canvas上创建圆角矩形裁剪区域
|
|
327
|
-
* @param {Object} ctx canvas上下文
|
|
328
|
-
* @param {Number} x x坐标
|
|
329
|
-
* @param {Number} y y坐标
|
|
330
|
-
* @param {Number} width 宽度
|
|
331
|
-
* @param {Number} height 高度
|
|
332
|
-
* @param {Number} radius 圆角半径
|
|
333
|
-
* @author jry ijry@qq.com
|
|
334
|
-
*/
|
|
335
|
-
clipRoundRect(ctx, x, y, width, height, radius) {
|
|
336
|
-
ctx.save();
|
|
337
|
-
ctx.beginPath();
|
|
338
|
-
ctx.arc(x + radius, y + radius, radius, Math.PI, Math.PI * 1.5);
|
|
339
|
-
ctx.lineTo(x + width - radius, y);
|
|
340
|
-
ctx.arc(x + width - radius, y + radius, radius, Math.PI * 1.5, Math.PI * 2);
|
|
341
|
-
ctx.lineTo(x + width, y + height - radius);
|
|
342
|
-
ctx.arc(x + width - radius, y + height - radius, radius, 0, Math.PI * 0.5);
|
|
343
|
-
ctx.lineTo(x + radius, y + height);
|
|
344
|
-
ctx.arc(x + radius, y + height - radius, radius, Math.PI * 0.5, Math.PI);
|
|
345
|
-
ctx.closePath();
|
|
346
|
-
ctx.clip();
|
|
347
|
-
},
|
|
348
|
-
|
|
349
|
-
/**
|
|
350
|
-
* 绘制带行数限制的文本
|
|
351
|
-
* @description 绘制可控制最大行数的文本,超出部分显示省略号
|
|
352
|
-
* @param {Object} ctx canvas上下文
|
|
353
|
-
* @param {String} text 文本内容
|
|
354
|
-
* @param {Number} x x坐标
|
|
355
|
-
* @param {Number} y y坐标
|
|
356
|
-
* @param {Number} maxWidth 最大宽度
|
|
357
|
-
* @param {Object} css 样式配置
|
|
358
|
-
* @author jry ijry@qq.com
|
|
359
|
-
*/
|
|
360
|
-
async drawTextWithLineClamp(ctx, text, x, y, maxWidth, css) {
|
|
361
|
-
const lineClamp = Math.max(1, parseInt(css.lineClamp, 10) || 1);
|
|
362
|
-
const lineHeight = css.lineHeight ? this.convertRpxToPx(css.lineHeight) : 20;
|
|
363
|
-
const characters = Array.from(String(text || ''));
|
|
364
|
-
const lines = [];
|
|
365
|
-
const ellipsis = '...';
|
|
366
|
-
const measurementCache = new Map();
|
|
367
|
-
const fontSize = css.fontSize ? this.convertRpxToPx(css.fontSize) : 20;
|
|
368
|
-
// 兜底估算:全角字符约占一个字号,半角约 0.56。测量失败时若按半角估算,
|
|
369
|
-
// 中文会短算 40% 而不换行,所以必须区分宽度。
|
|
370
|
-
const estimateWidth = (value) => Array.from(String(value)).reduce((width, char) => {
|
|
371
|
-
if (/[ᄀ-ᅟ⺀-〾-㏿㐀-䶿一-鿿ꀀ-가-힣豈-︰--⦆¢-₩]/.test(char)) {
|
|
372
|
-
return width + fontSize;
|
|
373
|
-
}
|
|
374
|
-
if (/\s/.test(char)) return width + fontSize * 0.28;
|
|
375
|
-
return width + fontSize * 0.56;
|
|
376
|
-
}, 0);
|
|
377
|
-
const measureTextWidth = async (value) => {
|
|
378
|
-
if (measurementCache.has(value)) {
|
|
379
|
-
return measurementCache.get(value);
|
|
380
|
-
}
|
|
381
|
-
let metrics;
|
|
382
|
-
if (ctx && typeof ctx.measureTextAsync === 'function') {
|
|
383
|
-
metrics = await ctx.measureTextAsync(value);
|
|
384
|
-
} else {
|
|
385
|
-
metrics = ctx.measureText(value);
|
|
386
|
-
}
|
|
387
|
-
let measuredWidth = Number(metrics && metrics.width) || 0;
|
|
388
|
-
// 测量不可用时(鸿蒙同步 measureText 恒返回 0)必须改用估算:
|
|
389
|
-
// 把 0 当作真实宽度会让下面的 <= maxWidth 恒成立,整段文本挤成一行。
|
|
390
|
-
if (!(measuredWidth > 0) && value) {
|
|
391
|
-
measuredWidth = estimateWidth(value);
|
|
392
|
-
}
|
|
393
|
-
measurementCache.set(value, measuredWidth);
|
|
394
|
-
return measuredWidth;
|
|
395
|
-
};
|
|
396
|
-
const findTextFitLength = async (values, suffix = '') => {
|
|
397
|
-
let low = 0;
|
|
398
|
-
let high = values.length;
|
|
399
|
-
while (low < high) {
|
|
400
|
-
const middle = Math.ceil((low + high) / 2);
|
|
401
|
-
const candidate = values.slice(0, middle).join('') + suffix;
|
|
402
|
-
if (await measureTextWidth(candidate) <= maxWidth) {
|
|
403
|
-
low = middle;
|
|
404
|
-
} else {
|
|
405
|
-
high = middle - 1;
|
|
406
|
-
}
|
|
262
|
+
});
|
|
263
|
+
});
|
|
264
|
+
|
|
265
|
+
case 'qrcode':
|
|
266
|
+
// 绘制二维码
|
|
267
|
+
if (item.text) {
|
|
268
|
+
// 使用 dd-qrcode 生成二维码图片
|
|
269
|
+
const qrCodeImageUrl = await generateQRCode(item.text, width, height);
|
|
270
|
+
const drawn = await ctx.drawImage(qrCodeImageUrl, left, top, width, height);
|
|
271
|
+
if (drawn === false) {
|
|
272
|
+
throw new Error('二维码图片无法绘制到海报画布');
|
|
407
273
|
}
|
|
408
|
-
|
|
409
|
-
|
|
274
|
+
} else {
|
|
275
|
+
// 绘制二维码占位符
|
|
276
|
+
ctx.setFillStyle('#f5f5f5');
|
|
277
|
+
ctx.fillRect(left, top, width, height);
|
|
278
|
+
ctx.setFillStyle('#999');
|
|
279
|
+
ctx.setFontSize(12);
|
|
280
|
+
ctx.setTextAlign('center');
|
|
281
|
+
ctx.fillText('QR', left + width/2, top + height/2);
|
|
282
|
+
ctx.setTextAlign('left');
|
|
283
|
+
}
|
|
284
|
+
break;
|
|
285
|
+
}
|
|
286
|
+
}
|
|
410
287
|
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
288
|
+
/**
|
|
289
|
+
* 绘制圆角矩形
|
|
290
|
+
* @description 绘制指定位置和尺寸的圆角矩形
|
|
291
|
+
* @param {Object} ctx canvas上下文
|
|
292
|
+
* @param {Number} x x坐标
|
|
293
|
+
* @param {Number} y y坐标
|
|
294
|
+
* @param {Number} width 宽度
|
|
295
|
+
* @param {Number} height 高度
|
|
296
|
+
* @param {Number} radius 圆角半径
|
|
297
|
+
* @param {String} fillColor 填充颜色
|
|
298
|
+
* @author jry ijry@qq.com
|
|
299
|
+
*/
|
|
300
|
+
function drawRoundRect(ctx: any, x: number, y: number, width: number, height: number, radius: number, fillColor?: any) {
|
|
301
|
+
ctx.save();
|
|
302
|
+
ctx.beginPath();
|
|
303
|
+
ctx.moveTo(x + radius, y);
|
|
304
|
+
ctx.lineTo(x + width - radius, y);
|
|
305
|
+
ctx.quadraticCurveTo(x + width, y, x + width, y + radius);
|
|
306
|
+
ctx.lineTo(x + width, y + height - radius);
|
|
307
|
+
ctx.quadraticCurveTo(x + width, y + height, x + width - radius, y + height);
|
|
308
|
+
ctx.lineTo(x + radius, y + height);
|
|
309
|
+
ctx.quadraticCurveTo(x, y + height, x, y + height - radius);
|
|
310
|
+
ctx.lineTo(x, y + radius);
|
|
311
|
+
ctx.quadraticCurveTo(x, y, x + radius, y);
|
|
312
|
+
ctx.closePath();
|
|
313
|
+
if (fillColor) {
|
|
314
|
+
ctx.setFillStyle(fillColor);
|
|
315
|
+
ctx.fill();
|
|
316
|
+
}
|
|
317
|
+
ctx.restore();
|
|
318
|
+
}
|
|
419
319
|
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
320
|
+
/**
|
|
321
|
+
* 裁剪圆角矩形区域
|
|
322
|
+
* @description 在canvas上创建圆角矩形裁剪区域
|
|
323
|
+
* @param {Object} ctx canvas上下文
|
|
324
|
+
* @param {Number} x x坐标
|
|
325
|
+
* @param {Number} y y坐标
|
|
326
|
+
* @param {Number} width 宽度
|
|
327
|
+
* @param {Number} height 高度
|
|
328
|
+
* @param {Number} radius 圆角半径
|
|
329
|
+
* @author jry ijry@qq.com
|
|
330
|
+
*/
|
|
331
|
+
function clipRoundRect(ctx: any, x: number, y: number, width: number, height: number, radius: number) {
|
|
332
|
+
ctx.save();
|
|
333
|
+
ctx.beginPath();
|
|
334
|
+
ctx.arc(x + radius, y + radius, radius, Math.PI, Math.PI * 1.5);
|
|
335
|
+
ctx.lineTo(x + width - radius, y);
|
|
336
|
+
ctx.arc(x + width - radius, y + radius, radius, Math.PI * 1.5, Math.PI * 2);
|
|
337
|
+
ctx.lineTo(x + width, y + height - radius);
|
|
338
|
+
ctx.arc(x + width - radius, y + height - radius, radius, 0, Math.PI * 0.5);
|
|
339
|
+
ctx.lineTo(x + radius, y + height);
|
|
340
|
+
ctx.arc(x + radius, y + height - radius, radius, Math.PI * 0.5, Math.PI);
|
|
341
|
+
ctx.closePath();
|
|
342
|
+
ctx.clip();
|
|
343
|
+
}
|
|
430
344
|
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
345
|
+
/**
|
|
346
|
+
* 绘制带行数限制的文本
|
|
347
|
+
* @description 绘制可控制最大行数的文本,超出部分显示省略号
|
|
348
|
+
* @param {Object} ctx canvas上下文
|
|
349
|
+
* @param {String} text 文本内容
|
|
350
|
+
* @param {Number} x x坐标
|
|
351
|
+
* @param {Number} y y坐标
|
|
352
|
+
* @param {Number} maxWidth 最大宽度
|
|
353
|
+
* @param {Object} css 样式配置
|
|
354
|
+
* @author jry ijry@qq.com
|
|
355
|
+
*/
|
|
356
|
+
async function drawTextWithLineClamp(ctx: any, text: any, x: number, y: number, maxWidth: number, css: any) {
|
|
357
|
+
const lineClamp = Math.max(1, parseInt(css.lineClamp, 10) || 1);
|
|
358
|
+
const lineHeight = css.lineHeight ? convertRpxToPx(css.lineHeight) : 20;
|
|
359
|
+
const characters = Array.from(String(text || ''));
|
|
360
|
+
const lines: string[] = [];
|
|
361
|
+
const ellipsis = '...';
|
|
362
|
+
const measurementCache = new Map<string, number>();
|
|
363
|
+
const fontSize = css.fontSize ? convertRpxToPx(css.fontSize) : 20;
|
|
364
|
+
// 兜底估算:全角字符约占一个字号,半角约 0.56。测量失败时若按半角估算,
|
|
365
|
+
// 中文会短算 40% 而不换行,所以必须区分宽度。
|
|
366
|
+
const estimateWidth = (value: string) => Array.from(String(value)).reduce((width: number, char: string) => {
|
|
367
|
+
if (/[ᄀ-ᅟ⺀-〾-㏿㐀-䶿一-鿿ꀀ-가-힣豈-︰--⦆¢-₩]/.test(char)) {
|
|
368
|
+
return width + fontSize;
|
|
369
|
+
}
|
|
370
|
+
if (/\s/.test(char)) return width + fontSize * 0.28;
|
|
371
|
+
return width + fontSize * 0.56;
|
|
372
|
+
}, 0);
|
|
373
|
+
const measureTextWidth = async (value: string) => {
|
|
374
|
+
if (measurementCache.has(value)) {
|
|
375
|
+
return measurementCache.get(value)!;
|
|
376
|
+
}
|
|
377
|
+
let metrics: any;
|
|
378
|
+
if (ctx && typeof ctx.measureTextAsync === 'function') {
|
|
379
|
+
metrics = await ctx.measureTextAsync(value);
|
|
380
|
+
} else {
|
|
381
|
+
metrics = ctx.measureText(value);
|
|
382
|
+
}
|
|
383
|
+
let measuredWidth = Number(metrics && metrics.width) || 0;
|
|
384
|
+
// 测量不可用时(鸿蒙同步 measureText 恒返回 0)必须改用估算:
|
|
385
|
+
// 把 0 当作真实宽度会让下面的 <= maxWidth 恒成立,整段文本挤成一行。
|
|
386
|
+
if (!(measuredWidth > 0) && value) {
|
|
387
|
+
measuredWidth = estimateWidth(value);
|
|
388
|
+
}
|
|
389
|
+
measurementCache.set(value, measuredWidth);
|
|
390
|
+
return measuredWidth;
|
|
391
|
+
};
|
|
392
|
+
const findTextFitLength = async (values: string[], suffix = '') => {
|
|
393
|
+
let low = 0;
|
|
394
|
+
let high = values.length;
|
|
395
|
+
while (low < high) {
|
|
396
|
+
const middle = Math.ceil((low + high) / 2);
|
|
397
|
+
const candidate = values.slice(0, middle).join('') + suffix;
|
|
398
|
+
if (await measureTextWidth(candidate) <= maxWidth) {
|
|
399
|
+
low = middle;
|
|
400
|
+
} else {
|
|
401
|
+
high = middle - 1;
|
|
434
402
|
}
|
|
403
|
+
}
|
|
404
|
+
return low;
|
|
405
|
+
};
|
|
435
406
|
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
*/
|
|
451
|
-
async generateQRCode(text, width, height) {
|
|
452
|
-
const qrCodeKey = `${text}_${width}_${height}`;
|
|
453
|
-
if (this.qrCodeMap.has(qrCodeKey)) {
|
|
454
|
-
return this.qrCodeMap.get(qrCodeKey);
|
|
407
|
+
let offset = 0;
|
|
408
|
+
while (offset < characters.length && lines.length < lineClamp) {
|
|
409
|
+
const remaining = characters.slice(offset);
|
|
410
|
+
const remainingText = remaining.join('');
|
|
411
|
+
if (await measureTextWidth(remainingText) <= maxWidth) {
|
|
412
|
+
lines.push(remainingText);
|
|
413
|
+
break;
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
const isLastLine = lines.length === lineClamp - 1;
|
|
417
|
+
if (isLastLine) {
|
|
418
|
+
let suffix = ellipsis;
|
|
419
|
+
while (suffix && await measureTextWidth(suffix) > maxWidth) {
|
|
420
|
+
suffix = suffix.slice(0, -1);
|
|
455
421
|
}
|
|
422
|
+
const fitLength = await findTextFitLength(remaining, suffix);
|
|
423
|
+
lines.push(remaining.slice(0, fitLength).join('') + suffix);
|
|
424
|
+
break;
|
|
425
|
+
}
|
|
456
426
|
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
427
|
+
const fitLength = Math.max(1, await findTextFitLength(remaining));
|
|
428
|
+
lines.push(remaining.slice(0, fitLength).join(''));
|
|
429
|
+
offset += fitLength;
|
|
430
|
+
}
|
|
460
431
|
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
432
|
+
const textBaseLine = css.fontSize ? convertRpxToPx(css.fontSize) / 2 : 10;
|
|
433
|
+
for (let i = 0; i < lines.length; i++) {
|
|
434
|
+
ctx.fillText(lines[i], x, y + (i * lineHeight) + textBaseLine);
|
|
435
|
+
}
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
/**
|
|
439
|
+
* 生成二维码图片
|
|
440
|
+
* @description 根据文本内容生成二维码图片URL
|
|
441
|
+
* @param {String} text 二维码内容
|
|
442
|
+
* @param {Number} width 二维码宽度
|
|
443
|
+
* @param {Number} height 二维码高度
|
|
444
|
+
* @returns {Promise<String>} 二维码图片URL
|
|
445
|
+
* @author jry ijry@qq.com
|
|
446
|
+
*/
|
|
447
|
+
async function generateQRCode(text: string, width: number, height: number) {
|
|
448
|
+
const qrCodeKey = `${text}_${width}_${height}`;
|
|
449
|
+
if (qrCodeMap.has(qrCodeKey)) {
|
|
450
|
+
return qrCodeMap.get(qrCodeKey)!;
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
qrCodeValue.value = text;
|
|
454
|
+
qrCodeSize.value = Math.max(width, height);
|
|
455
|
+
qrCodeShow.value = true;
|
|
456
|
+
|
|
457
|
+
try {
|
|
458
|
+
await nextTick();
|
|
459
|
+
const qrCodeInstance = qrCode.value;
|
|
460
|
+
if (!qrCodeInstance) {
|
|
461
|
+
throw new Error('无法获取二维码组件实例');
|
|
462
|
+
}
|
|
463
|
+
if (typeof qrCodeInstance.refreshCanvas === 'function') {
|
|
464
|
+
await qrCodeInstance.refreshCanvas();
|
|
465
|
+
}
|
|
466
|
+
const generatedPath = await qrCodeInstance._makeCode();
|
|
467
|
+
let tempFilePath = typeof generatedPath === 'string' ? generatedPath : '';
|
|
468
|
+
if (!tempFilePath) {
|
|
469
|
+
const res = await qrCodeInstance.toTempFilePath();
|
|
470
|
+
tempFilePath = res && (res.tempFilePath || res.apFilePath);
|
|
471
|
+
}
|
|
472
|
+
if (!tempFilePath) {
|
|
473
|
+
throw new Error('二维码组件未返回图片路径');
|
|
474
|
+
}
|
|
475
|
+
qrCodeMap.set(qrCodeKey, tempFilePath);
|
|
476
|
+
return tempFilePath;
|
|
477
|
+
} catch (error: any) {
|
|
478
|
+
throw new Error(`二维码生成失败: ${error && error.message ? error.message : error}`);
|
|
479
|
+
} finally {
|
|
480
|
+
qrCodeShow.value = false;
|
|
481
|
+
}
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
/**
|
|
485
|
+
* 将rpx单位转换为px
|
|
486
|
+
* @description 根据屏幕密度将rpx单位转换为px单位
|
|
487
|
+
* @param {String|Number} rpxValue rpx值
|
|
488
|
+
* @returns {Number} 转换后的px值
|
|
489
|
+
* @author jry ijry@qq.com
|
|
490
|
+
*/
|
|
491
|
+
function convertRpxToPx(rpxValue: string | number) {
|
|
492
|
+
if (typeof rpxValue === 'number') return rpxValue;
|
|
493
|
+
|
|
494
|
+
// 使用rpx2px方法
|
|
495
|
+
if (typeof rpxValue === 'string' && rpxValue.endsWith('rpx')) {
|
|
496
|
+
const value = parseFloat(rpxValue);
|
|
497
|
+
return value * getRpxRatio();
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
return parseFloat(rpxValue) || 0;
|
|
501
|
+
}
|
|
502
|
+
|
|
503
|
+
/**
|
|
504
|
+
* 获取 rpx -> px 的统一换算比例
|
|
505
|
+
* @description uni.upx2px 对每个值单独判断设备宽度:当 windowWidth 超过
|
|
506
|
+
* rpxCalcMaxDeviceWidth(默认960)时,只有恰好等于 750 的值使用真实设备宽度,
|
|
507
|
+
* 其余值回退到 rpxCalcBaseDeviceWidth(默认375)。海报需要所有尺寸共用同一
|
|
508
|
+
* 比例,逐值换算会让宽高用上不同基准而破坏宽高比:鸿蒙 windowWidth 上报为
|
|
509
|
+
* 物理像素1084,750rpx算得1084、1114rpx算得557,海报因此变成横向。
|
|
510
|
+
* 这里只取一次比例,且刻意用非 750 的值探测,避开上面的特例分支,
|
|
511
|
+
* 使超宽设备统一走 baseWidth,避免画布被放大到数十MB。
|
|
512
|
+
* @returns {Number} 每 1rpx 对应的 px 值
|
|
513
|
+
* @author jry ijry@qq.com
|
|
514
|
+
*/
|
|
515
|
+
function getRpxRatio() {
|
|
516
|
+
const PROBE = 1000; // 不能用 750,750 是 upx2px 的特例值
|
|
517
|
+
const ratio = rpx2px(PROBE) / PROBE;
|
|
518
|
+
return ratio > 0 ? ratio : 1;
|
|
519
|
+
}
|
|
520
|
+
|
|
521
|
+
/**
|
|
522
|
+
* 绘制渐变背景
|
|
523
|
+
* @description 绘制线性渐变或径向渐变背景
|
|
524
|
+
* @param {Object} ctx canvas上下文
|
|
525
|
+
* @param {Object} css 样式配置
|
|
526
|
+
* @param {Number} left 左边距
|
|
527
|
+
* @param {Number} top 上边距
|
|
528
|
+
* @param {Number} width 宽度
|
|
529
|
+
* @param {Number} height 高度
|
|
530
|
+
* @author jry ijry@qq.com
|
|
531
|
+
*/
|
|
532
|
+
function drawGradientBackground(ctx: any, css: any, left: number, top: number, width: number, height: number) {
|
|
533
|
+
const background = css.background;
|
|
534
|
+
let gradient: any = null;
|
|
535
|
+
|
|
536
|
+
// 处理线性渐变
|
|
537
|
+
if (background.includes('linear-gradient')) {
|
|
538
|
+
// 解析线性渐变角度和颜色
|
|
539
|
+
const angleMatch = background.match(/linear-gradient\((\d+)deg/);
|
|
540
|
+
const angle = angleMatch ? parseInt(angleMatch[1]) : 135;
|
|
487
541
|
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
542
|
+
// 根据角度计算渐变起点和终点
|
|
543
|
+
let startX = left, startY = top, endX = left + width, endY = top + height;
|
|
544
|
+
|
|
545
|
+
// 简化的角度处理(支持常见角度)
|
|
546
|
+
if (angle === 0) {
|
|
547
|
+
startX = left;
|
|
548
|
+
startY = top + height;
|
|
549
|
+
endX = left;
|
|
550
|
+
endY = top;
|
|
551
|
+
} else if (angle === 90) {
|
|
552
|
+
startX = left;
|
|
553
|
+
startY = top;
|
|
554
|
+
endX = left + width;
|
|
555
|
+
endY = top;
|
|
556
|
+
} else if (angle === 180) {
|
|
557
|
+
startX = left;
|
|
558
|
+
startY = top;
|
|
559
|
+
endX = left;
|
|
560
|
+
endY = top + height;
|
|
561
|
+
} else if (angle === 270) {
|
|
562
|
+
startX = left + width;
|
|
563
|
+
startY = top;
|
|
564
|
+
endX = left;
|
|
565
|
+
endY = top;
|
|
566
|
+
}
|
|
567
|
+
|
|
568
|
+
gradient = ctx.createLinearGradient(startX, startY, endX, endY);
|
|
569
|
+
|
|
570
|
+
// 解析颜色值
|
|
571
|
+
const colorMatches = background.match(/#[0-9a-fA-F]+|rgba?\([^)]+\)/g);
|
|
572
|
+
if (colorMatches && colorMatches.length >= 2) {
|
|
573
|
+
// 添加渐变色点
|
|
574
|
+
colorMatches.forEach((color: string, index: number) => {
|
|
575
|
+
const stop = index / (colorMatches.length - 1);
|
|
576
|
+
gradient.addColorStop(stop, color);
|
|
577
|
+
});
|
|
578
|
+
}
|
|
579
|
+
}
|
|
580
|
+
// 处理径向渐变
|
|
581
|
+
else if (background.includes('radial-gradient')) {
|
|
582
|
+
// 径向渐变从中心开始
|
|
583
|
+
const centerX = left + width / 2;
|
|
584
|
+
const centerY = top + height / 2;
|
|
585
|
+
const radius = Math.min(width, height) / 2;
|
|
586
|
+
|
|
587
|
+
gradient = ctx.createRadialGradient(centerX, centerY, 0, centerX, centerY, radius);
|
|
588
|
+
|
|
589
|
+
// 解析颜色值
|
|
590
|
+
const colorMatches = background.match(/#[0-9a-fA-F]+|rgba?\([^)]+\)/g);
|
|
591
|
+
if (colorMatches && colorMatches.length >= 2) {
|
|
592
|
+
// 添加渐变色点
|
|
593
|
+
colorMatches.forEach((color: string, index: number) => {
|
|
594
|
+
const stop = index / (colorMatches.length - 1);
|
|
595
|
+
gradient.addColorStop(stop, color);
|
|
596
|
+
});
|
|
597
|
+
}
|
|
598
|
+
}
|
|
599
|
+
|
|
600
|
+
if (gradient) {
|
|
601
|
+
ctx.setFillStyle(gradient);
|
|
602
|
+
// 处理圆角
|
|
603
|
+
if (css.radius) {
|
|
604
|
+
const radius = convertRpxToPx(css.radius);
|
|
605
|
+
drawRoundRect(ctx, left, top, width, height, radius, gradient);
|
|
606
|
+
} else {
|
|
607
|
+
ctx.fillRect(left, top, width, height);
|
|
608
|
+
}
|
|
609
|
+
}
|
|
610
|
+
}
|
|
503
611
|
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
const PROBE = 1000; // 不能用 750,750 是 upx2px 的特例值
|
|
521
|
-
const ratio = rpx2px(PROBE) / PROBE;
|
|
522
|
-
return ratio > 0 ? ratio : 1;
|
|
523
|
-
},
|
|
612
|
+
/**
|
|
613
|
+
* 将dataURL转换为Blob
|
|
614
|
+
* @description H5环境下将base64格式的dataURL转换为Blob对象
|
|
615
|
+
* @param {String} dataURL base64格式的图片数据
|
|
616
|
+
* @returns {Blob} Blob对象
|
|
617
|
+
* @author jry ijry@qq.com
|
|
618
|
+
*/
|
|
619
|
+
function dataURLToBlob(dataURL: any) {
|
|
620
|
+
// 检查是否为H5环境且是base64数据
|
|
621
|
+
// #ifdef H5
|
|
622
|
+
if (dataURL && dataURL.startsWith('data:image')) {
|
|
623
|
+
const parts = dataURL.split(';base64,');
|
|
624
|
+
const contentType = parts[0].split(':')[1];
|
|
625
|
+
const raw = window.atob(parts[1]);
|
|
626
|
+
const rawLength = raw.length;
|
|
627
|
+
const uInt8Array = new Uint8Array(rawLength);
|
|
524
628
|
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
* @param {Object} ctx canvas上下文
|
|
529
|
-
* @param {Object} css 样式配置
|
|
530
|
-
* @param {Number} left 左边距
|
|
531
|
-
* @param {Number} top 上边距
|
|
532
|
-
* @param {Number} width 宽度
|
|
533
|
-
* @param {Number} height 高度
|
|
534
|
-
* @author jry ijry@qq.com
|
|
535
|
-
*/
|
|
536
|
-
drawGradientBackground(ctx, css, left, top, width, height) {
|
|
537
|
-
const background = css.background;
|
|
538
|
-
let gradient = null;
|
|
539
|
-
|
|
540
|
-
// 处理线性渐变
|
|
541
|
-
if (background.includes('linear-gradient')) {
|
|
542
|
-
// 解析线性渐变角度和颜色
|
|
543
|
-
const angleMatch = background.match(/linear-gradient\((\d+)deg/);
|
|
544
|
-
const angle = angleMatch ? parseInt(angleMatch[1]) : 135;
|
|
545
|
-
|
|
546
|
-
// 根据角度计算渐变起点和终点
|
|
547
|
-
let startX = left, startY = top, endX = left + width, endY = top + height;
|
|
548
|
-
|
|
549
|
-
// 简化的角度处理(支持常见角度)
|
|
550
|
-
if (angle === 0) {
|
|
551
|
-
startX = left;
|
|
552
|
-
startY = top + height;
|
|
553
|
-
endX = left;
|
|
554
|
-
endY = top;
|
|
555
|
-
} else if (angle === 90) {
|
|
556
|
-
startX = left;
|
|
557
|
-
startY = top;
|
|
558
|
-
endX = left + width;
|
|
559
|
-
endY = top;
|
|
560
|
-
} else if (angle === 180) {
|
|
561
|
-
startX = left;
|
|
562
|
-
startY = top;
|
|
563
|
-
endX = left;
|
|
564
|
-
endY = top + height;
|
|
565
|
-
} else if (angle === 270) {
|
|
566
|
-
startX = left + width;
|
|
567
|
-
startY = top;
|
|
568
|
-
endX = left;
|
|
569
|
-
endY = top;
|
|
570
|
-
}
|
|
571
|
-
|
|
572
|
-
gradient = ctx.createLinearGradient(startX, startY, endX, endY);
|
|
573
|
-
|
|
574
|
-
// 解析颜色值
|
|
575
|
-
const colorMatches = background.match(/#[0-9a-fA-F]+|rgba?\([^)]+\)/g);
|
|
576
|
-
if (colorMatches && colorMatches.length >= 2) {
|
|
577
|
-
// 添加渐变色点
|
|
578
|
-
colorMatches.forEach((color, index) => {
|
|
579
|
-
const stop = index / (colorMatches.length - 1);
|
|
580
|
-
gradient.addColorStop(stop, color);
|
|
581
|
-
});
|
|
582
|
-
}
|
|
583
|
-
}
|
|
584
|
-
// 处理径向渐变
|
|
585
|
-
else if (background.includes('radial-gradient')) {
|
|
586
|
-
// 径向渐变从中心开始
|
|
587
|
-
const centerX = left + width / 2;
|
|
588
|
-
const centerY = top + height / 2;
|
|
589
|
-
const radius = Math.min(width, height) / 2;
|
|
590
|
-
|
|
591
|
-
gradient = ctx.createRadialGradient(centerX, centerY, 0, centerX, centerY, radius);
|
|
592
|
-
|
|
593
|
-
// 解析颜色值
|
|
594
|
-
const colorMatches = background.match(/#[0-9a-fA-F]+|rgba?\([^)]+\)/g);
|
|
595
|
-
if (colorMatches && colorMatches.length >= 2) {
|
|
596
|
-
// 添加渐变色点
|
|
597
|
-
colorMatches.forEach((color, index) => {
|
|
598
|
-
const stop = index / (colorMatches.length - 1);
|
|
599
|
-
gradient.addColorStop(stop, color);
|
|
600
|
-
});
|
|
601
|
-
}
|
|
602
|
-
}
|
|
603
|
-
|
|
604
|
-
if (gradient) {
|
|
605
|
-
ctx.setFillStyle(gradient);
|
|
606
|
-
// 处理圆角
|
|
607
|
-
if (css.radius) {
|
|
608
|
-
const radius = this.convertRpxToPx(css.radius);
|
|
609
|
-
this.drawRoundRect(ctx, left, top, width, height, radius, gradient);
|
|
610
|
-
} else {
|
|
611
|
-
ctx.fillRect(left, top, width, height);
|
|
612
|
-
}
|
|
613
|
-
}
|
|
614
|
-
},
|
|
629
|
+
for (let i = 0; i < rawLength; ++i) {
|
|
630
|
+
uInt8Array[i] = raw.charCodeAt(i);
|
|
631
|
+
}
|
|
615
632
|
|
|
616
|
-
|
|
617
|
-
* 将dataURL转换为Blob
|
|
618
|
-
* @description H5环境下将base64格式的dataURL转换为Blob对象
|
|
619
|
-
* @param {String} dataURL base64格式的图片数据
|
|
620
|
-
* @returns {Blob} Blob对象
|
|
621
|
-
* @author jry ijry@qq.com
|
|
622
|
-
*/
|
|
623
|
-
dataURLToBlob(dataURL) {
|
|
624
|
-
// 检查是否为H5环境且是base64数据
|
|
625
|
-
// #ifdef H5
|
|
626
|
-
if (dataURL && dataURL.startsWith('data:image')) {
|
|
627
|
-
const parts = dataURL.split(';base64,');
|
|
628
|
-
const contentType = parts[0].split(':')[1];
|
|
629
|
-
const raw = window.atob(parts[1]);
|
|
630
|
-
const rawLength = raw.length;
|
|
631
|
-
const uInt8Array = new Uint8Array(rawLength);
|
|
632
|
-
|
|
633
|
-
for (let i = 0; i < rawLength; ++i) {
|
|
634
|
-
uInt8Array[i] = raw.charCodeAt(i);
|
|
635
|
-
}
|
|
636
|
-
|
|
637
|
-
return new Blob([uInt8Array], { type: contentType });
|
|
638
|
-
}
|
|
639
|
-
// #endif
|
|
640
|
-
|
|
641
|
-
return null;
|
|
642
|
-
},
|
|
633
|
+
return new Blob([uInt8Array], { type: contentType });
|
|
643
634
|
}
|
|
635
|
+
// #endif
|
|
636
|
+
|
|
637
|
+
return null;
|
|
644
638
|
}
|
|
639
|
+
|
|
640
|
+
defineExpose({
|
|
641
|
+
exportImage,
|
|
642
|
+
flushPosterCanvas,
|
|
643
|
+
getPosterCanvas,
|
|
644
|
+
drawItem,
|
|
645
|
+
drawRoundRect,
|
|
646
|
+
clipRoundRect,
|
|
647
|
+
drawTextWithLineClamp,
|
|
648
|
+
generateQRCode,
|
|
649
|
+
convertRpxToPx,
|
|
650
|
+
getRpxRatio,
|
|
651
|
+
drawGradientBackground,
|
|
652
|
+
dataURLToBlob,
|
|
653
|
+
getTextStyle
|
|
654
|
+
})
|
|
645
655
|
</script>
|
|
646
656
|
|
|
647
657
|
<style lang="scss" scoped>
|
|
658
|
+
@import '../../scss/variables';
|
|
659
|
+
@import '../../scss/mixins';
|
|
648
660
|
.dd-poster {
|
|
649
661
|
position: relative;
|
|
650
662
|
|