时间:2021-05-26
HTML5真的是很强大,前端时间看到一个canvas实现九宫格的密码解锁。今天抽出时间模仿了一个,特定分享一下!
效果截图如下:
效果看起来还不错吧!
源码如下:
<!DOCTYPE html><html><head lang="zh-CN"> <meta content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" name="viewport"/> <meta charset="UTF-8"> <title>html5实现网页解锁功能</title> <style type="text/css"> html, body { margin: 0; padding: 0; width: 100%; height: 100%; } </style> <script type="text/javascript"> /** * 半径,画布宽度,画布高度,画布x内边距,画布y内边距 */ var R = 26, canvasWidth = 400, canvasHeight = 320, OffsetX = 30, OffsetY = 30; var circleArr = []; function createCirclePoint(diffX, diffY) { for (var row = 0; row < 3; row++) { for (var col = 0; col < 3; col++) { // 计算圆心坐标 var Point = { X: (OffsetX + col * diffX + ( col * 2 + 1) * R), Y: (OffsetY + row * diffY + (row * 2 + 1) * R) }; circleArr.push(Point); } } } window.onload = function () { var canvas = document.getElementById("lockCanvas"); canvasWidth = document.body.offsetWidth;//网页可见区域宽 canvas.width = canvasWidth; canvas.height = canvasHeight; var cxt = canvas.getContext("2d"); /** * 每行3个圆 * OffsetX为canvas x方向内边距 * */ var X = (canvasWidth - 2 * OffsetX - R * 2 * 3) / 2; var Y = (canvasHeight - 2 * OffsetY - R * 2 * 3) / 2; createCirclePoint(X, Y); bindEvent(canvas, cxt); //CW=2*offsetX+R*2*3+2*X Draw(cxt, circleArr, [],null); } function Draw(cxt, circleArr, pwdArr,touchPoint) { if (pwdArr.length > 0) { cxt.beginPath(); for (var i = 0; i < pwdArr.length; i++) { var pointIndex = pwdArr[i]; cxt.lineTo(circleArr[pointIndex].X, circleArr[pointIndex].Y); } cxt.lineWidth = 10; cxt.strokeStyle = "#627eed"; cxt.stroke(); cxt.closePath(); if(touchPoint!=null){ var lastPointIndex=pwdArr[pwdArr.length-1]; var lastPoint=circleArr[lastPointIndex]; cxt.beginPath(); cxt.moveTo(lastPoint.X,lastPoint.Y); cxt.lineTo(touchPoint.X,touchPoint.Y); cxt.stroke(); cxt.closePath(); } } for (var i = 0; i < circleArr.length; i++) { var Point = circleArr[i]; cxt.fillStyle = "#627eed"; cxt.beginPath(); cxt.arc(Point.X, Point.Y, R, 0, Math.PI * 2, true); cxt.closePath(); cxt.fill(); cxt.fillStyle = "#ffffff"; cxt.beginPath(); cxt.arc(Point.X, Point.Y, R - 3, 0, Math.PI * 2, true); cxt.closePath(); cxt.fill(); if(pwdArr.indexOf(i)>=0){ cxt.fillStyle = "#627eed"; cxt.beginPath(); cxt.arc(Point.X, Point.Y, R -16, 0, Math.PI * 2, true); cxt.closePath(); cxt.fill(); } } } /** * 计算选中的密码 */ function getSelectPwd(touches,pwdArr){ for (var i = 0; i < circleArr.length; i++) { var currentPoint = circleArr[i]; var xdiff = Math.abs(currentPoint.X - touches.pageX); var ydiff = Math.abs(currentPoint.Y - touches.pageY); var dir = Math.pow((xdiff * xdiff + ydiff * ydiff), 0.5); if(dir > R || pwdArr.indexOf(i) >= 0) continue; pwdArr.push(i); break; } } /** * 给画布绑定事件 */ function bindEvent(canvas, cxt) { var pwdArr = []; canvas.addEventListener("touchstart", function (e) { getSelectPwd(e.touches[0],pwdArr); }, false); canvas.addEventListener("touchmove", function (e) { e.preventDefault(); var touches = e.touches[0]; getSelectPwd(touches,pwdArr); cxt.clearRect(0,0,canvasWidth,canvasHeight); Draw(cxt,circleArr,pwdArr,{X:touches.pageX,Y:touches.pageY}); }, false); canvas.addEventListener("touchend", function (e) { cxt.clearRect(0,0,canvasWidth,canvasHeight); Draw(cxt,circleArr,pwdArr,null); alert("密码结果是:"+pwdArr.join("->")); pwdArr=[]; }, false); } </script></head><body><canvas id="lockCanvas"></canvas></body></html>以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
相信大家都有使用九宫格解锁,比如在设置手机安全项目中,可以使用九宫格解锁,提高安全性,以及在使用支付功能的时候,为了提高安全使用九宫锁,今天就为大家介绍Andr
当年感觉九宫格解锁很是高大上,一脸懵逼,今天正好要做解锁这一块业务,回头来看九宫格,这特么简单啊首先理清一下逻辑,我们要做NxN的九宫格下图是3x3的简单图例/
最近公司要求做个九宫格解锁,本人用的是小米手机,看着他那个设置锁屏九宫格很好看,就做了该组件,不使用图片资源,纯代码实现。尊重每个辛苦的博主,在http://b
这篇文章主要介绍了HTML九宫格布局实现方法,瀑布流,九宫格,响应式等布局方式已成为主流,对九宫格布局感兴趣的小伙伴们可以参考一下。 网站布局多样化是我们
本文实例为大家分享了js实现幸运抽奖九宫格大转盘效果,供大家参考,具体内容如下实现代码:九宫格大转盘*{padding:0;margin:0}