vue使用smooth-signature实现移动端电子签字,包括横竖屏

vue使用smooth-signature实现移动端电子签字,包括横竖屏

1.使用smooth-signature

npm install --save smooth-signature

二.页面引入插件

import SmoothSignature from "smooth-signature";

三.实现效果

企业微信截图_16983060016752.png

企业微信截图_16983060187445.png

四.完整代码

<template>
  <div class="sign-finish">
    <div class="wrap1" v-show="showFull">
      <span class="sign-title">请在区域内签字</span>
      <canvas class="canvas1" ref="canvas1" />
      <div class="actions">
          <button class="danger" @click="handleClear1" >清除</button>
          <button class="warning" @click="handleUndo1" >撤销</button>
          <button class="primary" @click="handleFull" >横屏</button>
          <button class="success" @click="handlePreview1" >保存</button>
      </div>
    </div>
    <div class="wrap2" v-show="!showFull">
      <div class="actionsWrap">
        <div class="actions">
          <button class="danger" @click="handleClear1" >清除</button>
          <button class="warning" @click="handleUndo1" >撤销</button>
          <button class="primary" @click="handleFull" >竖屏</button>
          <button class="success" @click="handlePreview1" >保存</button>
        </div>
      </div>
      <canvas class="canvas" ref="canvas2" />
    </div>
  </div>
</template>

<script>
import SmoothSignature from "smooth-signature";
export default {
  name: "mbDemo",
  data() {
    return {
      showFull: true,
    };
  },
  mounted() {
    this.initSignature1();
    this.initSignture2();
  },
  methods: {
    initSignature1() {
      const canvas = this.$refs["canvas1"];
      const options = {
        width: window.innerWidth - 30,
        height: 200,
        minWidth: 2,
        maxWidth: 6,
        openSmooth:true,
        // color: "#1890ff",
        bgColor: '#f6f6f6',
      };
      this.signature1 = new SmoothSignature(canvas, options);
    },
    initSignture2() {
      const canvas = this.$refs["canvas2"];
      const options = {
        width: window.innerWidth - 120,
        height: window.innerHeight - 80,
        minWidth: 3,
        maxWidth: 10,
        openSmooth:true,
        // color: "#1890ff",
        bgColor: '#f6f6f6',
      };
      this.signature2 = new SmoothSignature(canvas, options);
    },
    handleClear1() {
      this.signature1.clear();
    },
    handleClear2() {
      this.signature2.clear();
    },
    handleUndo1() {
      this.signature1.undo();
    },
    handleUndo2() {
      this.signature2.undo();
    },
    handleFull() {
      this.showFull = !this.showFull;
    },
    handlePreview1() {
      const isEmpty = this.signature1.isEmpty();
      if (isEmpty) {
        alert("isEmpty");
        return;
      }
      const pngUrl = this.signature1.getPNG();
      console.log(pngUrl);
      // window.previewImage(pngUrl);
    },
    handlePreview2() {
      const isEmpty = this.signature2.isEmpty();
      if (isEmpty) {
        alert("isEmpty");
        return;
      }
      const canvas = this.signature2.getRotateCanvas(-90);
      const pngUrl = canvas.toDataURL();
      console.log('pngUrl',pngUrl);
      // window.previewImage(pngUrl, 90);
    },
  },
};
</script>

<style lang="less">
.sign-finish {
  height: 100vh;
  width: 100vw;
  button {
    height: 32px;
    padding: 0 8px;
    font-size: 12px;
    border-radius: 2px;
  }
  .danger {
    color: #fff;
    background: #ee0a24;
    border: 1px solid #ee0a24;
  }
  .warning {
    color: #fff;
    background: #ff976a;
    border: 1px solid #ff976a;
  }
  .primary {
    color: #fff;
    background: #1989fa;
    border: 1px solid #1989fa;
  }
  .success {
    color: #fff;
    background: #07c160;
    border: 1px solid #07c160;
  }
  canvas {
    border-radius: 10px;
    border: 2px dashed #ccc;
    
  }
  .wrap1 {
    height: 100%;
    width: 96%;
    margin: auto;
    margin-top: 100px;
    .actions {
      display: flex;
      justify-content: space-around;
    }
  }
  .wrap2 {
    padding: 15px;
    height: 100%;
    display: flex;
    justify-content: center;
    .actionsWrap {
      width: 50px;
      display: flex;
      justify-content: center;
      align-items: center;
    }
    .canvas {
      flex: 1;
    }
    .actions {
      margin-right: 10px;
      white-space: nowrap;
      transform: rotate(90deg);
      button{
          margin-right: 20px;
      }
    }
  }
}
</style>

五.参考

https://github.com/linjc/smooth-signature

`vue-signature-pad` 是基于 `signature_pad` 的 Vue 封装组件,用于在网页应用上实现手写签名功能。它非常适合移动设备使用,并且可以很方便地集成到Vue项目当中。 关于移动端横屏效果的实现: 1. **页面布局调整** 要让整个页面适应横向屏幕显示,你可以通过CSS来进行控制。通常我们会设置HTML和body标签的高度宽度均为100%,并且利用Flexbox或其他类似技术保证内部元素能自适应填充整个视窗区域。 2. **Canvas尺寸设定** 对于`<canvas>`元素来说,在横屏模式下你需要动态计算其宽高值,使其能够占据最大可用空间而不失真。一种做法是在窗口大小改变事件(resize event)监听函数里实时更新画布尺寸属性。 3. **方向锁定或提示** 如果希望强制用户保持某一特定的方向操作,则可以在检测到当前设备方位变化时给出相应提醒;若允许自由切换则需考虑如何妥善处理坐标系转换的问题。 下面是一个简单的例子展示怎样配合`vue-signature-pad`做一些基础配置来支持横屏场景: ```html <div id="app" style="display:flex;justify-content:center;"> <div class="sig-container"> <!-- 签名板 --> <vue-signature-pad width="500px" height="300px"></vue-signature-pad> </div> </div> <style scoped> @media only screen and (orientation: landscape) { .sig-container{ transform: rotate(-90deg); /* 根据需要选择是否旋转 */ margin-left:-75vh; position:absolute; left:50%; top:50%; transform-origin:top left; -webkit-transform:translate(-50%,-50%) rotate(-90deg); -moz-transform:translate(-50%,-50%) rotate(-90deg); -ms-transform:translate(-50%,-50%) rotate(-90deg); -o-transform:translate(-50%,-50%) rotate(-90deg); transform:translate(-50%,-50%) rotate(-90deg); } } </style> <script src="https://cdn.jsdelivr.net/npm/vue@next"></script> <script src="path/to/vue-signature-pad.min.js"></script> // 引入vue-signature-pad库文件 ``` 注意上述示例只是粗略框架示意,实际开发过程中还需要结合具体业务需求完善更多细节部分比如样式优化、交互体验等。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值