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画一个阴阳八卦图
Mar 09 HTML / CSS
CSS3等相关属性制作分页导航实现代码
Dec 24 HTML / CSS
CSS3新属性transition-property transform box-shadow实例学习
Jun 06 HTML / CSS
css3 条纹化和透明化表格Firefox下测试成功
Apr 15 HTML / CSS
利用Bootstrap实现漂亮简洁的CSS3价格表实例源码
Mar 02 HTML / CSS
CSS3实现歌词进度文字颜色填充变化动态效果的思路详解
Jun 02 HTML / CSS
Html5实现单张、多张图片上传功能
Apr 28 HTML / CSS
利用html5的websocket实现websocket聊天室
Dec 12 HTML / CSS
配置H5的滚动条样式的示例代码
Mar 09 HTML / CSS
使用HTML5 Geolocation实现一个距离追踪器
Apr 09 HTML / CSS
CSS实现多个元素在盒子内两端对齐效果
Mar 30 HTML / CSS
开发微信小程序之WXSS样式教程
Apr 18 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
实用函数4
2007/11/08 PHP
一个php Mysql类 可以参考学习熟悉下
2009/06/21 PHP
md5 16位二进制与32位字符串相互转换示例
2013/12/30 PHP
php中filter_input函数用法分析
2014/11/15 PHP
php数组排序usort、uksort与sort函数用法
2014/11/17 PHP
通过php删除xml文档内容的方法
2015/01/23 PHP
php实现parent调用父类的构造方法与被覆写的方法
2015/02/11 PHP
PHP翻页跳转功能实现方法
2020/11/30 PHP
PHP实现单例模式建立数据库连接的方法分析
2020/02/11 PHP
详解phpstorm2020最新破解方法
2020/09/17 PHP
如何用js控制frame的隐藏或显示的解决办法
2013/03/20 Javascript
为什么要在引入的css或者js文件后面加参数的详细讲解
2013/05/03 Javascript
使用javascript实现ListBox左右全选,单选,多选,全请
2013/11/07 Javascript
javascript客户端遍历控件与获取父容器对象示例代码
2014/01/06 Javascript
js如何判断用户是否是用微信浏览器
2014/06/05 Javascript
javascript中的this详解
2014/12/08 Javascript
JavaScript正则表达式中的ignoreCase属性使用详解
2015/06/16 Javascript
js一维数组、多维数组和对象的混合使用方法
2016/04/03 Javascript
浅谈Angular2 模块懒加载的方法
2017/10/04 Javascript
vue中路由参数传递可能会遇到的坑
2017/12/07 Javascript
js解决软键盘遮挡输入框的问题分享
2017/12/19 Javascript
Vue自定义指令上报Google Analytics事件统计的方法
2019/02/25 Javascript
react高阶组件添加和删除props
2019/04/26 Javascript
Vue通过Blob对象实现导出Excel功能示例代码
2020/07/31 Javascript
使用python实现tcp自动重连
2017/07/02 Python
Python实现的读取文件内容并写入其他文件操作示例
2019/04/09 Python
利用PyQt5+Matplotlib 绘制静态/动态图的实现代码
2020/07/13 Python
使用scrapy ImagesPipeline爬取图片资源的示例代码
2020/09/28 Python
纯css3制作的火影忍者写轮眼开眼至轮回眼及进化过程实例
2014/11/11 HTML / CSS
Book Depository澳大利亚:世界领先的专业在线书店之一
2018/12/27 全球购物
几道数据库的面试题或笔试题
2014/05/31 面试题
创先争优活动党员公开承诺书
2014/08/29 职场文书
院党委组织查摆问题对照检查材料思想汇报2014
2014/10/08 职场文书
早安问候语大全
2015/11/10 职场文书
Python+Selenium实现抖音、快手、B站、小红书、微视、百度好看视频、西瓜视频、微信视频号、搜狐视频、一点号、大风号、趣头条等短视频自动发布
2022/04/13 Python
Docker部署Mysql8的实现步骤
2022/07/07 Servers