手势解锁小程序

手势解锁是app上常见的解锁方式,相比输入密码方式操作起来要方便许多。下面展示如何基于微信小程序实现手机解锁。最终实现效果如下图:

微信小程序开发实例(手势解锁)

整个功能基于canvas实现,首先添加画布组件,并设定样式

XML/HTML代码

   <!–index.wxml–>

    1. <!–index.wxml–>
    2. <view class=“container”>
    1.   <canvas canvas-id=“id-gesture-lock” class=“gesture-lock” bindtouchstart=“onTouchStart”
    2.     bindtouchmove=“onTouchMove” bindtouchend=“onTouchEnd”></canvas>
    1. </view>
    1. .gesture-lock {
    2.     margin: 100rpx auto;
    1.     width: 300px;
    2.     height: 300px;
  1.     background-color: #ffffff;
  2. }

手势解锁实现代码在gesture_lock.js中(完整源码地址见末尾)。

初始化

JavaScript代码
    1. constructor(canvasid, context, cb, opt){
    2.     this.touchPoints = [];
    1.     this.checkPoints = [];
    2.     this.canvasid = canvasid;
    1.     this.ctx = context;
    2.     this.width = opt && opt.width || 300; //画布长度
    1.     this.height = opt && opt.height || 300; //画布宽度
    2.     this.cycleNum = opt && opt.cycleNum || 3;
    1.     this.radius = 0;  //触摸点半径
    2.     this.isParamOk = false;
    1.     this.marge = this.margeCircle = 25; //触摸点及触摸点和画布边界间隔
    2.     this.initColor = opt && opt.initColor || ‘#C5C5C3’;
    1.     this.checkColor = opt && opt.checkColor || ‘#5AA9EC’;
    2.     this.errorColor = opt && opt.errorColor || ‘#e19984’;
    1.     this.touchState = “unTouch”;
    2.     this.checkParam();
    1.     this.lastCheckPoint = null;
    2.     if (this.isParamOk) {
    1.         // 计算触摸点的半径长度
    2.         this.radius = (this.width – this.marge * 2 – (this.margeCircle * (this.cycleNum – 1))) / (this.cycleNum * 2)
    1.         this.radius = Math.floor(this.radius);
    2.         // 计算每个触摸点的圆心位置
    1.         this.calCircleParams();
    2.     }
  1.     this.onEnd = cb; //滑动手势结束时的回调函数
  2. }

主要设置一些参数,如canvas的长宽,canvas的context,手势锁的个数(3乘3, 4乘4),手势锁的颜色,手势滑动结束时的回调函数等。并计算出手势锁的半径。

计算每个手势锁的圆心位置

JavaScript代码
    1. calCircleParams() {
    2.     let n = this.cycleNum;
    1.     let count = 0;
    2.     for (let i = 0; i < n; i++) {
    1.         for (let j = 0; j < n; j++){
    2.             count++;
    1.             let touchPoint = {
    2.                 x: this.marge + i * (this.radius * 2 + this.margeCircle) + this.radius,
    1.                 y: this.marge + j * (this.radius * 2 + this.margeCircle) + this.radius,
    2.                 index: count,
    1.                 check: “uncheck”,
    2.             }
    1.             this.touchPoints.push(touchPoint)
    2.         }
  1.     }
  2. }

绘制手势锁

JavaScript代码
    1. for (let i = 0; i < this.touchPoints.length; i++){
    2.        this.drawCircle(this.touchPoints[i].x, this.touchPoints[i].y, this.radius, this.initColor)
  1. }
  2. this.ctx.draw(true);

接下来就是识别用户的滑动行为,判断用户划过了哪些圆圈,进而识别出用户的手势。

在touchstart和touchmove事件中检测触发并更新画布

JavaScript代码
    1. onTouchStart(e) {
    2.     // 不识别多点触控
    1.     if (e.touches.length > 1){
    2.         this.touchState = “unTouch”;
    1.         return;
    2.     }
    1.     this.touchState = “startTouch”;
    2.     this.checkTouch(e);
    1.     let point = {x:e.touches[0].x, y:e.touches[0].y};
    2.     this.drawCanvas(this.checkColor, point);
    1. }
    1. onTouchMove(e) {
    2.     if (e.touchState === “unTouch”) {
    1.         return;
    2.     }
    1.     if (e.touches.length > 1){
    2.         this.touchState = “unTouch”;
    1.         return;
    2.     }
    1.     this.checkTouch(e);
    2.     let point = {x:e.touches[0].x, y:e.touches[0].y};
  1.     this.drawCanvas(this.checkColor, point);
  2. }

检测用户是否划过某个圆圈

JavaScript代码
    1. checkTouch(e) {
    2.     for (let i = 0; i < this.touchPoints.length; i++){
    1.         let point = this.touchPoints[i];
    2.         if (isPointInCycle(e.touches[0].x, e.touches[0].y, point.x, point.y, this.radius)) {
    1.             if (point.check === ‘uncheck’) {
    2.                 this.checkPoints.push(point);
    1.                 this.lastCheckPoint = point;
    2.             }
    1.             point.check = “check”
    2.             return;
    1.         }
    2.     }
  1. }

更新画布

JavaScript代码
    1. drawCanvas(color, point) {
    2.    //每次更新之前先清空画布
    1.    this.ctx.clearRect(0, 0, this.width, this.height);
    2.    //使用不同颜色和形式绘制已触发和未触发的锁
    1.    for (let i = 0; i < this.touchPoints.length; i++){
    2.        let point = this.touchPoints[i];
    1.        if (point.check === “check”) {
    2.            this.drawCircle(point.x, point.y, this.radius, color);
    1.            this.drawCircleCentre(point.x, point.y, color);
    2.        }
    1.        else {
    2.            this.drawCircle(this.touchPoints[i].x, this.touchPoints[i].y, this.radius, this.initColor)
    1.        }
    2.    }
    1.    //绘制已识别锁之间的线段
    2.    if (this.checkPoints.length > 1) {
    1.         let lastPoint = this.checkPoints[0];
    2.         for (let i = 1; i < this.checkPoints.length; i++) {
    1.             this.drawLine(lastPoint, this.checkPoints[i], color);
    2.             lastPoint = this.checkPoints[i];
    1.         }
    2.    }
    1.    //绘制最后一个识别锁和当前触摸点之间的线段
    2.    if (this.lastCheckPoint && point) {
    1.        this.drawLine(this.lastCheckPoint, point, color);
    2.    }
  1.    this.ctx.draw(true);

当用户滑动结束时调用回调函数并传递识别出的手势

JavaScript代码
    1. onTouchEnd(e) {
    2.     typeof this.onEnd === ‘function’ && this.onEnd(this.checkPoints, false);
    1. }
    1. onTouchCancel(e) {
    2.     typeof this.onEnd === ‘function’ && this.onEnd(this.checkPoints, true);
  1. }

重置和显示手势错误

JavaScript代码
    1. gestureError() {
    2.     this.drawCanvas(this.errorColor)
    1. }
    1. reset() {
    2.     for (let i = 0; i < this.touchPoints.length; i++) {
    1.         this.touchPoints[i].check = ‘uncheck’;
    2.     }
    1.     this.checkPoints = [];
    2.     this.lastCheckPoint = null;
  1.     this.drawCanvas(this.initColor);
  2. }

如何调用

在onload方法中创建lock对象并在用户触摸事件中调用相应方法

JavaScript代码
    1. onLoad: function () {
    2.   var s = this;
    1.   this.lock = new Lock(“id-gesture-lock”, wx.createCanvasContext(“id-gesture-lock”), function(checkPoints, isCancel) {
    2.     console.log(‘over’);
    1.     s.lock.gestureError();
    2.     setTimeout(function() {
    1.       s.lock.reset();
    2.     }, 1000);
    1.   }, {width:300, height:300})
    2.   this.lock.drawGestureLock();
    1.   console.log(‘onLoad’)
    2.   var that = this
    1.     //调用应用实例的方法获取全局数据
    2.   app.getUserInfo(function(userInfo){
    1.     //更新数据
    2.     that.setData({
    1.       userInfo:userInfo
    2.     })
    1.     that.update()
    2.   })
    1. },
    2. onTouchStart: function (e) {
    1.   this.lock.onTouchStart(e);
    2. },
    1. onTouchMove: function (e) {
    2.   this.lock.onTouchMove(e);
    1. },
    2. onTouchEnd: function (e) {
  1.   this.lock.onTouchEnd(e);
  2. }

下载地址: 手势解锁

关注公众号“大模型全栈程序员”回复“小程序”获取1000个小程序打包源码。更多免费资源在http://www.gitweixin.com/?p=2627

发表评论

邮箱地址不会被公开。 必填项已用*标注