Home  >  Article  >  Web Front-end  >  How to implement handwritten signature function in Vue?

How to implement handwritten signature function in Vue?

WBOY
WBOYOriginal
2023-06-25 19:40:394364browse

How to implement handwritten signature function in Vue?

With the advent of e-commerce and the digital age, more and more companies or individuals need to complete signatures in electronic documents. If the handwritten signature function can be implemented in Vue, it will bring a lot of benefits to users. Great convenience.

This article will introduce how to use Vue and some third-party libraries to implement the handwritten signature function.

1. Preparation

Before you start, you need to prepare the following tools:

  1. Vue.js

Vue. js is a lightweight front-end MVVM framework that draws on the design ideas of Angular and React, which is very helpful in improving the maintainability and scalability of web pages.

  1. Signature Pad

Signature Pad is a lightweight JavaScript library that supports creating handwritten signatures and converting signatures to image formats.

  1. Axios

Axios is a Promise-based HTTP client that can be used in browsers and Node.js, supporting various request methods and interceptors.

2. Integrate Signature Pad

  1. Use npm to install Signature Pad

Use npm to install the Signature Pad library in the project.

npm install signature_pad --save
  1. Introducing Signature Pad

In Vue, you can use the import keyword to introduce Signature Pad.

import SignaturePad from 'signature_pad';
  1. Use Signature Pad to create a drawing area

Create a canvas element in the Vue template for users to sign on it.

<template>
  <div>
    <canvas ref="canvas" :width="width" :height="height"></canvas>
  </div>
</template>

In Vue's script, use the mounted method to get a reference to the canvas element and pass it to the Signature Pad.

import SignaturePad from 'signature_pad';

export default {
  name: 'Signature',
  data() {
    return {
      width: 500,
      height: 300
    };
  },
  mounted() {
    const canvas = this.$refs.canvas;
    this.signaturePad = new SignaturePad(canvas);
  }
};
  1. Implementing the signature function

Signature Pad provides a series of methods for processing signature-related operations, such as clearing the drawing area, undoing the previous operation, and exporting the signature image wait.

export default {
  name: 'Signature',
  ...
  methods: {
    // 清空绘制区域
    clear() {
      this.signaturePad.clear();
    },
    // 撤销上一步操作
    undo() {
      const data = this.signaturePad.toData();
      if (data) {
        data.pop();
        this.signaturePad.fromData(data);
      }
    },
    // 判断绘图区域是否为空
    isEmpty() {
      return this.signaturePad.isEmpty();
    },
    // 获取签名图像的base64编码
    getDataUrl() {
      return this.signaturePad.toDataURL();
    }
  }
};

3. Export signature image

In Vue, you can use the Axios library to upload the signature image to the server, or you can submit it using form submission.

  1. Save the signature image to local

To save the signature image to a local file, you can use the 3499910bf9dac5ae3c52d5ede7383485 tag in HTML5 The download attribute implements the download function.

export default {
  name: 'Signature',
  ...
  methods: {
    ...
    // 下载签名图像
    download() {
      const link = document.createElement('a');
      link.href = this.getDataUrl();
      link.download = 'signature.png';
      document.body.appendChild(link);
      link.click();
      document.body.removeChild(link);
    }
  }
};
  1. Upload the signature image to the server

Using the Axios library, you can upload the signature image to the server.

import axios from 'axios';

export default {
  name: 'Signature',
  ...
  methods: {
    ...
    // 将签名图像上传到服务器
    upload() {
      const dataUrl = this.getDataUrl();
      const blob = this.dataURItoBlob(dataUrl);
      const formData = new FormData();
      formData.append('file', blob, 'signature.png');

      axios.post('/api/upload', formData, {
        headers: {
          'Content-Type': 'multipart/form-data'
        }
      }).then(res => {
        console.log(res.data);
      }).catch(err => {
        console.log(err);
      });
    },
    ...
  }
};

4. Summary

This article introduces how to integrate the Signature Pad library in Vue to realize the function of handwritten signature, and introduces how to export the signature image to local or upload it to the server.

Through the above method, the handwritten signature function can be easily implemented in the Vue application, allowing users to perform signature operations more conveniently.

The above is the detailed content of How to implement handwritten signature function in Vue?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn