|
|
|
@ -1,7 +1,7 @@ |
|
|
|
<template> |
|
|
|
<canvas |
|
|
|
:style="{ backgroundColor: backgroundColor, borderRadius: borderRadius }" |
|
|
|
ref="inkCanvas" |
|
|
|
ref="inkCanvasRef" |
|
|
|
:width="width" |
|
|
|
:height="height" |
|
|
|
id="ink-canvas" |
|
|
|
@ -74,11 +74,11 @@ const state = reactive<State>({ |
|
|
|
list: [] //返回汉字列表 |
|
|
|
}) |
|
|
|
|
|
|
|
const inkCanvas = ref<HTMLCanvasElement | null>(null) |
|
|
|
const ctx = computed(() => (inkCanvas.value as HTMLCanvasElement).getContext('2d')) |
|
|
|
const inkCanvasRef = ref<HTMLCanvasElement | null>(null) |
|
|
|
const ctxComRef = computed(() => (inkCanvasRef.value as HTMLCanvasElement).getContext('2d')) |
|
|
|
|
|
|
|
function updateBound() { |
|
|
|
const bound = (inkCanvas.value as HTMLCanvasElement).getBoundingClientRect() |
|
|
|
const bound = (inkCanvasRef.value as HTMLCanvasElement).getBoundingClientRect() |
|
|
|
state.X = bound.x |
|
|
|
state.Y = bound.y |
|
|
|
} |
|
|
|
@ -90,7 +90,7 @@ function down(ev: MouseEvent | TouchEvent) { |
|
|
|
clearTimeout(state.timer) |
|
|
|
state.oldX = cx |
|
|
|
state.oldY = cy |
|
|
|
ctx.value && ctx.value.beginPath() |
|
|
|
ctxComRef.value && ctxComRef.value.beginPath() |
|
|
|
state.isClick = true |
|
|
|
} |
|
|
|
function move(ev: MouseEvent | TouchEvent) { |
|
|
|
@ -104,11 +104,11 @@ function move(ev: MouseEvent | TouchEvent) { |
|
|
|
state.clickC.push(0) |
|
|
|
|
|
|
|
//画图 |
|
|
|
;(ctx.value as CanvasRenderingContext2D).lineWidth = 8 |
|
|
|
;(ctx.value as CanvasRenderingContext2D).lineCap = 'round' |
|
|
|
ctx.value && ctx.value.moveTo(state.oldX, state.oldY) |
|
|
|
ctx.value && ctx.value.lineTo(cx, cy) |
|
|
|
ctx.value && ctx.value.stroke() |
|
|
|
;(ctxComRef.value as CanvasRenderingContext2D).lineWidth = 8 |
|
|
|
;(ctxComRef.value as CanvasRenderingContext2D).lineCap = 'round' |
|
|
|
ctxComRef.value && ctxComRef.value.moveTo(state.oldX, state.oldY) |
|
|
|
ctxComRef.value && ctxComRef.value.lineTo(cx, cy) |
|
|
|
ctxComRef.value && ctxComRef.value.stroke() |
|
|
|
state.oldX = cx |
|
|
|
state.oldY = cy |
|
|
|
} |
|
|
|
@ -127,19 +127,19 @@ function mouseUp() { |
|
|
|
} |
|
|
|
|
|
|
|
function reload() { |
|
|
|
if (!inkCanvas.value) { |
|
|
|
if (!inkCanvasRef.value) { |
|
|
|
return |
|
|
|
} |
|
|
|
state.clickX = [] |
|
|
|
state.clickY = [] |
|
|
|
state.clickC = [] |
|
|
|
;(ctx.value as CanvasRenderingContext2D).strokeStyle = props.strokeStyle |
|
|
|
;(ctx.value as CanvasRenderingContext2D).fillStyle = props.fillStyle |
|
|
|
;(ctx.value as CanvasRenderingContext2D).clearRect(0, 0, props.width, props.height) |
|
|
|
;(ctx.value as CanvasRenderingContext2D).font = `bold ${props.fillFontSize} Arial` |
|
|
|
;(ctx.value as CanvasRenderingContext2D).textAlign = 'center' |
|
|
|
;(ctx.value as CanvasRenderingContext2D).textBaseline = 'middle' |
|
|
|
ctx.value && ctx.value.fillText(props.fillText, props.width / 2, props.height / 2, 1000) |
|
|
|
;(ctxComRef.value as CanvasRenderingContext2D).strokeStyle = props.strokeStyle |
|
|
|
;(ctxComRef.value as CanvasRenderingContext2D).fillStyle = props.fillStyle |
|
|
|
;(ctxComRef.value as CanvasRenderingContext2D).clearRect(0, 0, props.width, props.height) |
|
|
|
;(ctxComRef.value as CanvasRenderingContext2D).font = `bold ${props.fillFontSize} Arial` |
|
|
|
;(ctxComRef.value as CanvasRenderingContext2D).textAlign = 'center' |
|
|
|
;(ctxComRef.value as CanvasRenderingContext2D).textBaseline = 'middle' |
|
|
|
ctxComRef.value && ctxComRef.value.fillText(props.fillText, props.width / 2, props.height / 2, 1000) |
|
|
|
} |
|
|
|
function _getHandWriting() { |
|
|
|
const params = { |
|
|
|
@ -168,14 +168,19 @@ watch( |
|
|
|
onMounted(() => { |
|
|
|
updateBound() |
|
|
|
reload() |
|
|
|
;(inkCanvas.value as HTMLElement).addEventListener('touchstart', down) |
|
|
|
;(inkCanvas.value as HTMLElement).addEventListener('touchmove', move) |
|
|
|
;(inkCanvas.value as HTMLElement).addEventListener('touchend', mouseUp) |
|
|
|
;(inkCanvasRef.value as HTMLElement).addEventListener('touchstart', down) |
|
|
|
;(inkCanvasRef.value as HTMLElement).addEventListener('touchmove', move) |
|
|
|
;(inkCanvasRef.value as HTMLElement).addEventListener('touchend', mouseUp) |
|
|
|
}) |
|
|
|
onBeforeUnmount(() => { |
|
|
|
;(inkCanvas.value as HTMLElement).removeEventListener('touchstart', down) |
|
|
|
;(inkCanvas.value as HTMLElement).removeEventListener('touchmove', move) |
|
|
|
;(inkCanvas.value as HTMLElement).removeEventListener('touchend', mouseUp) |
|
|
|
;(inkCanvasRef.value as HTMLElement).removeEventListener('touchstart', down) |
|
|
|
;(inkCanvasRef.value as HTMLElement).removeEventListener('touchmove', move) |
|
|
|
;(inkCanvasRef.value as HTMLElement).removeEventListener('touchend', mouseUp) |
|
|
|
}) |
|
|
|
|
|
|
|
defineExpose({ |
|
|
|
updateBound, |
|
|
|
reload |
|
|
|
}) |
|
|
|
|
|
|
|
defineExpose({ |
|
|
|
|