侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

canvas实现手机的手势解锁的步骤详细

技术  /  管理员 发布于 7年前   202

本文介绍了canvas手机的手势解锁,分享给大家,顺便给自己留个笔记,废话不多说,具体如下:

按照国际惯例,先放效果图

1、js动态初始化Dom结构

首先在index.html中添加基本样式

body{background:pink;text-align: center;}

加个移动端meta头

<meta name="viewport" content="width=device-width,initial-scale=1.0,user-scalable=no">

引入index.js脚本

<script src="index.js"></script>

index.js

// 匿名函数自执行(function(){    // canvasLock是全局对象    window.canvasLock=function(obj){        this.width=obj.width;        this.height=obj.height;    }    //动态生成DOM    canvasLock.prototype.initDom=function(){        //创建一个div        var div=document.createElement("div");        var h4="<h4 id='title' class='title'>绘制解锁图案</h4>";        div.innerHTML=h4;        div.setAttribute("style","position:absolute;top:0;left:0;right:0;bottom:0;");        //创建canvas        var canvas=document.createElement("canvas");        canvas.setAttribute("id","canvas");        //cssText 的本质就是设置 HTML 元素的 style 属性值        canvas.style.cssText="background:pink;display:inine-block;margin-top:15px;";        div.appendChild(canvas);        document.body.appendChild(div);        //设置canvas默认宽高        var width=this.width||300;        var height=this.height||300;        canvas.style.width=width+"px";        canvas.style.height=height+"px";        canvas.width=width;        canvas.height=height;    }        //init代表初始化,程序的入口    canvasLock.prototype.init=function(){        //动态生成DOM        this.initDom();        //创建画布        this.canvas=document.getElementById("canvas");        this.ctx=this.canvas.getContext("2d");    }})();

在index.html中创建实例并初始化

new canvasLock({}).init();

效果图

2、 画圆函数

需要补充一下画布宽度与圆的半径的关系

如果一行3个圆,则有4个间距,间距的宽度与圆的直径相同,相当于7个直径,即14个半径

如果一行4个圆,则有5个间距,间距的宽度与圆的直径相同,相当于9个直径,即18个半径

如果一行n个圆,则有n+1个间距,间距的宽度与圆的直径相同,相当于2n+1个直径,即4n+2个半径

补充两个方法:

//以给定坐标点为圆心画出单个圆    canvasLock.prototype.drawCircle=function(x,y){        this.ctx.strokeStyle="#abcdef";        this.ctx.lineWidth=2;        this.ctx.beginPath();        this.ctx.arc(x,y,this.r,0,2*Math.PI,true);        this.ctx.closePath();        this.ctx.stroke();    }        //绘制出所有的圆    canvasLock.prototype.createCircle=function(){        var n=this.circleNum;//一行几个圆        var count=0;        this.r=this.canvas.width/(4*n+2);//公式计算出每个圆的半径        this.lastPoint=[];//储存点击过的圆的信息        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:(4*j+3)*r,        y:(4*i+3)*r,        index:count//给每个圆标记索引    };    this.arr.push(obj);    this.restPoint.push(obj);//初始化时为所有点}        }        //清屏        this.ctx.clearRect(0,0,this.canvas.width,this.canvas.height);        //以给定坐标点为圆心画出所有圆        for(var i=0;i<this.arr.length;i++){//循环调用画单个圆的方法this.drawCircle(this.arr[i].x,this.arr[i].y);        }    }

初始化的时候记得调用

canvasLock.prototype.init=function(){        //动态生成DOM        this.initDom();        //创建画布        this.canvas=document.getElementById("canvas");        this.ctx=this.canvas.getContext("2d");        //绘制出所有的圆        this.createCircle();    }

别忘了在index.html中实例化时传入参数(一行定义几个圆)

new canvasLock({circleNum:3}).init();

效果图

3、canvas事件操作——实现画圆和画线

getPosition方法用来得到鼠标触摸点离canvas的距离(左边和上边)

canvasLock.prototype.getPosition=function(e){        var rect=e.currentTarget.getBoundingClientRect();//获得canvas距离屏幕的上下左右距离        var po={//鼠标与视口的左距离 - canvas距离视口的左距离 = 鼠标与canvas的左距离x:(e.touches[0].clientX-rect.left),//鼠标与视口的上距离 - canvas距离视口的上距离 = 鼠标距离canvas的上距离y:(e.touches[0].clientY-rect.top)        };        return po;    }

给canvas添加 touchstart 事件,判断触摸点是否在圆内

触摸点在圆内则允许拖拽,并将该圆添加到 lastPoint 中,从 restPoint 中剔除

this.canvas.addEventListener("touchstart",function(e){var po=self.getPosition(e);//鼠标距离canvas的距离//判断是否在圆内for(var i=0;i<self.arr.lenth;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.lastPoint.push(self.arr[i]);//点击过的点        self.restPoint.splice(i,1);//剩下的点剔除这个被点击的点        break;    }}        },false);

判断是否在圆内的原理:

圆心的x轴偏移和鼠标点的x轴偏移的距离的绝对值小于半径

并且

圆心的y轴偏移和鼠标点的y轴偏移的距离的绝对值小于半径

则可以判断鼠标位于圆内

给touchmove绑定事件,在触摸点移动时给点击过的圆画上实心圆,并画线

//触摸点移动时的动画    canvasLock.prototype.update=function(po){        //清屏,canvas动画前必须清空原来的内容        this.ctx.clearRect(0,0,this.canvas.width,this.canvas.height);        //以给定坐标点为圆心画出所有圆        for(var i=0;i<this.arr.length;i++){this.drawCircle(this.arr[i].x,this.arr[i].y);        }        this.drawPoint();//点击过的圆画实心圆        this.drawLine(po);//画线    }    //画实心圆    canvasLock.prototype.drawPoint=function(){        for(var i=0;i<this.lastPoint.length;i++){this.ctx.fillStyle="#abcdef";this.ctx.beginPath();this.ctx.arc(this.lastPoint[i].x,this.lastPoint[i].y,this.r/2,0,2*Math.PI,true);this.ctx.closePath();this.ctx.fill();        }    }    //画线    canvasLock.prototype.drawLine=function(po){        this.ctx.beginPath();        this.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();    }

效果图

4、canvas手势链接操作实现

在touchmove中补充当碰到下一个目标圆时的操作

//碰到下一个圆时只需要push到lastPoint当中去        for(var i=0;i<this.restPoint.length;i++){if((Math.abs(po.x-this.restPoint[i].x)<this.r) && (Math.abs(po.y-this.restPoint[i].y)<this.r)){    this.lastPoint.push(this.restPoint[i]);//将这个新点击到的点存入lastPoint    this.restPoint.splice(i,1);//从restPoint中剔除这个新点击到的点    break;}        }

效果图

5、解锁成功与否的判断

//设置密码    canvasLock.prototype.storePass=function(){        if(this.checkPass()){document.getElementById("title").innerHTML="解锁成功";this.drawStatusPoint("lightgreen");        }else{document.getElementById("title").innerHTML="解锁失败";this.drawStatusPoint("orange");        }    }    //判断输入的密码    canvasLock.prototype.checkPass=function(){        var p1="123",//成功的密码p2="";        for(var i=0;i<this.lastPoint.length;i++){p2+=this.lastPoint[i].index;        }        return p1===p2;    }    //绘制判断结束后的状态    canvasLock.prototype.drawStatusPoint=function(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,2*Math.PI,true);this.ctx.closePath();this.ctx.stroke();        }    }    //程序全部结束后重置    canvasLock.prototype.reset=function(){        this.createCircle();    }

大功告成!!下面晒出所有代码

index.html

<!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8">    <title>手势解锁</title>    <!-- 移动端meta头 -->    <meta name="viewport" content="width=device-width,initial-scale=1.0,user-scalable=no">    <style>           body{background:pink;text-align: center;}    </style></head><body>    <script src="index.js"></script>    <script>        // circleNum:3 表示一行3个圆        new canvasLock({circleNum:3}).init();    </script>  </body></html>

index.js

// 匿名函数自执行(function(){    // canvasLock是全局对象    window.canvasLock=function(obj){        this.width=obj.width;        this.height=obj.height;        this.circleNum=obj.circleNum;    }    //动态生成DOM    canvasLock.prototype.initDom=function(){        //创建一个div        var div=document.createElement("div");        var h4="<h4 id='title' class='title'>绘制解锁图案</h4>";        div.innerHTML=h4;        div.setAttribute("style","position:absolute;top:0;left:0;right:0;bottom:0;");        //创建canvas        var canvas=document.createElement("canvas");        canvas.setAttribute("id","canvas");        //cssText 的本质就是设置 HTML 元素的 style 属性值        canvas.style.cssText="background:pink;display:inine-block;margin-top:15px;";        div.appendChild(canvas);        document.body.appendChild(div);        //设置canvas默认宽高        var width=this.width||300;        var height=this.height||300;        canvas.style.width=width+"px";        canvas.style.height=height+"px";        canvas.width=width;        canvas.height=height;    }    //以给定坐标点为圆心画出单个圆    canvasLock.prototype.drawCircle=function(x,y){        this.ctx.strokeStyle="#abcdef";        this.ctx.lineWidth=2;        this.ctx.beginPath();        this.ctx.arc(x,y,this.r,0,2*Math.PI,true);        this.ctx.closePath();        this.ctx.stroke();    }        //绘制出所有的圆    canvasLock.prototype.createCircle=function(){        var n=this.circleNum;//一行几个圆        var count=0;        this.r=this.canvas.width/(4*n+2);//公式计算出每个圆的半径        this.lastPoint=[];//储存点击过的圆的信息        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:(4*j+3)*r,        y:(4*i+3)*r,        index:count//给每个圆标记索引    };    this.arr.push(obj);    this.restPoint.push(obj);//初始化时为所有点}        }        //清屏        this.ctx.clearRect(0,0,this.canvas.width,this.canvas.height);        //以给定坐标点为圆心画出所有圆        for(var i=0;i<this.arr.length;i++){//循环调用画单个圆的方法this.drawCircle(this.arr[i].x,this.arr[i].y);        }    }    //添加事件    canvasLock.prototype.bindEvent=function(){        var self=this;        this.canvas.addEventListener("touchstart",function(e){var po=self.getPosition(e);//鼠标距离canvas的距离//判断是否在圆内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.lastPoint.push(self.arr[i]);//点击过的点        self.restPoint.splice(i,1);//剩下的点剔除这个被点击的点        break;    }}        },false);        this.canvas.addEventListener("touchmove",function(e){if(self.touchFlag){    //触摸点移动时的动画    self.update(self.getPosition(e));}        },false);        //触摸离开时        this.canvas.addEventListener("touchend",function(e){if(self.touchFlag){    self.storePass(self.lastPoint);    setTimeout(function(){        self.reset();    },300);}        },false);    }    //设置密码    canvasLock.prototype.storePass=function(){        if(this.checkPass()){document.getElementById("title").innerHTML="解锁成功";this.drawStatusPoint("lightgreen");        }else{document.getElementById("title").innerHTML="解锁失败";this.drawStatusPoint("orange");        }    }    //判断输入的密码    canvasLock.prototype.checkPass=function(){        var p1="123",//成功的密码p2="";        for(var i=0;i<this.lastPoint.length;i++){p2+=this.lastPoint[i].index;        }        return p1===p2;    }    //绘制判断结束后的状态    canvasLock.prototype.drawStatusPoint=function(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,2*Math.PI,true);this.ctx.closePath();this.ctx.stroke();        }    }    //程序全部结束后重置    canvasLock.prototype.reset=function(){        this.createCircle();    }        //获取鼠标点击处离canvas的距离    canvasLock.prototype.getPosition=function(e){        var rect=e.currentTarget.getBoundingClientRect();//获得canvas距离屏幕的上下左右距离        var po={//鼠标与视口的左距离 - canvas距离视口的左距离 = 鼠标与canvas的左距离x:(e.touches[0].clientX-rect.left),//鼠标与视口的上距离 - canvas距离视口的上距离 = 鼠标距离canvas的上距离y:(e.touches[0].clientY-rect.top)        };        return po;    }    //触摸点移动时的动画    canvasLock.prototype.update=function(po){        //清屏,canvas动画前必须清空原来的内容        this.ctx.clearRect(0,0,this.canvas.width,this.canvas.height);        //以给定坐标点为圆心画出所有圆        for(var i=0;i<this.arr.length;i++){this.drawCircle(this.arr[i].x,this.arr[i].y);        }        // 鼠标每移动一下都会重绘canvas,update操作相当于每一个move事件都会触发        this.drawPoint();//点击过的圆画实心圆        this.drawLine(po);//画线        //碰到下一个圆时只需要push到lastPoint当中去        for(var i=0;i<this.restPoint.length;i++){if((Math.abs(po.x-this.restPoint[i].x)<this.r) && (Math.abs(po.y-this.restPoint[i].y)<this.r)){    this.lastPoint.push(this.restPoint[i]);//将这个新点击到的点存入lastPoint    this.restPoint.splice(i,1);//从restPoint中剔除这个新点击到的点    break;}        }    }    //画实心圆    canvasLock.prototype.drawPoint=function(){        for(var i=0;i<this.lastPoint.length;i++){this.ctx.fillStyle="#abcdef";this.ctx.beginPath();this.ctx.arc(this.lastPoint[i].x,this.lastPoint[i].y,this.r/2,0,2*Math.PI,true);this.ctx.closePath();this.ctx.fill();        }    }    //画线    canvasLock.prototype.drawLine=function(po){        this.ctx.beginPath();        this.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();    }    //init代表初始化,程序的入口    canvasLock.prototype.init=function(){        //动态生成DOM        this.initDom();        //创建画布        this.canvas=document.getElementById("canvas");        this.ctx=this.canvas.getContext("2d");        //默认不允许拖拽        this.touchFlag=false;        //绘制出所有的圆        this.createCircle();        //添加事件        this.bindEvent();    }})();

到此这篇关于canvas实现手机的手势解锁的步骤详细 的文章就介绍到这了,更多相关canvas手势解锁内容请搜索以前的文章或继续浏览下面的相关文章,希望大家以后多多支持!


  • 上一条:
    my.cnf(my.ini)重要参数优化配置说明
    下一条:
    canvas如何实现多张图片编辑的图片编辑器
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 2024.07.09日OpenAI将终止对中国等国家和地区API服务(0个评论)
    • 2024/6/9最新免费公益节点SSR/V2ray/Shadowrocket/Clash节点分享|科学上网|免费梯子(1个评论)
    • 国外服务器实现api.openai.com反代nginx配置(0个评论)
    • 2024/4/28最新免费公益节点SSR/V2ray/Shadowrocket/Clash节点分享|科学上网|免费梯子(1个评论)
    • 近期文章
    • 在go中实现一个常用的先进先出的缓存淘汰算法示例代码(0个评论)
    • 在go+gin中使用"github.com/skip2/go-qrcode"实现url转二维码功能(0个评论)
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(0个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 欧盟关于强迫劳动的规定的官方举报渠道及官方举报网站(0个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf文件功能(0个评论)
    • Laravel从Accel获得5700万美元A轮融资(0个评论)
    • 在go + gin中gorm实现指定搜索/区间搜索分页列表功能接口实例(0个评论)
    • 在go语言中实现IP/CIDR的ip和netmask互转及IP段形式互转及ip是否存在IP/CIDR(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2016-10
    • 2016-11
    • 2017-07
    • 2017-08
    • 2017-09
    • 2018-01
    • 2018-07
    • 2018-08
    • 2018-09
    • 2018-12
    • 2019-01
    • 2019-02
    • 2019-03
    • 2019-04
    • 2019-05
    • 2019-06
    • 2019-07
    • 2019-08
    • 2019-09
    • 2019-10
    • 2019-11
    • 2019-12
    • 2020-01
    • 2020-03
    • 2020-04
    • 2020-05
    • 2020-06
    • 2020-07
    • 2020-08
    • 2020-09
    • 2020-10
    • 2020-11
    • 2021-04
    • 2021-05
    • 2021-06
    • 2021-07
    • 2021-08
    • 2021-09
    • 2021-10
    • 2021-12
    • 2022-01
    • 2022-02
    • 2022-03
    • 2022-04
    • 2022-05
    • 2022-06
    • 2022-07
    • 2022-08
    • 2022-09
    • 2022-10
    • 2022-11
    • 2022-12
    • 2023-01
    • 2023-02
    • 2023-03
    • 2023-04
    • 2023-05
    • 2023-06
    • 2023-07
    • 2023-08
    • 2023-09
    • 2023-10
    • 2023-12
    • 2024-02
    • 2024-04
    • 2024-05
    • 2024-06
    • 2025-02
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客