当前位置:网站首页>微信小程序做电子签名,并导出图片
微信小程序做电子签名,并导出图片
2022-08-06 00:30:00 【Swn_】
wxml:
<canvas class="canvas" id="canvas" canvas-id="canvas" disable-scroll="true" bindtouchstart="canvasStart" bindtouchmove="canvasMove" bindtouchend="canvasEnd" touchcancel="canvasEnd" binderror="canvasIdErrorCallback"></canvas>
<button type="default" bindtap="cleardraw">清除画布</button>
<button type="default" bindtap="getimg">导出图片</button>
wxss:
.canvas {
width: 100%;
min-height: 400rpx;
border: 1rpx solid #000; /*//突出canvas画布范围 */
box-sizing: border-box;
}
js:
var context = null;// 使用 wx.createContext 获取绘图上下文
var isButtonDown = false;
var arrx = [];
var arry = [];
var arrz = [];
var canvasw = 0;
var canvash = 0;
//获取系统信息
wx.getSystemInfo({
success: function (res) {
canvasw = res.windowWidth;//设备宽度
canvash = res.windowHeight; //设备高度
}
});
Page({
canvasIdErrorCallback: function (e) {
console.error(e.detail.errMsg)
},
//绘制开始
canvasStart: function (event) {
isButtonDown = true;
arrz.push(0);
arrx.push(event.changedTouches[0].x);
arry.push(event.changedTouches[0].y);
},
//绘制过程
canvasMove: function (event) {
if (isButtonDown) {
arrz.push(1);
arrx.push(event.changedTouches[0].x);
arry.push(event.changedTouches[0].y);
};
for (var i = 0; i < arrx.length; i++) {
if (arrz[i] == 0) {
context.moveTo(arrx[i], arry[i])
} else {
context.lineTo(arrx[i], arry[i])
};
};
context.clearRect(0, 0, canvasw, canvash);
context.setStrokeStyle('#000000');
context.setLineWidth(4);
context.setLineCap('round');
context.setLineJoin('round');
context.stroke();
context.draw(false);
},
canvasEnd: function (event) {
isButtonDown = false;
},
cleardraw: function () {
//清除画布
arrx = [];
arry = [];
arrz = [];
context.clearRect(0, 0, canvasw, canvash);
context.draw(true);
},
//导出图片
getimg: function () {
if (arrx.length == 0) {
wx.showModal({
title: '提示',
content: '签名内容不能为空!',
showCancel: false
});
return false;
};
//生成图片
wx.canvasToTempFilePath({
canvasId: 'canvas',
success: function (res) {
wx.saveImageToPhotosAlbum({
filePath: res.tempFilePath,
success(res) {
// console.log(res)
wx.showToast({
title: '保存成功',
});
},
fail(err) {
console.log(err)
}
})
//将照片存入服务器
// wx.uploadFile({
// url: '', //接口地址
// filePath: res.tempFilePath,
// name: 'file',
// formData: {
// 'user': 'test'
// },
// success function (res) {
// console.log(res);
// },
// fail: function (res) {
// console.log(res);
// },
// complete: function (res) {
// }
// });
}
})
},
/** * 页面的初始数据 */
data: {
},
/** * 生命周期函数--监听页面加载 */
onLoad: function (options) {
// 使用 wx.createContext 获取绘图上下文 context
context = wx.createCanvasContext('canvas');
context.beginPath()
context.setStrokeStyle('#000000');
context.setLineWidth(4);
context.setLineCap('round');
context.setLineJoin('round');
}
})
边栏推荐
猜你喜欢

How can el-table's data be turned into a picture plus text button?
![Embedded system driver primary [7] - kernel memory management](/img/78/046c841e9920cc80f5fd0798030b23.png)
Embedded system driver primary [7] - kernel memory management

解决删除凭据管理器后仍然可以访问问题

接口安全性测试该从哪些方面入手?

openCV第三篇

【论文阅读】Virtual Adversarial Training: a Regularization Method for SL and SSL

Kubernetes implements grayscale and blue-green releases

uni-app 从零开始-页面通讯 (三)

jsp学习

Lateral federal study - gradient security polymerization
随机推荐
openstack架构中给虚拟机提供存储资源的组件是什么
Pycharm 专业版与社区版的区别
QML绘图系统
【kali-漏洞利用】(3.2)Metasploit基础(中):Armitage工具利用过程
jsp学习
SRE ops declassified - service quality target: the SLI, SLO, SLA
ERP库存管理
What is a man-in-the-middle attack?
Spark基础【五种运行模式】
Windows iis环境下创建织梦php网站图文教程
NFT 游戏含义:游戏中的 NFT 是什么?
[PMP Study Notes] Chapter 4 Project Integration Management
Taught you how to style of the CSP series - the SRC
如何对齐微信小程序胶囊按钮?
新型数据中心——推动数字经济发展的动力引擎
熵力(entropic force)
使用 shell 脚本: 关闭特定进程
IDEA中如何导入jar包、IDEA中找不到对应类改怎样解决?(详细图解过程)
Solve the problems arising from the mysql statement MAX () function
在国信证券股票开户怎么样?安全吗?