react怎麼實作圖片驗證
react實現圖片驗證的方法:1、開啟對應的react檔案;2、透過「randomNum = (min, max) => {...}」方法產生一個隨機數;3、透過“drawLine(ctx) {...}”方法繪製幹擾線;4、使用“randomCode() {...}”方法隨機產生驗證碼即可。
本教學操作環境:Windows10系統、react18版、Dell G3電腦。
react怎麼實作圖片驗證?
react實作圖片驗證碼
效果如圖所示:
import React, { Component } from 'react' import { Icon, Input, Form } from 'antd'; class OperationalDataManagement extends Component { state = { code: '', codeLength: 4, fontSizeMin: 20, fontSizeMax: 22, backgroundColorMin: 240, backgroundColorMax: 250, colorMin: 10, colorMax: 20, lineColorMin: 40, lineColorMax: 180, contentWidth: 96, contentHeight: 38, showError: false, // 默认不显示验证码的错误信息 } componentWillMount() { this.canvas = React.createRef() } componentDidMount() { this.drawPic() } // 生成一个随机数 // eslint-disable-next-line arrow-body-style randomNum = (min, max) => { return Math.floor(Math.random() * (max - min) + min) } drawPic = () => { this.randomCode() } // 生成一个随机的颜色 // eslint-disable-next-line react/sort-comp randomColor(min, max) { const r = this.randomNum(min, max) const g = this.randomNum(min, max) const b = this.randomNum(min, max) return `rgb(${r}, ${g}, ${b})` } drawText(ctx, txt, i) { ctx.fillStyle = this.randomColor(this.state.colorMin, this.state.colorMax) const fontSize = this.randomNum(this.state.fontSizeMin, this.state.fontSizeMax) ctx.font = fontSize + 'px SimHei' const padding = 10; const offset = (this.state.contentWidth - 40) / (this.state.code.length - 1) let x = padding; if (i > 0) { x = padding + (i * offset) } let y = this.randomNum(this.state.fontSizeMax, this.state.contentHeight - 5) if (fontSize > 40) { y = 40 } const deg = this.randomNum(-10, 10) // 修改坐标原点和旋转角度 ctx.translate(x, y) ctx.rotate(deg * Math.PI / 180) ctx.fillText(txt, 0, 0) // 恢复坐标原点和旋转角度 ctx.rotate(-deg * Math.PI / 180) ctx.translate(-x, -y) } drawLine(ctx) { // 绘制干扰线 for (let i = 0; i < 1; i++) { ctx.strokeStyle = this.randomColor(this.state.lineColorMin, this.state.lineColorMax) ctx.beginPath() ctx.moveTo(this.randomNum(0, this.state.contentWidth), this.randomNum(0, this.state.contentHeight)) ctx.lineTo(this.randomNum(0, this.state.contentWidth), this.randomNum(0, this.state.contentHeight)) ctx.stroke() } } drawDot(ctx) { // 绘制干扰点 for (let i = 0; i < 100; i++) { ctx.fillStyle = this.randomColor(0, 255) ctx.beginPath() ctx.arc(this.randomNum(0, this.state.contentWidth), this.randomNum(0, this.state.contentHeight), 1, 0, 2 * Math.PI) ctx.fill() } } reloadPic = () => { this.drawPic() this.props.form.setFieldsValue({ sendcode: '', }); } // 输入验证码 changeCode = e => { if (e.target.value.toLowerCase() !== '' && e.target.value.toLowerCase() !== this.state.code.toLowerCase()) { this.setState({ showError: true }) } else if (e.target.value.toLowerCase() === '') { this.setState({ showError: false }) } else if (e.target.value.toLowerCase() === this.state.code.toLowerCase()) { this.setState({ showError: false }) } } // 随机生成验证码 randomCode() { let random = '' // 去掉了I l i o O,可自行添加 const str = 'QWERTYUPLKJHGFDSAZXCVBNMqwertyupkjhgfdsazxcvbnm1234567890' for (let i = 0; i < this.state.codeLength; i++) { const index = Math.floor(Math.random() * 57); random += str[index]; } this.setState({ code: random }, () => { const canvas = this.canvas.current; const ctx = canvas.getContext('2d') ctx.textBaseline = 'bottom' // 绘制背景 ctx.fillStyle = this.randomColor(this.state.backgroundColorMin, this.state.backgroundColorMax) ctx.fillRect(0, 0, this.state.contentWidth, this.state.contentHeight) // 绘制文字 for (let i = 0; i < this.state.code.length; i++) { this.drawText(ctx, this.state.code[i], i) } this.drawLine(ctx) this.drawDot(ctx) }) } render() { const { getFieldDecorator } = this.props.form; return ( <div style={{ display: 'flex', alignItems: 'center' }}> <div style={{ width: 300 }}> <Form.Item className='for-form'> {getFieldDecorator('sendcode', { rules: [ { required: true, message: '请输入校验码!' }, { validator: (rule, value, callback) => { if (value) { if(value.toLowerCase()===this.state.code.toLowerCase()){ callback() this.setState({ sendcode: value, showError: false }) } else { callback('请输入正确的验证码') this.setState({ showError: true }) } } else { callback() } } } ], })( <Input prefix={<Icon type="lock" style={{ color: 'rgba(0,0,0,.25)' }} />} onChange={this.changeCode} placeholder="请输入校验码" /> )} </Form.Item> </div> <div> <canvas onClick={this.reloadPic} ref={this.canvas} width='100' height='30'> </canvas> </div> </div> ) } } const WrappedRegistrationForm = Form.create()(OperationalDataManagement); export default WrappedRegistrationForm;
#推薦學習:《react影片教學》
以上是react怎麼實作圖片驗證的詳細內容。更多資訊請關注PHP中文網其他相關文章!

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

熱門話題

如何利用React和WebSocket建立即時聊天應用程式引言:隨著網路的快速發展,即時通訊越來越受到人們的關注。即時聊天應用程式已成為現代社交和工作生活中不可或缺的一部分。本文將介紹如何利用React和WebSocket建立一個簡單的即時聊天應用,並提供具體的程式碼範例。一、技術準備在開始建立即時聊天應用之前,我們需要準備以下技術和工具:React:一個用於構建

React前後端分離指南:如何實現前後端的解耦和獨立部署,需要具體程式碼範例在當今的Web開發環境中,前後端分離已經成為一種趨勢。透過將前端和後端程式碼分開,可以讓開發工作更加靈活、高效,並且方便進行團隊協作。本文將介紹如何使用React實現前後端分離,從而實現解耦和獨立部署的目標。首先,我們要先理解什麼是前後端分離。傳統的Web開發模式中,前端和後端是耦合在

如何利用React和Flask建構簡單易用的網路應用引言:隨著網路的發展,網路應用的需求也越來越多樣化和複雜化。為了滿足使用者對於易用性和效能的要求,使用現代化的技術堆疊來建立網路應用變得越來越重要。 React和Flask是兩個在前端和後端開發中非常受歡迎的框架,它們可以很好的結合在一起,用來建立簡單易用的網路應用。本文將詳細介紹如何利用React和Flask

React響應式設計指南:如何實現自適應的前端佈局效果隨著行動裝置的普及和使用者對多螢幕體驗的需求增加,響應式設計成為了現代前端開發的重要考量之一。而React作為目前最受歡迎的前端框架之一,提供了豐富的工具和元件,能夠幫助開發人員實現自適應的佈局效果。本文將分享一些關於使用React實現響應式設計的指南和技巧,並提供具體的程式碼範例供參考。使用React的Fle

如何利用React和RabbitMQ建立可靠的訊息傳遞應用程式引言:現代化的應用程式需要支援可靠的訊息傳遞,以實現即時更新和資料同步等功能。 React是一種流行的JavaScript庫,用於建立使用者介面,而RabbitMQ是一種可靠的訊息傳遞中間件。本文將介紹如何結合React和RabbitMQ建立可靠的訊息傳遞應用,並提供具體的程式碼範例。 RabbitMQ概述:

React程式碼偵錯指南:如何快速定位並解決前端bug引言:在開發React應用程式時,經常會遇到各種各樣的bug,這些bug可能會使應用程式崩潰或導致不正確的行為。因此,掌握調試技巧是每個React開發者必備的能力。本文將介紹一些定位和解決前端bug的實用技巧,並提供具體的程式碼範例,幫助讀者快速定位和解決React應用程式中的bug。一、調試工具的選擇:在Re

ReactRouter使用指南:如何實現前端路由控制隨著單頁應用的流行,前端路由成為了一個不可忽視的重要部分。 ReactRouter作為React生態系統中最受歡迎的路由庫,提供了豐富的功能和易用的API,使得前端路由的實作變得非常簡單和靈活。本文將介紹ReactRouter的使用方法,並提供一些具體的程式碼範例。安裝ReactRouter首先,我們需要

如何利用React和GoogleBigQuery建立快速的資料分析應用引言:在當今資訊爆炸的時代,資料分析已經成為了各產業中不可或缺的環節。而其中,建構快速、有效率的資料分析應用則成為了許多企業和個人追求的目標。本文將介紹如何利用React和GoogleBigQuery結合來建立快速的資料分析應用,並提供詳細的程式碼範例。一、概述React是用來構建
