html5 canvas手式解锁源代码共享

日期:2020-12-11 类型:科技新闻 

关键词:如何制作微信小程序,微信小程序源码,小程序码生成,凡科网微信小程序,微信公众号小程序

先放图

demo.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF⑻">
    <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、1行3个圆14个半径,1行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、检验手式挪动的部位是不是处在下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手式解锁源代码共享,期待对大伙儿有一定的协助,假如大伙儿有任何疑惑请给我留言,网编会立即回应大伙儿的。在此也十分谢谢大伙儿对脚本制作之家网站的适用!
假如你感觉本文对你有协助,欢迎转载,烦请注明出处,感谢!