vue利用canvas自定义手写签名

16 篇文章 0 订阅

1.利用vue第三方vue-esign

npm install vue-esign --save

2.在入口文件main.js中引入


import vueEsign from 'vue-esign'
Vue.use(vueEsign)

3.html代码

 <div class="pupu_box">
        <div class="desc_txt">
          <span>注:注:个人签名要求字迹工整、清晰规范、易于识别,杜绝过于艺术化的签名。</span>
        </div>
        <h2>签名区</h2>
        <div class="cavas_box">
          <vue-esign
            ref="esign"
            style="width: 94%;margin:auto;border-radius:6px"
            :height="canHeight"
            :isCrop="isCrop"
            :lineWidth="lineWidth"
            :lineColor="lineColor"
            :bgColor.sync="bgColor"
          />
        </div>
        <div class="bot_box">
          <div @click="handleReset">重新写</div>
          <div @click="handleGenerate">确认</div>
        </div>
        <img
        :style="{'width':imgWidth+'px','height':imgHeight+'px'}"
        :src="simgImg"
        alt
      />
    </div>

4.js代码

data() {//vue中的data中写入
    return {
      simgImg: "", //签名图片
      imgWidth: window.screen.width - window.screen.width * 0.1 - 20,//计算签名图片的宽
      imgHeight:
        ((window.screen.width - window.screen.width * 0.1 - 20) / 5) * 2,//计算签名图片的高,宽高比例为5:2
      boxHeight:
        ((window.screen.width - window.screen.width * 0.1 - 20) / 5) * 2 + 20,
      istile: true,
      isShow: false,
      lineWidth: 2,
      lineColor: "#000000",
      bgColor: "#fff",
      resultImg: "",
      isCrop: true,
      canHeight: window.screen.height - window.screen.height * 0.66 - 20,
    };

5.methods函数方法

handleReset() {
      this.$refs.esign.reset(); //清空画布
    },
handleGenerate() {
      this.$refs.esign
        .generate()
        .then((res) => {
          this.resultImg = res; //生成图片,这里生成的是base64格式
        })
        .catch((err) => {
          alert(err); // 画布没有签字时会执行这里 'Not Signned'
        });
    },

6.预览图

7.注意base64位的图片如果要上传到后台需要做处理

// 将base64转换成file文件流  dataurl是你需要转换的base64的图片
    basetyprfile(dataurl, filename = "file") {
      let arr = dataurl.split(",");
      let mime = arr[0].match(/:(.*?);/)[1];
      let suffix = mime.split("/")[1];
      let bstr = atob(arr[1]);
      let n = bstr.length;
      let u8arr = new Uint8Array(n);
      while (n--) {
        u8arr[n] = bstr.charCodeAt(n);
      }
      let file = new File([u8arr], `${filename}.${suffix}`, { type: mime });
      this.Uploader(file);//上传图片的接口
    },
    Uploader(file) {
      var content = file;
      var data = new FormData();
      data.append("file", content);
      this.$axios({
        url: home.upload,
        method: "POST",
        data: data,
        headers: {
          "Content-Type": "multipart/form-data",
        },
      }).then((res) => {
        if (res.code == 1) {
          console.log(res.data);
        }
      });
    },

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值