html5 canvas手势解锁源码分享


Posted in HTML / CSS onJanuary 07, 2020

先放图

html5 canvas手势解锁源码分享

html5 canvas手势解锁源码分享

demo.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
    <title>手势解锁</title>
    <style type="text/css">
        body{
            text-align: center;
            background: #305066;
        }
        h4{
            color: #22C3AA;
        }
    </style>
</head>
<body>
    <script type="text/javascript" src="src/index.js"></script>
    <script type="text/javascript">
        // 1、生成背景
        // 2、title生成
        // 3、用js动态生成canvas标签
        // 4、js方式动态生成h4标签和canvas标签
        new canvasLock({chooseType:3}).init();
    </script>
</body>
</html>

index.js

(function(){
        /**
         * 实现画圆和划线:
         * 1、添加事件touchstart、touchmove、touchend
         * 2、touchstart判断是否点击的位置处于圆内getPosition,处于则初始化
         * lastpoint、restPoint
         * 3、touchmove做的就是:画圆drawPoint和画线drawLine
         *
         * 实现自动画圆的效果
         * 1、检测手势移动的位置是否处于圆内
         * 2、圆内的话则画圆 drawPoint
         * 3、已经画过实心圆的圆,无需重复检测
         *
         * 实现解锁成功:
         * 1、检测路径是否是对的
         * 2、如果是对的就重置,圆圈变绿
         * 3、不对也重置,圆圈变红
         * 4、重置
         */
        window.canvasLock = function(obj){
            this.height = obj.height;
            this.width = obj.width;
            this.chooseType = obj.chooseType;
        };
        // js方式动态生成dom
        canvasLock.prototype.initDom = function(){
            var wrap = document.createElement('div');
            var str = '<h4 id="title" class="title">绘制解锁图案</h4>';
            wrap.setAttribute('style','position: absolute;top:0;left:0;right:0;bottom:0;');
            var canvas = document.createElement('canvas');
            canvas.setAttribute('id','canvas');
            canvas.style.cssText = 'background-color: #305066;display: inline-block;margin-top: 15px;';
            wrap.innerHTML = str;
            wrap.appendChild(canvas);
            var width = this.width || 300;
            var height = this.height || 300;
            document.body.appendChild(wrap);
            // 高清屏锁放
            canvas.style.width = width + "px";
            canvas.style.height = height + "px";
            canvas.width = width;
            canvas.height = height;
        }
        canvasLock.prototype.drawCle = function(x, y) { // 初始化解锁密码面板
            this.ctx.strokeStyle = '#CFE6FF';
            this.ctx.lineWidth = 2;
            this.ctx.beginPath();
            this.ctx.arc(x, y, this.r, 0, Math.PI * 2, true);
            this.ctx.closePath();
            this.ctx.stroke();
        }
        canvasLock.prototype.createCircle = function() {// 创建解锁点的坐标,根据canvas的大小来平均分配半径

            var n = this.chooseType;
            var count = 0;
            this.r = this.ctx.canvas.width / (2 + 4 * n);// 公式计算
            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: 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.ctx.canvas.width, this.ctx.canvas.height);
            for (var i = 0 ; i < this.arr.length ; i++) {
                // 画圆函数
                this.drawCle(this.arr[i].x, this.arr[i].y);
            }
            //return arr;
        }

        // 程序初始化
        canvasLock.prototype.init = function() {
            this.initDom();
            this.canvas = document.getElementById('canvas');
            this.ctx = this.canvas.getContext('2d');
            this.touchFlag = false;
            // 1、确定半径
            // 2、确定每一个圆的中心坐标点
            // 3、一行3个圆14个半径,一行4个圆有18个半径
            this.createCircle();
            this.bindEvent();
        }

        canvasLock.prototype.bindEvent = function(){
            var self = this;
            this.canvas.addEventListener("touchstart", function (e) {
                // 2、touchstart判断是否点击的位置处于圆内getPosition,处于则初始化
                //          * lastpoint、restPoint
                
                // po有x和y,并且是相较于canvas边距
                var po = self.getPosition(e);
                console.log(po.x)
                // 判断是否在圆内的原理:多出来的这条 x/y < r 在圆内
                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;

                        // lastPoint存放的就是选中的圆圈的x/y坐标值
                        self.lastPoint.push(self.arr[i]);

                        self.restPoint.splice(i,1);
                        break;
                    }
                }


            }, false);

            this.canvas.addEventListener("touchmove", function (e) {

               // touchmove做的就是:画圆drawPoint和划线drawLine
               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.getPosition = function(e) {// 获取touch点相对于canvas的坐标
            var rect = e.currentTarget.getBoundingClientRect();
            var po = {
                x: (e.touches[0].clientX - rect.left),
                y: (e.touches[0].clientY - rect.top)
            };
            return po;
        }

        
        canvasLock.prototype.update = function(po) {// 核心变换方法在touchmove时候调用
            this.ctx.clearRect(0, 0, this.ctx.canvas.width, this.ctx.canvas.height);

            // 重新画9个圆圈
            for (var i = 0 ; i < this.arr.length ; i++) { // 每帧先把面板画出来
                this.drawCle(this.arr[i].x, this.arr[i].y);
            }

            this.drawPoint();// 画圆
            this.drawLine(po);// 画线

            // 1、检测手势移动的位置是否处于下一个圆内
            // 2、圆内的话则画圆 drawPoint
            // 3、已经画过实心圆的圆,无需重复检测
            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.drawPoint();
                    this.lastPoint.push(this.restPoint[i]);
                    this.restPoint.splice(i, 1);
                    break;
                }
            }

            console.log(this.lastPoint)

        }
        canvasLock.prototype.drawLine = function(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();
        }
        canvasLock.prototype.drawPoint = function() { // 初始化圆心 
            for (var i = 0 ; i < this.lastPoint.length ; i++) {
                this.ctx.fillStyle = '#CFE6FF';
                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();
            }
        }

        // 1、检测路径是否是对的
        // 2、如果是对的就重置,圆圈变绿
        // 3、不对也重置,圆圈变红
        // 4、重置
        canvasLock.prototype.storePass = function() {
            if (this.checkPass()) {
                document.getElementById('title').innerHTML = '解锁成功';
                this.drawStatusPoint('#2CFF26');
            }else{
                document.getElementById('title').innerHTML = '解锁失败';
                this.drawStatusPoint('red');
            }
        }
        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, Math.PI * 2, true);
                this.ctx.closePath();
                this.ctx.stroke();
            }
        }
        canvasLock.prototype.reset = function(){
            this.createCircle();
        }
})();

总结

以上所述是小编给大家介绍的html5 canvas手势解锁源码分享,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对三水点靠木网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!

HTML / CSS 相关文章推荐
CSS3 选择器 属性选择器介绍
Jan 21 HTML / CSS
巧用CSS3 border实现图片遮罩效果代码
Apr 09 HTML / CSS
定义css设备类型-Media Queries图表简介及使用方法
Jan 21 HTML / CSS
纯CSS3制作漂亮带动画效果的主机价格表
Apr 25 HTML / CSS
HTML5 Web存储方式的localStorage和sessionStorage进行数据本地存储案例应用
Dec 09 HTML / CSS
浅谈h5自定义audio(问题及解决)
Aug 19 HTML / CSS
canvas进阶之贝塞尔公式推导与物体跟随复杂曲线的轨迹运动
Jan 10 HTML / CSS
Html5页面中的返回实现的方法
Feb 26 HTML / CSS
HTML5 Canvas实现放大镜效果示例
Mar 25 HTML / CSS
Html5 webview元素定位工具的实现
Aug 07 HTML / CSS
Html5通过数据流方式播放视频的实现
Apr 27 HTML / CSS
css3应用示例:新增的选择器
Mar 16 HTML / CSS
HTML5自定义视频播放器源码
Jan 06 #HTML / CSS
使用html2canvas实现将html内容写入到canvas中生成图片
Jan 03 #HTML / CSS
HTML5去掉输入框type为number时的上下箭头的实现方法
Jan 03 #HTML / CSS
使用canvas实现黑客帝国数字雨效果
Jan 02 #HTML / CSS
HTML5中外部浏览器唤起微信分享
Jan 02 #HTML / CSS
Html5获取高德地图定位天气的方法
Dec 26 #HTML / CSS
鼠标滚轮事件和Mac触控板双指事件
Dec 23 #HTML / CSS
You might like
PHP 开源框架22个简单简介
2009/08/24 PHP
PHP仿盗链代码
2012/06/03 PHP
PHP SPL标准库之数据结构栈(SplStack)介绍
2015/05/12 PHP
PHP常用文件操作函数和简单实例分析
2016/06/03 PHP
PHP简单实现正则匹配省市区的方法
2018/04/13 PHP
动态控制Table的js代码
2007/03/07 Javascript
详解JavaScript的Polymer框架中的通知交互
2015/07/29 Javascript
JavaScript中利用jQuery绑定事件的几种方式小结
2016/03/06 Javascript
CSS3 media queries结合jQuery实现响应式导航
2016/09/30 Javascript
微信小程序 animation API详解及实例代码
2016/10/08 Javascript
AngularJS通过ng-route实现基本的路由功能实例详解
2016/12/13 Javascript
react native实现往服务器上传网络图片的实例
2017/08/07 Javascript
在Node.js中将SVG图像转换为PNG,JPEG,TIFF,WEBP和HEIF格式的方法
2019/08/22 Javascript
vue element-ui中table合计指定列求和实例
2020/11/02 Javascript
python使用rabbitmq实现网络爬虫示例
2014/02/20 Python
Python多线程和队列操作实例
2015/06/21 Python
Python+selenium实现自动循环扔QQ邮箱漂流瓶
2018/05/29 Python
numpy的文件存储.npy .npz 文件详解
2018/07/09 Python
Python解析、提取url关键字的实例详解
2018/12/17 Python
使用python PIL库实现简单验证码的去噪方法步骤
2019/05/10 Python
django中账号密码验证登陆功能的实现方法
2019/07/15 Python
Django后端接收嵌套Json数据及解析详解
2019/07/17 Python
python+requests接口压力测试500次,查看响应时间的实例
2020/04/30 Python
CSS3 animation实现逐帧动画效果
2016/06/02 HTML / CSS
巴西图书和电子产品购物网站:Saraiva
2017/06/07 全球购物
美国眼镜网:GlassesUSA
2017/09/07 全球购物
学习党的群众路线教育实践活动心得体会
2014/03/01 职场文书
出生公证书样本
2014/04/04 职场文书
省文明单位申报材料
2014/05/08 职场文书
学生安全责任书模板
2014/07/25 职场文书
感恩老师演讲稿400字
2014/08/28 职场文书
酒店辞职书范文
2015/02/26 职场文书
2015高三毕业寄语赠言
2015/02/27 职场文书
2014年度个人工作总结范文
2015/03/09 职场文书
餐厅营销的秘密:为什么老顾客会流水?
2019/08/08 职场文书
如何利用golang运用mysql数据库
2022/03/13 Golang