微信小程序?qū)崿F(xiàn)動態(tài)驗證碼
本文實例為大家分享了微信小程序?qū)崿F(xiàn)動態(tài)驗證碼的具體代碼,供大家參考,具體內(nèi)容如下

一、創(chuàng)建自定義組件verification-code
verification-code.js
// pages/test1/verificationQr/verificationQr.js
var ctx;
Component({
? /**
? ?* 組件的屬性列表
? ?*/
? properties: {
? ? width:{
? ? ? type: Number,
? ? ? value: 150
? ? },
? ? height: {
? ? ? type: Number,
? ? ? value: 48
? ? },
? ? count:{
? ? ? type:Number,
? ? ? value:4
? ? },
? ? fontSize: {
? ? ? type: Number,
? ? ? value: 34
? ? },
? ? fontFamily:{
? ? ? type: String,
? ? ? value: "SimHei"
? ? }
? },
? /**
? ?* 組件的初始數(shù)據(jù)
? ?*/
? data: {
? ? text: '',
? ? count: 4,
? ? width:90,
? ? height:28,
? ? fontSize:30,
? ? fontFamily:"SimHei"
? },
? ready() {
? ? this.setData({
? ? ? count:this.properties.count,
? ? ? width:this.properties.width,
? ? ? height:this.properties.height,
? ? ? fontSize:this.properties.fontSize,
? ? ? fontFamily:this.properties.fontFamily
? ? })
? ? this.drawPic(this)
? },
? /**
? ?* 組件的方法列表
? ?*/
? methods: {
? ? crash(){
? ? ? this.drawPic(this)
? ? },
? ? /**繪制驗證碼圖片**/
? ? drawPic(that) {
? ? ? ctx = wx.createCanvasContext('canvas',this);
? ? ? /**繪制背景色**/
? ? ? ctx.fillStyle = randomColor(180, 240); //顏色若太深可能導致看不清
? ? ? ctx.fillRect(0, 0, that.data.width, that.data.height)
? ? ? /**繪制文字**/
? ? ? var arr;
? ? ? var text = '';
? ? ? var str = 'ABCEFGHJKLMNPQRSTWXY123456789';
? ? ? for (var i = 0; i < that.data.count; i++) {
? ? ? ? var txt = str[randomNum(0, str.length)];
? ? ? ? ctx.fillStyle = randomColor(50, 160); //隨機生成字體顏色
? ? ? ? ctx.font = randomNum(20, 26) + 'px SimHei'; //隨機生成字體大小
? ? ? ? var x = 10 + i * 20;
? ? ? ? var y = randomNum(25, 30);
? ? ? ? var deg = randomNum(-30, 30);
? ? ? ? //修改坐標原點和旋轉(zhuǎn)角度
? ? ? ? ctx.translate(x, y);
? ? ? ? ctx.rotate(deg * Math.PI / 180);
? ? ? ? ctx.fillText(txt, 5, 0);
? ? ? ? text = text + txt;
? ? ? ? //恢復坐標原點和旋轉(zhuǎn)角度
? ? ? ? ctx.rotate(-deg * Math.PI / 180);
? ? ? ? ctx.translate(-x, -y);
? ? ? }
? ? ? /**繪制干擾線**/
? ? ? for (var i = 0; i < 4; i++) {
? ? ? ? ctx.strokeStyle = randomColor(40, 180);
? ? ? ? ctx.beginPath();
? ? ? ? ctx.moveTo(randomNum(0, that.data.width), randomNum(0, that.data.height));
? ? ? ? ctx.lineTo(randomNum(0, that.data.width), randomNum(0, that.data.height));
? ? ? ? ctx.stroke();
? ? ? }
? ? ? /**繪制干擾點**/
? ? ? for (var i = 0; i < 20; i++) {
? ? ? ? ctx.fillStyle = randomColor(0, 255);
? ? ? ? ctx.beginPath();
? ? ? ? ctx.arc(randomNum(0, that.data.width), randomNum(0, that.data.height), 1, 0, 2 * Math.PI);
? ? ? ? ctx.fill();
? ? ? }
? ? ? ctx.draw(false, function() {
? ? ? ? console.log(text)
? ? ? ? that.triggerEvent('result', { text });
? ? ? ? that.setData({
? ? ? ? ? text: text,
? ? ? ? })
? ? ? })
? ? }
? }
})
function randomNum(min, max) {
? return Math.floor(Math.random() * (max - min) + min);
}
/**生成一個隨機色**/
function randomColor(min, max) {
? var r = randomNum(min, max);
? var g = randomNum(min, max);
? var b = randomNum(min, max);
? return "rgb(" + r + "," + g + "," + b + ")";
}verification-puzzle.json
{
? "component": true,
? "usingComponents": {}
}verification-puzzle.wxml
<canvas style="width:{{width}}px;height:{{height}}px" canvas-id="canvas" bindtap='crash'></canvas>二、在index頁面使用
index.wxml
<verification-code id="verification"></verification-code> <view bindtap="crash">刷新</view>
index.js
// pages/test/test/test.js
Page({
? /**
? ?* 頁面的初始數(shù)據(jù)
? ?*/
? data: {},
? crash() {
? ? this.verification.crash()
? },
? /**
? ?* 生命周期函數(shù)--監(jiān)聽頁面加載
? ?*/
? onLoad: function (options) {
??
? },
? /**
? ?* 生命周期函數(shù)--監(jiān)聽頁面初次渲染完成
? ?*/
? onReady: function () {
?? ?this.verification = this.selectComponent('#verification')
? },
? /**
? ?* 生命周期函數(shù)--監(jiān)聽頁面顯示
? ?*/
? onShow: function () {
? },
? /**
? ?* 生命周期函數(shù)--監(jiān)聽頁面隱藏
? ?*/
? onHide: function () {
? },
? /**
? ?* 生命周期函數(shù)--監(jiān)聽頁面卸載
? ?*/
? onUnload: function () {
? },
? /**
? ?* 頁面相關(guān)事件處理函數(shù)--監(jiān)聽用戶下拉動作
? ?*/
? onPullDownRefresh: function () {
? },
? /**
? ?* 頁面上拉觸底事件的處理函數(shù)
? ?*/
? onReachBottom: function () {
? },
? /**
? ?* 用戶點擊右上角分享
? ?*/
? onShareAppMessage: function () {
? }
})index.json
{
? "usingComponents": {
? ?? ?"verification-code": "/components/verification-code/verification-code"
?? ?}
}以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
JavaScript null和undefined區(qū)別分析
在JavaScript開發(fā)中,被人問到:null與undefined到底有啥區(qū)別?2009-10-10
解決layui-table單元格設(shè)置為百分比在ie8下不能自適應(yīng)的問題
今天小編就為大家分享一篇解決layui-table單元格設(shè)置為百分比在ie8下不能自適應(yīng)的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-09-09
javascript中JSON對象與JSON字符串相互轉(zhuǎn)換實例
這篇文章主要介紹了javascript中JSON對象與JSON字符串相互轉(zhuǎn)換,實例分析了json對象與字符串常用的幾種轉(zhuǎn)換技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-07-07
通過JavaScript腳本復制網(wǎng)頁上的一個表格
通過JavaScript腳本復制網(wǎng)頁上的一個表格...2006-07-07
javascript模擬select,jselect的方法實現(xiàn)
由于主流瀏覽器對select元素渲染不同,所以在每種瀏覽器下顯示也不一樣,最主要的是默認情況下UI太粗糙,即使通過css加以美化也不能達到很美觀的效果2012-11-11
JavaScript立即執(zhí)行函數(shù)與函數(shù)劫持的作用
IIFE全拼Imdiately Invoked Function Expression,是一個在定義的時候就立即執(zhí)行的JavaScript函數(shù),這篇文章主要給大家介紹了關(guān)于Javascript立即執(zhí)行函數(shù)的相關(guān)資料,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考下2023-01-01

