vue中canvas签名

vue用canvas横屏签名

最近遇到一个签名需求由于canvas一些特性,横屏签名不好控制,也是多方借鉴才解决,写真就是为了记录下,方便有需要的同学。在这里插入图片描述
js代码如下:

import Draw from "../../../libs/draw";
export default {
  name: "canvas",
  data() {
    return {
    
      degree: 0 // 屏幕整体旋转的角度, 可取 -90,90,180等值
    };
  },
  components: {
    Draw
  },
  inject: ["reload"],
  mounted() {
    this.canvasBox = document.getElementById("canvasBox");
    this.initCanvas();
    var evt = "onorientationchange" in window ? "orientationchange" : "resize";
    var that = this;
    window.addEventListener(
      evt,
      function() {
        // alert(evt);
        setTimeout(function() {
          that.reload();
        }, 300);
      },
      false
    );
  },
  computed: {
    getHorizontalStyle() {
      const d = document;
      const w =
        window.innerWidth ||
        d.documentElement.clientWidth ||
        d.body.clientWidth;
      const h =
        window.innerHeight ||
        d.documentElement.clientHeight ||
        d.body.clientHeight;
      let length = (h - w) / 2;
      let width = w;
      let height = h;
      if (width < height) {
        this.degree = 90;
        width = h;
        height = w;
      } else {
        length = 0;
      }
      if (this.canvasBox) {
        this.canvasBox.removeChild(document.querySelector("canvas"));
        this.canvasBox.appendChild(document.createElement("canvas"));
        setTimeout(() => {
          this.initCanvas();
        }, 200);
      }
      return {
        transform: `rotate(${
          this.degree
        }deg) translate(${length}px,${length}px)`,
        width: `${width}px`,
        height: `${height}px`,
        transformOrigin: "center center"
      };
    }
  },
  methods: {
    initCanvas() {
      const canvas = document.querySelector("canvas");
      this.draw = new Draw(canvas, -this.degree);
    },
    clear() {
      this.draw.clear();
    },
   
    download() {
      const image = this.draw.getPNGImage();
      const blob = this.draw.dataURLtoBlob(image);
      const url = "";
       const successCallback = response => {
         console.log(response);
       };
      const failureCallback = error => {
        console.log(error);
      };
      this.draw.upload(blob, url, successCallback, failureCallback);
    }
  }
};

html代码:

<template>
  <div id="canvasBox" :style="getHorizontalStyle">
    <canvas style="background-color:#fff;"></canvas>
    <div class="greet">
      <div class="btnBox">
        <div class="btn" @click.native="clear()">重置</div>
        <div class="btn" @click.native="download()" style="backgroundColor:#409EEF">确认</div>
      </div>
    </div>
  </div>
</template>

css部分


<style lang="less" scoped>
#canvasBox {
  display: flex;
  flex-direction: column;
  height: 100%;
}
.greet {
  padding: 20px;
  font-size: 20px;
  user-select: none;
}
.greet a {
  cursor: pointer;
}
.greet select {
  font-size: 18px;
}
canvas {
  flex: 1;
  cursor: crosshair;
}
.btnBox {
  padding: 0 2%;
  box-sizing: border-box;
  .btn {
    text-align: center;
    color: #fff;
    background-color: #f91;
    height: 40px;
    line-height: 40px;
    font-weight: 300;
    font-size: 1em;
    border-radius: 4px;
  }
}
</style>

这个项目需要一个插件draw.js
毕竟不是app无法控制横竖屏,所以本来是横屏就直接签名,竖屏旋转。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值