溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

怎么使用前端中canvas實(shí)現(xiàn)手勢(shì)解鎖

發(fā)布時(shí)間:2022-02-28 15:55:46 來源:億速云 閱讀:142 作者:iii 欄目:開發(fā)技術(shù)

本篇內(nèi)容介紹了“怎么使用前端中canvas實(shí)現(xiàn)手勢(shì)解鎖”的有關(guān)知識(shí),在實(shí)際案例的操作過程中,不少人都會(huì)遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!

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>手勢(shì)解鎖</title>
    <style type="text/css">
        body{
            text-align: center;
            background: #305066;
        }
        h5{
            color: #22C3AA;
        }
    </style>
</head>
<body>
    <script type="text/javascript" src="src/index.js"></script>
    <script type="text/javascript">
        // 1、生成背景
        // 2、title生成
        // 3、用js動(dòng)態(tài)生成canvas標(biāo)簽
        // 4、js方式動(dòng)態(tài)生成h5標(biāo)簽和canvas標(biāo)簽
        new canvasLock({chooseType:3}).init();
    </script>
</body>
</html>

index.js:

(function(){
        /**
         * 實(shí)現(xiàn)畫圓和劃線:
         * 1、添加事件touchstart、touchmove、touchend
         * 2、touchstart判斷是否點(diǎn)擊的位置處于圓內(nèi)getPosition,處于則初始化
         * lastpoint、restPoint
         * 3、touchmove做的就是:畫圓drawPoint和畫線drawLine
         *
         * 實(shí)現(xiàn)自動(dòng)畫圓的效果
         * 1、檢測(cè)手勢(shì)移動(dòng)的位置是否處于圓內(nèi)
         * 2、圓內(nèi)的話則畫圓 drawPoint
         * 3、已經(jīng)畫過實(shí)心圓的圓,無需重復(fù)檢測(cè)
         *
         * 實(shí)現(xiàn)解鎖成功:
         * 1、檢測(cè)路徑是否是對(duì)的
         * 2、如果是對(duì)的就重置,圓圈變綠
         * 3、不對(duì)也重置,圓圈變紅
         * 4、重置
         */
        window.canvasLock = function(obj){
            this.height = obj.height;
            this.width = obj.width;
            this.chooseType = obj.chooseType;
        };
        // js方式動(dòng)態(tài)生成dom
        canvasLock.prototype.initDom = function(){
            var wrap = document.createElement('div');
            var str = '<h5 id="title" class="title">繪制解鎖圖案</h5>';
            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() {// 創(chuàng)建解鎖點(diǎn)的坐標(biāo),根據(jù)canvas的大小來平均分配半徑

            var n = this.chooseType;
            var count = 0;
            this.r = this.ctx.canvas.width / (2 + 4 * n);// 公式計(jì)算
            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++) {
                // 畫圓函數(shù)
                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、確定每一個(gè)圓的中心坐標(biāo)點(diǎn)
            // 3、一行3個(gè)圓14個(gè)半徑,一行4個(gè)圓有18個(gè)半徑
            this.createCircle();
            this.bindEvent();
        }

        canvasLock.prototype.bindEvent = function(){
            var self = this;
            this.canvas.addEventListener("touchstart", function (e) {
                // 2、touchstart判斷是否點(diǎn)擊的位置處于圓內(nèi)getPosition,處于則初始化
                //          * lastpoint、restPoint
                
                // po有x和y,并且是相較于canvas邊距
                var po = self.getPosition(e);
                console.log(po.x)
                // 判斷是否在圓內(nèi)的原理:多出來的這條 x/y < r 在圓內(nèi)
                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坐標(biāo)值
                        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點(diǎn)相對(duì)于canvas的坐標(biāo)
            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時(shí)候調(diào)用
            this.ctx.clearRect(0, 0, this.ctx.canvas.width, this.ctx.canvas.height);

            // 重新畫9個(gè)圓圈
            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、檢測(cè)手勢(shì)移動(dòng)的位置是否處于下一個(gè)圓內(nèi)
            // 2、圓內(nèi)的話則畫圓 drawPoint
            // 3、已經(jīng)畫過實(shí)心圓的圓,無需重復(fù)檢測(cè)
            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、檢測(cè)路徑是否是對(duì)的
        // 2、如果是對(duì)的就重置,圓圈變綠
        // 3、不對(duì)也重置,圓圈變紅
        // 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();
        }
})();

“怎么使用前端中canvas實(shí)現(xiàn)手勢(shì)解鎖”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI