当前位置:网站首页>Verification code native JS canvas
Verification code native JS canvas
2022-06-25 19:33:00 【Good first】
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.box {
width: 400px;
/* height: 100px;/ */
/* border: 1px solid ; */
margin: 100px auto;
display: flex;
align-items: center;
justify-content: space-between;
}
#inp {
width: 170px;
height: 30px;
}
canvas{
border: 1px solid;
border-radius: 10px;
/* canvas A special You need to set the width and height Write in line This is its attribute If you want to write here It needs to be converted to a block element */
}
</style>
</head>
<body>
<div class="box">
<input id="inp" type="text" placeholder=" Please enter the verification code ( Case sensitive )">
<canvas id="can" width="120" height="50"></canvas>
<button id="submit"> Submit </button>
</div>
<script>
var can = document.getElementById('can');
var arr = 'qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890'
var ctx = can.getContext('2d')
// Random output
function random(a, b) {
return Math.floor(Math.random() * (b - a + 1)) + a;
}
can.onclick = function () {
console.log(1);
getCode()
}
var code = ''; // Record random verification code
getCode()
function getCode() {
// Clear the previous before each click
ctx.clearRect(0, 0, 120, 50);
// Random lines 6 Heel root
for (let i = 0; i < 6; i++) {
// Began to draw
ctx.beginPath();
// Random starting point
ctx.moveTo(random(0,120),random(0,50));
// A random destination
ctx.lineTo(random(0,120),random(0,50));
// Randomly generate fill colors
ctx.strokeStyle =`rgb(${
random(0,255)},${
random(0,255)},${
random(0,255)}`;
// Draw line
ctx.stroke()
}
// Random A circle
for (let i = 0; i < 20; i++) {
// Began to draw
ctx.beginPath();
// Random circle drawing arc(x,y,r,start,end); Draw a circle ( Radian unit )
ctx.arc(random(0,120),random(0,50), random(0,5),0,Math.PI * 2) ;
// Color of random circle
ctx.fillStyle = `rgb(${
random(0,255)},${
random(0,255)},${
random(0,255)}`;
// Packing circle
ctx.fill()
}
// Randomly generated letters 4 individual
for (let i = 0; i < 4; i++) {
var index = Math.floor(Math.random() * arr.length)
var letter = arr.charAt(index);// arr[index] Take out the letters at random
// Record random letters
code += letter
// Random letter size
ctx.font = random(30 , 50) + 'px Regular script ';
// Random color
ctx.fillStyle = `rgb(${
random(0,255)},${
random(0,255)},${
random(0,255)}`
// The writing method of the normal display of letters I
// ctx.fillText(letter,15 + i * 25, Math.random() * 10 + 30 )
// The letters are shown obliquely
var x = 15 + i * 25 ; // Point of font
var y = Math.random() * 10 + 30 ;
// Move the canvas rotation center to this letter
ctx.translate(x, y);
// Rotate the canvas Go around the letters ‘
var range = (Math.random() * 40 - 20) / 180 * Math.PI;
// Rotate within this range of letters Up
ctx.rotate(range);
ctx.fillText(letter,0,0) // fill
// Down
ctx.rotate(-range);
ctx.translate(-x,-y)
}
}
// Submit validation
submit.onclick = function () {
if (inp.value == code) {
alert(' Verification passed ');
// inp.value = ''
getCode()
}else{
alert(' Verification code error, please re-enter ')
}
}
</script>
</body>
</html>
边栏推荐
- Many varieties of EA can be used
- Laravel validation rule followed Role of auth:: id()
- Do you want to know how new investors open accounts? Is online account opening safe?
- ActiveMQ--CVE-2016-3088
- What should I pay attention to in GoogleSEO content station optimization?
- QQ机器人官方插件加载配置方法【beta2版本】
- Tcp/ip test questions (V)
- Analysis on development status and development suggestions of e-commerce industry in Xinjiang in 2020 [figure]
- Tcp/ip test questions (III)
- DataX script task development record
猜你喜欢
Analysis on development status and development suggestions of e-commerce industry in Xinjiang in 2020 [figure]
Analysis on market scale and supply of China's needle coke industry in 2020 [figure]
Elastic high-performance computing on the cloud supports the rapid development of the life science industry, reducing costs and increasing efficiency
Trend ea- fixed stop loss and profit per order
Uncover ges super large scale graph computing engine hyg: Graph Segmentation
广州华锐互动VR全景为各行各业带来发展
PHP synchronizes website content to hundreds of websites to improve SEO ranking
Current situation and trend analysis of China's glass packaging containers in 2021: the revenue of glass packaging containers increases year by year [figure]
Apifox simple understanding -- the integrator of web side testing
Bloom filter
随机推荐
Vscode debugging PHP configuration Xdebug
Paddleocr learning (II) paddleocr detection model training
Kotlin Compose 终结toDo项目 点击可以编辑修改todo
R语言使用DALEX包的model_profile函数基于条件依赖CDP方法解释多个分类模型中某个连续特征和目标值y的关系(Conditional Dependence Plots)
PHP synchronizes website content to hundreds of websites to improve SEO ranking
谈谈CNN中的位置和尺度问题
Solidity date tool
MySQL view explanation
rmi-registry-bind-deserialization
ECS 7-day practical training camp (Advanced route) -- day04 -- build a portal using ECs and polardb
Overview and trend analysis of China's foreign direct investment industry in 2020 [figure]
electron 基础项目搭建 &&主线程和渲染线程的通信
solidity日期工具
最新数据挖掘赛事方案梳理!
QQ robot flash forwarding / recall message forwarding [latest beta2 version]
Apifox simple understanding -- the integrator of web side testing
Electronic basic project construction & communication between main thread and rendering thread
五、HikariCP源码分析之初始化分析二
一、HikariCP获取连接流程源码分析一
最新數據挖掘賽事方案梳理!