微信小程序實現(xiàn)手指觸摸畫板
更新時間:2018年07月09日 15:27:43 作者:a_靖
這篇文章主要為大家詳細介紹了微信小程序實現(xiàn)手指觸摸畫板,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了微信小程序實現(xiàn)手指觸摸畫板的具體代碼,供大家參考,具體內容如下
先看效果圖:

wxml
<!--pages/shouxieban/shouxieban.wxml-->
<view class="container">
<view>手寫板(請在下方區(qū)域手寫內容)</view>
<canvas style="width: {{canvasw}}px; height: {{canvash}}px" class="canvas" id="canvas" canvas-id="canvas" disable-scroll="true" bindtouchstart="canvasStart" bindtouchmove="canvasMove" bindtouchend="canvasEnd" touchcancel="canvasEnd" binderror="canvasIdErrorCallback"></canvas>
<view class='btns canvaspd'>
<button bindtap="cleardraw">清除畫板</button>
<button bindtap="setSign">確定</button>
</view>
<image src='{{canvasimgsrc}}'></image>
</view>
js
var context = null;// 使用 wx.createContext 獲取繪圖上下文 context
var isButtonDown = false;//是否在繪制中
var arrx = [];//動作橫坐標
var arry = [];//動作縱坐標
var arrz = [];//總做狀態(tài),標識按下到抬起的一個組合
var canvasw = 0;//畫布寬度
var canvash = 0;//畫布高度
// pages/shouxieban/shouxieban.js
Page({
/**
* 頁面的初始數(shù)據(jù)
*/
data: {
//canvas寬高
canvasw: 0,
canvash: 0,
//canvas生成的圖片路徑
canvasimgsrc: ""
},
//畫布初始化執(zhí)行
startCanvas: function () {
var that = this;
//創(chuàng)建canvas
this.initCanvas();
//獲取系統(tǒng)信息
wx.getSystemInfo({
success: function (res) {
canvasw = res.windowWidth - 0;//設備寬度
canvash = canvasw;
that.setData({ 'canvasw': canvasw });
that.setData({ 'canvash': canvash });
}
});
},
//初始化函數(shù)
initCanvas: function () {
// 使用 wx.createContext 獲取繪圖上下文 context
context = wx.createCanvasContext('canvas');
context.beginPath()
context.setStrokeStyle('#000000');
context.setLineWidth(4);
context.setLineCap('round');
context.setLineJoin('round');
},
//事件監(jiān)聽
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);
},
//提交簽名內容
setSign: function () {
var that = this;
if (arrx.length == 0) {
wx.showModal({
title: '提示',
content: '簽名內容不能為空!',
showCancel: false
});
return false;
};
console.log("不是空的,canvas即將生成圖片")
//生成圖片
wx.canvasToTempFilePath({
canvasId: 'canvas',
success: function (res) {
console.log("canvas可以生成圖片")
console.log(res.tempFilePath, 'canvas圖片地址');
that.setData({ canvasimgsrc: res.tempFilePath })
//code 比如上傳操作
},
fail: function () {
console.log("canvas不可以生成圖片")
wx.showModal({
title: '提示',
content: '微信當前版本不支持,請更新到最新版本!',
showCancel: false
});
},
complete: function () {
}
})
},
/**
* 生命周期函數(shù)--監(jiān)聽頁面加載
*/
onLoad: function (options) {
//畫布初始化執(zhí)行
this.startCanvas();
}
})
css
/* pages/shouxieban/shouxieban.wxss */
/*手寫板 */
page{
background: #f6f6f6;
padding: 5px auto
}
canvas{
border: 1px solid #d0d0d0;
margin: 5rpx;
background: #f2f2f2
}
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Layui數(shù)據(jù)表格判斷編輯輸入的值,是否為我需要的類型詳解
今天小編就為大家分享一篇Layui數(shù)據(jù)表格判斷編輯輸入的值,是否為我需要的類型詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-10-10
layui中使用jquery控制radio選中事件的示例代碼
今天小編就為大家分享一篇layui中使用jquery控制radio選中事件的示例代碼,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-08-08
js實現(xiàn)的頁面加載完畢之前l(fā)oading提示效果完整示例【附demo源碼下載】
這篇文章主要介紹了js實現(xiàn)的頁面加載完畢之前l(fā)oading提示效果,結合完整實例形式分析了js頁面加載時顯示loading效果的實現(xiàn)技巧,需要的朋友可以參考下2016-08-08

