微信小程式二維碼canvas繪製
var canvas = { width: 100, height:36 }; function verification(ctx) { // //清空画布 ctx.clearRect(0, 0, canvas.width, canvas.height); // //生成随机颜色 function getRandomColor() { return "#" + ("00000" + ((Math.random() * 16777215 + 0.5) >> 0).toString(16)).slice(-6); } // //定义线性渐变 var gradient = ctx.createLinearGradient(0, 0, canvas.width, canvas.height); gradient.addColorStop("0", getRandomColor()); gradient.addColorStop("0.5", getRandomColor()); gradient.addColorStop("1.0", getRandomColor()); // //生成随机数 function rnd(min, max) { return min + Math.floor(Math.random() * (max - min + 1)); } // //绘制干扰线条 function line() { ctx.beginPath(); ctx.moveTo(rnd(0, canvas.width), rnd(0, canvas.height)); ctx.lineTo(rnd(0, canvas.width), rnd(0, canvas.height)); ctx.closePath(); ctx.lineWidth = rnd(1, 3); ctx.setFillStyle(gradient) ctx.stroke(); } // //绘制干扰点 function point() { ctx.fillRect(rnd(0, canvas.width), rnd(0, canvas.height), 2, 2); } // //绘制验证码 var text = rnd(1000, 9999); ctx.setFontSize(30) ctx.setFillStyle(gradient) ctx.fillText(text,2, 25); // //生成干扰元素 for (var i = 0; i < 8; i++) { line(); } for (var i = 0; i <100; i++) { point(); } ctx.draw() return text; } module.exports = { verification: verification };
上面直接放在一個js中吧方法暴露出來就可以了,順便說一下關於樣式的問題,因為沒必要再寫一篇了,樣式:padding ,
以前在html頁面上我有時候習慣padding:0px,這樣設置為0,但是在小程式中寫成padding:0rpx,仍會有內邊距存在,寫成padding:0,不要帶單位就可以了
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
更多微信小程式 二維碼canvas繪製實例詳解相關文章請關注PHP中文網!
陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

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

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

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章
R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
3 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳圖形設置
3 週前By尊渡假赌尊渡假赌尊渡假赌
刺客信條陰影:貝殼謎語解決方案
2 週前ByDDD
R.E.P.O.如果您聽不到任何人,如何修復音頻
3 週前By尊渡假赌尊渡假赌尊渡假赌
WWE 2K25:如何解鎖Myrise中的所有內容
4 週前By尊渡假赌尊渡假赌尊渡假赌

熱工具

Safe Exam Browser
Safe Exam Browser是一個安全的瀏覽器環境,安全地進行線上考試。該軟體將任何電腦變成一個安全的工作站。它控制對任何實用工具的訪問,並防止學生使用未經授權的資源。

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

SAP NetWeaver Server Adapter for Eclipse
將Eclipse與SAP NetWeaver應用伺服器整合。

SublimeText3 英文版
推薦:為Win版本,支援程式碼提示!

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