SignaturePad.vue 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <template>
  2. <div class="signature-wrapper">
  3. <canvas
  4. ref="canvas"
  5. :width="width"
  6. :height="height"
  7. class="signature-canvas"
  8. ></canvas>
  9. <div class="signature-actions">
  10. <el-button size="small" type="primary" @click="save">保存签名</el-button>
  11. <el-button size="small" @click="clear">{{ clearButtonText }}</el-button>
  12. </div>
  13. <div v-if="preview" class="signature-preview">
  14. <p>签名预览:</p>
  15. <img :src="preview" alt="签名" />
  16. </div>
  17. </div>
  18. </template>
  19. <script>
  20. import SignaturePad from 'signature_pad'
  21. // import SignaturePad from 'signature_pad/dist/signature_pad.min.js'
  22. export default {
  23. name: 'SignaturePad',
  24. props: {
  25. modelValue: {
  26. type: String,
  27. default: ''
  28. },
  29. width: {
  30. type: Number,
  31. default: 500
  32. },
  33. height: {
  34. type: Number,
  35. default: 200
  36. },
  37. penColor: {
  38. type: String,
  39. default: '#000'
  40. },
  41. backgroundColor: {
  42. type: String,
  43. default: '#fff'
  44. },
  45. clearButtonText: {
  46. type: String,
  47. default: '清空签名'
  48. }
  49. },
  50. data() {
  51. return {
  52. signaturePad: null,
  53. preview: this.modelValue
  54. }
  55. },
  56. mounted() {
  57. const canvas = this.$refs.canvas
  58. const ctx = canvas.getContext('2d')
  59. ctx.fillStyle = this.backgroundColor
  60. ctx.fillRect(0, 0, this.width, this.height)
  61. this.signaturePad = new SignaturePad(canvas, {
  62. penColor: this.penColor,
  63. backgroundColor: this.backgroundColor
  64. })
  65. this.signaturePad.onEnd = () => {
  66. const dataUrl = this.signaturePad.toDataURL()
  67. this.preview = dataUrl
  68. this.$emit('update:modelValue', dataUrl)
  69. }
  70. },
  71. methods: {
  72. clear() {
  73. this.signaturePad.clear()
  74. this.preview = ''
  75. this.$emit('update:modelValue', '')
  76. },
  77. save() {
  78. if (this.signaturePad.isEmpty()) {
  79. this.$message.warning('请先签名')
  80. return
  81. }
  82. const dataUrl = this.signaturePad.toDataURL()
  83. this.preview = dataUrl
  84. this.$emit('update:modelValue', dataUrl)
  85. this.$message.success('签名已保存')
  86. }
  87. }
  88. }
  89. </script>
  90. <style scoped>.signature-wrapper {
  91. display: flex;
  92. flex-direction: column;
  93. align-items: flex-start;
  94. }
  95. .signature-canvas {
  96. border: 1px solid #ccc;
  97. border-radius: 4px;
  98. cursor: crosshair;
  99. }
  100. .signature-actions {
  101. margin-top: 8px;
  102. }
  103. .signature-preview {
  104. margin-top: 10px;
  105. }
  106. .signature-preview img {
  107. border: 1px solid #ddd;
  108. max-width: 100%;
  109. height: auto;
  110. }
  111. </style>