uniapp开发小程序手写板、签名、签字

在uniapp中实现手写板、签名、签字功能,可以使用小程序的canvas组件。以下是一个简单的示例,展示了如何在uniapp中创建一个可以手写的画板,并保存为图片。

  1. 在页面的.vue文件中添加canvas组件:



<template>
  <view>
    <canvas canvas-id="signature-pad" style="width: 300px; height: 200px;"></canvas>
    <button @click="saveSignature">保存签名</button>
  </view>
</template>
  1. <script>中添加相关的方法:



<script>
export default {
  data() {
    return {
      context: null,
      isTouchMove: false,
      lastPoint: {}
    };
  },
  onLoad() {
    const ctx = uni.createCanvasContext('signature-pad', this);
    this.context = ctx;
    this.clearSignature();
  },
  methods: {
    clearSignature() {
      const ctx = this.context;
      ctx.setFillStyle('#fff');
      ctx.fillRect(0, 0, 300, 200);
      ctx.setStrokeStyle('#000');
      ctx.setLineWidth(2);
      ctx.setLineCap('round');
      ctx.setLineJoin('round');
    },
    draw(e) {
      if (e.type === 'touchstart') {
        this.isTouchMove = false;
        this.lastPoint.x = e.touches[0].x;
        this.lastPoint.y = e.touches[0].y;
      } else if (e.type === 'touchmove') {
        this.isTouchMove = true;
        const currentPoint = { x: e.touches[0].x, y: e.touches[0].y };
        this.context.moveTo(this.lastPoint.x, this.lastPoint.y);
        this.context.lineTo(currentPoint.x, currentPoint.y);
        this.context.stroke();
        this.lastPoint = currentPoint;
      } else if (e.type === 'touchend' && !this.isTouchMove) {
        // 触发清除
        this.clearSignature();
      }
    },
    saveSignature() {
      const ctx = this.context;
      uni.canvasToTempFilePath({
        canvasId: 'signature-pad',
        success: (res) => {
          console.log('签名图片保存成功:', res.tempFilePath);
          // 处理保存的图片,例如上传到服务器等
          // uni.uploadFile({
          //   url: 'YOUR_UPLOAD_API',
          //   filePath: res.tempFilePath,
          //   name: 'file',
          //   success: (uploadRes) => {
          //     console.log('上传成功:', uploadRes);
          //   },
          //   fail: (uploadErr) => {
          //     console.log('上传失败:', uploadErr);
          //   }
          // });
        },
        fail: (err) => {
          console.error('签名保存失败:', err);
        }
      }, this);
    }
  }
}
</script>
  1. <style>中添加样式(可选):



<style>
button {
  margin-top: 10px;
}
</style>

这段代码实现了一个基本的手写板功能,用户可以在画布上签名,然后点击保存按钮将签名保存为图片。你可以根据自己的需求对代码

评论已关闭

推荐阅读

Vue中使用mind-map实现在线思维导图
2024年08月04日
VUE
Web前端最全Vue实现免密登录跳转的方式_vue怎么样不登录返回首页,最强技术实现
2024年08月04日
VUE
vue3 项目搭建教程(基于create-vue,vite,Vite + Vue)
2024年08月04日
VUE
Vue-颜色选择器实现方案——>Vue-Color( 实战*1+ Demo*7)
2024年08月04日
VUE
Vue项目卡顿慢加载?这些优化技巧告诉你!_vue数据多渲染卡顿
2024年08月04日
VUE
vue中的keep-alive详解与应用场景
2024年08月04日
VUE
Vue、React实现excel导出功能(三种实现方式保姆级讲解)
2024年08月04日
vue-office/docx插件实现docx文件预览
2024年08月04日
VUE
java调用js文件的两种方法(支持V8引擎)
2024年08月04日
JavaScript:解决计算精度问题/mathjs/bignumber.js/big.js/decimal.js
2024年08月04日
两周从爬虫小白变大神 _yjs_js_security_passport
2024年08月04日
JS笔记(对象、函数、数组)
2024年08月04日
Markdown.js:强大的纯JavaScript Markdown解析器
2024年08月04日
Vue项目:js模拟点击a标签下载文件并重命名,URL文件地址下载方法、请求接口下载文件方法总结。
2024年08月04日
vue 父组件怎么获取子组件里面的data数据
2024年08月04日
VUE
个人开发实现AI套壳网站快速搭建(Vue+elementUI+SpringBoot)
2024年08月04日
el-table 表格封装并改造实现单元格可编辑
2024年08月04日
none
nodejs环境下创建vue项目、SSH密钥登陆!!!
2024年08月04日
vue+quill+element-ui实现视频、图片上传及缩放保姆级教程,轻松使用富文本
2024年08月04日
【three.js】22. Imported Models导入模型
2024年08月04日