蝙蝠岛资源网 Design By www.hbtsch.com
设计思路流程图
1、全局常量
constructor(page,opts){
// 初始化全局常量数据
this.page = page;
this.width = opts.width || 300;
this.height = opts.height || 300;
this.canvasId = opts.canvasId || 'lock';
this.type = opts.type || 3;
this.cleColor = opts.cleColor || 'rgba(0,136,204,1)';
this.size = this.width / this.type / 2;//坐标点之间的半间距
this.R = this.size / 2;//外圆半径
this.r = this.size / 4;//內圆半径
// 判断是否在缓存中存在密码,如果存在,直接进行第二步骤:解码,如果不存在,进行初始化,设置密码
this.pswObj = wx.getStorageSync('password') "htmlcode">
init(){
const _this = this;
// 定义全局变量,标记start,手势锁的每个坐标的中心点数组,记录选中数组
_this.flag = false;
_this.locationArr = [];
_this.lastPoint = [];
_this.restPoint = [];
// 设置canvas的宽高
_this.page.setData({
width : _this.width,
height : _this.height
});
this.ctx = wx.createCanvasContext(this.canvasId, this);
// 初始化中心坐标数组
this.location();
// 初始化绘制图形圆
this.drawPo();
// 初始化绑定事件
this.bindEvent();
}
3、初始化坐标数组locationArr 和restPoint
location(){
// 计算坐标的x,y坐标,同时记录当前位置代表的数
let count = 0,arr = [],arr0 = [];
for(let i = 0; i < this.type; i++){
for(let j = 0 ; j < this.type; j++){
count++;
arr.push({
x: this.size * ((j + 1) * 2 - 1),//奇数个坐标间半间距
y: this.size * ((i + 1) * 2 - 1),//奇数个坐标间半间距
count: count//每个坐标代表的数
});
arr0.push({
x: this.size * ((j + 1) * 2 - 1),//奇数个坐标间半间距
y: this.size * ((i + 1) * 2 - 1),//奇数个坐标间半间距
count: count//每个坐标代表的数
});
}
}
this.locationArr = arr;
this.restPoint = arr0;
}
4、绘制手势锁矩阵
绘制圆函数(bool值判断当前绘制的是空心还是实心)
drawCle(x, y, r, bool){
// 设置边框颜色。
bool "htmlcode">
drawPo(){
// 绘制空心圆,绘制之前,清空canvas,防止重复绘制
this.ctx.clearRect(0, 0, this.width, this.height);
this.locationArr.forEach(current => {
this.drawCle(current.x, current.y, this.R, true);
});
}
5、触发move时线的绘制函数
drawLine(po) {// 解锁轨迹
this.ctx.beginPath();
// 线宽
this.ctx.lineWidth = 3;
// 起始点
this.ctx.moveTo(this.lastPoint[0].x, this.lastPoint[0].y);
// 中间转换的点
for (var i = 1; i < this.lastPoint.length; i++) {
this.ctx.lineTo(this.lastPoint[i].x, this.lastPoint[i].y);
}
// 正在移动选择的点
if (po) { this.ctx.lineTo(po.x, po.y);}
this.ctx.stroke();
this.ctx.closePath();
this.ctx.draw(true);
}
6、获取当前位置的坐标点函数
getPosition(e) { // 获取touch点相对于canvas的坐标
return {
x: e.touches[0].x,
y: e.touches[0].y
};
}
7、触发touchstart事件处理
_this.page.onTouchStart = function(e){
let po = _this.getPosition(e);//获取当前准确坐标
for (let [key,val] of _this.locationArr.entries()){//循环对比最近的坐标
if (Math.abs(val.x - po.x) < _this.r && Math.abs(val.y - po.y) < _this.r){
_this.flag = true;//进入判断,触发touchstart事件成功
_this.drawCle(val.x, val.y, _this.r, false);//绘制该点的实心内圆
_this.lastPoint.push(val);//记录该点坐标到lastPoint
_this.restPoint.splice(key,1);//删除记录数组restPoint的该点坐标
break;//找到坐标,跳出循环
}
}
}
8、触发touchmove事件处理
_this.page.onTouchMove = function (e) {
_this.flag && _this.updata(_this.getPosition(e));
}
判断是否触发touchstart,如果触发,执行updata函数。
更新最后点坐标函数
updata(po){
//清空canvas
this.ctx.clearRect(0, 0, this.width, this.height);
//重新绘制矩阵
for (let val of this.locationArr) {
this.drawCle(val.x, val.y, this.R, true);
}
//绘制已记录坐标的实心圆
for (let val of this.lastPoint) {
this.drawCle(val.x, val.y, this.r ,false);
}
//绘制解锁路线
this.drawLine(po);
//找到移动中的还未落点的精确坐标
for (let [key, val] of this.restPoint.entries()) {
if (Math.abs(po.x - val.x) < this.r && Math.abs(po.y - val.y) < this.r) {
this.drawCle(val.x, val.y, this.r, false);
this.lastPoint.push(val);
this.restPoint.splice(key, 1);
break;
}
}
}
9、触发touchend事件处理
_this.page.onTouchEnd = function (e) {
if(_this.flag){
_this.flag = false;
_this.endData();
_this.checkPassword(_this.lastPoint);
setTimeout(function () {
_this.reset();
}, 500);
}
}
通过流程图,可以更加清楚的认识到做一个功能需要创建的变量和函数,流程步骤更加清楚,当然也需要制作的过程进行优化。建议制作一些大的功能的时候,如果流程不清楚,最好绘制流程图,思路清晰,开发更快,考虑更周全。
总结
以上所述是小编给大家介绍的微信小程序实现之手势锁详解,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对网站的支持!
标签:
微信小程序手势锁,微信小程序手势
蝙蝠岛资源网 Design By www.hbtsch.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
蝙蝠岛资源网 Design By www.hbtsch.com
暂无微信小程序实现之手势锁功能实例代码的评论...
更新日志
2025年10月24日
2025年10月24日
- 小骆驼-《草原狼2(蓝光CD)》[原抓WAV+CUE]
- 群星《欢迎来到我身边 电影原声专辑》[320K/MP3][105.02MB]
- 群星《欢迎来到我身边 电影原声专辑》[FLAC/分轨][480.9MB]
- 雷婷《梦里蓝天HQⅡ》 2023头版限量编号低速原抓[WAV+CUE][463M]
- 群星《2024好听新歌42》AI调整音效【WAV分轨】
- 王思雨-《思念陪着鸿雁飞》WAV
- 王思雨《喜马拉雅HQ》头版限量编号[WAV+CUE]
- 李健《无时无刻》[WAV+CUE][590M]
- 陈奕迅《酝酿》[WAV分轨][502M]
- 卓依婷《化蝶》2CD[WAV+CUE][1.1G]
- 群星《吉他王(黑胶CD)》[WAV+CUE]
- 齐秦《穿乐(穿越)》[WAV+CUE]
- 发烧珍品《数位CD音响测试-动向效果(九)》【WAV+CUE】
- 邝美云《邝美云精装歌集》[DSF][1.6G]
- 吕方《爱一回伤一回》[WAV+CUE][454M]
