首页 > 编程语言 >微信小程序canvas实现人员签名

微信小程序canvas实现人员签名

时间:2023-12-21 09:46:31浏览次数:29  
标签:canvas 微信 ctx height width 签名 res data

这里使用获取canvas节点实现的小程序最新的api

签字面板效果图

            

 

wxml部分:

<view class="container" >   <view class="sig_txt">     <canvas type="2d" id="myCanvas"       style="width:{{width}}px;height:{{height}}px;"       bindtouchstart="handleTouchStart1"       bindtouchmove="handleTouchMove1"       bindtouchcancel="handleTouchEnd1"       bindtouchend="handleTouchEnd1"     ></canvas>   </view>   <view class="sig_st_justify sig_wh">     <view class="sig_st_justify sig_left_imgwh1" bindtap="handldrewrite">       <view style="margin-left: 3px;"><image class="sig_cimg" src="/images/svg/r_refresh.svg" mode="aspectFit" /></view>       <view>撤销</view>     </view>     <view class="sig_st_justify sig_left_imgwh2" bindtap="confirmWrite">       <view style="margin-left: 3px;"><image class="sig_cimg" src="/images/svg/s_pen.svg" mode="aspectFit" /></view>       <view>确定</view>     </view>   </view> </view>     js部分:
Page({   data: {     startX: undefined, // 线条的坐标点     startY: undefined,     canvas: '',     ctx: '',     pr:0,     width: 296,     height: 300,     stindex:0,   },   // 获取系统信息   getSystemInfo() {     let that = this;     wx.getSystemInfo({       success(res) {         that.setData({           pr:res.pixelRatio,           width: res.windowWidth-24,           // height: res.windowHeight - 75,         })       }     })   },     // 初始化画布   initCanvas() {     const pr = this.data.pr; // 像素比     const query = wx.createSelectorQuery();     query.select('#myCanvas').fields({ node: true, size: true }).exec((res) => {       const canvas = res[0].node;       const ctx = canvas.getContext('2d');       canvas.width = this.data.width*pr; // 画布宽度       canvas.height = this.data.height*pr; // 画布高度       ctx.scale(pr,pr); // 缩放比       ctx.lineGap = 'round';       ctx.lineJoin = 'round';       ctx.lineWidth = 3; // 字体粗细       ctx.font = '40px Arial'; // 字体大小,       ctx.fillStyle = '#ecf0ef'; // 填充颜色       ctx.fillText('请在此区域签名', res[0].width / 2 - 140, res[0].height / 2)              this.setData({           ctx: ctx,           canvas: canvas       })     })   },   //开始   handleTouchStart1(e) {     if (this.data.stindex==0) {       //清空(请在此区域签名)提示信息       this.data.ctx.clearRect(0, 0, this.data.width, this.data.height);       this.data.ctx.beginPath();     }     this.setData({         startX: e.touches[0].x,         startY: e.touches[0].y,         stindex: 1,     })   },   // 移动   handleTouchMove1(e) {     this.data.ctx.moveTo(this.data.startX, this.data.startY);     this.data.ctx.lineTo(e.touches[0].x, e.touches[0].y);     this.data.ctx.stroke();     this.setData({         startX: e.touches[0].x,         startY: e.touches[0].y,     })   },   // 结束   handleTouchEnd1() {     this.data.ctx.closePath();   },   // 清空画布   handldrewrite:function () {     this.data.ctx.clearRect(0, 0, this.data.width, this.data.height);     this.data.ctx.beginPath();     this.data.ctx.fillText('请在此区域签名', this.data.width / 2 - 140, this.data.height / 2);     this.setData({         startX: "",         startY: "",         stindex:0,     })   }, // 确认提交 confirmWrite() {   if (!this.data.startX && !this.data.startY) {     wx.showModal({       title: '提示',       content: '签名内容不能为空!',       showCancel: false     });     return false;   };   wx.canvasToTempFilePath({     canvas: this.data.canvas,     success(res) {       const tempFilePath = res.tempFilePath; // 取图片文件路径       //将 tempFilePath 传递给后端接口       // uploadFile({fileType: 'image', tempFilePath: tempFilePath})       // .then(file => {       // // 由于签名面板在表单中,提交表单需要传签名文件id,在这里赋值       //     that.setData({ userSignatureId: file.id })       // })       // .catch(err => {       //     console.error(err)       // })     }   })   },   onLoad: function (options) {     this.getSystemInfo();     this.initCanvas();   } })     wxss部分: .sig_st_justify{   display: flex;   justify-content: flex-start;   align-items: center; } .sig_wh{   width: 100%; }
.sig_txt{   color: #1A1A1A;   font-size: 14px;   margin-left: 10px;   margin-bottom: 10px;   border: 1px solid #E4E4E4; } .sig_cimg{   width: 26px;   height: 26px;   margin-top: 8px; } .sig_left_imgwh1{   width: 75px;   height: 30px;   line-height: 30px;   color: #FF3939;   font-size: 18px;   border: 1px solid #cccccc;   border-radius: 10px;   margin-left: calc((50% - 75px)/2); } .sig_left_imgwh2{   width: 75px;   height: 30px;   line-height: 30px;   color: #4C74F4  ;   font-size: 18px;   border: 1px solid #4C74F4;   border-radius: 10px;   margin-left: calc((50% - 75px)/2); }    

标签:canvas,微信,ctx,height,width,签名,res,data
From: https://www.cnblogs.com/fenxiangboke/p/17918278.html

相关文章

  • 微信标签如何分类,可以标签群发吗?
    微信群组太多?管理起来有难度?不知道怎么分类标签管理?别急,这就教你个好办法,可以轻松分类好友标签,还可以根据标签进行批量群发。在微信中,建立标签有三种途径,分别是:添加新联系人:设置备注和标签功能中,输入标签后,会自动建立新标签并将当前联系人加入此标签修改联系人备注和标签:修改备注和......
  • 手把手教你使用ArkTS中的canvas实现签名板功能
     一、屏幕旋转● 实现签名板的第一个功能就是旋转屏幕。旋转屏幕在各种框架中都有不一样的方式,比如:在H5端,我们一般是使用CSS中的transform属性中的rotate()方法来强制将网页横屏,然后实现一系列功能● 在嵌套第三方APP中,我们一般是调用对应的SDK提供的方法,即可实现旋转屏幕......
  • 【Python微信机器人】第六篇:优化使用方式,可pip安装
    优化内容这篇不聊技术点,说一下优化后的Python机器人代码怎么使用,优化内容如下:将hook库独立成一个库,发布到pypi,可使用pip安装将微信相关的代码发布成另一个库,也可以pip安装git仓库统一,以后都在这个仓库更新,不再一篇文章一个仓库开始建群,根据群里反馈增加功能和修复bug使用......
  • 如何在 Apple Pages 中插入自己的手写电子签名 All In One
    如何在ApplePages中插入自己的手写电子签名AllInOnesulotionApple的多设备协同能力非常强大,如果您有iPad或是iPhone的话就简单很多啦~选择最上方的“媒体(Media)”按钮->选择“添加速绘(AddSketch)”就可以在iPad或是iPhone上手写签名插入了。https://discussionschi......
  • 微信小程序使用iconfont通过transfonter转化成只有css样式的方式
    1.通过iconfont 网站选择对应图表加入购物车,然后添加到自己对应项目中,现在至本地。2.解压下载到本地的文件 3.进入 transfonter 后,点击Addfonts按钮,将上图中红色框中的文件上传上去,勾选启用 Base64encode ,点击Convert按钮,完成后点击下载Download 4.解压转......
  • 打工笔记--------------------winform程序报错CLR20r3签名System.I0.IOException
    先看问题编写了一个程序在我本机运行没有问题,放到别人电脑上就有可能报这种错误System.I0.IOException  首先我问了一下ChatPgt:他说:CLR20r3是一个通用的错误代码,表示在.NETFramework中发生了未处理的异常。System.IO.IOException是与输入/输出操作相关的一个常见......
  • 微信小游戏中的场景拖拽显示范围
    usingOrg.BouncyCastle.Crypto.Macs;usingSystem;usingSystem.Collections.Generic;usingUnityEngine;[RequireComponent(typeof(Camera))]publicclassCameraControl:MonoBehaviour{publicstaticCameraControlinstance;publicList<string>......
  • 微信小游戏中拖拽场景位置的限制代码
    usingSystem.Collections.Generic;usingUnityEngine;[RequireComponent(typeof(Camera))]publicclassCameraControl:MonoBehaviour{publicstaticCameraControlinstance;publicList<string>list_RayName=newList<string>();publ......
  • 微信小程序校园跑腿系统怎么做,如何做,要做多久
    ​ 在这个互联网快速发展、信息爆炸的时代,人人都离不开手机,每个人都忙于各种各样的事情,大学生也一样,有忙于学习,忙于考研,忙着赚学分,忙于参加社团,当然也有忙于打游戏的(还很多),但生活中的一些琐事无形当中会占用你的一些时间,例如排队打饭、排队取快递、寄快递、拿外卖,打印资料等等等等......
  • 微信小程序顶部自定义标题对齐胶囊按钮
    微信小程序顶部自定义标题样式对其胶囊按钮css中使用了uniapp的var(--status-bar-height))获取系统栏高度,js中使用了uni.getMenuButtonBoundingClientRect();该api获取小程序菜单按钮的位置信息,返回的有胶囊按钮的宽、高、上、右、下、左,本例中使用了胶囊按钮的top信息示例图......