1
1
<template >
2
2
<div :class =" classes" >
3
- <div :class =" ['nut-signature-inner', taroEnv === 'WEAPP' ? ' spcanvas_WEAPP' : ' ']" >
3
+ <div :class =" ['nut-signature-inner', ' spcanvas_WEAPP']" >
4
4
<canvas
5
5
ref =" spcanvas"
6
6
class =" spcanvas"
@@ -79,7 +79,7 @@ export default create({
79
79
state .ctx .strokeStyle = props .strokeStyle ;
80
80
};
81
81
82
- const moveEventHandler = (event ) => {
82
+ const moveEventHandler = (event : { preventDefault : () => void ; changedTouches : any [] } ) => {
83
83
event .preventDefault ();
84
84
if (! state .ctx ) {
85
85
return false ;
@@ -94,16 +94,19 @@ export default create({
94
94
mouseX = evt .clientX - coverPos .left ;
95
95
mouseY = evt .clientY - coverPos .top ;
96
96
}
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
+ });
100
103
};
101
104
102
- const endEventHandler = (event ) => {
105
+ const endEventHandler = (event : { preventDefault : () => void } ) => {
103
106
event .preventDefault ();
104
107
emit (' end' );
105
108
};
106
- const leaveEventHandler = (event ) => {
109
+ const leaveEventHandler = (event : { preventDefault : () => void } ) => {
107
110
event .preventDefault ();
108
111
};
109
112
const clear = () => {
@@ -139,25 +142,13 @@ export default create({
139
142
emit (' confirm' , result );
140
143
}
141
144
});
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
- // });
154
145
});
155
146
};
156
147
157
148
onMounted (() => {
158
149
Taro .nextTick (() => {
159
150
setTimeout (() => {
160
- if (Taro .getEnv () === ' WEAPP' ) {
151
+ if (Taro .getEnv () === ' WEAPP' || Taro . getEnv () === ' JD ' ) {
161
152
Taro .createSelectorQuery ()
162
153
.select (' #spcanvas' )
163
154
.fields (
@@ -167,31 +158,55 @@ export default create({
167
158
},
168
159
function (res ) {
169
160
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;
175
171
}
176
172
)
177
173
.exec ();
178
174
} else {
179
- console .log (document .getElementById (' spcanvas' )?.tagName );
180
175
const canvasDom: HTMLElement | null = document .getElementById (' spcanvas' );
181
176
let canvas: HTMLCanvasElement = canvasDom as HTMLCanvasElement ;
182
177
if (canvasDom ?.tagName !== ' CANVAS' ) {
183
178
canvas = canvasDom ?.getElementsByTagName (' canvas' )[0 ] as HTMLCanvasElement ;
184
179
}
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;
191
194
}
192
195
}, 1000 );
193
196
});
194
197
});
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
+ };
195
210
return {
196
211
taroEnv: Taro .getEnv (),
197
212
spcanvas ,
0 commit comments