11<script setup lang="ts">
2- import VueEsign from ' vue-esign'
2+ import type { SignCanvasExpose , SignCanvasOptions } from ' sign-canvas'
3+ import SignCanvas from ' sign-canvas'
34import Alert from ' ./components/alert.vue'
45import Command from ' ./components/command.vue'
56
6- const esignRef = useTemplateRef <any >(' esignRef' )
7- const options = ref ({
8- lineWidth: 6 ,
9- lineColor: ' #000000' ,
7+ const signCanvasRef = useTemplateRef <SignCanvasExpose >(' signCanvasRef' )
8+ const signature = shallowRef <string | null >(null )
9+ const result = shallowRef <string | null >(null )
10+ const options = reactive <SignCanvasOptions >({
11+ canvasWidth: 600 ,
12+ canvasHeight: 360 ,
13+ isDpr: true ,
14+ isSign: true ,
15+ writeColor: ' #101010' ,
1016 bgColor: ' #f0f0f0' ,
11- isCrop: false ,
17+ isShowBorder: false ,
18+ imgType: ' png' ,
1219})
13- const result = ref (' ' )
1420
1521function handleReset() {
16- esignRef .value .reset ()
17- nextTick (() => {
18- options .value .bgColor = ' #f0f0f0'
19- })
22+ signCanvasRef .value ?.clear ()
23+ signature .value = null
24+ result .value = null
2025}
2126function handleGenerate() {
22- esignRef .value .generate ().then ((res : string ) => {
23- result .value = res
24- }).catch (() => {
27+ const image = signCanvasRef .value ?.toDataURL ()
28+ if (! image ) {
2529 useFaToast ().warning (' 画板为空,无法生成图片' )
26- })
30+ return
31+ }
32+ result .value = image
2733}
2834function handleDownload() {
29- esignRef .value .generate ().then ((res : string ) => {
30- const image = new Image ()
31- image .setAttribute (' crossOrigin' , ' anonymous' )
32- image .onload = () => {
33- const a = document .createElement (' a' )
34- const event = new MouseEvent (' click' )
35- a .download = Date .parse (new Date ().toString ()).toString ()
36- a .href = image .src
37- a .dispatchEvent (event )
38- }
39- image .src = res
40- })
35+ const image = signCanvasRef .value ?.downloadSignImg (Date .now ().toString ())
36+ if (! image ) {
37+ useFaToast ().warning (' 画板为空,无法下载图片' )
38+ }
4139}
4240
4341function open(url : string ) {
@@ -50,15 +48,15 @@ function open(url: string) {
5048 <Alert />
5149 <FaPageHeader title="电子签名">
5250 <template #description >
53- <Command text="pnpm add vue-esign " />
51+ <Command text="pnpm add sign-canvas " />
5452 </template >
55- <FaButton variant="outline" size="icon" @click =" open (' https://github.com/JaimeCheng /vue-esign ' )" >
53+ <FaButton variant="outline" size="icon" @click =" open (' https://github.com/langyuxiansheng /vue-sign-canvas ' )" >
5654 <FaIcon name="i-simple-icons:github " />
5755 </FaButton >
5856 </FaPageHeader >
5957 <FaPageMain >
6058 <div class =" space-y-2" >
61- <VueEsign ref="esignRef " v-model : bg-color = " options . bgColor " :width = " 500 " : height = " 300 " : is-crop = " options . isCrop " : line-width =" options . lineWidth " : line-color = " options . lineColor " />
59+ <SignCanvas ref="signCanvasRef " v-model = " signature " :options =" options " />
6260 <div class =" space-x-2" >
6361 <FaButton @click =" handleReset " >
6462 清空画板
@@ -70,7 +68,7 @@ function open(url: string) {
7068 下载图片
7169 </FaButton >
7270 </div >
73- <img v-if =" result" :src =" result" :width =" 500 " :height =" 300 " >
71+ <img v-if =" result" :src =" result" :width =" 600 " :height =" 360 " >
7472 </div >
7573 </FaPageMain >
7674 </div >
0 commit comments