uniapp——生成一个签字板

在开发项目中有签名/签字的需求,以下实现:

javascript 复制代码
<template>
  <view class="new_file" v-if="showAutograph">
    <view class="popupBox">
      <view class="popupTopBox">签字板</view>
      <canvas class="mycanvas" canvas-id="mycanvas" @touchstart="touchstart" @touchmove="touchmove" @touchend="touchend"></canvas>
      <view class="determineBtn" @click="confirm">确定</view>
    </view>
    <view class="closeIcon" @click="cancel">
      <van-icon name="close" size="40" />
    </view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      showAutograph: false,
      ctx: '', //绘图图像
      points: [], //路径点集合
      signature: '',
      type: ''
    };
  },
  methods: {
    //创建并显示签名画布
    autograph(type) {
      this.type = type
      this.showAutograph = true;
      //创建绘图对象
      this.ctx = uni.createCanvasContext('mycanvas', this);
      //设置画笔样式
      this.ctx.lineWidth = 4;
      this.ctx.lineCap = 'round';
      this.ctx.lineJoin = 'round';
    },
    //触摸开始,获取到起点
    touchstart(e) {
      let startX = e.changedTouches[0].x;
      let startY = e.changedTouches[0].y;
      let startPoint = {
        X: startX,
        Y: startY
      };
      this.points.push(startPoint);
      //每次触摸开始,开启新的路径
      this.ctx.beginPath();
    },
    //触摸移动,获取到路径点
    touchmove(e) {
      let moveX = e.changedTouches[0].x;
      let moveY = e.changedTouches[0].y;
      let movePoint = {
        X: moveX,
        Y: moveY
      };
      this.points.push(movePoint); //存点
      let len = this.points.length;
      if (len >= 2) {
        this.draw(); //绘制路径
      }
    },
    // 触摸结束,将未绘制的点清空防止对后续路径产生干扰
    touchend() {
      this.points = [];
    },
    /* ***********************************************
          #   绘制笔迹
          #	1.为保证笔迹实时显示,必须在移动的同时绘制笔迹
          #	2.为保证笔迹连续,每次从路径集合中区两个点作为起点(moveTo)和终点(lineTo)
          #	3.将上一次的终点作为下一次绘制的起点(即清除第一个点)
          ************************************************ */
    draw() {
      let point1 = this.points[0];
      let point2 = this.points[1];
      this.points.shift();
      this.ctx.moveTo(point1.X, point1.Y);
      this.ctx.lineTo(point2.X, point2.Y);
      this.ctx.stroke();
      this.ctx.draw(true);
    },
    //清空画布
    clear() {
      let that = this;
      uni.getSystemInfo({
        success: function (res) {
          let canvasw = res.windowWidth;
          let canvash = res.windowHeight;
          that.ctx.clearRect(0, 0, canvasw, canvash);
          that.ctx.draw(true);
        }
      });
    },
    //关闭并清空画布
    cancel() {
      this.showAutograph = false;
      this.clear();
    },
    //完成绘画并保存到本地
    confirm() {
      let that = this;
      uni.canvasToTempFilePath({
        canvasId: 'mycanvas',
        success: function (res) {
          that.$emit('ok', that.type, res.tempFilePath)
          that.cancel()
        }
      });
    }
  }
};
</script>

<style lang="less" scoped>
page {
  font-family: Source Han Sans CN;
}
.new_file {
  position: fixed;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0.5);
  .buttONbOX {
    position: fixed;
    left: 50%;
    top: 50%;
  }
  .popupBox {
    position: fixed;
    left: 50%;
    bottom: 188rpx;
    transform: translate(-50%, 0);
    width: 720rpx;
    height: 1055rpx;
    background: #ffffff;
    border-radius: 16rpx;
    .popupTopBox {
      width: 100%;
      height: 80rpx;
      background: #1f75b4;
      border-radius: 16rpx 16rpx 0px 0px;
      text-align: center;
      line-height: 80rpx;
      font-size: 32rpx;
      color: #ffffff;
    }
    .mycanvas {
      width: 100%;
      height: 860rpx;
    }
    .determineBtn {
      width: 280rpx;
      height: 69rpx;
      background: #1f75b4;
      border-radius: 35rpx;
      margin: 0 auto;
      font-size: 30rpx;
      color: #ffffff;
      text-align: center;
      line-height: 69rpx;
    }
  }
  .closeIcon {
    position: fixed;
    left: 50%;
    bottom: 92rpx;
    transform: translate(-50%, 0);
    width: 50rpx;
    height: 50rpx;
    image {
      width: 100%;
      height: 100%;
    }
  }
}
</style> 

实现效果:

相关推荐
Cutecat_2 小时前
视频字幕处理工具横向:提取模式 vs 编辑模式,该如何选择
android·前端·ios·语音识别
qq_422152573 小时前
PDF 加水印工具怎么选?2026 年文档版权保护方案对比
前端·pdf·github
kyriewen3 小时前
手写 Promise.all、race、any:不到 30 行代码,解决并发异步的所有姿势
前端·javascript·面试
brucelee1864 小时前
OpenClaw 浏览器控制(Chrome MCP)完整教程
前端·chrome
ct9784 小时前
React 状态管理方案深度对比
开发语言·前端·react
胡志辉的博客4 小时前
深入浅出理解浏览器事件循环:从一道输出题讲到 Chrome 源码
前端·javascript·chrome·chromium·event loop
代码不加糖4 小时前
js中不会冒泡的事件有哪些?
前端·javascript·vue.js
懂懂tty4 小时前
Vue2与Vue3之间API差异
前端·javascript·vue.js
AI焦点5 小时前
跨越协议鸿沟:Tool Use状态机从Anthropic到OpenAI兼容体系的适配要点
前端·人工智能
Dxy12393102165 小时前
Python线程锁:为什么多线程会“打架“,以及怎么解决
开发语言·前端·python