@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.
Files changed (60) hide show
  1. package/README.md +7 -7
  2. package/components/dd-album/dd-album.vue +280 -228
  3. package/components/dd-avatar/dd-avatar.vue +132 -75
  4. package/components/dd-avatar-group/dd-avatar-group.vue +52 -30
  5. package/components/dd-barcode/dd-barcode.vue +884 -947
  6. package/components/dd-button/dd-button.vue +47 -21
  7. package/components/dd-canvas/dd-canvas.vue +1156 -985
  8. package/components/dd-car-keyboard/dd-car-keyboard.vue +12 -12
  9. package/components/dd-champion-card/dd-champion-card.vue +49 -21
  10. package/components/dd-checkbox-group/dd-checkbox-group.vue +2 -0
  11. package/components/dd-copy/dd-copy.vue +51 -56
  12. package/components/dd-cropper/dd-cropper.vue +1224 -1135
  13. package/components/dd-divider/dd-divider.vue +9 -2
  14. package/components/dd-fold/dd-fold.vue +133 -0
  15. package/components/dd-form/dd-form.vue +2 -0
  16. package/components/dd-icon/dd-icon.vue +1 -1
  17. package/components/dd-keyboard/dd-keyboard.vue +95 -46
  18. package/components/dd-link/dd-link.vue +72 -49
  19. package/components/dd-markdown/dd-markdown.vue +154 -154
  20. package/components/dd-number-keyboard/dd-number-keyboard.vue +109 -94
  21. package/components/dd-parse/dd-parse.vue +473 -442
  22. package/components/dd-poster/dd-poster.vue +579 -567
  23. package/components/dd-pull-refresh/dd-pull-refresh.vue +1 -9
  24. package/components/dd-qrcode/dd-qrcode.vue +431 -405
  25. package/components/dd-radio-group/dd-radio-group.vue +1 -1
  26. package/components/dd-segmented-tab/dd-segmented-tab.vue +22 -14
  27. package/components/dd-signature/dd-signature.vue +377 -357
  28. package/components/dd-step/dd-step.vue +1 -1
  29. package/components/dd-sticky/dd-sticky.vue +5 -10
  30. package/components/dd-swipe-action/dd-swipe-action.vue +1 -9
  31. package/components/dd-swipe-item/dd-swipe-item.vue +2 -0
  32. package/components/dd-swipeable-tab/dd-swipeable-tab.vue +1 -5
  33. package/components/dd-switch/dd-switch.vue +1 -1
  34. package/components/dd-text/dd-text.vue +304 -82
  35. package/components/dd-waterfall/dd-waterfall.vue +89 -0
  36. package/index.ts +8 -6
  37. package/libs/function/index.d.ts +88 -0
  38. package/libs/function/index.js +14 -0
  39. package/libs/function/test.d.ts +65 -0
  40. package/package.json +1 -1
  41. package/scss/_mixins.scss +8 -0
  42. package/scss/_theme-tokens.scss +7 -0
  43. package/scss/_variables.scss +283 -156
  44. package/components/dd-album/album.js +0 -19
  45. package/components/dd-album/props.js +0 -89
  46. package/components/dd-avatar/avatar.js +0 -19
  47. package/components/dd-avatar/props.js +0 -84
  48. package/components/dd-avatar-group/avatarGroup.js +0 -14
  49. package/components/dd-avatar-group/props.js +0 -57
  50. package/components/dd-keyboard/keyboard.js +0 -19
  51. package/components/dd-keyboard/props.js +0 -79
  52. package/components/dd-link/link.js +0 -17
  53. package/components/dd-link/props.js +0 -44
  54. package/components/dd-number-keyboard/numberKeyboard.js +0 -8
  55. package/components/dd-number-keyboard/props.js +0 -24
  56. package/components/dd-parse/props.js +0 -57
  57. package/components/dd-text/props.js +0 -120
  58. package/components/dd-text/text.js +0 -30
  59. package/components/dd-text/value.js +0 -87
  60. package/scss/_dd-shared.scss +0 -40
@@ -1,30 +1,30 @@
1
- <template>
1
+ <template>
2
2
  <view class="dd-cropper">
3
- <dd-canvas ref="avatarCanvas" :canvas-id="'avatar-canvas-' + instanceId" class="my-canvas" :width="windowWidth || 0" :height="windowHeight || 0" :style="{ top: styleTop, height: cvsStyleHeight, zIndex: zc }" bg-color="transparent"></dd-canvas>
4
- <dd-canvas ref="operCanvas" :canvas-id="'oper-canvas-' + instanceId" class="oper-canvas" :width="windowWidth || 0" :height="windowHeight || 0" :style="{ top: styleTop, height: cvsStyleHeight, zIndex: zcOper }" bg-color="transparent" @touchstart="start" @touchmove="move" @touchend="end"></dd-canvas>
5
- <dd-canvas ref="prvCanvas" :canvas-id="'prv-canvas-' + instanceId" class="prv-canvas" :width="windowWidth || 0" :height="windowHeight || 0" bg-color="transparent" @touchstart="hideImg" :style="{ height: cvsStyleHeight, top: prvTop, zIndex: zcPrv }"></dd-canvas>
6
- <view class="oper-wrapper" :style="{ display: styleDisplay, zIndex: zcBar }">
7
- <view class="oper">
8
- <view class="btn-wrapper" v-if="showOper">
9
- <view @click="select" hover-class="hover" :style="{ width: btnWidth }">
3
+ <dd-canvas ref="avatarCanvas" :canvas-id="'avatar-canvas-' + instanceId" class="dd-cropper__avatar-canvas" :width="windowWidth || 0" :height="windowHeight || 0" :style="{ top: styleTop, height: cvsStyleHeight, zIndex: zc }" bg-color="transparent"></dd-canvas>
4
+ <dd-canvas ref="operCanvas" :canvas-id="'oper-canvas-' + instanceId" class="dd-cropper__oper-canvas" :width="windowWidth || 0" :height="windowHeight || 0" :style="{ top: styleTop, height: cvsStyleHeight, zIndex: zcOper }" bg-color="transparent" @touchstart="start" @touchmove="move" @touchend="end"></dd-canvas>
5
+ <dd-canvas ref="prvCanvas" :canvas-id="'prv-canvas-' + instanceId" class="dd-cropper__preview-canvas" :width="windowWidth || 0" :height="windowHeight || 0" bg-color="transparent" @touchstart="hideImg" :style="{ height: cvsStyleHeight, top: prvTop, zIndex: zcPrv }"></dd-canvas>
6
+ <view class="dd-cropper__oper-wrapper" :style="{ display: styleDisplay, zIndex: zcBar }">
7
+ <view class="dd-cropper__oper">
8
+ <view class="dd-cropper__btn-wrapper" v-if="showOper">
9
+ <view @click="select" hover-class="dd-cropper__btn-hover" :style="{ width: btnWidth }">
10
10
  <text>重选</text>
11
11
  </view>
12
- <view @click="close" hover-class="hover" :style="{ width: btnWidth }">
12
+ <view @click="close" hover-class="dd-cropper__btn-hover" :style="{ width: btnWidth }">
13
13
  <text>关闭</text>
14
14
  </view>
15
- <view @click="rotate" hover-class="hover" :style="{ width: btnWidth, display: btnDsp }">
15
+ <view @click="rotate" hover-class="dd-cropper__btn-hover" :style="{ width: btnWidth, display: btnDsp }">
16
16
  <text>旋转</text>
17
17
  </view>
18
- <view @click="preview" hover-class="hover" :style="{ width: btnWidth }">
18
+ <view @click="preview" hover-class="dd-cropper__btn-hover" :style="{ width: btnWidth }">
19
19
  <text>预览</text>
20
20
  </view>
21
- <view @click="confirm" hover-class="hover" :style="{ width: btnWidth }">
21
+ <view @click="confirm" hover-class="dd-cropper__btn-hover" :style="{ width: btnWidth }">
22
22
  <text>确定</text>
23
23
  </view>
24
24
  </view>
25
- <view class="clr-wrapper" v-else>
26
- <slider class="my-slider" @change="colorChange" block-size="25" value="0" min="-100" max="100" activeColor="red" backgroundColor="green" block-color="grey" show-value></slider>
27
- <view @click="prvUpload" hover-class="hover" :style="{ width: btnWidth }">
25
+ <view class="dd-cropper__clear-wrapper" v-else>
26
+ <slider class="dd-cropper__slider" @change="colorChange" block-size="25" value="0" min="-100" max="100" activeColor="red" backgroundColor="green" block-color="grey" show-value></slider>
27
+ <view @click="prvUpload" hover-class="dd-cropper__btn-hover" :style="{ width: btnWidth }">
28
28
  <text>确定</text>
29
29
  </view>
30
30
  </view>
@@ -36,1191 +36,1280 @@
36
36
  </view>
37
37
  </template>
38
38
 
39
- <script>
39
+ <script setup lang="ts">
40
+ import { nextTick, onMounted, ref } from 'vue'
40
41
  import zIndexConfig from '../../libs/config/zIndex.js'
41
42
 
42
- // 画布系组件(dd-canvas / dd-signature / dd-qrcode 同族)统一采用 Options API,
43
- // 命令式 canvas 状态挂在 this 上不走响应式,避免触摸高频重绘的代理开销。
43
+ // 画布系组件(dd-canvas / dd-signature / dd-qrcode 同族)命令式 canvas 状态挂在 setup 作用域的
44
+ // 普通变量上、不走响应式,避免触摸高频重绘的代理开销。
44
45
 
45
46
  // 底部操作条高度(px),与样式 .oper-wrapper 的 height 保持一致
46
47
  const tabHeight = 50;
47
48
 
48
- // z-index 统一取 libs/config/zIndex.js(cropper 档 + 显式偏移;Options API 经 data 暴露给模板)。
49
+ // z-index 统一取 libs/config/zIndex.js(cropper 档 + 显式偏移;经 ref 暴露给模板)。
49
50
  // ×2 原语义:预览层须盖过工作画布与操作条。
50
51
  const Z_CROPPER = zIndexConfig.cropper
51
52
  const Z_CROPPER_OPER = Z_CROPPER + 1
52
53
  const Z_CROPPER_PRV = Z_CROPPER * 2
53
54
  const Z_CROPPER_BAR = Z_CROPPER + 9
54
55
 
55
- export default {
56
- name: "dd-cropper",
57
- emits: ['avtinit', 'confirm', 'cancel'],
58
- props: {
59
- // 最小缩放倍数
60
- minScale: {
61
- type: [Number, String],
62
- default: 0.3
63
- },
64
- // 最大缩放倍数
65
- maxScale: {
66
- type: [Number, String],
67
- default: 4
68
- },
69
- // 是否允许双指缩放
70
- canScale: {
71
- type: Boolean,
72
- default: true
73
- },
74
- // 是否允许旋转(inner 模式下不可用)
75
- canRotate: {
76
- type: Boolean,
77
- default: true
78
- },
79
- lockWidth: {
80
- type: String,
81
- default: ''
82
- },
83
- lockHeight: {
84
- type: String,
85
- default: ''
86
- },
87
- // 拉伸模式:x / y / long / short / longSel / shortSel
88
- stretch: {
89
- type: String,
90
- default: ''
91
- },
92
- // 锁定模式:x / y / long / short / longSel / shortSel
93
- lock: {
94
- type: String,
95
- default: ''
96
- },
97
- // 页面无底部 tabbar 时保持 true
98
- noTab: {
99
- type: Boolean,
100
- default: true
101
- },
102
- // 内部模式:隐藏旋转按钮、按钮加宽
103
- inner: {
104
- type: Boolean,
105
- default: false
106
- },
107
- // 导出图片质量(0-1),仅对压缩格式生效
108
- quality: {
109
- type: [Number, String],
110
- default: 0.9
111
- },
112
- // 实例标识,confirm 事件原样回传
113
- index: {
114
- type: [Number, String],
115
- default: ''
116
- },
117
- // 是否允许拖拽四角控制点调整裁剪框大小
118
- canChangeSize: {
119
- type: Boolean,
120
- default: false
121
- },
122
- // 裁剪区域宽度,如 '300rpx'
123
- areaWidth: {
124
- type: String,
125
- default: '300rpx'
126
- },
127
- // 裁剪区域高度,如 '300rpx'
128
- areaHeight: {
129
- type: String,
130
- default: '300rpx'
131
- },
132
- // 导出图片宽度,如 '260rpx'
133
- exportWidth: {
134
- type: String,
135
- default: '260rpx'
136
- },
137
- // 导出图片高度,如 '260rpx'
138
- exportHeight: {
139
- type: String,
140
- default: '260rpx'
141
- },
142
- // 画布填充色,默认透明,可传 'black'、'#ffffff' 等
143
- fillColor: {
144
- type: String,
145
- default: 'transparent'
146
- },
147
- },
148
- data() {
149
- return {
150
- // 实例ID用于区分不同实例的 canvas-id
151
- instanceId: Date.now() + '-' + Math.random().toString(36).substring(2, 11),
152
- cvsStyleHeight: '0px',
153
- styleDisplay: 'none',
154
- styleTop: '-10000px',
155
- prvTop: '-10000px',
156
- selStyle: {},
157
- showOper: true,
158
- // confirm 回载荷(avatar 字段)
159
- imgSrc: {
160
- imgSrc: ''
161
- },
162
- btnWidth: '19%',
163
- btnDsp: 'flex',
164
- // 导出图片宽度(px,经 rpx 换算)
165
- expWidth: '',
166
- // 导出图片高度(px,经 rpx 换算)
167
- expHeight: '',
168
- // 是否允许调整裁剪框大小
169
- letChangeSize: false,
170
- // 底部安全区大小
171
- safeAreaInsetsBottom: 0,
172
- // z-index 档位(libs/config/zIndex.js cropper 档派生,见 script 头部常量)
173
- zc: Z_CROPPER,
174
- zcOper: Z_CROPPER_OPER,
175
- zcPrv: Z_CROPPER_PRV,
176
- zcBar: Z_CROPPER_BAR,
177
- };
178
- },
179
- created() {
180
- this.ctxCanvas = null;
181
- this.ctxCanvasOper = null;
182
- this.ctxCanvasPrv = null;
183
- this.qlty = parseInt(this.quality) || 0.9;
184
- this.letRotate = (this.canRotate === false || this.inner === true) ? 0 : 1;
185
- this.letScale = this.canScale === false ? 0 : 1;
186
- this.letChangeSize = this.canChangeSize;
187
- this.isin = this.inner === true ? 1 : 0;
188
- this.indx = this.index || undefined;
189
- this.mnScale = this.minScale || 0.3;
190
- this.mxScale = this.maxScale || 4;
191
- this.noBar = this.noTab === true ? 1 : 0;
192
- this.stc = this.stretch;
193
- this.lck = this.lock;
194
- if (this.isin) {
195
- this.btnWidth = '24%';
196
- this.btnDsp = 'none';
197
- } else {
198
- this.btnWidth = '19%';
199
- this.btnDsp = 'flex';
200
- }
201
-
202
- if (this.noBar) {
203
- this.moreHeight = 0;
204
- this.windowResize();
205
- } else {
206
- uni.showTabBar({
207
- complete: (res) => {
208
- this.moreHeight = (res.errMsg === 'showTabBar:ok') ? 50 : 0;
209
- this.windowResize();
210
- }
211
- });
212
- }
213
- },
214
- mounted() {
215
- this.initCanvasRefs(true);
216
- },
217
- methods: {
218
- async initCanvasRefs(force = false) {
219
- await this.$nextTick();
220
- const avatarCanvas = this.$refs.avatarCanvas;
221
- const operCanvas = this.$refs.operCanvas;
222
- const prvCanvas = this.$refs.prvCanvas;
223
- if (!avatarCanvas || !operCanvas || !prvCanvas) return false;
224
- await Promise.all([
225
- avatarCanvas.initCanvas(force),
226
- operCanvas.initCanvas(force),
227
- prvCanvas.initCanvas(force)
228
- ]);
229
- this.ctxCanvas = avatarCanvas;
230
- this.ctxCanvasOper = operCanvas;
231
- this.ctxCanvasPrv = prvCanvas;
232
- return true;
233
- },
234
- windowResize() {
235
- let sysInfo = uni.getSystemInfoSync();
236
- this.platform = sysInfo.platform;
237
- this.pixelRatio = sysInfo.pixelRatio;
238
- this.windowWidth = sysInfo.windowWidth;
239
- this.safeAreaInsetsBottom = sysInfo.safeAreaInsets.bottom;
240
- // #ifdef H5
241
- this.drawTop = sysInfo.windowTop;
242
- this.windowHeight = sysInfo.windowHeight + sysInfo.windowBottom;
243
- this.cvsStyleHeight = this.windowHeight - tabHeight + 'px';
244
- // #endif
245
- // #ifdef APP-PLUS
246
- if (this.platform === 'android') {
247
- this.windowHeight = sysInfo.screenHeight + sysInfo.statusBarHeight;
248
- this.cvsStyleHeight = this.windowHeight - tabHeight + 'px';
249
- } else {
250
- this.windowHeight = sysInfo.windowHeight + this.moreHeight;
251
- this.cvsStyleHeight = this.windowHeight - tabHeight - this.safeAreaInsetsBottom + 6 + 'px';
252
- }
253
- // #endif
254
- // #ifdef MP
255
- this.windowHeight = sysInfo.windowHeight + this.moreHeight;
256
- this.cvsStyleHeight = this.windowHeight - tabHeight - this.safeAreaInsetsBottom - 2 + 'px';
257
- // #endif
258
- this.pxRatio = this.windowWidth / 750;
56
+ interface Props {
57
+ // 最小缩放倍数
58
+ minScale?: number | string;
59
+ // 最大缩放倍数
60
+ maxScale?: number | string;
61
+ // 是否允许双指缩放
62
+ canScale?: boolean;
63
+ // 是否允许旋转(inner 模式下不可用)
64
+ canRotate?: boolean;
65
+ lockWidth?: string;
66
+ lockHeight?: string;
67
+ // 拉伸模式:x / y / long / short / longSel / shortSel
68
+ stretch?: string;
69
+ // 锁定模式:x / y / long / short / longSel / shortSel
70
+ lock?: string;
71
+ // 页面无底部 tabbar 时保持 true
72
+ noTab?: boolean;
73
+ // 内部模式:隐藏旋转按钮、按钮加宽
74
+ inner?: boolean;
75
+ // 导出图片质量(0-1),仅对压缩格式生效
76
+ quality?: number | string;
77
+ // 实例标识,confirm 事件原样回传
78
+ index?: number | string;
79
+ // 是否允许拖拽四角控制点调整裁剪框大小
80
+ canChangeSize?: boolean;
81
+ // 裁剪区域宽度,如 '300rpx'
82
+ areaWidth?: string;
83
+ // 裁剪区域高度,如 '300rpx'
84
+ areaHeight?: string;
85
+ // 导出图片宽度,如 '260rpx'
86
+ exportWidth?: string;
87
+ // 导出图片高度,如 '260rpx'
88
+ exportHeight?: string;
89
+ // 画布填充色,默认透明,可传 'black'、'#ffffff' 等
90
+ fillColor?: string;
91
+ }
259
92
 
260
- this.expWidth && (this.expWidth = this.expWidth.indexOf('rpx') >= 0 ? parseInt(this.expWidth) * this.pxRatio : parseInt(this.expWidth));
261
- this.expHeight && (this.expHeight = this.expHeight.indexOf('rpx') >= 0 ? parseInt(this.expHeight) * this.pxRatio : parseInt(this.expHeight));
93
+ const props = withDefaults(defineProps<Props>(), {
94
+ minScale: 0.3,
95
+ maxScale: 4,
96
+ canScale: true,
97
+ canRotate: true,
98
+ lockWidth: '',
99
+ lockHeight: '',
100
+ stretch: '',
101
+ lock: '',
102
+ noTab: true,
103
+ inner: false,
104
+ quality: 0.9,
105
+ index: '',
106
+ canChangeSize: false,
107
+ areaWidth: '300rpx',
108
+ areaHeight: '300rpx',
109
+ exportWidth: '260rpx',
110
+ exportHeight: '260rpx',
111
+ fillColor: 'transparent'
112
+ });
113
+
114
+ const emit = defineEmits<{
115
+ (e: 'avtinit'): void;
116
+ (e: 'confirm', val: { avatar: any; path: any; index: any; data: any }): void;
117
+ (e: 'cancel'): void;
118
+ }>();
119
+
120
+ // 实例ID用于区分不同实例的 canvas-id
121
+ const instanceId = ref(Date.now() + '-' + Math.random().toString(36).substring(2, 11));
122
+ const cvsStyleHeight = ref('0px');
123
+ const styleDisplay = ref('none');
124
+ const styleTop = ref('-10000px');
125
+ const prvTop = ref('-10000px');
126
+ const selStyle = ref<any>({});
127
+ const showOper = ref(true);
128
+ // confirm 回载荷(avatar 字段)
129
+ const imgSrc = ref<any>({
130
+ imgSrc: ''
131
+ });
132
+ const btnWidth = ref('19%');
133
+ const btnDsp = ref('flex');
134
+ // 导出图片宽度(px,经 rpx 换算)
135
+ const expWidth = ref<any>('');
136
+ // 导出图片高度(px,经 rpx 换算)
137
+ const expHeight = ref<any>('');
138
+ // 是否允许调整裁剪框大小
139
+ const letChangeSize = ref(false);
140
+ // 底部安全区大小
141
+ const safeAreaInsetsBottom = ref(0);
142
+ // z-index 档位(libs/config/zIndex.js cropper 档派生,见 script 头部常量)
143
+ const zc = ref(Z_CROPPER);
144
+ const zcOper = ref(Z_CROPPER_OPER);
145
+ const zcPrv = ref(Z_CROPPER_PRV);
146
+ const zcBar = ref(Z_CROPPER_BAR);
147
+
148
+ // 画布组件实例(原 $refs)
149
+ const avatarCanvas = ref<any>(null);
150
+ const operCanvas = ref<any>(null);
151
+ const prvCanvas = ref<any>(null);
152
+
153
+ // 以下状态与 Options API 时期一致:挂在实例上、不走响应式
154
+ let ctxCanvas: any = null;
155
+ let ctxCanvasOper: any = null;
156
+ let ctxCanvasPrv: any = null;
157
+ let qlty: any = 0.9;
158
+ let letRotate: any = 1;
159
+ let letScale: any = 1;
160
+ let isin: any = 0;
161
+ let indx: any = undefined;
162
+ let mnScale: any = 0.3;
163
+ let mxScale: any = 4;
164
+ let noBar: any = 1;
165
+ let stc: any = '';
166
+ let lck: any = '';
167
+ let moreHeight = 0;
168
+ let platform: any = undefined;
169
+ let pixelRatio: any = undefined;
170
+ let windowWidth = 0;
171
+ let windowHeight = 0;
172
+ let drawTop = 0;
173
+ let pxRatio = 0;
174
+ let imgPath: any = undefined;
175
+ let imgWidth = 0;
176
+ let imgHeight = 0;
177
+ let path: any = undefined;
178
+ let hasSel: any = false;
179
+ let fixWidth = 0;
180
+ let fixHeight = 0;
181
+ let lckWidth = 0;
182
+ let lckHeight = 0;
183
+ let scaleWidth = 0;
184
+ let scaleHeight = 0;
185
+ let scaleSize = 1;
186
+ let rotateDeg = 0;
187
+ let posWidth = 0;
188
+ let posHeight = 0;
189
+ let useWidth = 0;
190
+ let useHeight = 0;
191
+ let drawTm = 0;
192
+ let prvTm = 0;
193
+ let _drawToken = 0;
194
+ let fSelecting = false;
195
+ let fUploading = false;
196
+ let fPrvUploading = false;
197
+ let fPreviewing = false;
198
+ let fRotateing = false;
199
+ let prvImg = '';
200
+ let prvImgData: any = null;
201
+ let prvImgTmp: any = null;
202
+ let target: any = null;
203
+ let prvX = 0;
204
+ let prvY = 0;
205
+ let prvWidth = 0;
206
+ let prvHeight = 0;
207
+ let rtn: any = undefined;
208
+ let resizeHandle: any = null;
209
+ let touch0: any = null;
210
+ let touch1: any = null;
211
+ let fgDistance = 0;
212
+
213
+ // created(保持 Options API 时期的初始化时序:setup 阶段即执行)
214
+ qlty = parseInt(props.quality as any) || 0.9;
215
+ letRotate = (props.canRotate === false || props.inner === true) ? 0 : 1;
216
+ letScale = props.canScale === false ? 0 : 1;
217
+ letChangeSize.value = props.canChangeSize;
218
+ isin = props.inner === true ? 1 : 0;
219
+ indx = props.index || undefined;
220
+ mnScale = props.minScale || 0.3;
221
+ mxScale = props.maxScale || 4;
222
+ noBar = props.noTab === true ? 1 : 0;
223
+ stc = props.stretch;
224
+ lck = props.lock;
225
+ if (isin) {
226
+ btnWidth.value = '24%';
227
+ btnDsp.value = 'none';
228
+ } else {
229
+ btnWidth.value = '19%';
230
+ btnDsp.value = 'flex';
231
+ }
262
232
 
263
- if (this.styleDisplay === 'flex') {
264
- this.drawInit(true);
265
- }
266
- this.hideImg();
267
- },
268
- select() {
269
- if (this.fSelecting) return;
270
- this.fSelecting = true;
271
- setTimeout(() => { this.fSelecting = false; }, 500);
272
- uni.chooseImage({
273
- count: 1,
274
- sizeType: ['original', 'compressed'],
275
- sourceType: ['album', 'camera'],
276
- success: (r) => {
277
- uni.showLoading({ mask: true });
278
- let path = this.imgPath = r.tempFilePaths[0];
279
- uni.getImageInfo({
280
- src: path,
281
- success: r => {
282
- this.imgWidth = r.width;
283
- this.imgHeight = r.height;
284
- this.path = path;
285
- if (!this.hasSel) {
286
- let style = this.selStyle || {};
287
- if (this.areaWidth && this.areaHeight) {
288
- let areaWidth = this.areaWidth.indexOf('rpx') >= 0 ? parseInt(this.areaWidth) * this.pxRatio : parseInt(this.areaWidth),
289
- areaHeight = this.areaHeight.indexOf('rpx') >= 0 ? parseInt(this.areaHeight) * this.pxRatio : parseInt(this.areaHeight);
290
- style.width = areaWidth + 'px';
291
- style.height = areaHeight + 'px';
292
- style.top = (this.windowHeight - areaHeight - tabHeight) / 2 + 'px';
293
- style.left = (this.windowWidth - areaWidth) / 2 + 'px';
294
- } else {
295
- uni.showModal({
296
- title: "裁剪框的宽或高没有设置",
297
- showCancel: false
298
- })
299
- return;
300
- }
301
- this.selStyle = style;
302
- }
233
+ if (noBar) {
234
+ moreHeight = 0;
235
+ windowResize();
236
+ } else {
237
+ uni.showTabBar({
238
+ complete: (res: any) => {
239
+ moreHeight = (res.errMsg === 'showTabBar:ok') ? 50 : 0;
240
+ windowResize();
241
+ }
242
+ });
243
+ }
303
244
 
304
- if (this.noBar) {
305
- this.drawInit(true);
306
- } else {
307
- uni.hideTabBar({
308
- complete: () => {
309
- this.drawInit(true);
310
- }
311
- });
312
- }
313
- },
314
- fail: () => {
315
- uni.showToast({
316
- title: "读取图片失败",
317
- duration: 2000,
318
- })
319
- },
320
- complete() {
321
- uni.hideLoading();
322
- }
323
- });
324
- },
325
- fail: () => {
326
- this.$emit('cancel')
327
- }
328
- })
329
- },
330
- async confirm() {
331
- if (this.fUploading) return;
332
- this.fUploading = true;
333
- setTimeout(() => { this.fUploading = false; }, 1000)
334
-
335
- let style = this.selStyle,
336
- x = parseInt(style.left),
337
- y = parseInt(style.top),
338
- width = parseInt(style.width),
339
- height = parseInt(style.height),
340
- expWidth = this.expWidth || width,
341
- expHeight = this.expHeight || height;
245
+ onMounted(() => {
246
+ initCanvasRefs(true);
247
+ });
248
+
249
+ async function initCanvasRefs(force = false) {
250
+ await nextTick();
251
+ const avatarCanvasRef = avatarCanvas.value;
252
+ const operCanvasRef = operCanvas.value;
253
+ const prvCanvasRef = prvCanvas.value;
254
+ if (!avatarCanvasRef || !operCanvasRef || !prvCanvasRef) return false;
255
+ await Promise.all([
256
+ avatarCanvasRef.initCanvas(force),
257
+ operCanvasRef.initCanvas(force),
258
+ prvCanvasRef.initCanvas(force)
259
+ ]);
260
+ ctxCanvas = avatarCanvasRef;
261
+ ctxCanvasOper = operCanvasRef;
262
+ ctxCanvasPrv = prvCanvasRef;
263
+ return true;
264
+ }
342
265
 
343
- // #ifdef H5
344
- expWidth = width;
345
- expHeight = height;
346
- // #endif
266
+ function windowResize() {
267
+ const sysInfo: any = uni.getSystemInfoSync();
268
+ platform = sysInfo.platform;
269
+ pixelRatio = sysInfo.pixelRatio;
270
+ windowWidth = sysInfo.windowWidth;
271
+ safeAreaInsetsBottom.value = sysInfo.safeAreaInsets.bottom;
272
+ // #ifdef H5
273
+ drawTop = sysInfo.windowTop;
274
+ windowHeight = sysInfo.windowHeight + sysInfo.windowBottom;
275
+ cvsStyleHeight.value = windowHeight - tabHeight + 'px';
276
+ // #endif
277
+ // #ifdef APP-PLUS
278
+ if (platform === 'android') {
279
+ windowHeight = sysInfo.screenHeight + sysInfo.statusBarHeight;
280
+ cvsStyleHeight.value = windowHeight - tabHeight + 'px';
281
+ } else {
282
+ windowHeight = sysInfo.windowHeight + moreHeight;
283
+ cvsStyleHeight.value = windowHeight - tabHeight - safeAreaInsetsBottom.value + 6 + 'px';
284
+ }
285
+ // #endif
286
+ // #ifdef MP
287
+ windowHeight = sysInfo.windowHeight + moreHeight;
288
+ cvsStyleHeight.value = windowHeight - tabHeight - safeAreaInsetsBottom.value - 2 + 'px';
289
+ // #endif
290
+ pxRatio = windowWidth / 750;
291
+
292
+ expWidth.value && (expWidth.value = expWidth.value.indexOf('rpx') >= 0 ? parseInt(expWidth.value) * pxRatio : parseInt(expWidth.value));
293
+ expHeight.value && (expHeight.value = expHeight.value.indexOf('rpx') >= 0 ? parseInt(expHeight.value) * pxRatio : parseInt(expHeight.value));
294
+
295
+ if (styleDisplay.value === 'flex') {
296
+ drawInit(true);
297
+ }
298
+ hideImg();
299
+ }
347
300
 
301
+ function select() {
302
+ if (fSelecting) return;
303
+ fSelecting = true;
304
+ setTimeout(() => { fSelecting = false; }, 500);
305
+ uni.chooseImage({
306
+ count: 1,
307
+ sizeType: ['original', 'compressed'],
308
+ sourceType: ['album', 'camera'],
309
+ success: (r: any) => {
348
310
  uni.showLoading({ mask: true });
349
- this.styleDisplay = 'none';
350
- this.styleTop = '-10000px';
351
- this.hasSel = false;
352
- this.hideImg();
353
- await this.initCanvasRefs();
354
- this.ctxCanvas.toTempFilePath({
355
- x: x,
356
- y: y,
357
- width: width,
358
- height: height,
359
- destWidth: expWidth,
360
- destHeight: expHeight,
361
- fileType: 'png',
362
- quality: this.qlty,
363
- success: (r) => {
364
- r = r.tempFilePath;
365
- // #ifdef H5
366
- this.btop(r).then(async (r) => {
367
- if (this.expWidth && this.expHeight) {
368
- let ctxCanvas = this.ctxCanvas;
369
- expWidth = this.expWidth,
370
- expHeight = this.expHeight;
371
-
372
- // 立即模式画布:清掉裁剪大图再画导出小图,防止透明像素透出底图
373
- ctxCanvas.clearRect(0, 0, this.windowWidth, this.windowHeight);
374
- await ctxCanvas.drawImage(r, 0, 0, expWidth, expHeight);
375
- ctxCanvas.draw(false, () => {
376
- ctxCanvas.toTempFilePath({
377
- x: 0,
378
- y: 0,
379
- width: expWidth,
380
- height: expHeight,
381
- destWidth: expWidth,
382
- destHeight: expHeight,
383
- fileType: 'png',
384
- quality: this.qlty,
385
- success: (r) => {
386
- r = r.tempFilePath;
387
- this.btop(r).then((r) => {
388
- this.$emit("confirm", { avatar: this.imgSrc, path: r, index: this.indx, data: this.rtn });
389
- });
390
- },
391
- fail: () => {
392
- uni.showToast({
393
- title: "导出图片失败",
394
- duration: 2000,
395
- })
396
- }
397
- });
398
- });
311
+ imgPath = r.tempFilePaths[0];
312
+ const selectedPath = imgPath;
313
+ uni.getImageInfo({
314
+ src: selectedPath,
315
+ success: (res: any) => {
316
+ imgWidth = res.width;
317
+ imgHeight = res.height;
318
+ path = selectedPath;
319
+ if (!hasSel) {
320
+ const style = selStyle.value || {};
321
+ if (props.areaWidth && props.areaHeight) {
322
+ let areaWidth = props.areaWidth.indexOf('rpx') >= 0 ? parseInt(props.areaWidth) * pxRatio : parseInt(props.areaWidth),
323
+ areaHeight = props.areaHeight.indexOf('rpx') >= 0 ? parseInt(props.areaHeight) * pxRatio : parseInt(props.areaHeight);
324
+ style.width = areaWidth + 'px';
325
+ style.height = areaHeight + 'px';
326
+ style.top = (windowHeight - areaHeight - tabHeight) / 2 + 'px';
327
+ style.left = (windowWidth - areaWidth) / 2 + 'px';
399
328
  } else {
400
- this.$emit("confirm", { avatar: this.imgSrc, path: r, index: this.indx, data: this.rtn });
329
+ uni.showModal({
330
+ title: "裁剪框的宽或高没有设置",
331
+ showCancel: false
332
+ })
333
+ return;
401
334
  }
402
- })
403
- // #endif
404
- // #ifndef H5
405
- this.$emit("confirm", { avatar: this.imgSrc, path: r, index: this.indx, data: this.rtn });
406
- // #endif
335
+ selStyle.value = style;
336
+ }
337
+
338
+ if (noBar) {
339
+ drawInit(true);
340
+ } else {
341
+ uni.hideTabBar({
342
+ complete: () => {
343
+ drawInit(true);
344
+ }
345
+ });
346
+ }
407
347
  },
408
348
  fail: () => {
409
349
  uni.showToast({
410
- title: "导出图片失败",
350
+ title: "读取图片失败",
411
351
  duration: 2000,
412
352
  })
413
353
  },
414
- complete: () => {
354
+ complete() {
415
355
  uni.hideLoading();
416
- this.noBar || uni.showTabBar();
417
356
  }
418
357
  });
419
358
  },
420
- // 预览模式下点击"确认":将预览画布的裁剪结果导出
421
- async prvUpload() {
422
- if (this.fPrvUploading) return;
423
- this.fPrvUploading = true;
424
- setTimeout(() => { this.fPrvUploading = false; }, 1000)
425
-
426
- let prvX = this.prvX,
427
- prvY = this.prvY,
428
- prvWidth = this.prvWidth,
429
- prvHeight = this.prvHeight,
430
- expWidth = this.expWidth || prvWidth,
431
- expHeight = this.expHeight || prvHeight;
359
+ fail: () => {
360
+ emit('cancel')
361
+ }
362
+ })
363
+ }
432
364
 
365
+ async function confirm() {
366
+ if (fUploading) return;
367
+ fUploading = true;
368
+ setTimeout(() => { fUploading = false; }, 1000)
369
+
370
+ const style = selStyle.value,
371
+ x = parseInt(style.left),
372
+ y = parseInt(style.top),
373
+ width = parseInt(style.width),
374
+ height = parseInt(style.height);
375
+ let expW = expWidth.value || width;
376
+ let expH = expHeight.value || height;
377
+
378
+ // #ifdef H5
379
+ expW = width;
380
+ expH = height;
381
+ // #endif
382
+
383
+ uni.showLoading({ mask: true });
384
+ styleDisplay.value = 'none';
385
+ styleTop.value = '-10000px';
386
+ hasSel = false;
387
+ hideImg();
388
+ await initCanvasRefs();
389
+ ctxCanvas.toTempFilePath({
390
+ x: x,
391
+ y: y,
392
+ width: width,
393
+ height: height,
394
+ destWidth: expW,
395
+ destHeight: expH,
396
+ fileType: 'png',
397
+ quality: qlty,
398
+ success: (res: any) => {
399
+ const tempPath = res.tempFilePath;
433
400
  // #ifdef H5
434
- expWidth = prvWidth;
435
- expHeight = prvHeight;
401
+ btop(tempPath).then(async (url: any) => {
402
+ if (expWidth.value && expHeight.value) {
403
+ const canvasRef = ctxCanvas;
404
+ expW = expWidth.value,
405
+ expH = expHeight.value;
406
+
407
+ // 立即模式画布:清掉裁剪大图再画导出小图,防止透明像素透出底图
408
+ canvasRef.clearRect(0, 0, windowWidth, windowHeight);
409
+ await canvasRef.drawImage(url, 0, 0, expW, expH);
410
+ canvasRef.draw(false, () => {
411
+ canvasRef.toTempFilePath({
412
+ x: 0,
413
+ y: 0,
414
+ width: expW,
415
+ height: expH,
416
+ destWidth: expW,
417
+ destHeight: expH,
418
+ fileType: 'png',
419
+ quality: qlty,
420
+ success: (out: any) => {
421
+ const outPath = out.tempFilePath;
422
+ btop(outPath).then((finalPath: any) => {
423
+ emit("confirm", { avatar: imgSrc.value, path: finalPath, index: indx, data: rtn });
424
+ });
425
+ },
426
+ fail: () => {
427
+ uni.showToast({
428
+ title: "导出图片失败",
429
+ duration: 2000,
430
+ })
431
+ }
432
+ });
433
+ });
434
+ } else {
435
+ emit("confirm", { avatar: imgSrc.value, path: url, index: indx, data: rtn });
436
+ }
437
+ })
436
438
  // #endif
439
+ // #ifndef H5
440
+ emit("confirm", { avatar: imgSrc.value, path: tempPath, index: indx, data: rtn });
441
+ // #endif
442
+ },
443
+ fail: () => {
444
+ uni.showToast({
445
+ title: "导出图片失败",
446
+ duration: 2000,
447
+ })
448
+ },
449
+ complete: () => {
450
+ uni.hideLoading();
451
+ noBar || uni.showTabBar();
452
+ }
453
+ });
454
+ }
437
455
 
438
- uni.showLoading({ mask: true });
439
-
440
- this.styleDisplay = 'none';
441
- this.styleTop = '-10000px';
442
- this.hasSel = false;
443
- this.hideImg();
444
- await this.initCanvasRefs();
445
- this.ctxCanvasPrv.toTempFilePath({
446
- x: prvX,
447
- y: prvY,
448
- width: prvWidth,
449
- height: prvHeight,
450
- destWidth: expWidth,
451
- destHeight: expHeight,
452
- fileType: 'png',
453
- quality: this.qlty,
454
- success: (r) => {
455
- r = r.tempFilePath;
456
- // #ifdef H5
457
- this.btop(r).then(async (r) => {
458
- if (this.expWidth && this.expHeight) {
459
- let ctxCanvas = this.ctxCanvas;
460
- expWidth = this.expWidth,
461
- expHeight = this.expHeight;
462
-
463
- // 立即模式画布:清掉裁剪大图再画导出小图,防止透明像素透出底图
464
- ctxCanvas.clearRect(0, 0, this.windowWidth, this.windowHeight);
465
- await ctxCanvas.drawImage(r, 0, 0, expWidth, expHeight);
466
- ctxCanvas.draw(false, () => {
467
- ctxCanvas.toTempFilePath({
468
- x: 0,
469
- y: 0,
470
- width: expWidth,
471
- height: expHeight,
472
- destWidth: expWidth,
473
- destHeight: expHeight,
474
- fileType: 'png',
475
- quality: this.qlty,
476
- success: (r) => {
477
- r = r.tempFilePath;
478
- this.btop(r).then((r) => {
479
- this.$emit("confirm", { avatar: this.imgSrc, path: r, index: this.indx, data: this.rtn });
480
- });
481
- },
482
- fail: () => {
483
- uni.showToast({
484
- title: "导出图片失败",
485
- duration: 2000,
486
- })
487
- }
456
+ // 预览模式下点击"确认":将预览画布的裁剪结果导出
457
+ async function prvUpload() {
458
+ if (fPrvUploading) return;
459
+ fPrvUploading = true;
460
+ setTimeout(() => { fPrvUploading = false; }, 1000)
461
+
462
+ const curX = prvX,
463
+ curY = prvY,
464
+ curWidth = prvWidth,
465
+ curHeight = prvHeight;
466
+ let expW = expWidth.value || curWidth;
467
+ let expH = expHeight.value || curHeight;
468
+
469
+ // #ifdef H5
470
+ expW = curWidth;
471
+ expH = curHeight;
472
+ // #endif
473
+
474
+ uni.showLoading({ mask: true });
475
+
476
+ styleDisplay.value = 'none';
477
+ styleTop.value = '-10000px';
478
+ hasSel = false;
479
+ hideImg();
480
+ await initCanvasRefs();
481
+ ctxCanvasPrv.toTempFilePath({
482
+ x: curX,
483
+ y: curY,
484
+ width: curWidth,
485
+ height: curHeight,
486
+ destWidth: expW,
487
+ destHeight: expH,
488
+ fileType: 'png',
489
+ quality: qlty,
490
+ success: (res: any) => {
491
+ const tempPath = res.tempFilePath;
492
+ // #ifdef H5
493
+ btop(tempPath).then(async (url: any) => {
494
+ if (expWidth.value && expHeight.value) {
495
+ const canvasRef = ctxCanvas;
496
+ expW = expWidth.value,
497
+ expH = expHeight.value;
498
+
499
+ // 立即模式画布:清掉裁剪大图再画导出小图,防止透明像素透出底图
500
+ canvasRef.clearRect(0, 0, windowWidth, windowHeight);
501
+ await canvasRef.drawImage(url, 0, 0, expW, expH);
502
+ canvasRef.draw(false, () => {
503
+ canvasRef.toTempFilePath({
504
+ x: 0,
505
+ y: 0,
506
+ width: expW,
507
+ height: expH,
508
+ destWidth: expW,
509
+ destHeight: expH,
510
+ fileType: 'png',
511
+ quality: qlty,
512
+ success: (out: any) => {
513
+ const outPath = out.tempFilePath;
514
+ btop(outPath).then((finalPath: any) => {
515
+ emit("confirm", { avatar: imgSrc.value, path: finalPath, index: indx, data: rtn });
488
516
  });
489
- });
490
- } else {
491
- this.$emit("confirm", { avatar: this.imgSrc, path: r, index: this.indx, data: this.rtn });
492
- }
493
- })
494
- // #endif
495
- // #ifndef H5
496
- this.$emit("confirm", { avatar: this.imgSrc, path: r, index: this.indx, data: this.rtn });
497
- // #endif
498
- },
499
- fail: () => {
500
- uni.showToast({
501
- title: "导出图片失败",
502
- duration: 2000,
503
- })
504
- },
505
- complete: () => {
506
- uni.hideLoading();
507
- this.noBar || uni.showTabBar();
517
+ },
518
+ fail: () => {
519
+ uni.showToast({
520
+ title: "导出图片失败",
521
+ duration: 2000,
522
+ })
523
+ }
524
+ });
525
+ });
526
+ } else {
527
+ emit("confirm", { avatar: imgSrc.value, path: url, index: indx, data: rtn });
508
528
  }
509
- });
529
+ })
530
+ // #endif
531
+ // #ifndef H5
532
+ emit("confirm", { avatar: imgSrc.value, path: tempPath, index: indx, data: rtn });
533
+ // #endif
510
534
  },
511
- async drawInit(ini = false) {
512
- await this.initCanvasRefs();
513
- let allWidth = this.windowWidth,
514
- allHeight = this.windowHeight,
515
- imgWidth = this.imgWidth,
516
- imgHeight = this.imgHeight,
517
- imgRadio = imgWidth / imgHeight,
518
- useWidth = allWidth - 40,
519
- useHeight = allHeight - tabHeight - 80,
520
- selWidth = parseInt(this.selStyle.width),
521
- selHeight = parseInt(this.selStyle.height);
522
-
523
- this.fixWidth = 0;
524
- this.fixHeight = 0;
525
- this.lckWidth = 0;
526
- this.lckHeight = 0;
527
- switch (this.stc) {
528
- case 'x': this.fixWidth = 1; break;
529
- case 'y': this.fixHeight = 1; break;
530
- case 'long': if (imgRadio > 1) this.fixWidth = 1; else this.fixHeight = 1; break;
531
- case 'short': if (imgRadio > 1) this.fixHeight = 1; else this.fixWidth = 1; break;
532
- case 'longSel': if (selWidth > selHeight) this.fixWidth = 1; else this.fixHeight = 1; break;
533
- case 'shortSel': if (selWidth > selHeight) this.fixHeight = 1; else this.fixWidth = 1; break;
534
- }
535
- // lck 锁定裁剪框的宽/高调整方向:
536
- // 'x' 锁宽 / 'y' 锁高 / 'long'/'short' 按图片长短边 / 'longSel'/'shortSel' 按选择框长短边
537
- switch (this.lck) {
538
- case 'x': this.lckWidth = 1; break;
539
- case 'y': this.lckHeight = 1; break;
540
- case 'long': if (imgRadio > 1) this.lckWidth = 1; else this.lckHeight = 1; break;
541
- case 'short': if (imgRadio > 1) this.lckHeight = 1; else this.lckWidth = 1; break;
542
- case 'longSel': if (selWidth > selHeight) this.lckWidth = 1; else this.lckHeight = 1; break;
543
- case 'shortSel': if (selWidth > selHeight) this.lckHeight = 1; else this.lckWidth = 1; break;
535
+ fail: () => {
536
+ uni.showToast({
537
+ title: "导出图片失败",
538
+ duration: 2000,
539
+ })
540
+ },
541
+ complete: () => {
542
+ uni.hideLoading();
543
+ noBar || uni.showTabBar();
544
+ }
545
+ });
546
+ }
547
+
548
+ async function drawInit(ini = false) {
549
+ await initCanvasRefs();
550
+ const allWidth = windowWidth,
551
+ allHeight = windowHeight,
552
+ imgW = imgWidth,
553
+ imgH = imgHeight,
554
+ imgRadio = imgW / imgH;
555
+ let useW = allWidth - 40,
556
+ useH = allHeight - tabHeight - 80;
557
+ const selW = parseInt(selStyle.value.width),
558
+ selH = parseInt(selStyle.value.height);
559
+
560
+ fixWidth = 0;
561
+ fixHeight = 0;
562
+ lckWidth = 0;
563
+ lckHeight = 0;
564
+ switch (stc) {
565
+ case 'x': fixWidth = 1; break;
566
+ case 'y': fixHeight = 1; break;
567
+ case 'long': if (imgRadio > 1) fixWidth = 1; else fixHeight = 1; break;
568
+ case 'short': if (imgRadio > 1) fixHeight = 1; else fixWidth = 1; break;
569
+ case 'longSel': if (selW > selH) fixWidth = 1; else fixHeight = 1; break;
570
+ case 'shortSel': if (selW > selH) fixHeight = 1; else fixWidth = 1; break;
571
+ }
572
+ // lck 锁定裁剪框的宽/高调整方向:
573
+ // 'x' 锁宽 / 'y' 锁高 / 'long'/'short' 按图片长短边 / 'longSel'/'shortSel' 按选择框长短边
574
+ switch (lck) {
575
+ case 'x': lckWidth = 1; break;
576
+ case 'y': lckHeight = 1; break;
577
+ case 'long': if (imgRadio > 1) lckWidth = 1; else lckHeight = 1; break;
578
+ case 'short': if (imgRadio > 1) lckHeight = 1; else lckWidth = 1; break;
579
+ case 'longSel': if (selW > selH) lckWidth = 1; else lckHeight = 1; break;
580
+ case 'shortSel': if (selW > selH) lckHeight = 1; else lckWidth = 1; break;
581
+ }
582
+ if (fixWidth) {
583
+ useW = selW;
584
+ useH = useW / imgRadio;
585
+ } else if (fixHeight) {
586
+ useH = selH;
587
+ useW = useH * imgRadio;
588
+ } else if (imgRadio < 1) {
589
+ if (imgH < useH) {
590
+ useW = imgW;
591
+ useH = imgH;
592
+ } else {
593
+ useH = useH;
594
+ useW = useH * imgRadio;
595
+ }
596
+ } else {
597
+ if (imgW < useW) {
598
+ useW = imgW;
599
+ useH = imgH;
600
+ } else {
601
+ useW = useW;
602
+ useH = useW / imgRadio;
603
+ }
604
+ }
605
+ if (isin) {
606
+ scaleWidth = 0;
607
+ scaleHeight = 0;
608
+ if (useW < selW) {
609
+ useW = selW;
610
+ useH = useW / imgRadio;
611
+ lckHeight = 0;
612
+ }
613
+ if (useH < selH) {
614
+ useH = selH;
615
+ useW = useH * imgRadio;
616
+ lckWidth = 0;
617
+ }
618
+ }
619
+
620
+ scaleSize = 1;
621
+ rotateDeg = 0;
622
+ posWidth = (allWidth - useW) / 2;
623
+ posHeight = (allHeight - useH - tabHeight) / 2;
624
+ useWidth = useW;
625
+ useHeight = useH;
626
+
627
+ const style = selStyle.value,
628
+ left = parseInt(style.left),
629
+ top = parseInt(style.top),
630
+ width = parseInt(style.width),
631
+ height = parseInt(style.height),
632
+ canvasRef = ctxCanvas,
633
+ canvasOperRef = ctxCanvasOper;
634
+
635
+ // H5/小程序 type=2d 为立即模式画布,重绘遮罩前须清掉上一帧,否则拖控制点时遮罩残留
636
+ canvasOperRef.clearRect(0, 0, windowWidth, windowHeight);
637
+
638
+ canvasOperRef.setLineWidth(3);
639
+ canvasOperRef.setStrokeStyle('grey');
640
+ canvasOperRef.setGlobalAlpha(0.4);
641
+ canvasOperRef.setFillStyle('black');
642
+ canvasOperRef.strokeRect(left, top, width, height);
643
+ canvasOperRef.fillRect(0, 0, windowWidth, top);
644
+ canvasOperRef.fillRect(0, top, left, height);
645
+ canvasOperRef.fillRect(0, top + height, windowWidth, windowHeight - height - top - tabHeight);
646
+ canvasOperRef.fillRect(left + width, top, windowWidth - width - left, height);
647
+ canvasOperRef.setStrokeStyle('red');
648
+ canvasOperRef.moveTo(left + 20, top); canvasOperRef.lineTo(left, top); canvasOperRef.lineTo(left, top + 20);
649
+ canvasOperRef.moveTo(left + width - 20, top); canvasOperRef.lineTo(left + width, top); canvasOperRef.lineTo(left + width, top + 20);
650
+ canvasOperRef.moveTo(left + 20, top + height); canvasOperRef.lineTo(left, top + height); canvasOperRef.lineTo(left, top + height - 20);
651
+ canvasOperRef.moveTo(left + width - 20, top + height); canvasOperRef.lineTo(left + width, top + height); canvasOperRef.lineTo(left + width, top + height - 20);
652
+
653
+ // 四角控制点(canChangeSize 时可拖拽调整裁剪框)
654
+ const controlPointSize = 10;
655
+ canvasOperRef.setFillStyle('white');
656
+ canvasOperRef.setStrokeStyle('grey');
657
+ canvasOperRef.setLineWidth(1);
658
+ canvasOperRef.fillRect(left - controlPointSize / 2, top - controlPointSize / 2, controlPointSize, controlPointSize);
659
+ canvasOperRef.strokeRect(left - controlPointSize / 2, top - controlPointSize / 2, controlPointSize, controlPointSize);
660
+ canvasOperRef.fillRect(left + width - controlPointSize / 2, top - controlPointSize / 2, controlPointSize, controlPointSize);
661
+ canvasOperRef.strokeRect(left + width - controlPointSize / 2, top - controlPointSize / 2, controlPointSize, controlPointSize);
662
+ canvasOperRef.fillRect(left - controlPointSize / 2, top + height - controlPointSize / 2, controlPointSize, controlPointSize);
663
+ canvasOperRef.strokeRect(left - controlPointSize / 2, top + height - controlPointSize / 2, controlPointSize, controlPointSize);
664
+ canvasOperRef.fillRect(left + width - controlPointSize / 2, top + height - controlPointSize / 2, controlPointSize, controlPointSize);
665
+ canvasOperRef.strokeRect(left + width - controlPointSize / 2, top + height - controlPointSize / 2, controlPointSize, controlPointSize);
666
+
667
+ canvasOperRef.stroke();
668
+
669
+ canvasOperRef.draw(false, () => {
670
+ if (ini) {
671
+ styleDisplay.value = 'flex';
672
+ // #ifdef H5
673
+ styleTop.value = drawTop + 'px';
674
+ // #endif
675
+ // #ifndef H5
676
+ styleTop.value = '0';
677
+ // #endif
678
+ if (props.fillColor && props.fillColor !== 'transparent') {
679
+ canvasRef.setFillStyle(props.fillColor);
544
680
  }
545
- if (this.fixWidth) {
546
- useWidth = selWidth;
547
- useHeight = useWidth / imgRadio;
548
- } else if (this.fixHeight) {
549
- useHeight = selHeight;
550
- useWidth = useHeight * imgRadio;
551
- } else if (imgRadio < 1) {
552
- if (imgHeight < useHeight) {
553
- useWidth = imgWidth;
554
- useHeight = imgHeight;
555
- } else {
556
- useHeight = useHeight;
557
- useWidth = useHeight * imgRadio;
558
- }
681
+ drawImage();
682
+ }
683
+ });
684
+
685
+ emit("avtinit");
686
+ }
687
+
688
+ async function drawImage() {
689
+ await initCanvasRefs();
690
+ const tm_now = Date.now();
691
+ if (tm_now - drawTm < 20) return;
692
+ drawTm = tm_now;
693
+ const canvasRef = ctxCanvas;
694
+ // 图片对象先取回(dd-canvas 内部有缓存,二次绘制走同步路径);
695
+ // 加载期间若新帧已触发(token 失配)则丢弃本帧,避免旧帧续体把图片画回旧位置
696
+ _drawToken = (_drawToken || 0) + 1;
697
+ const token = _drawToken;
698
+ const image = await canvasRef.loadImage(imgPath).catch(() => null);
699
+ if (!image || token !== _drawToken) return;
700
+ // H5/小程序 type=2d 为立即模式画布,ctx.draw() 在 dd-canvas 内是 no-op,
701
+ // 上一帧像素不会自动清空——拖动残影的根因,每帧必须先 clearRect
702
+ canvasRef.clearRect(0, 0, windowWidth, windowHeight);
703
+ if (props.fillColor && props.fillColor !== 'transparent') {
704
+ canvasRef.fillRect(0, 0, windowWidth, windowHeight - tabHeight);
705
+ }
706
+ canvasRef.save();
707
+ canvasRef.translate(posWidth + useWidth / 2, posHeight + useHeight / 2);
708
+ canvasRef.scale(scaleSize, scaleSize);
709
+ canvasRef.rotate(rotateDeg * Math.PI / 180);
710
+ canvasRef.drawImage(image, -useWidth / 2, -useHeight / 2, useWidth, useHeight);
711
+ canvasRef.restore();
712
+ canvasRef.draw(false);
713
+ }
714
+
715
+ function hideImg() {
716
+ prvImg = '';
717
+ prvTop.value = '-10000px';
718
+ showOper.value = true;
719
+ prvImgData = null;
720
+ target = null;
721
+ }
722
+
723
+ function close() {
724
+ styleDisplay.value = 'none';
725
+ styleTop.value = '-10000px';
726
+ hasSel = false;
727
+ hideImg();
728
+ noBar || uni.showTabBar();
729
+ emit('cancel');
730
+ }
731
+
732
+ async function preview() {
733
+ if (fPreviewing) return;
734
+ fPreviewing = true;
735
+ setTimeout(() => { fPreviewing = false; }, 1000);
736
+ const style = selStyle.value,
737
+ x = parseInt(style.left),
738
+ y = parseInt(style.top),
739
+ width = parseInt(style.width),
740
+ height = parseInt(style.height);
741
+
742
+ uni.showLoading({ mask: true });
743
+ await initCanvasRefs();
744
+ ctxCanvas.toTempFilePath({
745
+ x: x,
746
+ y: y,
747
+ width: width,
748
+ height: height,
749
+ fileType: 'png',
750
+ quality: qlty,
751
+ success: async (res: any) => {
752
+ prvImgTmp = res.tempFilePath;
753
+
754
+ const canvasPrvRef = ctxCanvasPrv;
755
+ let pX = windowWidth,
756
+ pY = parseInt(cvsStyleHeight.value),
757
+ pW = parseInt(selStyle.value.width),
758
+ pH = parseInt(selStyle.value.height),
759
+ useW = pX - 40,
760
+ useH = pY - 80,
761
+ radio = useW / pW,
762
+ rHeight = pH * radio;
763
+ // 立即模式画布:清掉上一次预览的残影再画新帧
764
+ canvasPrvRef.clearRect(0, 0, windowWidth, windowHeight);
765
+ if (rHeight < useH) {
766
+ pW = useW;
767
+ pH = rHeight;
559
768
  } else {
560
- if (imgWidth < useWidth) {
561
- useWidth = imgWidth;
562
- useHeight = imgHeight;
563
- } else {
564
- useWidth = useWidth;
565
- useHeight = useWidth / imgRadio;
566
- }
769
+ radio = useH / pH;
770
+ pW *= radio;
771
+ pH = useH;
567
772
  }
568
- if (this.isin) {
569
- this.scaleWidth = 0;
570
- this.scaleHeight = 0;
571
- if (useWidth < selWidth) {
572
- useWidth = selWidth;
573
- useHeight = useWidth / imgRadio;
574
- this.lckHeight = 0;
575
- }
576
- if (useHeight < selHeight) {
577
- useHeight = selHeight;
578
- useWidth = useHeight * imgRadio;
579
- this.lckWidth = 0;
580
- }
773
+ if (props.fillColor && props.fillColor !== 'transparent') {
774
+ canvasPrvRef.setFillStyle(props.fillColor);
775
+ canvasPrvRef.fillRect(0, 0, pX, pY);
776
+ canvasPrvRef.fillRect(x, y, width, height);
581
777
  }
582
-
583
- this.scaleSize = 1;
584
- this.rotateDeg = 0;
585
- this.posWidth = (allWidth - useWidth) / 2;
586
- this.posHeight = (allHeight - useHeight - tabHeight) / 2;
587
- this.useWidth = useWidth;
588
- this.useHeight = useHeight;
589
-
590
- let style = this.selStyle,
591
- left = parseInt(style.left),
592
- top = parseInt(style.top),
593
- width = parseInt(style.width),
594
- height = parseInt(style.height),
595
- ctxCanvas = this.ctxCanvas,
596
- ctxCanvasOper = this.ctxCanvasOper;
597
-
598
- // H5/小程序 type=2d 为立即模式画布,重绘遮罩前须清掉上一帧,否则拖控制点时遮罩残留
599
- ctxCanvasOper.clearRect(0, 0, this.windowWidth, this.windowHeight);
600
-
601
- ctxCanvasOper.setLineWidth(3);
602
- ctxCanvasOper.setStrokeStyle('grey');
603
- ctxCanvasOper.setGlobalAlpha(0.4);
604
- ctxCanvasOper.setFillStyle('black');
605
- ctxCanvasOper.strokeRect(left, top, width, height);
606
- ctxCanvasOper.fillRect(0, 0, this.windowWidth, top);
607
- ctxCanvasOper.fillRect(0, top, left, height);
608
- ctxCanvasOper.fillRect(0, top + height, this.windowWidth, this.windowHeight - height - top - tabHeight);
609
- ctxCanvasOper.fillRect(left + width, top, this.windowWidth - width - left, height);
610
- ctxCanvasOper.setStrokeStyle('red');
611
- ctxCanvasOper.moveTo(left + 20, top); ctxCanvasOper.lineTo(left, top); ctxCanvasOper.lineTo(left, top + 20);
612
- ctxCanvasOper.moveTo(left + width - 20, top); ctxCanvasOper.lineTo(left + width, top); ctxCanvasOper.lineTo(left + width, top + 20);
613
- ctxCanvasOper.moveTo(left + 20, top + height); ctxCanvasOper.lineTo(left, top + height); ctxCanvasOper.lineTo(left, top + height - 20);
614
- ctxCanvasOper.moveTo(left + width - 20, top + height); ctxCanvasOper.lineTo(left + width, top + height); ctxCanvasOper.lineTo(left + width, top + height - 20);
615
-
616
- // 四角控制点(canChangeSize 时可拖拽调整裁剪框)
617
- const controlPointSize = 10;
618
- ctxCanvasOper.setFillStyle('white');
619
- ctxCanvasOper.setStrokeStyle('grey');
620
- ctxCanvasOper.setLineWidth(1);
621
- ctxCanvasOper.fillRect(left - controlPointSize / 2, top - controlPointSize / 2, controlPointSize, controlPointSize);
622
- ctxCanvasOper.strokeRect(left - controlPointSize / 2, top - controlPointSize / 2, controlPointSize, controlPointSize);
623
- ctxCanvasOper.fillRect(left + width - controlPointSize / 2, top - controlPointSize / 2, controlPointSize, controlPointSize);
624
- ctxCanvasOper.strokeRect(left + width - controlPointSize / 2, top - controlPointSize / 2, controlPointSize, controlPointSize);
625
- ctxCanvasOper.fillRect(left - controlPointSize / 2, top + height - controlPointSize / 2, controlPointSize, controlPointSize);
626
- ctxCanvasOper.strokeRect(left - controlPointSize / 2, top + height - controlPointSize / 2, controlPointSize, controlPointSize);
627
- ctxCanvasOper.fillRect(left + width - controlPointSize / 2, top + height - controlPointSize / 2, controlPointSize, controlPointSize);
628
- ctxCanvasOper.strokeRect(left + width - controlPointSize / 2, top + height - controlPointSize / 2, controlPointSize, controlPointSize);
629
-
630
- ctxCanvasOper.stroke();
631
-
632
- ctxCanvasOper.draw(false, () => {
633
- if (ini) {
634
- this.styleDisplay = 'flex';
635
- // #ifdef H5
636
- this.styleTop = this.drawTop + 'px';
637
- // #endif
638
- // #ifndef H5
639
- this.styleTop = '0';
640
- // #endif
641
- if (this.fillColor && this.fillColor !== 'transparent') {
642
- ctxCanvas.setFillStyle(this.fillColor);
643
- }
644
- this.drawImage();
778
+ prvX = pX = (pX - pW) / 2;
779
+ prvY = pY = (pY - pH) / 2;
780
+ prvWidth = pW;
781
+ prvHeight = pH;
782
+ await canvasPrvRef.drawImage(prvImgTmp, pX, pY, pW, pH);
783
+ canvasPrvRef.draw(false, () => {
784
+ // #ifdef H5
785
+ btop(prvImgTmp).then(() => {
786
+ showOper.value = false;
787
+ prvTop.value = drawTop + 'px';
788
+ })
789
+ // #endif
790
+ // #ifndef H5
791
+ if (platform != 'android') {
792
+ showOper.value = false;
645
793
  }
794
+ prvTop.value = '0';
795
+ // #endif
646
796
  });
647
-
648
- this.$emit("avtinit");
649
797
  },
650
- async drawImage() {
651
- await this.initCanvasRefs();
652
- let tm_now = Date.now();
653
- if (tm_now - this.drawTm < 20) return;
654
- this.drawTm = tm_now;
655
- let ctxCanvas = this.ctxCanvas;
656
- // 图片对象先取回(dd-canvas 内部有缓存,二次绘制走同步路径);
657
- // 加载期间若新帧已触发(token 失配)则丢弃本帧,避免旧帧续体把图片画回旧位置
658
- const token = this._drawToken = (this._drawToken || 0) + 1;
659
- const image = await ctxCanvas.loadImage(this.imgPath).catch(() => null);
660
- if (!image || token !== this._drawToken) return;
661
- // H5/小程序 type=2d 为立即模式画布,ctx.draw() 在 dd-canvas 内是 no-op,
662
- // 上一帧像素不会自动清空——拖动残影的根因,每帧必须先 clearRect
663
- ctxCanvas.clearRect(0, 0, this.windowWidth, this.windowHeight);
664
- if (this.fillColor && this.fillColor !== 'transparent') {
665
- ctxCanvas.fillRect(0, 0, this.windowWidth, this.windowHeight - tabHeight);
666
- }
667
- ctxCanvas.save();
668
- ctxCanvas.translate(this.posWidth + this.useWidth / 2, this.posHeight + this.useHeight / 2);
669
- ctxCanvas.scale(this.scaleSize, this.scaleSize);
670
- ctxCanvas.rotate(this.rotateDeg * Math.PI / 180);
671
- ctxCanvas.drawImage(image, -this.useWidth / 2, -this.useHeight / 2, this.useWidth, this.useHeight);
672
- ctxCanvas.restore();
673
- ctxCanvas.draw(false);
674
- },
675
- hideImg() {
676
- this.prvImg = '';
677
- this.prvTop = '-10000px';
678
- this.showOper = true;
679
- this.prvImgData = null;
680
- this.target = null;
681
- },
682
- close() {
683
- this.styleDisplay = 'none';
684
- this.styleTop = '-10000px';
685
- this.hasSel = false;
686
- this.hideImg();
687
- this.noBar || uni.showTabBar();
688
- this.$emit('cancel');
798
+ fail: () => {
799
+ uni.showToast({
800
+ title: "生成预览失败",
801
+ duration: 2000,
802
+ })
689
803
  },
690
- async preview() {
691
- if (this.fPreviewing) return;
692
- this.fPreviewing = true;
693
- setTimeout(() => { this.fPreviewing = false; }, 1000);
694
- let style = this.selStyle,
695
- x = parseInt(style.left),
696
- y = parseInt(style.top),
697
- width = parseInt(style.width),
698
- height = parseInt(style.height);
804
+ complete: () => {
805
+ uni.hideLoading();
806
+ }
807
+ });
808
+ }
699
809
 
700
- uni.showLoading({ mask: true });
701
- await this.initCanvasRefs();
702
- this.ctxCanvas.toTempFilePath({
703
- x: x,
704
- y: y,
705
- width: width,
706
- height: height,
707
- fileType: 'png',
708
- quality: this.qlty,
709
- success: async (r) => {
710
- this.prvImgTmp = r = r.tempFilePath;
711
-
712
- let ctxCanvasPrv = this.ctxCanvasPrv,
713
- prvX = this.windowWidth,
714
- prvY = parseInt(this.cvsStyleHeight),
715
- prvWidth = parseInt(this.selStyle.width),
716
- prvHeight = parseInt(this.selStyle.height),
717
- useWidth = prvX - 40,
718
- useHeight = prvY - 80,
719
- radio = useWidth / prvWidth,
720
- rHeight = prvHeight * radio;
721
- // 立即模式画布:清掉上一次预览的残影再画新帧
722
- ctxCanvasPrv.clearRect(0, 0, this.windowWidth, this.windowHeight);
723
- if (rHeight < useHeight) {
724
- prvWidth = useWidth;
725
- prvHeight = rHeight;
726
- } else {
727
- radio = useHeight / prvHeight;
728
- prvWidth *= radio;
729
- prvHeight = useHeight;
730
- }
731
- if (this.fillColor && this.fillColor !== 'transparent') {
732
- ctxCanvasPrv.setFillStyle(this.fillColor);
733
- ctxCanvasPrv.fillRect(0, 0, prvX, prvY);
734
- ctxCanvasPrv.fillRect(x, y, width, height);
735
- }
736
- this.prvX = prvX = (prvX - prvWidth) / 2;
737
- this.prvY = prvY = (prvY - prvHeight) / 2;
738
- this.prvWidth = prvWidth;
739
- this.prvHeight = prvHeight;
740
- await ctxCanvasPrv.drawImage(r, prvX, prvY, prvWidth, prvHeight);
741
- ctxCanvasPrv.draw(false, () => {
742
- // #ifdef H5
743
- this.btop(this.prvImgTmp).then(() => {
744
- this.showOper = false;
745
- this.prvTop = this.drawTop + 'px';
746
- })
747
- // #endif
748
- // #ifndef H5
749
- if (this.platform != 'android') {
750
- this.showOper = false;
751
- }
752
- this.prvTop = '0';
753
- // #endif
754
- });
755
- },
756
- fail: () => {
757
- uni.showToast({
758
- title: "生成预览失败",
759
- duration: 2000,
760
- })
761
- },
762
- complete: () => {
763
- uni.hideLoading();
764
- }
765
- });
766
- },
767
- chooseImage(index = undefined, params = undefined, data = undefined) {
768
- if (params) {
769
- let areaWidth = params.areaWidth || this.areaWidth,
770
- areaHeight = params.areaHeight || this.areaHeight,
771
- expWidth = params.exportWidth || this.exportWidth,
772
- expHeight = params.exportHeight || this.exportHeight,
773
- quality = params.quality,
774
- canRotate = params.canRotate,
775
- canScale = params.canScale,
776
- canChangeSize = params.canChangeSize,
777
- minScale = params.minScale,
778
- maxScale = params.maxScale,
779
- stretch = params.stretch,
780
- inner = params.inner,
781
- lock = params.lock;
782
-
783
- expWidth && (this.expWidth = expWidth.indexOf('rpx') >= 0 ? parseInt(expWidth) * this.pxRatio : parseInt(expWidth));
784
- expHeight && (this.expHeight = expHeight.indexOf('rpx') >= 0 ? parseInt(expHeight) * this.pxRatio : parseInt(expHeight));
785
- this.isin = inner === true ? 1 : 0;
786
- this.letRotate = (canRotate === false || this.isin) ? 0 : 1;
787
- this.letScale = canScale === false ? 0 : 1;
788
- this.letChangeSize = canChangeSize || false;
789
- this.qlty = parseInt(quality) || 0.9;
790
- this.mnScale = minScale || 0.3;
791
- this.mxScale = maxScale || 4;
792
- this.stc = stretch;
793
- this.lck = lock;
794
- if (this.isin) {
795
- this.btnWidth = '24%';
796
- this.btnDsp = 'none';
797
- } else {
798
- this.btnWidth = '19%';
799
- this.btnDsp = 'flex';
800
- }
810
+ function chooseImage(index: any = undefined, params: any = undefined, data: any = undefined) {
811
+ if (params) {
812
+ let areaWidth = params.areaWidth || props.areaWidth,
813
+ areaHeight = params.areaHeight || props.areaHeight,
814
+ expW = params.exportWidth || props.exportWidth,
815
+ expH = params.exportHeight || props.exportHeight,
816
+ quality = params.quality,
817
+ canRotate = params.canRotate,
818
+ canScale = params.canScale,
819
+ canChangeSize = params.canChangeSize,
820
+ minScale = params.minScale,
821
+ maxScale = params.maxScale,
822
+ stretch = params.stretch,
823
+ inner = params.inner,
824
+ lock = params.lock;
825
+
826
+ expW && (expWidth.value = expW.indexOf('rpx') >= 0 ? parseInt(expW) * pxRatio : parseInt(expW));
827
+ expH && (expHeight.value = expH.indexOf('rpx') >= 0 ? parseInt(expH) * pxRatio : parseInt(expH));
828
+ isin = inner === true ? 1 : 0;
829
+ letRotate = (canRotate === false || isin) ? 0 : 1;
830
+ letScale = canScale === false ? 0 : 1;
831
+ letChangeSize.value = canChangeSize || false;
832
+ qlty = parseInt(quality) || 0.9;
833
+ mnScale = minScale || 0.3;
834
+ mxScale = maxScale || 4;
835
+ stc = stretch;
836
+ lck = lock;
837
+ if (isin) {
838
+ btnWidth.value = '24%';
839
+ btnDsp.value = 'none';
840
+ } else {
841
+ btnWidth.value = '19%';
842
+ btnDsp.value = 'flex';
843
+ }
801
844
 
802
- if (areaWidth && areaHeight) {
803
- areaWidth = areaWidth.indexOf('rpx') >= 0 ? parseInt(areaWidth) * this.pxRatio : parseInt(areaWidth);
804
- areaHeight = areaHeight.indexOf('rpx') >= 0 ? parseInt(areaHeight) * this.pxRatio : parseInt(areaHeight);
805
- this.selStyle.width = areaWidth + 'px';
806
- this.selStyle.height = areaHeight + 'px';
807
- this.selStyle.top = (this.windowHeight - areaHeight - tabHeight) / 2 + 'px';
808
- this.selStyle.left = (this.windowWidth - areaWidth) / 2 + 'px';
809
- this.hasSel = true;
810
- }
811
- }
812
- this.rtn = data;
813
- this.indx = index;
814
- this.select();
815
- },
816
- rotate() {
817
- // #ifdef APP-PLUS
818
- if (this.platform === 'android') {
819
- if (this.fRotateing) return;
820
- this.fRotateing = true;
821
- setTimeout(() => { this.fRotateing = false; }, 500);
822
- }
823
- // #endif
845
+ if (areaWidth && areaHeight) {
846
+ areaWidth = areaWidth.indexOf('rpx') >= 0 ? parseInt(areaWidth) * pxRatio : parseInt(areaWidth);
847
+ areaHeight = areaHeight.indexOf('rpx') >= 0 ? parseInt(areaHeight) * pxRatio : parseInt(areaHeight);
848
+ selStyle.value.width = areaWidth + 'px';
849
+ selStyle.value.height = areaHeight + 'px';
850
+ selStyle.value.top = (windowHeight - areaHeight - tabHeight) / 2 + 'px';
851
+ selStyle.value.left = (windowWidth - areaWidth) / 2 + 'px';
852
+ hasSel = true;
853
+ }
854
+ }
855
+ rtn = data;
856
+ indx = index;
857
+ select();
858
+ }
824
859
 
825
- this.rotateDeg += 90 - this.rotateDeg % 90;
826
- this.drawImage();
827
- },
828
- start(e) {
829
- let touches = e.touches,
830
- touch0 = touches[0],
831
- touch1 = touches[1];
832
-
833
- this.touch0 = touch0;
834
- this.touch1 = touch1;
835
-
836
- if (touch1) {
837
- let x = touch1.x - touch0.x,
838
- y = touch1.y - touch0.y;
839
- this.fgDistance = Math.sqrt(x * x + y * y);
840
- } else if (this.letChangeSize) {
841
- // 单指落在四角控制点附近时进入裁剪框调整模式
842
- const controlPointSize = 20;
843
- const x = touch0.x;
844
- const y = touch0.y;
845
- const style = this.selStyle;
846
- const left = parseInt(style.left);
847
- const top = parseInt(style.top);
848
- const width = parseInt(style.width);
849
- const height = parseInt(style.height);
850
-
851
- if (Math.abs(x - left) < controlPointSize && Math.abs(y - top) < controlPointSize) {
852
- this.resizeHandle = 'top-left';
853
- } else if (Math.abs(x - (left + width)) < controlPointSize && Math.abs(y - top) < controlPointSize) {
854
- this.resizeHandle = 'top-right';
855
- } else if (Math.abs(x - left) < controlPointSize && Math.abs(y - (top + height)) < controlPointSize) {
856
- this.resizeHandle = 'bottom-left';
857
- } else if (Math.abs(x - (left + width)) < controlPointSize && Math.abs(y - (top + height)) < controlPointSize) {
858
- this.resizeHandle = 'bottom-right';
859
- } else {
860
- this.resizeHandle = null;
861
- }
862
- } else {
863
- this.resizeHandle = null;
860
+ function rotate() {
861
+ // #ifdef APP-PLUS
862
+ if (platform === 'android') {
863
+ if (fRotateing) return;
864
+ fRotateing = true;
865
+ setTimeout(() => { fRotateing = false; }, 500);
866
+ }
867
+ // #endif
868
+
869
+ rotateDeg += 90 - rotateDeg % 90;
870
+ drawImage();
871
+ }
872
+
873
+ function start(e: any) {
874
+ const touches = e.touches,
875
+ t0 = touches[0],
876
+ t1 = touches[1];
877
+
878
+ touch0 = t0;
879
+ touch1 = t1;
880
+
881
+ if (t1) {
882
+ const x = t1.x - t0.x,
883
+ y = t1.y - t0.y;
884
+ fgDistance = Math.sqrt(x * x + y * y);
885
+ } else if (letChangeSize.value) {
886
+ // 单指落在四角控制点附近时进入裁剪框调整模式
887
+ const controlPointSize = 20;
888
+ const x = t0.x;
889
+ const y = t0.y;
890
+ const style = selStyle.value;
891
+ const left = parseInt(style.left);
892
+ const top = parseInt(style.top);
893
+ const width = parseInt(style.width);
894
+ const height = parseInt(style.height);
895
+
896
+ if (Math.abs(x - left) < controlPointSize && Math.abs(y - top) < controlPointSize) {
897
+ resizeHandle = 'top-left';
898
+ } else if (Math.abs(x - (left + width)) < controlPointSize && Math.abs(y - top) < controlPointSize) {
899
+ resizeHandle = 'top-right';
900
+ } else if (Math.abs(x - left) < controlPointSize && Math.abs(y - (top + height)) < controlPointSize) {
901
+ resizeHandle = 'bottom-left';
902
+ } else if (Math.abs(x - (left + width)) < controlPointSize && Math.abs(y - (top + height)) < controlPointSize) {
903
+ resizeHandle = 'bottom-right';
904
+ } else {
905
+ resizeHandle = null;
906
+ }
907
+ } else {
908
+ resizeHandle = null;
909
+ }
910
+ }
911
+
912
+ function move(e: any) {
913
+ const touches = e.touches,
914
+ t0 = touches[0],
915
+ t1 = touches[1];
916
+
917
+ if (t1) {
918
+ let x = t1.x - t0.x,
919
+ y = t1.y - t0.y;
920
+ const nextDistance = Math.sqrt(x * x + y * y),
921
+ deltaScale = 0.005 * (nextDistance - fgDistance),
922
+ beScaleSize = scaleSize + deltaScale;
923
+
924
+ do {
925
+ if (!letScale) break;
926
+ if (beScaleSize < mnScale) break;
927
+ if (beScaleSize > mxScale) break;
928
+ if (isin) {
929
+ const imgW = useWidth * beScaleSize,
930
+ imgH = useHeight * beScaleSize,
931
+ rx0 = posWidth + useWidth / 2,
932
+ ry0 = posHeight + useHeight / 2,
933
+ l = rx0 - imgW / 2, t = ry0 - imgH / 2,
934
+ r = l + imgW, b = t + imgH,
935
+ left = parseInt(selStyle.value.left),
936
+ top = parseInt(selStyle.value.top),
937
+ width = parseInt(selStyle.value.width),
938
+ height = parseInt(selStyle.value.height);
939
+ if (left < l || left + width > r || top < t || top + height > b) break;
940
+ scaleWidth = (useWidth - imgW) / 2;
941
+ scaleHeight = (useHeight - imgH) / 2;
864
942
  }
865
- },
866
- move(e) {
867
- let touches = e.touches,
868
- touch0 = touches[0],
869
- touch1 = touches[1];
870
-
871
- if (touch1) {
872
- let x = touch1.x - touch0.x,
873
- y = touch1.y - touch0.y,
874
- fgDistance = Math.sqrt(x * x + y * y),
875
- scaleSize = 0.005 * (fgDistance - this.fgDistance),
876
- beScaleSize = this.scaleSize + scaleSize;
877
-
878
- do {
879
- if (!this.letScale) break;
880
- if (beScaleSize < this.mnScale) break;
881
- if (beScaleSize > this.mxScale) break;
882
- if (this.isin) {
883
- let imgWidth = this.useWidth * beScaleSize,
884
- imgHeight = this.useHeight * beScaleSize,
885
- rx0 = this.posWidth + this.useWidth / 2,
886
- ry0 = this.posHeight + this.useHeight / 2,
887
- l = rx0 - imgWidth / 2, t = ry0 - imgHeight / 2,
888
- r = l + imgWidth, b = t + imgHeight,
889
- left = parseInt(this.selStyle.left),
890
- top = parseInt(this.selStyle.top),
891
- width = parseInt(this.selStyle.width),
892
- height = parseInt(this.selStyle.height);
893
- if (left < l || left + width > r || top < t || top + height > b) break;
894
- this.scaleWidth = (this.useWidth - imgWidth) / 2;
895
- this.scaleHeight = (this.useHeight - imgHeight) / 2;
896
- }
897
943
 
898
- this.scaleSize = beScaleSize;
899
- } while (0);
900
- this.fgDistance = fgDistance;
944
+ scaleSize = beScaleSize;
945
+ } while (0);
946
+ fgDistance = nextDistance;
901
947
 
902
- if (touch1.x !== touch0.x && this.letRotate) {
903
- x = (this.touch1.y - this.touch0.y) / (this.touch1.x - this.touch0.x);
904
- y = (touch1.y - touch0.y) / (touch1.x - touch0.x);
905
- this.rotateDeg += Math.atan((y - x) / (1 + x * y)) * 180 / Math.PI;
906
- this.touch0 = touch0;
907
- this.touch1 = touch1;
908
- }
948
+ if (t1.x !== t0.x && letRotate) {
949
+ x = (touch1.y - touch0.y) / (touch1.x - touch0.x);
950
+ y = (t1.y - t0.y) / (t1.x - t0.x);
951
+ rotateDeg += Math.atan((y - x) / (1 + x * y)) * 180 / Math.PI;
952
+ touch0 = t0;
953
+ touch1 = t1;
954
+ }
909
955
 
910
- this.drawImage();
911
- } else if (this.touch0) {
912
- if (this.resizeHandle && this.letChangeSize) {
913
- // 拖拽控制点调整裁剪框
914
- const style = { ...this.selStyle };
915
- const left = parseInt(style.left);
916
- const top = parseInt(style.top);
917
- const width = parseInt(style.width);
918
- const height = parseInt(style.height);
919
- const minWidth = 50;
920
- const minHeight = 50;
921
-
922
- switch (this.resizeHandle) {
923
- case 'top-left':
924
- style.left = touch0.x + 'px';
925
- style.top = touch0.y + 'px';
926
- style.width = (left + width - touch0.x) + 'px';
927
- style.height = (top + height - touch0.y) + 'px';
928
- break;
929
- case 'top-right':
930
- style.top = touch0.y + 'px';
931
- style.width = (touch0.x - left) + 'px';
932
- style.height = (top + height - touch0.y) + 'px';
933
- break;
934
- case 'bottom-left':
935
- style.left = touch0.x + 'px';
936
- style.width = (left + width - touch0.x) + 'px';
937
- style.height = (touch0.y - top) + 'px';
938
- break;
939
- case 'bottom-right':
940
- style.width = (touch0.x - left) + 'px';
941
- style.height = (touch0.y - top) + 'px';
942
- break;
943
- }
956
+ drawImage();
957
+ } else if (touch0) {
958
+ if (resizeHandle && letChangeSize.value) {
959
+ // 拖拽控制点调整裁剪框
960
+ const style = { ...selStyle.value };
961
+ const left = parseInt(style.left);
962
+ const top = parseInt(style.top);
963
+ const width = parseInt(style.width);
964
+ const height = parseInt(style.height);
965
+ const minWidth = 50;
966
+ const minHeight = 50;
967
+
968
+ switch (resizeHandle) {
969
+ case 'top-left':
970
+ style.left = t0.x + 'px';
971
+ style.top = t0.y + 'px';
972
+ style.width = (left + width - t0.x) + 'px';
973
+ style.height = (top + height - t0.y) + 'px';
974
+ break;
975
+ case 'top-right':
976
+ style.top = t0.y + 'px';
977
+ style.width = (t0.x - left) + 'px';
978
+ style.height = (top + height - t0.y) + 'px';
979
+ break;
980
+ case 'bottom-left':
981
+ style.left = t0.x + 'px';
982
+ style.width = (left + width - t0.x) + 'px';
983
+ style.height = (t0.y - top) + 'px';
984
+ break;
985
+ case 'bottom-right':
986
+ style.width = (t0.x - left) + 'px';
987
+ style.height = (t0.y - top) + 'px';
988
+ break;
989
+ }
944
990
 
945
- // 约束裁剪框:最小尺寸、屏幕边界,inner 模式下再限制在图片内
946
- let nl = parseInt(style.left);
947
- let nt = parseInt(style.top);
948
- let nw = parseInt(style.width);
949
- let nh = parseInt(style.height);
950
-
951
- if (nl < 0) nl = 0;
952
- if (nt < 0) nt = 0;
953
- if (nl + nw > this.windowWidth) nw = this.windowWidth - nl;
954
- if (nt + nh > this.windowHeight - tabHeight) nh = this.windowHeight - tabHeight - nt;
955
-
956
- if (this.isin) {
957
- const imgWidth = this.useWidth * this.scaleSize;
958
- const imgHeight = this.useHeight * this.scaleSize;
959
- const rx0 = this.posWidth + this.useWidth / 2;
960
- const ry0 = this.posHeight + this.useHeight / 2;
961
- const imgL = rx0 - imgWidth / 2;
962
- const imgT = ry0 - imgHeight / 2;
963
- const imgR = imgL + imgWidth;
964
- const imgB = imgT + imgHeight;
965
- if (nl < imgL) nl = imgL;
966
- if (nt < imgT) nt = imgT;
967
- if (nl + nw > imgR) nw = imgR - nl;
968
- if (nt + nh > imgB) nh = imgB - nt;
969
- }
991
+ // 约束裁剪框:最小尺寸、屏幕边界,inner 模式下再限制在图片内
992
+ let nl = parseInt(style.left);
993
+ let nt = parseInt(style.top);
994
+ let nw = parseInt(style.width);
995
+ let nh = parseInt(style.height);
996
+
997
+ if (nl < 0) nl = 0;
998
+ if (nt < 0) nt = 0;
999
+ if (nl + nw > windowWidth) nw = windowWidth - nl;
1000
+ if (nt + nh > windowHeight - tabHeight) nh = windowHeight - tabHeight - nt;
1001
+
1002
+ if (isin) {
1003
+ const imgW = useWidth * scaleSize;
1004
+ const imgH = useHeight * scaleSize;
1005
+ const rx0 = posWidth + useWidth / 2;
1006
+ const ry0 = posHeight + useHeight / 2;
1007
+ const imgL = rx0 - imgW / 2;
1008
+ const imgT = ry0 - imgH / 2;
1009
+ const imgR = imgL + imgW;
1010
+ const imgB = imgT + imgH;
1011
+ if (nl < imgL) nl = imgL;
1012
+ if (nt < imgT) nt = imgT;
1013
+ if (nl + nw > imgR) nw = imgR - nl;
1014
+ if (nt + nh > imgB) nh = imgB - nt;
1015
+ }
970
1016
 
971
- if (nw >= minWidth && nh >= minHeight) {
972
- style.left = nl + 'px';
973
- style.top = nt + 'px';
974
- style.width = nw + 'px';
975
- style.height = nh + 'px';
976
- this.selStyle = style;
977
- this.drawInit();
1017
+ if (nw >= minWidth && nh >= minHeight) {
1018
+ style.left = nl + 'px';
1019
+ style.top = nt + 'px';
1020
+ style.width = nw + 'px';
1021
+ style.height = nh + 'px';
1022
+ selStyle.value = style;
1023
+ drawInit();
1024
+ }
1025
+ } else {
1026
+ // 单指拖动图片
1027
+ let x = t0.x - touch0.x,
1028
+ y = t0.y - touch0.y,
1029
+ beX = posWidth + x,
1030
+ beY = posHeight + y;
1031
+ if (isin) {
1032
+ const imgW = useWidth * scaleSize,
1033
+ imgH = useHeight * scaleSize,
1034
+ rx0 = beX + useWidth / 2,
1035
+ ry0 = beY + useHeight / 2,
1036
+ l = rx0 - imgW / 2, t = ry0 - imgH / 2,
1037
+ r = l + imgW, b = t + imgH,
1038
+ left = parseInt(selStyle.value.left),
1039
+ top = parseInt(selStyle.value.top),
1040
+ width = parseInt(selStyle.value.width),
1041
+ height = parseInt(selStyle.value.height);
1042
+ if (!lckWidth && Math.abs(x) < 100) {
1043
+ if (left >= l && left + width <= r) {
1044
+ posWidth = beX;
1045
+ } else if (left < l) {
1046
+ posWidth = left - scaleWidth;
1047
+ } else if (left + width > r) {
1048
+ posWidth = left - (imgW - width) - scaleWidth;
978
1049
  }
979
- } else {
980
- // 单指拖动图片
981
- let x = touch0.x - this.touch0.x,
982
- y = touch0.y - this.touch0.y,
983
- beX = this.posWidth + x,
984
- beY = this.posHeight + y;
985
- if (this.isin) {
986
- let imgWidth = this.useWidth * this.scaleSize,
987
- imgHeight = this.useHeight * this.scaleSize,
988
- rx0 = beX + this.useWidth / 2,
989
- ry0 = beY + this.useHeight / 2,
990
- l = rx0 - imgWidth / 2, t = ry0 - imgHeight / 2,
991
- r = l + imgWidth, b = t + imgHeight,
992
- left = parseInt(this.selStyle.left),
993
- top = parseInt(this.selStyle.top),
994
- width = parseInt(this.selStyle.width),
995
- height = parseInt(this.selStyle.height);
996
- if (!this.lckWidth && Math.abs(x) < 100) {
997
- if (left >= l && left + width <= r) {
998
- this.posWidth = beX;
999
- } else if (left < l) {
1000
- this.posWidth = left - this.scaleWidth;
1001
- } else if (left + width > r) {
1002
- this.posWidth = left - (imgWidth - width) - this.scaleWidth;
1003
- }
1004
- }
1005
- if (!this.lckHeight && Math.abs(y) < 100) {
1006
- if (top >= t && top + height <= b) {
1007
- this.posHeight = beY;
1008
- } else if (top < t) {
1009
- this.posHeight = top - this.scaleHeight;
1010
- } else if (top + height > b) {
1011
- this.posHeight = top - (imgHeight - height) - this.scaleHeight;
1012
- }
1013
- }
1014
- } else {
1015
- if (Math.abs(x) < 100 && !this.lckWidth) this.posWidth = beX;
1016
- if (Math.abs(y) < 100 && !this.lckHeight) this.posHeight = beY;
1050
+ }
1051
+ if (!lckHeight && Math.abs(y) < 100) {
1052
+ if (top >= t && top + height <= b) {
1053
+ posHeight = beY;
1054
+ } else if (top < t) {
1055
+ posHeight = top - scaleHeight;
1056
+ } else if (top + height > b) {
1057
+ posHeight = top - (imgH - height) - scaleHeight;
1017
1058
  }
1018
-
1019
- this.touch0 = touch0;
1020
- this.drawImage();
1021
1059
  }
1022
- }
1023
- },
1024
- end(e) {
1025
- let touches = e.touches,
1026
- touch0 = touches && touches[0];
1027
- if (touch0) {
1028
- this.touch0 = touch0;
1029
1060
  } else {
1030
- this.touch0 = null;
1031
- this.touch1 = null;
1032
- this.resizeHandle = null;
1061
+ if (Math.abs(x) < 100 && !lckWidth) posWidth = beX;
1062
+ if (Math.abs(y) < 100 && !lckHeight) posHeight = beY;
1033
1063
  }
1034
- },
1035
- async getImgData() {
1036
- await this.initCanvasRefs();
1037
- return new Promise((resolve, reject) => {
1038
- let prvX = this.prvX,
1039
- prvY = this.prvY,
1040
- prvWidth = this.prvWidth,
1041
- prvHeight = this.prvHeight;
1042
- // #ifdef APP-PLUS||H5
1043
- prvX *= this.pixelRatio;
1044
- prvY *= this.pixelRatio;
1045
- prvWidth *= this.pixelRatio;
1046
- prvHeight *= this.pixelRatio;
1047
- // #endif
1048
- this.ctxCanvasPrv.getImageData({
1049
- x: prvX,
1050
- y: prvY,
1051
- width: prvWidth,
1052
- height: prvHeight,
1053
- success(res) {
1054
- resolve(res.data);
1055
- },
1056
- fail(err) {
1057
- reject(err);
1058
- }
1059
- });
1060
- });
1061
- },
1062
- // 预览模式饱和度滑杆:HSL 调整后 putImageData 回写预览画布
1063
- async colorChange(e) {
1064
- let tm_now = Date.now();
1065
- if (tm_now - this.prvTm < 100) return;
1066
- this.prvTm = tm_now;
1067
1064
 
1068
- uni.showLoading({ mask: true });
1065
+ touch0 = t0;
1066
+ drawImage();
1067
+ }
1068
+ }
1069
+ }
1069
1070
 
1070
- if (!this.prvImgData) {
1071
- if (!(this.prvImgData = await this.getImgData().catch(() => {
1072
- uni.showToast({
1073
- title: "读取图片失败",
1074
- duration: 2000,
1075
- })
1076
- }))) return;
1077
- this.target = new Uint8ClampedArray(this.prvImgData.length);
1071
+ function end(e: any) {
1072
+ const touches = e.touches,
1073
+ t0 = touches && touches[0];
1074
+ if (t0) {
1075
+ touch0 = t0;
1076
+ } else {
1077
+ touch0 = null;
1078
+ touch1 = null;
1079
+ resizeHandle = null;
1080
+ }
1081
+ }
1082
+
1083
+ async function getImgData(): Promise<any> {
1084
+ await initCanvasRefs();
1085
+ return new Promise((resolve, reject) => {
1086
+ let x = prvX,
1087
+ y = prvY,
1088
+ width = prvWidth,
1089
+ height = prvHeight;
1090
+ // #ifdef APP-PLUS||H5
1091
+ x *= pixelRatio;
1092
+ y *= pixelRatio;
1093
+ width *= pixelRatio;
1094
+ height *= pixelRatio;
1095
+ // #endif
1096
+ ctxCanvasPrv.getImageData({
1097
+ x: x,
1098
+ y: y,
1099
+ width: width,
1100
+ height: height,
1101
+ success(res: any) {
1102
+ resolve(res.data);
1103
+ },
1104
+ fail(err: any) {
1105
+ reject(err);
1078
1106
  }
1107
+ });
1108
+ });
1109
+ }
1079
1110
 
1080
- let data = this.prvImgData,
1081
- target = this.target,
1082
- i = e.detail.value,
1083
- r, g, b, a, h, s, l, d, p, q, min, max, hK, tR, tG, tB;
1111
+ // 预览模式饱和度滑杆:HSL 调整后 putImageData 回写预览画布
1112
+ async function colorChange(e: any) {
1113
+ const tm_now = Date.now();
1114
+ if (tm_now - prvTm < 100) return;
1115
+ prvTm = tm_now;
1084
1116
 
1085
- if (i === 0) {
1086
- target = data;
1087
- } else {
1088
- i = (i + 100) / 200;
1089
- if (i < 0.005) i = 0;
1090
- if (i > 0.995) i = 1;
1091
- for (let n = data.length - 1; n >= 0; n -= 4) {
1092
- r = data[n - 3] / 255;
1093
- g = data[n - 2] / 255;
1094
- b = data[n - 1] / 255;
1095
- max = Math.max(r, g, b);
1096
- min = Math.min(r, g, b);
1097
- d = max - min;
1098
- if (max === min) {
1099
- h = 0;
1100
- } else if (max === r && g >= b) {
1101
- h = 60 * ((g - b) / d);
1102
- } else if (max === r && g < b) {
1103
- h = 60 * ((g - b) / d) + 360;
1104
- } else if (max === g) {
1105
- h = 60 * ((b - r) / d) + 120;
1106
- } else if (max === b) {
1107
- h = 60 * ((r - g) / d) + 240;
1108
- }
1109
- l = (max + min) / 2;
1110
- if (l === 0 || max === min) {
1111
- s = 0;
1112
- } else if (0 < l && l <= 0.5) {
1113
- s = d / (2 * l);
1114
- } else if (l > 0.5) {
1115
- s = d / (2 - 2 * l);
1116
- }
1117
- data[n] && (a = data[n]);
1117
+ uni.showLoading({ mask: true });
1118
1118
 
1119
- if (i < 0.5) {
1120
- s = s * i / 0.5;
1121
- } else if (i > 0.5) {
1122
- s = 2 * s + 2 * i - (s * i / 0.5) - 1;
1123
- }
1119
+ if (!prvImgData) {
1120
+ if (!(prvImgData = await getImgData().catch(() => {
1121
+ uni.showToast({
1122
+ title: "读取图片失败",
1123
+ duration: 2000,
1124
+ })
1125
+ }))) return;
1126
+ target = new Uint8ClampedArray(prvImgData.length);
1127
+ }
1124
1128
 
1125
- if (s === 0) {
1126
- r = g = b = Math.round(l * 255);
1127
- } else {
1128
- if (l < 0.5) {
1129
- q = l * (1 + s);
1130
- } else if (l >= 0.5) {
1131
- q = l + s - (l * s);
1132
- }
1133
- p = 2 * l - q;
1134
- hK = h / 360;
1135
- tR = hK + 1 / 3;
1136
- tG = hK;
1137
- tB = hK - 1 / 3;
1138
- let correctRGB = (t) => {
1139
- if (t < 0) {
1140
- return t + 1.0;
1141
- }
1142
- if (t > 1) {
1143
- return t - 1.0;
1144
- }
1145
- return t;
1146
- };
1147
- let createRGB = (t) => {
1148
- if (t < (1 / 6)) {
1149
- return p + ((q - p) * 6 * t);
1150
- } else if (t >= (1 / 6) && t < (1 / 2)) {
1151
- return q;
1152
- } else if (t >= (1 / 2) && t < (2 / 3)) {
1153
- return p + ((q - p) * 6 * ((2 / 3) - t));
1154
- }
1155
- return p;
1156
- };
1157
- r = tR = Math.round(createRGB(correctRGB(tR)) * 255);
1158
- g = tG = Math.round(createRGB(correctRGB(tG)) * 255);
1159
- b = tB = Math.round(createRGB(correctRGB(tB)) * 255);
1160
- }
1161
- a && (target[n] = a);
1162
- target[n - 3] = r;
1163
- target[n - 2] = g;
1164
- target[n - 1] = b;
1165
- }
1129
+ const data = prvImgData;
1130
+ let tar = target,
1131
+ i = e.detail.value,
1132
+ r: any, g: any, b: any, a: any, h: any, s: any, l: any, d: any, p: any, q: any, min: any, max: any, hK: any, tR: any, tG: any, tB: any;
1133
+
1134
+ if (i === 0) {
1135
+ tar = data;
1136
+ } else {
1137
+ i = (i + 100) / 200;
1138
+ if (i < 0.005) i = 0;
1139
+ if (i > 0.995) i = 1;
1140
+ for (let n = data.length - 1; n >= 0; n -= 4) {
1141
+ r = data[n - 3] / 255;
1142
+ g = data[n - 2] / 255;
1143
+ b = data[n - 1] / 255;
1144
+ max = Math.max(r, g, b);
1145
+ min = Math.min(r, g, b);
1146
+ d = max - min;
1147
+ if (max === min) {
1148
+ h = 0;
1149
+ } else if (max === r && g >= b) {
1150
+ h = 60 * ((g - b) / d);
1151
+ } else if (max === r && g < b) {
1152
+ h = 60 * ((g - b) / d) + 360;
1153
+ } else if (max === g) {
1154
+ h = 60 * ((b - r) / d) + 120;
1155
+ } else if (max === b) {
1156
+ h = 60 * ((r - g) / d) + 240;
1166
1157
  }
1167
- let prvX = this.prvX,
1168
- prvY = this.prvY,
1169
- prvWidth = this.prvWidth,
1170
- prvHeight = this.prvHeight;
1171
-
1172
- if (this.fillColor && this.fillColor !== 'transparent') {
1173
- this.ctxCanvasPrv.setFillStyle(this.fillColor);
1174
- this.ctxCanvasPrv.fillRect(prvX, prvY, prvWidth, prvHeight);
1158
+ l = (max + min) / 2;
1159
+ if (l === 0 || max === min) {
1160
+ s = 0;
1161
+ } else if (0 < l && l <= 0.5) {
1162
+ s = d / (2 * l);
1163
+ } else if (l > 0.5) {
1164
+ s = d / (2 - 2 * l);
1175
1165
  }
1176
- this.ctxCanvasPrv.draw(true);
1166
+ data[n] && (a = data[n]);
1177
1167
 
1178
- // #ifdef APP-PLUS||H5
1179
- prvX *= this.pixelRatio;
1180
- prvY *= this.pixelRatio;
1181
- prvWidth *= this.pixelRatio;
1182
- prvHeight *= this.pixelRatio;
1183
- // #endif
1184
- this.ctxCanvasPrv.putImageData({
1185
- x: prvX,
1186
- y: prvY,
1187
- width: prvWidth,
1188
- height: prvHeight,
1189
- data: target,
1190
- fail() {
1191
- uni.showToast({
1192
- title: "导出图片失败",
1193
- duration: 2000
1194
- })
1195
- },
1196
- complete() {
1197
- uni.hideLoading();
1198
- }
1199
- });
1200
- },
1201
- // H5 专用:base64 转 blob URL,规避部分浏览器 canvas 跨域限制
1202
- btop(base64) {
1203
- return new Promise(function (resolve) {
1204
- var arr = base64.split(','),
1205
- mime = arr[0].match(/:(.*?);/)[1],
1206
- bstr = atob(arr[1]),
1207
- n = bstr.length,
1208
- u8arr = new Uint8Array(n);
1209
- while (n--) {
1210
- u8arr[n] = bstr.charCodeAt(n);
1168
+ if (i < 0.5) {
1169
+ s = s * i / 0.5;
1170
+ } else if (i > 0.5) {
1171
+ s = 2 * s + 2 * i - (s * i / 0.5) - 1;
1172
+ }
1173
+
1174
+ if (s === 0) {
1175
+ r = g = b = Math.round(l * 255);
1176
+ } else {
1177
+ if (l < 0.5) {
1178
+ q = l * (1 + s);
1179
+ } else if (l >= 0.5) {
1180
+ q = l + s - (l * s);
1211
1181
  }
1212
- return resolve((window.URL || window.webkitURL).createObjectURL(new Blob([u8arr], { type: mime })));
1213
- });
1214
- },
1182
+ p = 2 * l - q;
1183
+ hK = h / 360;
1184
+ tR = hK + 1 / 3;
1185
+ tG = hK;
1186
+ tB = hK - 1 / 3;
1187
+ const correctRGB = (t: any) => {
1188
+ if (t < 0) {
1189
+ return t + 1.0;
1190
+ }
1191
+ if (t > 1) {
1192
+ return t - 1.0;
1193
+ }
1194
+ return t;
1195
+ };
1196
+ const createRGB = (t: any) => {
1197
+ if (t < (1 / 6)) {
1198
+ return p + ((q - p) * 6 * t);
1199
+ } else if (t >= (1 / 6) && t < (1 / 2)) {
1200
+ return q;
1201
+ } else if (t >= (1 / 2) && t < (2 / 3)) {
1202
+ return p + ((q - p) * 6 * ((2 / 3) - t));
1203
+ }
1204
+ return p;
1205
+ };
1206
+ r = tR = Math.round(createRGB(correctRGB(tR)) * 255);
1207
+ g = tG = Math.round(createRGB(correctRGB(tG)) * 255);
1208
+ b = tB = Math.round(createRGB(correctRGB(tB)) * 255);
1209
+ }
1210
+ a && (tar[n] = a);
1211
+ tar[n - 3] = r;
1212
+ tar[n - 2] = g;
1213
+ tar[n - 1] = b;
1214
+ }
1215
+ }
1216
+ let pX = prvX,
1217
+ pY = prvY,
1218
+ pW = prvWidth,
1219
+ pH = prvHeight;
1220
+
1221
+ if (props.fillColor && props.fillColor !== 'transparent') {
1222
+ ctxCanvasPrv.setFillStyle(props.fillColor);
1223
+ ctxCanvasPrv.fillRect(pX, pY, pW, pH);
1215
1224
  }
1225
+ ctxCanvasPrv.draw(true);
1226
+
1227
+ // #ifdef APP-PLUS||H5
1228
+ pX *= pixelRatio;
1229
+ pY *= pixelRatio;
1230
+ pW *= pixelRatio;
1231
+ pH *= pixelRatio;
1232
+ // #endif
1233
+ ctxCanvasPrv.putImageData({
1234
+ x: pX,
1235
+ y: pY,
1236
+ width: pW,
1237
+ height: pH,
1238
+ data: tar,
1239
+ fail() {
1240
+ uni.showToast({
1241
+ title: "导出图片失败",
1242
+ duration: 2000
1243
+ })
1244
+ },
1245
+ complete() {
1246
+ uni.hideLoading();
1247
+ }
1248
+ });
1249
+ }
1250
+
1251
+ // H5 专用:base64 转 blob URL,规避部分浏览器 canvas 跨域限制
1252
+ function btop(base64: string): any {
1253
+ // #ifdef H5
1254
+ return new Promise(function (resolve) {
1255
+ const arr = base64.split(','),
1256
+ mime = arr[0].match(/:(.*?);/)![1],
1257
+ bstr = atob(arr[1]),
1258
+ u8arr = new Uint8Array(bstr.length);
1259
+ let n = bstr.length;
1260
+ while (n--) {
1261
+ u8arr[n] = bstr.charCodeAt(n);
1262
+ }
1263
+ return resolve((window.URL || (window as any).webkitURL).createObjectURL(new Blob([u8arr], { type: mime })));
1264
+ });
1265
+ // #endif
1216
1266
  }
1267
+
1268
+ defineExpose({
1269
+ instanceId,
1270
+ cvsStyleHeight,
1271
+ styleDisplay,
1272
+ styleTop,
1273
+ prvTop,
1274
+ selStyle,
1275
+ showOper,
1276
+ imgSrc,
1277
+ btnWidth,
1278
+ btnDsp,
1279
+ expWidth,
1280
+ expHeight,
1281
+ letChangeSize,
1282
+ safeAreaInsetsBottom,
1283
+ zc,
1284
+ zcOper,
1285
+ zcPrv,
1286
+ zcBar,
1287
+ initCanvasRefs,
1288
+ windowResize,
1289
+ select,
1290
+ confirm,
1291
+ prvUpload,
1292
+ drawInit,
1293
+ drawImage,
1294
+ hideImg,
1295
+ close,
1296
+ preview,
1297
+ chooseImage,
1298
+ rotate,
1299
+ start,
1300
+ move,
1301
+ end,
1302
+ getImgData,
1303
+ colorChange,
1304
+ btop
1305
+ });
1217
1306
  </script>
1218
1307
 
1219
1308
  <style lang="scss" scoped>
1220
1309
  @import '../../scss/variables';
1221
-
1310
+ @import '../../scss/mixins';
1222
1311
  .dd-cropper {
1223
- .my-canvas {
1312
+ .dd-cropper__avatar-canvas {
1224
1313
  display: flex;
1225
1314
  position: fixed !important;
1226
1315
  background: $dd-color-black;
@@ -1229,14 +1318,14 @@
1229
1318
  width: 100%;
1230
1319
  }
1231
1320
 
1232
- .oper-canvas {
1321
+ .dd-cropper__oper-canvas {
1233
1322
  display: flex;
1234
1323
  position: fixed !important;
1235
1324
  left: 0;
1236
1325
  width: 100%;
1237
1326
  }
1238
1327
 
1239
- .prv-canvas {
1328
+ .dd-cropper__preview-canvas {
1240
1329
  display: flex;
1241
1330
  position: fixed !important;
1242
1331
  background: $dd-color-black;
@@ -1244,8 +1333,8 @@
1244
1333
  width: 100%;
1245
1334
  }
1246
1335
 
1247
- .oper-wrapper {
1248
- height: 50px;
1336
+ .dd-cropper__oper-wrapper {
1337
+ height: 100rpx;
1249
1338
  position: fixed !important;
1250
1339
  box-sizing: border-box;
1251
1340
  border: 1px solid $dd-neutral-50;
@@ -1256,7 +1345,7 @@
1256
1345
  flex-direction: row;
1257
1346
  }
1258
1347
 
1259
- .oper {
1348
+ .dd-cropper__oper {
1260
1349
  display: flex;
1261
1350
  flex-direction: column;
1262
1351
  justify-content: center;
@@ -1267,19 +1356,19 @@
1267
1356
  align-self: center;
1268
1357
  }
1269
1358
 
1270
- .btn-wrapper {
1359
+ .dd-cropper__btn-wrapper {
1271
1360
  display: flex;
1272
1361
  flex-direction: row;
1273
1362
  /* #ifndef H5 */
1274
1363
  flex-grow: 1;
1275
1364
  /* #endif */
1276
1365
  /* #ifdef H5 */
1277
- height: 50px;
1366
+ height: 100rpx;
1278
1367
  /* #endif */
1279
1368
  justify-content: space-between;
1280
1369
  }
1281
1370
 
1282
- .btn-wrapper view {
1371
+ .dd-cropper__btn-wrapper view {
1283
1372
  display: flex;
1284
1373
  align-items: center;
1285
1374
  justify-content: center;
@@ -1289,18 +1378,18 @@
1289
1378
  border-radius: 6%;
1290
1379
  }
1291
1380
 
1292
- .hover {
1381
+ .dd-cropper__btn-hover {
1293
1382
  background: $dd-neutral-50;
1294
1383
  border-radius: 6%;
1295
1384
  }
1296
1385
 
1297
- .clr-wrapper {
1386
+ .dd-cropper__clear-wrapper {
1298
1387
  display: flex;
1299
1388
  flex-direction: row;
1300
1389
  flex-grow: 1;
1301
1390
  }
1302
1391
 
1303
- .clr-wrapper view {
1392
+ .dd-cropper__clear-wrapper view {
1304
1393
  display: flex;
1305
1394
  align-items: center;
1306
1395
  justify-content: center;
@@ -1310,7 +1399,7 @@
1310
1399
  border-radius: 6%;
1311
1400
  }
1312
1401
 
1313
- .my-slider {
1402
+ .dd-cropper__slider {
1314
1403
  flex-grow: 1;
1315
1404
  }
1316
1405
  }