chore(Signature): improve ctx typing (#11793)

This commit is contained in:
neverland 2023-05-01 11:57:20 +08:00 committed by GitHub
parent 21e3c13928
commit 8987c5856a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 15 deletions

View File

@ -1,8 +1,8 @@
import { import {
defineComponent,
reactive,
ref, ref,
reactive,
onMounted, onMounted,
defineComponent,
type ExtractPropTypes, type ExtractPropTypes,
} from 'vue'; } from 'vue';
import { createNamespace, makeNumberProp, makeStringProp } from '../utils'; import { createNamespace, makeNumberProp, makeStringProp } from '../utils';
@ -27,13 +27,13 @@ export default defineComponent({
emits: ['submit', 'clear', 'start', 'end', 'signing'], emits: ['submit', 'clear', 'start', 'end', 'signing'],
setup(props, { emit }) { setup(props, { emit }) {
const canvasRef = ref<HTMLCanvasElement | null>(null); const canvasRef = ref<HTMLCanvasElement>();
const wrapRef = ref<HTMLElement | null>(null); const wrapRef = ref<HTMLElement>();
const state = reactive({ const state = reactive({
width: 0, width: 0,
height: 0, height: 0,
ctx: null as any, ctx: null as CanvasRenderingContext2D | null | undefined,
isSupportTouch: 'ontouchstart' in window, isSupportTouch: 'ontouchstart' in window,
}); });
@ -46,6 +46,7 @@ export default defineComponent({
if (!state.ctx) { if (!state.ctx) {
return false; return false;
} }
const evt = event.changedTouches const evt = event.changedTouches
? event.changedTouches[0] ? event.changedTouches[0]
: event.targetTouches[0]; : event.targetTouches[0];
@ -59,6 +60,7 @@ export default defineComponent({
mouseX = evt.clientX - (coverPos?.left || 0); mouseX = evt.clientX - (coverPos?.left || 0);
mouseY = evt.clientY - (coverPos?.top || 0); mouseY = evt.clientY - (coverPos?.top || 0);
} }
state.ctx.lineCap = 'round'; state.ctx.lineCap = 'round';
state.ctx.lineJoin = 'round'; state.ctx.lineJoin = 'round';
state.ctx?.lineTo(mouseX, mouseY); state.ctx?.lineTo(mouseX, mouseY);
@ -94,25 +96,24 @@ export default defineComponent({
} }
const isEmpty = isCanvasEmpty(canvas); const isEmpty = isCanvasEmpty(canvas);
const _canvas = isEmpty ? null : canvas; const filePath = isEmpty
const _filePath = isEmpty
? '' ? ''
: canvas.toDataURL( : canvas.toDataURL(
`image/${props.type}`, `image/${props.type}`,
props.type === 'jpg' ? 0.9 : null props.type === 'jpg' ? 0.9 : null
); );
const data = { emit('submit', {
canvas: _canvas, canvas: isEmpty ? null : canvas,
filePath: _filePath, filePath,
}; });
emit('submit', data);
}; };
const clear = () => { const clear = () => {
if (state.ctx) {
state.ctx.clearRect(0, 0, state.width, state.height); state.ctx.clearRect(0, 0, state.width, state.height);
state.ctx.closePath(); state.ctx.closePath();
}
emit('clear'); emit('clear');
}; };

View File

@ -16,6 +16,7 @@
background-color: var(--van-signature-content-background); background-color: var(--van-signature-content-background);
border: var(--van-signature-content-border); border: var(--van-signature-content-border);
} }
&__footer { &__footer {
display: flex; display: flex;
justify-content: flex-end; justify-content: flex-end;