微信小程序实现之手势锁功能实例代码


Posted in Javascript onJuly 19, 2018

设计思路流程图

微信小程序实现之手势锁功能实例代码

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') ? {
   step: 2,
   password: JSON.parse(wx.getStorageSync('password'))
  } : { step: 0 };
  // 启动手势锁初始化
  this.init();
 }

2、全局变量

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 ? this.ctx.setStrokeStyle(this.cleColor) : this.ctx.setFillStyle(this.cleColor);; // 注意用set
  // 设置线条的宽度。
  this.ctx.setLineWidth(2); // 注意用set
  // 开始创建一个路径,需要调用fill或者stroke才会使用路径进行填充或描边。
  this.ctx.beginPath();
  // 画一条弧线。
  this.ctx.arc(x, y, r, 0, Math.PI * 2, true);
  // 关闭一个路径
  this.ctx.closePath();
  // 画出当前路径的边框。默认颜色色为黑色。
  bool ? this.ctx.stroke():this.ctx.fill();
  // 将之前在绘图上下文中的描述(路径、变形、样式)画到 canvas 中。
  this.ctx.draw(true);
 }

矩阵绘制

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);
 }
}

通过流程图,可以更加清楚的认识到做一个功能需要创建的变量和函数,流程步骤更加清楚,当然也需要制作的过程进行优化。建议制作一些大的功能的时候,如果流程不清楚,最好绘制流程图,思路清晰,开发更快,考虑更周全。

总结

以上所述是小编给大家介绍的微信小程序实现之手势锁详解,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对三水点靠木网站的支持!

Javascript 相关文章推荐
取得窗口大小 兼容所有浏览器的js代码
Aug 09 Javascript
Javascript 遮罩层和加载效果代码
Aug 01 Javascript
js+css简单实现网页换肤效果
Dec 29 Javascript
基于jQuery实现音乐播放试听列表
Apr 14 Javascript
JS实现复制内容到剪贴板功能兼容所有浏览器(推荐)
Jun 17 Javascript
dul无法加载bootstrap实现unload table/user恢复
Sep 29 Javascript
jQuery实现遮罩层登录对话框
Dec 29 Javascript
jquery+css实现下拉列表功能
Sep 03 jQuery
JS中的多态实例详解
Oct 15 Javascript
Vue.js 中的 v-model 指令及绑定表单元素的方法
Dec 03 Javascript
微信小程序下拉框搜索功能的实现方法
Jul 31 Javascript
详解使用mocha对webpack打包的项目进行&quot;冒烟测试&quot;的大致流程
Apr 27 Javascript
React组件重构之嵌套+继承及高阶组件详解
Jul 19 #Javascript
微信小程序实现折叠展开效果
Jul 19 #Javascript
详解Angularjs 自定义指令中的数据绑定
Jul 19 #Javascript
微信小程序实现天气预报功能
Jul 18 #Javascript
vue代理和跨域问题的解决
Jul 18 #Javascript
小程序自定义组件实现城市选择功能
Jul 18 #Javascript
微信小程序实践之动态控制组件的显示/隐藏功能
Jul 18 #Javascript
You might like
phpStudy中升级MySQL版本到5.7.17的方法步骤
2017/08/03 PHP
关于Laravel-admin的基础用法总结和自定义model详解
2019/10/08 PHP
jquery 模拟雅虎首页的点击对话框效果
2010/04/11 Javascript
基于jquery的拖动布局插件
2011/11/25 Javascript
javascript实现数字+字母验证码的简单实例
2014/02/10 Javascript
jquery设置按钮停顿3秒不可用
2014/03/07 Javascript
判断一个对象是否为jquery对象的方法
2014/03/12 Javascript
js实现进度条的方法
2015/02/13 Javascript
JavaScript基于ajax编辑信息用法实例
2015/07/15 Javascript
js实现创建删除html元素小结
2015/09/30 Javascript
jQuery简单实现提交数据出现loading进度条的方法
2016/03/29 Javascript
基于Bootstrap的UI扩展 StyleBootstrap
2016/06/17 Javascript
JavaScript获取css行间样式,内连样式和外链样式的简单方法
2016/07/18 Javascript
AngularJS指令用法详解
2016/11/02 Javascript
bootstrap中添加额外的图标实例代码
2017/02/15 Javascript
vue动态组件实现选项卡切换效果
2017/03/08 Javascript
nodejs+websocket实时聊天系统改进版
2017/05/18 NodeJs
angularjs $http实现form表单提交示例
2017/06/09 Javascript
详解Node.js利用node-git-server快速搭建git服务器
2017/09/27 Javascript
微信小程序实现折叠与展开文章功能
2018/06/12 Javascript
JavaScript前端页面搜索功能案例【基于jQuery】
2019/07/10 jQuery
python cookielib 登录人人网的实现代码
2012/12/19 Python
Python iter()函数用法实例分析
2018/03/17 Python
三步实现Django Paginator分页的方法
2019/06/11 Python
浅析python内置模块collections
2019/11/15 Python
python实现人脸签到系统
2020/04/13 Python
英国最大的邮寄种子和植物公司:Thompson & Morgan
2017/09/21 全球购物
车间调度岗位职责
2013/11/30 职场文书
科研课题实施方案
2014/03/18 职场文书
工会趣味活动方案
2014/08/18 职场文书
督导岗位职责范本
2015/04/10 职场文书
2015年政协委员工作总结
2015/05/20 职场文书
小学四年级班务总结该怎么写?
2019/08/16 职场文书
python munch库的使用解析
2021/05/25 Python
mysql定时自动备份数据库的方法步骤
2021/07/07 MySQL
ConditionalOnProperty配置swagger不生效问题及解决
2022/06/14 Java/Android