当前位置:网站首页>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>
边栏推荐
- Gbpnzd firm offer for 14 months, simulation for 19 months, test stable
- LeetCode-101-对称二叉树
- Principles of MySQL clustered index and non clustered index
- SEO outsourcing reliable company, enterprise SEO outsourcing company which reliable?
- SQL is used for field data types in various databases
- 什么是算子?
- Apifox simple understanding -- the integrator of web side testing
- Sorting out the latest data mining competition scheme!
- 为什么生命科学企业都在陆续上云?
- Network security detection and prevention test questions (4)
猜你喜欢

Vulnhub range the planes:earth

什么是算子?

Using flex to implement the Holy Grail layout is as simple as that

Leetcode-101-symmetric binary tree

One night I worked as an XPath Terminator: XPath Helper Plus

【C语言练习——打印上三角及其变形(带空格版)】

为什么生命科学企业都在陆续上云?
![Analysis on market scale and supply of China's needle coke industry in 2020 [figure]](/img/79/6b08b62be8768484f548b6e18bd810.jpg)
Analysis on market scale and supply of China's needle coke industry in 2020 [figure]

削足适履 - 谈谈赛道上的坡道改造
![Analysis on development status and development suggestions of e-commerce industry in Xinjiang in 2020 [figure]](/img/d1/8ed2958ef365e17494bade6e29ee04.jpg)
Analysis on development status and development suggestions of e-commerce industry in Xinjiang in 2020 [figure]
随机推荐
Miner's Diary: why should I go mining on April 5, 2021
Analysis on market scale and supply of China's needle coke industry in 2020 [figure]
Convert word to PDF through libreoffice
Solve the problem that sublime Text3 package control cannot install plug-ins
二、HikariCP獲取連接流程源碼分析二
Google cloud SSH enable root password login
Paddleocr learning (II) paddleocr detection model training
Overview and trend analysis of China's CT examination equipment industry in 2021 [figure]
Current situation and trend analysis of China's glass packaging containers in 2021: the revenue of glass packaging containers increases year by year [figure]
Tcp/ip test questions (I)
三、HikariCP获取连接流程源码分析三
最新数据挖掘赛事方案梳理!
Gbpnzd firm offer for 14 months, simulation for 19 months, test stable
What should I pay attention to in GoogleSEO content station optimization?
广州华锐互动VR全景为各行各业带来发展
Is CICC wealth safe? How long does it take to open an account
Shell jump loop shift parameter left use of function
Divine reversion EA
Trend ea- fixed stop loss and profit per order
想知道新股民怎样炒股票开户?在线开户安全么?