




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
第微信小程序用canvas實現(xiàn)電子簽名本文實例為大家分享了微信小程序用canvas實現(xiàn)電子簽名的具體代碼,供大家參考,具體內(nèi)容如下
view
view
canvasid="canvas"canvas-id="canvas"class="{{sysType==='iOS''canvas':'canvasbg000'}}"disable-scroll="true"bindtouchstart="canvasStart"bindtouchmove="canvasMove"bindtouchend="canvasEnd"touchcancel="canvasEnd"binderror="canvasIdErrorCallback"/canvas
/view
view
button
catchtap="cleardraw"清除簽名/button
button
catchtap="uploadImg"上傳簽名/button
/view
/view
varcontext=null;//使用wx.createContext獲取繪圖上下文context
varisButtonDown=false;//是否在繪制中
vararrx=[];//動作橫坐標(biāo)
vararry=[];//動作縱坐標(biāo)
vararrz=[];//總做狀態(tài),標(biāo)識按下到抬起的一個組合
varcanvasw=0;//畫布寬度
varcanvash=0;//畫布高度
Page({
data:{
canvasw:'',
canvash:'',
imgUrl:'',
info:{},
signBase64:'',
sysType:''//判斷機型
onLoad:function(options){
letthat=this
letres=wx.getSystemInfoSync()
constsystem=res.system.split('')
that.setData({
sysType:system[0],
})
letparams=JSON.parse(options.params)
that.setData({
info:params,
})
that.startCanvas();
that.initCanvas()
/**
*以下-手寫簽名/上傳簽名
startCanvas(){//畫布初始化執(zhí)行
varthat=this;
//獲取系統(tǒng)信息
wx.getSystemInfo({
success:function(res){
canvasw=res.windowWidth;
canvash=res.windowHeight;
that.setData({canvasw:canvasw});
that.setData({canvash:canvash});
}
});
this.initCanvas();
this.cleardraw();
//初始化函數(shù)
initCanvas(){
context=wx.createCanvasContext('canvas');
context.beginPath()
if(this.data.sysType==='iOS'){
context.fillStyle='rgba(255,255,255,1)';
context.setStrokeStyle('#444');
}else{
context.fillStyle='rgba(0,0,0,1)';
context.setStrokeStyle('#aaa');
}
context.setLineWidth(4);
context.setLineCap('round');
context.setLineJoin('round');
canvasStart(event){
isButtonDown=true;
arrz.push(0);
arrx.push(event.changedTouches[0].x);
arry.push(event.changedTouches[0].y);
canvasMove(event){
if(isButtonDown){
arrz.push(1);
arrx.push(event.changedTouches[0].x);
arry.push(event.changedTouches[0].y);
}
for(vari=0;iarrx.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);
if(this.data.sysType==='iOS'){
context.fillStyle='rgba(255,255,255,1)';
context.setStrokeStyle('#444');
}else{
context.fillStyle='rgba(0,0,0,1)';
context.setStrokeStyle('#aaa');
}
context.setLineWidth(3);
context.setLineCap('round');
context.setLineJoin('round');
context.stroke();
context.draw(false);
canvasEnd(event){
isButtonDown=false;
//清除畫布
cleardraw(){
arrx=[];
arry=[];
arrz=[];
context.clearRect(0,0,canvasw,canvash);
if(this.data.sysType==='iOS'){
context.fillStyle='rgba(255,255,255,1)';
context.setStrokeStyle('#444');
}else{
context.fillStyle='rgba(0,0,0,1)';
context.setStrokeStyle('#aaa');
}
context.draw(true);
uploadImg(){
varthat=this
//生成圖片
//context.draw(true,()={
setTimeout(()={
wx.canvasToTempFilePath({
canvasId:'canvas',
//設(shè)置輸出圖片的寬高
fileType:'jpg',
quality:1,
success:function(res){
//canvas圖片地址res.tempFilePath
letimgBase64=wx.getFileSystemManager().readFileSync(res.tempFilePath,'base64')
that.setData({
imgUrl:res.tempFilePath,
signBase64:imgBase64
})
that.submitSign()
console.log('imgBase64','data:image/jpeg;base64,'+imgBase64)
//wx.saveImageToPhotosAlbum({
//
filePath:res.tempFilePath,
//
success(res4){
//
console.log(res,'保存res4');
//
wx.showToast({
//
title:'已成功保存到相冊',
//
duration:2000
//
});
//
}
//})
},
fail:function(){
wx.showModal({
title:'提示',
content:'canvas生成圖片失敗。微信當(dāng)前版本不支持,請更新到最新版本!',
showCancel:false
});
},
complete:function(){}
},5000)
})
//})
//提交簽名
submitSign(){
letthat=this
wx.showLoading({
title:'正在提交...',
mask:true
})
lettype='1'
if(that.data.sysType==='iOS'){
type='0'
}else{
type='1'
}
wx.$getWxLoginCode(resp={
constparams={
qmbase64:that.data.signBase64,
}
("入?yún)?,params)
wx.kservice.yyyurl(params,res={
wx.hideLoading()
if(res.statusCode==='200'){
wx.showModal({
title:'提示',
content:res.message,
showCancel:false,
confirmText:'返回首頁',
success(res){
if(res.confirm){
wx.reLaunch({
url:'/pages/index/index'
})
}
}
})
}else{
wx.showModal({
title:'提示',
content:res.message,
showCancel:true,
cancelText:'返回首頁',
confirmText:'重新提交',
success:(result)={
if(result.cancel){
//取消停留
wx.reLaunch({
url:'/pages/index/index'
})
}elseif(result.confirm){
//重新提交
that.submitSign()
}
},
});
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 文創(chuàng)集團面試題目及答案
- 電子基礎(chǔ)試題及答案
- 2025年計算機二級考試模擬題解析及答案
- 2025智能機器人專項技術(shù)服務(wù)合同
- 湖北電工單相電機考試題及答案
- 重要知識點初級社會工作者試題及答案
- 2025成品房屋買賣合同范本
- 2016助理試題及答案
- 保育師筆試題庫及答案
- 多媒體內(nèi)容創(chuàng)作測試及答案
- 校長在高考壯行儀式上講話以青春做槳讓夢想為帆
- 中國鴉膽子油行業(yè)市場現(xiàn)狀調(diào)查及前景戰(zhàn)略研判報告
- 倉庫管理測試題及答案
- 2025年國際貿(mào)易實務(wù)考試模擬題及答案
- 2025-2030年中國熱風(fēng)槍行業(yè)市場現(xiàn)狀供需分析及投資評估規(guī)劃分析研究報告
- 2025年團的基礎(chǔ)知識試題及答案
- 3D打印技術(shù)在建材中的應(yīng)用-洞察闡釋
- 5G網(wǎng)絡(luò)中SDN與邊緣計算的深度融合-洞察闡釋
- 車庫贈送協(xié)議書范本
- 裝修續(xù)簽協(xié)議合同協(xié)議
- 生產(chǎn)管理-乳業(yè)生產(chǎn)工藝流程
評論
0/150
提交評論