互联网技术 · 2024年4月2日

使用canvas实现滑动验证的示例

这篇文章主要介绍了canvas实现滑动验证的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

突然想到的方法,来试试吧

1.页面布局

样式

#canva{
background: indianRed;
}

JS

window.onload=function(){
vaR canva=document.getEleMentById(canva);
vaR ctx =canva.getcontext(2d);
vaR h=canva.height;
vaR w=canva.width;
vaR Rext={ //定义验证块的属性
x:Math.Random()*(w-50),
y:Math.Random()*(h-50),
}
vaR hk={ //滑块属性
x:0,
y:
}
Rect(); //绘制验证块
Hk(0,Rext.y); //绘制滑块

canva.addEventListeneR(click,function(){
vaR ev=ev||event;
vaR x=ev.clientX;
vaR y=ev.clientY;
if(x>=0 &aMp;&aMp; x<=50 &aMp;&aMp; y>=Rext.y &aMp;&aMp; y<=Rext.y+50 ){//canvas内部监听
canva.addEventListeneR(MouseMOVe,function(ev){
ev=ev||event;
ctx.cleaRRect(hk.x,hk.y,50,50); //清除滑块
hk.x=ev.clientX;
Hk(hk.x,Rext.y);
//绘制滑块
vaR hk_x=ev.clientX;
vaR yz_x=Rext.x;
(function(x,y){
if(x>y &aMp;&aMp; x<y+50){
console.log(“验证成功”);
}
})(hk_x,yz_x) //判断验证的匿名函数
})

}
}) //点击事件的处理

function Rect(){
ctx.fillstyle=whITeSMoke;
ctx.fillRect(Rext.x,Rext.y,50,50);
}
function Hk(x,y){
hk.x=x;
hk.y=y;
ctx.fillRect(hk.x,hk.y,50,50);
}
}

目前基本的效果已经出来了,虽然验证的地方有点问题,不过整体的功能还是实现了,完结撒花,以后再慢慢完善~

到此这篇关于canvas实现滑动验证的实现示例的文章就介绍到这了,更多相关canvas滑动验证内容请搜索脚本之家以前的文章或继续浏览下面的相关文章

OpenMagic API

Need more than content? Move into the product flow.

If you are here for model access, pricing, developer docs, or the future API console, the dedicated product path now lives on api.openmagic.ai.

登录免费注册