算术验证码
效果图:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>验证码</title>
<style>
.codeCanvas {
border: 1px solid #3A71A8;
}
</style>
</head>
<body>
<div id="app">
<!-- 验证码 -->
<div id="canvas-input">
<input class="canvas-input-class" @blur="checks()" size="large" type='text' v-model="inputCode" placeholder="请输入右侧图案的值"/>
<canvas class="codeCanvas" ref="checkCode" @click="getCode()"></canvas>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/vue.js"></script>
<script>
new Vue({
el: '#app',
data() {
return {
// 验证码
inputCode: '', // 输入的值
checkCode: '', // 图片验证码的值
expressValue: '', // 表达式的值
// canvas各种设置
cvs: {
w: 300, // 给出默认宽度 宽度会在图片绘制时根据长度更改
h: 40, // 高 与input保持一致
fontSize: 24, // 字体大小
str: '+-*', // 符号生成范围
line: 3 // 噪音线个数
}
}
},
mounted() {
this.getCode();
},
methods: {
// canvas
// 随机整数生成器,范围[0, max)
rInt(max) {
return Math.floor(Math.random() * 100000 % max);
},
// 生成随机表达式
rCode() {
let a = this.rInt(100);
let b = this.rInt(10