위 챗 애플 릿 은 제스처 도안 잠 금 스크린 기능 을 실현 한다.

본 논문 의 사례 는 여러분 에 게 위 챗 애플 릿 제스처 도안 잠 금 스크린 의 구체 적 인 코드 를 공유 하여 참고 하 시기 바 랍 니 다.구체 적 인 내용 은 다음 과 같 습 니 다.
레 퍼 런 스
H5lock
효과 도
手势图案解锁效果图
WXML

<view class="container">
  <view class="reset" bindtap="resetPwd">    </view>
  <view class="title">{{title}}</view>
  <canvas canvas-id="canvas" class="canvas" bindtouchend="onTouchEnd"
   bindtouchstart="onTouchStart" bindtouchmove="onTouchMove"></canvas>
</view>
JS

var Locker = class {
 constructor(page,opt){
  var obj = opt || {};

  this.page = page;
  this.width = obj.width || 300;
  this.height = obj.height || 300;
  this.canvasId = obj.id || 'canvas';
  this.cleColor = obj.cleColor || '#CFE6FF';
  this.cleCenterColor = obj.cleCenterColor || '#CFE6FF';

  var chooseType = obj.chooseType || 3;
  //        chooseType,     ,        
  this.chooseType = Number(wx.getStorageSync('chooseType')) || chooseType;

  this.init();
 }
 init(){
  this.pswObj = wx.getStorageSync('passwordxx') ? {
   step: 2,
   spassword: JSON.parse(wx.getStorageSync('passwordxx'))
  } : {};

  this.makeState();
  //    canvas      (   canvasId)
  this.ctx = wx.createCanvasContext(this.canvasId,this);
  this.touchFlag = false;
  this.lastPoint = [];

  //    
  this.createCircle();
  // canvas    
  this.bindEvent();
 }
 makeState() {
  if (this.pswObj.step == 2) {
   this.page.setData({ title:'   '});
  } else if (this.pswObj.step == 1) {
   // pass
  } else {
   // pass
  }
 }
 //     
 drawCle(x,y){
  //       。
  this.ctx.setStrokeStyle(this.cleColor); //    set
  //        。
  this.ctx.setLineWidth(2); //    set
  //         ,    fill  stroke             。
  this.ctx.beginPath();
  //      。
  this.ctx.arc(x, y, this.r, 0, Math.PI * 2, true);
  //       
  this.ctx.closePath();
  //          。        。
  this.ctx.stroke();
  //              (  、  、  )   canvas  。
  this.ctx.draw(true);
 }

 //             
 getDis(a, b) {
  return Math.sqrt(Math.pow(a.x - b.x, 2) + Math.pow(a.y - b.y, 2));
 }

 //         ,  canvas   (  300px)       
 createCircle() {
  var n = this.chooseType;
  var count = 0;
  //      
  this.r = this.width / (2 + 4 * n);
  this.arr = [];
  this.restPoint = [];
  var r = this.r;
  //       ,          
  for (var i = 0; i < n; i++) {
   for (var j = 0; j < n; j++) {
    count++;
    var obj = {
     x: j * 4 * r + 3 * r,
     y: i * 4 * r + 3 * r,
     index: count
    };
    this.arr.push(obj);
    this.restPoint.push(obj);
   }
  }
  //     
  this.ctx.clearRect(0, 0, this.width, this.height);

  //       
  this.arr.forEach(current => {this.drawCle(current.x, current.y);});
 }



 //       
 getPosition(e) { //   touch    canvas   
  var po = {
   x: e.touches[0].x,
   y: e.touches[0].y
  };
  return po;
 }
 precisePosition(po){
  var arr = this.restPoint.filter(current => Math.abs(po.x - current.x) < this.r && Math.abs(po.y - current.y) < this.r);
  return arr[0];
 }
 drawPoint(obj) { //      

  for (var i = 0; i < this.lastPoint.length; i++) {
   this.ctx.setFillStyle(this.cleCenterColor); //    set  
   this.ctx.beginPath();
   this.ctx.arc(this.lastPoint[i].x, this.lastPoint[i].y, this.r / 2, 0, Math.PI * 2, true);
   this.ctx.closePath();
   this.ctx.fill();
   this.ctx.draw(true);
  }
 }
 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);
  }
  this.ctx.lineTo(po.x, po.y);
  this.ctx.stroke();
  this.ctx.closePath();
  this.ctx.draw(true);
 }
 pickPoints(fromPt, toPt) {
  var lineLength = this.getDis(fromPt, toPt);
  var dir = toPt.index > fromPt.index ? 1 : -1;

  var len = this.restPoint.length;
  var i = dir === 1 ? 0 : (len - 1);
  var limit = dir === 1 ? len : -1;

  while (i !== limit) {
   var pt = this.restPoint[i];

   if (this.getDis(pt, fromPt) + this.getDis(pt, toPt) === lineLength) {
    this.drawPoint(pt.x, pt.y);
    this.lastPoint.push(pt);
    this.restPoint.splice(i, 1);
    if (limit > 0) {
     i--;
     limit--;
    }
   }

   i += dir;
  }
 }
 update(po) {//        touchmove    
  this.ctx.clearRect(0, 0, this.width, this.height);

  for (var i = 0; i < this.arr.length; i++) { //          
   this.drawCle(this.arr[i].x, this.arr[i].y);
  }

  this.drawPoint(this.lastPoint);//      
  this.drawLine(po, this.lastPoint);//      

  for (var i = 0; i < this.restPoint.length; i++) {
   var pt = this.restPoint[i];

   if (Math.abs(po.x - pt.x) < this.r && Math.abs(po.y - pt.y) < this.r) {
    this.drawPoint(pt.x, pt.y);
    this.pickPoints(this.lastPoint[this.lastPoint.length - 1], pt);
    break;
   }
  }
 }
 checkPass(psw1, psw2) {//     
  var p1 = '',
   p2 = '';
  for (var i = 0; i < psw1.length; i++) {
   p1 += psw1[i].index + psw1[i].index;
  }
  for (var i = 0; i < psw2.length; i++) {
   p2 += psw2[i].index + psw2[i].index;
  }
  return p1 === p2;
 }
 storePass(psw) {// touchend             
  if (this.pswObj.step == 1) {
   if (this.checkPass(this.pswObj.fpassword, psw)) {
    this.pswObj.step = 2;
    this.pswObj.spassword = psw;

    this.page.setData({title:'      '});

    this.drawStatusPoint('#2CFF26');
    wx.setStorageSync('passwordxx', JSON.stringify(this.pswObj.spassword));
    wx.setStorageSync('chooseType', this.chooseType);
   } else {
    this.page.setData({ title: '     ,    ' });
    this.drawStatusPoint('red');
    delete this.pswObj.step;
   }
  } else if (this.pswObj.step == 2) {
   if (this.checkPass(this.pswObj.spassword, psw)) {
    this.page.setData({ title: '    ' });
    this.drawStatusPoint('#2CFF26');
   } else {
    this.drawStatusPoint('red');
    this.page.setData({ title: '    ' });
   }
  } else {
   this.pswObj.step = 1;
   this.pswObj.fpassword = psw;
   this.page.setData({ title: '    ' });
  }
 }
 drawStatusPoint(type) { //        
  for (var i = 0; i < this.lastPoint.length; i++) {
   this.ctx.strokeStyle = type;
   this.ctx.beginPath();
   this.ctx.arc(this.lastPoint[i].x, this.lastPoint[i].y, this.r, 0, Math.PI * 2, true);
   this.ctx.closePath();
   this.ctx.stroke();
   this.ctx.draw(true);
  }
 }

 updatePassword() {
  wx.removeStorageSync('passwordxx');
  wx.removeStorageSync('chooseType');
  this.pswObj = {};
  this.page.setData({ title: '      ' });
  this.reset();
 }
 reset() {
  this.makeState();
  this.createCircle();
 }
 bindEvent(){
  var self = this;
  this.page.onTouchStart = function(e){
   var po = self.getPosition(e);
   self.lastPoint = [];
   for (var i = 0; i < self.arr.length; i++) {
    if (Math.abs(po.x - self.arr[i].x) < self.r && Math.abs(po.y - self.arr[i].y) < self.r) {

     self.touchFlag = true;
     self.drawPoint(self.arr[i].x, self.arr[i].y);
     self.lastPoint.push(self.arr[i]);
     self.restPoint.splice(i, 1);
     break;
    }
   }
  }

  this.page.onTouchMove = function(e){
   if (self.touchFlag) {
    self.update(self.getPosition(e));
   }
  }

  this.page.onTouchEnd = function(e){
   if (self.touchFlag) {
    self.touchFlag = false;
    self.storePass(self.lastPoint);
    setTimeout(function () {
     self.reset();
    }, 300);
   }
  }
 }
}
module.exports = Locker;
DEMO 다운로드 하 다.
이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.

좋은 웹페이지 즐겨찾기