1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
| <template> <div style="padding: 80px;"> <a-input style="width: 300px;" type="text" v-model:value="inputValue" placeholder="点击验证码刷新" /> <canvas ref="myCanvas" @click="refreshCode" width="120" height="36" style="margin: 0 40px;border: 1px solid rgb(136,131,131); border-radius: 4px;" ></canvas> <a-button type="primary" @click="handleSubmit" >提交</a-button> </div> </template> <script setup lang="ts"> import { ref, onMounted, Ref } from 'vue'
let inputValue = ref<string>('')
let myCanvas = ref()
let context: CanvasRenderingContext2D;
let canvas_width: number; let canvas_height: number;
let codeArray: string[] = ['A','B','C','E','F','G','H','J','K','L','M','N','P','Q','R','S','T','W','X','Y','Z','1','2','3','4','5','6','7','8','9','0']
let showNum: string[] = []
onMounted(() => { context = myCanvas.value.getContext('2d') canvas_width = myCanvas.value.clientWidth canvas_height = myCanvas.value.clientHeight initDrawCode()
})
const initDrawCode = (): void => { context.clearRect(0,0,canvas_width,canvas_height); drawText() drawLine() drawPoint() }
const drawText = (): void => { for(let i = 0; i <= 3; i++){ let randomIndex = Math.floor(Math.random() * codeArray.length) showNum[i] = codeArray[randomIndex] let deg = Math.random() * 40 * Math.PI / 180 let x = 10 + i * 20 let y = 20 + Math.random() * 8 context.font = 'bold 24px 微软雅黑' context.translate(x, y) context.rotate(deg) context.fillStyle = randomColor() context.fillText(showNum[i], 0, 0) context.rotate(-deg) context.translate(-x, -y) } }
const drawLine = (): void => { for (let i = 0; i < 5; i++){ context.strokeStyle = randomColor() context.beginPath() context.moveTo(Math.random()*canvas_width, Math.random()*canvas_height) context.lineTo(Math.random()*canvas_width, Math.random()*canvas_height) context.stroke() } }
const drawPoint = (): void => { for(let i = 0; i <= 30; i ++){ context.strokeStyle = randomColor() context.beginPath() let x = Math.random() * canvas_width let y = Math.random() * canvas_height context.moveTo(x, y) context.lineTo(x + 1, y + 1) context.stroke() } }
const randomColor = (): string => { let r = Math.floor(Math.random() * 256) let g = Math.floor(Math.random() * 256) let b = Math.floor(Math.random() * 256) return `rgb(${r},${g},${b})` }
const refreshCode = (): void => { initDrawCode() }
const handleSubmit = (): void => { let v = inputValue.value ? inputValue.value.trim() : '' if(!v){ alert('请输入验证码') return; } if(v.toUpperCase() === showNum.join('').toUpperCase()){ alert('验证成功') return; } alert('验证失败') } </script>
|