原生JavaScript實(shí)現(xiàn)簡(jiǎn)單的圖形驗(yàn)證碼
前天接到需求要在老項(xiàng)目登陸界面加上驗(yàn)證碼功能,因?yàn)槭莾?nèi)部項(xiàng)目且無(wú)需短信驗(yàn)證環(huán)節(jié),那就直接用原生js寫一個(gè)簡(jiǎn)單的圖形驗(yàn)證碼。
示例:
思路:此處假設(shè)驗(yàn)證碼為4位隨機(jī)數(shù)值,數(shù)值刷新滿足兩個(gè)條件①頁(yè)面新進(jìn)/刷新。②點(diǎn)擊圖片刷新。(實(shí)際情況下還要考慮登錄出錯(cuò)刷新,此處只做樣式不寫進(jìn)去) 實(shí)現(xiàn)過(guò)程為1.先寫一個(gè)canvas標(biāo)簽做繪圖容器。 → 2.將拿到的值繪制到容器中并寫好樣式。 → 3.點(diǎn)擊刷新重新繪制。
寫一個(gè)canvas標(biāo)簽當(dāng)容器
<canvas style="width: 100px;border: 2px solid rgb(60, 137, 209);background-image: url('https://gd-hbimg.huaban.com/aa3c7f23dfdc7b2d317aa4b77bd6c7b8469564d2dfa8b-Btd5c6_fw658webp');" id="captchaCanvas"></canvas>
并設(shè)置容器寬高背景顏色或圖片等樣式
寫一個(gè)數(shù)值繪制到canvas的方法
//text為傳遞的數(shù)值 function generateCaptcha(text, callback) { var canvas = document.getElementById('captchaCanvas'); var ctx = canvas.getContext('2d'); // 設(shè)置字體及大小 ctx.font = '100px Comic Sans MS'; // 設(shè)置字體顏色 ctx.fillStyle = 'rgb(' + Math.floor(Math.random() * 256) + ', ' + Math.floor(Math.random() * 256) + ', ' + Math.floor(Math.random() * 256) + ')'; // 調(diào)整文字圖形位置 ctx.textAlign = 'left'; ctx.textBaseline = 'top'; // 調(diào)整陰影范圍 ctx.shadowBlur = Math.random() * 20; // 調(diào)整陰影顏色 ctx.shadowColor = 'rgb(' + Math.floor(Math.random() * 256) + ', ' + Math.floor(Math.random() * 256) + ', ' + Math.floor(Math.random() * 256) + ')'; // 調(diào)整陰影位置(偏移量) ctx.shadowOffsetX = Math.random() * 10; ctx.shadowOffsetY = Math.random() * 10; // 繪制文字圖形及其偏移量 ctx.fillText(text, 25, 35); // 繪制文字邊框及其偏移量 ctx.strokeText(text, Math.random() * 35, Math.random() * 45); var imgDataUrl = canvas.toDataURL(); callback(imgDataUrl); }
拿到數(shù)值調(diào)用繪制方法
此處為樣式示例,因此數(shù)值我用4位隨機(jī)數(shù)表示,實(shí)際情況為你從后端取得的值,并依靠這個(gè)值在后端判斷驗(yàn)證碼是否一致。
// 調(diào)用函數(shù)生成驗(yàn)證碼并顯示在頁(yè)面上 generateCaptcha(Math.floor(Math.random() * 9000) + 1000, function (imgDataUrl) { });
監(jiān)聽(tīng)標(biāo)簽點(diǎn)擊實(shí)現(xiàn)點(diǎn)擊刷新
此處要注意一定要先清空canvas中已繪制圖像再渲染新數(shù)值,因此直接將清除范圍設(shè)置較大。
// 監(jiān)聽(tīng)點(diǎn)擊更新驗(yàn)證碼 document.getElementById("captchaCanvas").addEventListener("click", function (event) { // 清空畫布 document.getElementById("captchaCanvas").getContext("2d").clearRect(0, 0, 9999, 9999); // 調(diào)用函數(shù)生成驗(yàn)證碼并顯示在頁(yè)面上 generateCaptcha(Math.floor(Math.random() * 9000) + 1000, function (imgDataUrl) { }); })
最后實(shí)現(xiàn)效果:
完整代碼演示
<!DOCTYPE html> <html> <head> <title>String to Captcha</title> </head> <body> <canvas style="width: 100px;border: 2px solid rgb(60, 137, 209);background-image: url('https://gd-hbimg.huaban.com/aa3c7f23dfdc7b2d317aa4b77bd6c7b8469564d2dfa8b-Btd5c6_fw658webp');" id="captchaCanvas"></canvas> <script> // 監(jiān)聽(tīng)點(diǎn)擊更新驗(yàn)證碼 document.getElementById("captchaCanvas").addEventListener("click", function (event) { // 清空畫布 document.getElementById("captchaCanvas").getContext("2d").clearRect(0, 0, 9999, 9999); // 調(diào)用函數(shù)生成驗(yàn)證碼并顯示在頁(yè)面上 generateCaptcha(Math.floor(Math.random() * 9000) + 1000, function (imgDataUrl) { }); }) function generateCaptcha(text, callback) { var canvas = document.getElementById('captchaCanvas'); var ctx = canvas.getContext('2d'); // 設(shè)置字體及大小 ctx.font = '100px Comic Sans MS'; // 設(shè)置字體顏色 ctx.fillStyle = 'rgb(' + Math.floor(Math.random() * 256) + ', ' + Math.floor(Math.random() * 256) + ', ' + Math.floor(Math.random() * 256) + ')'; // 調(diào)整文字圖形位置 ctx.textAlign = 'left'; ctx.textBaseline = 'top'; // 調(diào)整陰影范圍 ctx.shadowBlur = Math.random() * 20; // 調(diào)整陰影顏色 ctx.shadowColor = 'rgb(' + Math.floor(Math.random() * 256) + ', ' + Math.floor(Math.random() * 256) + ', ' + Math.floor(Math.random() * 256) + ')'; // 調(diào)整陰影位置(偏移量) ctx.shadowOffsetX = Math.random() * 10; ctx.shadowOffsetY = Math.random() * 10; // 繪制文字圖形及其偏移量 ctx.fillText(text, 25, 35); // 繪制文字邊框及其偏移量 ctx.strokeText(text, Math.random() * 35, Math.random() * 45); var imgDataUrl = canvas.toDataURL(); callback(imgDataUrl); } // 調(diào)用函數(shù)生成驗(yàn)證碼并顯示在頁(yè)面上 generateCaptcha(Math.floor(Math.random() * 9000) + 1000, function (imgDataUrl) { }); </script> </body> </html>
到此這篇關(guān)于原生JavaScript實(shí)現(xiàn)簡(jiǎn)單的圖形驗(yàn)證碼的文章就介紹到這了,更多相關(guān)JavaScript圖形驗(yàn)證碼內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Three.js+React使二維圖片呈現(xiàn)3D效果
這篇文章主要為大家介紹了如何利用Three.js+React技術(shù)棧,將二維漫畫圖片轉(zhuǎn)化為三維視覺(jué)效果。文中的實(shí)現(xiàn)方法講解詳細(xì),需要的可以參考一下2022-02-02微信小程序使用uni-app和springboot實(shí)現(xiàn)一鍵登錄功能(JWT鑒權(quán))
微信一鍵登錄是指用戶在使用小程序時(shí),可以通過(guò)微信賬號(hào)進(jìn)行快速登錄,而無(wú)需額外的注冊(cè)和密碼設(shè)置,這篇文章主要給大家介紹了關(guān)于微信小程序使用uni-app和springboot實(shí)現(xiàn)一鍵登錄功能的相關(guān)資料,需要的朋友可以參考下2023-11-11JavaScript編程設(shè)計(jì)模式之構(gòu)造器模式實(shí)例分析
這篇文章主要介紹了JavaScript編程設(shè)計(jì)模式之構(gòu)造器模式,簡(jiǎn)單講述了構(gòu)造器模式的概念、原理,并結(jié)合實(shí)例形式分析了構(gòu)造器模式的定義與使用方法,需要的朋友可以參考下2017-10-10UniApp中Scroll-View設(shè)置占滿下方剩余高度的方法記錄
在使用uniapp開發(fā)項(xiàng)目過(guò)程中有時(shí)候會(huì)想讓一些組件占有屏幕剩余的高度,下面這篇文章主要給大家介紹了關(guān)于UniApp中Scroll-View設(shè)置占滿下方剩余高度的方法,需要的朋友可以參考下2023-04-04使用pdf-lib.js實(shí)現(xiàn)拼接兩個(gè)pdf文件并添加水印
這篇文章主要為大家詳細(xì)介紹了如何使用pdf-lib.js實(shí)現(xiàn)拼接兩個(gè)pdf文件并添加水印,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解下2024-11-11Math.js解決js中小數(shù)精度丟失問(wèn)題
在JavaScript中進(jìn)行小數(shù)運(yùn)算時(shí),會(huì)容易出現(xiàn)精度丟失的問(wèn)題,例如在進(jìn)行兩個(gè)小數(shù)相加時(shí),結(jié)果并不是預(yù)期的精確值,而是一個(gè)近似值,,使用第三方庫(kù)Math.js可以避免精度丟失的問(wèn)題,本文導(dǎo)入Math.js庫(kù)和使用Math.js的方法來(lái)進(jìn)行小數(shù)運(yùn)算,同時(shí)還可以指定格式來(lái)保留小數(shù)位數(shù)2023-12-12