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中文網其他相關文章!

React是前端框架,用於構建用戶界面;後端框架用於構建服務器端應用程序。 React提供組件化和高效的UI更新,後端框架提供完整的後端服務解決方案。選擇技術棧時需考慮項目需求、團隊技能和可擴展性。

HTML和React的關係是前端開發的核心,它們共同構建現代Web應用的用戶界面。 1)HTML定義內容結構和語義,React通過組件化構建動態界面。 2)React組件使用JSX語法嵌入HTML,實現智能渲染。 3)組件生命週期管理HTML渲染,根據狀態和屬性動態更新。 4)使用組件優化HTML結構,提高可維護性。 5)性能優化包括避免不必要渲染,使用key屬性,保持組件單一職責。

React是構建交互式前端體驗的首選工具。 1)React通過組件化和虛擬DOM簡化UI開發。 2)組件分為函數組件和類組件,函數組件更簡潔,類組件提供更多生命週期方法。 3)React的工作原理依賴虛擬DOM和調和算法,提高性能。 4)狀態管理使用useState或this.state,生命週期方法如componentDidMount用於特定邏輯。 5)基本用法包括創建組件和管理狀態,高級用法涉及自定義鉤子和性能優化。 6)常見錯誤包括狀態更新不當和性能問題,調試技巧包括使用ReactDevTools和優

React是一個用於構建用戶界面的JavaScript庫,其核心是組件化和狀態管理。 1)通過組件化和狀態管理簡化UI開發。 2)工作原理包括調和和渲染,優化可通過React.memo和useMemo實現。 3)基本用法是創建並渲染組件,高級用法包括使用Hooks和ContextAPI。 4)常見錯誤如狀態更新不當,可使用ReactDevTools調試。 5)性能優化包括使用React.memo、虛擬化列表和CodeSplitting,保持代碼可讀性和可維護性是最佳實踐。

React通過JSX與HTML結合,提升用戶體驗。 1)JSX嵌入HTML,使開發更直觀。 2)虛擬DOM機制優化性能,減少DOM操作。 3)組件化管理UI,提高可維護性。 4)狀態管理和事件處理增強交互性。

React組件可以通過函數或類定義,封裝UI邏輯並通過props接受輸入數據。 1)定義組件:使用函數或類,返回React元素。 2)渲染組件:React調用render方法或執行函數組件。 3)復用組件:通過props傳遞數據,構建複雜UI。組件的生命週期方法允許在不同階段執行邏輯,提升開發效率和代碼可維護性。

React嚴格模式是一種開發工具,可通過激活其他檢查和警告來突出反應應用中的潛在問題。它有助於識別遺產代碼,不安全的生命週期和副作用,鼓勵現代反應實踐。


熱AI工具

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

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

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

MinGW - Minimalist GNU for Windows
這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。

MantisBT
Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。

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

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

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