vue-esign实现电子签名

  1. 导入依赖
pnpm install vue-esign --save
  1. sign.vue代码实现
<template>
  <div id="app">
    <div class="signMask" v-if="autographStatus">
      <div class="sigh-btns">
        <button class="btn" type="info" @click="handleCancel">取消</button>
        <button class="btn" type="danger" @click="handleReset">清空画板</button>
        <button class="btn" type="primary" @click="handleGenerate">
          确认签名
        </button>
      </div>
      <div class="sign-box">
        <div class="canvsborder">
          <vue-esign ref="esign" :width="400" :height="800" :isCrop="isCrop" :lineWidth="lineWidth"
            :lineColor="lineColor" :format="'image/png'" :bgColor="bgColor" />
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import vueEsign from "vue-esign";
export default {
  data() {
    return {
      autographStatus: true, //todo false
      lineWidth: 6,
      lineColor: "#000000",
      resultImg: "",
      isCrop: false,
      bgColor: "#e9e8e8",
    };
  },
  components: {
    vueEsign,
  },
  methods: {
    //确认签名后展示签名画板 false->true
    agreeSign() {
      this.autographStatus = true;
    },
    handleCancel() {
      //todo 返回上一层
    },
    // 清空画布
    handleReset() {
      this.$refs.esign.reset();
    },
    // 生成签名的base64图片
    handleGenerate() {
      this.$refs.esign
        .generate()
        .then((res) => {
          this.imgSrc = res;
          console.log(res, this.base64ImgtoFile(res));
        })
        .catch((err) => {
          console.log(err, "画布没有签字!");
        });
    },
    // 附:base64转化成图片
    base64ImgtoFile(dataurl, fileName = "file") {
      const arr = dataurl.split(",");
      const mime = arr[0].match(/:(.*?);/)[1];
      const suffix = mime.split("/")[1];
      const bstr = atob(arr[1]);
      let n = bstr.length;
      var u8arr = new Uint8Array(n);
      while (n--) {
        u8arr[n] = bstr.charCodeAt(n);
      }
      return new File([u8arr], `${fileName}.${suffix}`, { type: mime });
    },
  },
};
</script>
<style scoped>
.signMask {
  width: 100%;
  height: 100%;
  background: #fff;
  position: fixed;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  flex-direction: row;
}

.sign-box,
.signMask {
  margin: auto;
  display: flex;
}

.sign-box {
  width: 80%;
  height: 90%;
  flex-direction: column;
  text-align: center;
}

.sigh-btns,
.sign-view {
  height: 100%;
}

.sigh-btns {
  margin: auto;
  display: flex;
  flex-direction: column;
  justify-content: space-around;
}

.btn {
  height: 40px;
  padding: 8rpx 40rpx;
  font-size: 14px;
  transform: rotate(90deg);
  border: 1rpx solid grey;
}

.mycanvas {
  margin: auto 0rpx;
  background-color: #ececec;
}

.canvsborder {
  display: flex;
  justify-content: center;
  align-items: center;
}
</style>

3.效果图
在这里插入图片描述
在这里插入图片描述

相关推荐

  1. 使用 Vue.js 实现一个电子签名系统

    2024-06-06 07:50:02       6 阅读
  2. vue h5项目预览pdf文件+电子签名

    2024-06-06 07:50:02       9 阅读
  3. 快速制作个人电子签名

    2024-06-06 07:50:02       9 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-06-06 07:50:02       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-06-06 07:50:02       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-06-06 07:50:02       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-06-06 07:50:02       18 阅读

热门阅读

  1. Django中间件

    2024-06-06 07:50:02       5 阅读
  2. Python环境与编辑器:探索编程世界的双翼

    2024-06-06 07:50:02       11 阅读
  3. Flink 入门案例介绍

    2024-06-06 07:50:02       6 阅读
  4. 论文阅读:Fast Neural Scene Flow

    2024-06-06 07:50:02       7 阅读
  5. Openstack学习(1)——架构

    2024-06-06 07:50:02       6 阅读
  6. 022、键管理_遍历键

    2024-06-06 07:50:02       6 阅读
  7. Pspark从hive读数据写到Pgsql数据库

    2024-06-06 07:50:02       8 阅读