Skip to content

Commit a8b4257

Browse files
authored
fix(Signature): 签名组件文档,demo 优化 (#1856)
1 parent 66c5755 commit a8b4257

File tree

5 files changed

+71
-52
lines changed

5 files changed

+71
-52
lines changed

src/packages/__VUE/signature/doc.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
### 介绍
44

5-
基于Canvas的签名组件。
5+
基于Canvas的签名组件。默认竖屏模式使用,如使用横屏模式,请开发者自行设置旋转角度或者宽高
66

77
### 安装
88

src/packages/__VUE/signature/index.scss

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
.nut-signature {
22
.nut-signature-inner {
3-
// height: 10rem;
3+
height: 10rem;
44
margin-bottom: 1rem;
55
border: 1px solid #dadada;
66
display: flex;
77
justify-content: center;
88
align-items: center;
99
}
1010
.spcanvas_WEAPP {
11-
height: 10rem;
11+
width: 100%;
12+
height: 100%;
13+
#spcanvas {
14+
width: 100%;
15+
}
1216
}
1317

1418
.nut-signature-btn {

src/packages/__VUE/signature/index.taro.vue

+47-32
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<template>
22
<div :class="classes">
3-
<div :class="['nut-signature-inner', taroEnv === 'WEAPP' ? 'spcanvas_WEAPP' : '']">
3+
<div :class="['nut-signature-inner', 'spcanvas_WEAPP']">
44
<canvas
55
ref="spcanvas"
66
class="spcanvas"
@@ -79,7 +79,7 @@ export default create({
7979
state.ctx.strokeStyle = props.strokeStyle;
8080
};
8181
82-
const moveEventHandler = (event) => {
82+
const moveEventHandler = (event: { preventDefault: () => void; changedTouches: any[] }) => {
8383
event.preventDefault();
8484
if (!state.ctx) {
8585
return false;
@@ -94,16 +94,19 @@ export default create({
9494
mouseX = evt.clientX - coverPos.left;
9595
mouseY = evt.clientY - coverPos.top;
9696
}
97-
98-
state.ctx?.lineTo(mouseX, mouseY);
99-
state.ctx?.stroke();
97+
Taro.nextTick(() => {
98+
state.ctx.lineCap = 'round';
99+
state.ctx.lineJoin = 'round';
100+
state.ctx?.lineTo(mouseX, mouseY);
101+
state.ctx?.stroke();
102+
});
100103
};
101104
102-
const endEventHandler = (event) => {
105+
const endEventHandler = (event: { preventDefault: () => void }) => {
103106
event.preventDefault();
104107
emit('end');
105108
};
106-
const leaveEventHandler = (event) => {
109+
const leaveEventHandler = (event: { preventDefault: () => void }) => {
107110
event.preventDefault();
108111
};
109112
const clear = () => {
@@ -139,25 +142,13 @@ export default create({
139142
emit('confirm', result);
140143
}
141144
});
142-
143-
// Taro.canvasToTempFilePath({
144-
// canvas: res[0].node,
145-
// canvasId: 'spcanvas',
146-
// fileType: props.type
147-
// })
148-
// .then((res) => {
149-
// emit('confirm', res.tempFilePath);
150-
// })
151-
// .catch((e) => {
152-
// emit('confirm', e);
153-
// });
154145
});
155146
};
156147
157148
onMounted(() => {
158149
Taro.nextTick(() => {
159150
setTimeout(() => {
160-
if (Taro.getEnv() === 'WEAPP') {
151+
if (Taro.getEnv() === 'WEAPP' || Taro.getEnv() === 'JD') {
161152
Taro.createSelectorQuery()
162153
.select('#spcanvas')
163154
.fields(
@@ -167,31 +158,55 @@ export default create({
167158
},
168159
function (res) {
169160
const canvas = res.node;
170-
const ctx = canvas.getContext('2d');
171-
state.canvas = canvas;
172-
state.ctx = ctx;
173-
state.canvasWidth = res.width;
174-
state.canvasHeight = res.height;
161+
canvasSetting(canvas, res.width, res.height);
162+
// const dpr = Taro.getSystemInfoSync().pixelRatio;
163+
// const ctx = canvas.getContext('2d');
164+
// canvas.width = res.width * dpr;
165+
// canvas.height = res.height * dpr;
166+
// state.canvas = canvas;
167+
// ctx.scale(dpr, dpr);
168+
// state.ctx = ctx;
169+
// state.canvasWidth = res.width * dpr;
170+
// state.canvasHeight = res.height * dpr;
175171
}
176172
)
177173
.exec();
178174
} else {
179-
console.log(document.getElementById('spcanvas')?.tagName);
180175
const canvasDom: HTMLElement | null = document.getElementById('spcanvas');
181176
let canvas: HTMLCanvasElement = canvasDom as HTMLCanvasElement;
182177
if (canvasDom?.tagName !== 'CANVAS') {
183178
canvas = canvasDom?.getElementsByTagName('canvas')[0] as HTMLCanvasElement;
184179
}
185-
// const canvas: any = document.getElementById('spcanvas')?.getElementsByTagName('canvas')[0];
186-
const ctx = canvas.getContext('2d');
187-
state.canvas = canvas;
188-
state.ctx = ctx;
189-
state.canvasWidth = canvas.width;
190-
state.canvasHeight = canvas.height;
180+
canvasSetting(canvas, canvasDom?.offsetWidth as number, canvasDom?.offsetHeight as number);
181+
182+
// const ctx: any = canvas.getContext('2d');
183+
// const dpr = Taro.getSystemInfoSync().pixelRatio;
184+
// const _w = (canvasDom?.offsetWidth as number) * dpr;
185+
// const _h = (canvasDom?.offsetHeight as number) * dpr;
186+
187+
// canvas.width = _w;
188+
// canvas.height = _h;
189+
// state.canvas = canvas;
190+
// ctx?.scale(dpr, dpr);
191+
// state.ctx = ctx;
192+
// state.canvasWidth = _w;
193+
// state.canvasHeight = _h;
191194
}
192195
}, 1000);
193196
});
194197
});
198+
const canvasSetting = (canvasDom: any, width: number, height: number) => {
199+
const canvas = canvasDom;
200+
const dpr = Taro.getSystemInfoSync().pixelRatio;
201+
const ctx = canvas.getContext('2d');
202+
canvas.width = width * dpr;
203+
canvas.height = height * dpr;
204+
state.canvas = canvas;
205+
ctx.scale(dpr, dpr);
206+
state.ctx = ctx;
207+
state.canvasWidth = width * dpr;
208+
state.canvasHeight = height * dpr;
209+
};
195210
return {
196211
taroEnv: Taro.getEnv(),
197212
spcanvas,

src/sites/mobile-taro/vue/src/business/pages/signature/index.vue

+16-16
Original file line numberDiff line numberDiff line change
@@ -10,38 +10,28 @@
1010
@signing="signing"
1111
@end="end"
1212
/>
13+
<image :src="demoSignUrl" class="demoSignUrl" v-if="demoSignUrl" />
1314
</div>
1415
</template>
1516

1617
<script lang="ts">
17-
import Taro from '@tarojs/taro';
18-
import { reactive } from 'vue';
18+
import { ref, reactive } from 'vue';
1919
export default {
2020
props: {},
2121
setup() {
22+
const demoSignUrl = ref('');
2223
const state = reactive({
2324
lineWidth: 4,
2425
strokeStyle: 'green',
2526
testimg: ''
2627
});
2728
const clear = () => {
29+
demoSignUrl.value = '';
2830
console.log('清除事件');
2931
};
3032
const confirm = (canvas, data: any) => {
33+
demoSignUrl.value = data;
3134
console.log('图片地址', canvas, data);
32-
Taro.saveImageToPhotosAlbum({
33-
filePath: `${data}`,
34-
success(res) {
35-
Taro.showToast({
36-
title: '保存成功'
37-
});
38-
},
39-
fail(err) {
40-
Taro.showToast({
41-
title: '保存失败'
42-
});
43-
}
44-
});
4535
};
4636
const start = () => {
4737
console.log('签名开始');
@@ -52,12 +42,15 @@ export default {
5242
const end = () => {
5343
console.log('签名结束');
5444
};
55-
return { ...state, confirm, clear, start, signing, end };
45+
return { ...state, confirm, clear, start, signing, end, demoSignUrl };
5646
}
5747
};
5848
</script>
5949

6050
<style lang="scss">
51+
#app .demo {
52+
height: auto;
53+
}
6154
.nut-cell {
6255
padding: 20px 0;
6356
height: 120px;
@@ -69,4 +62,11 @@ export default {
6962
width: 80%;
7063
}
7164
}
65+
#spcanvas {
66+
height: 400px;
67+
}
68+
.demoSignUrl {
69+
width: 200px;
70+
height: 200px;
71+
}
7272
</style>

src/sites/mobile-taro/vue/src/exhibition/pages/countup/index.vue

+1-1
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ export default {
114114
}
115115
.show-demo {
116116
background: #ffffff;
117-
padding: 0 20px;
117+
padding: 0;
118118
}
119119
h2 {
120120
padding: 0 20px;

0 commit comments

Comments
 (0)