<canvas bindtouchend="onTap" style="width:{ {cvs.width}}px;height:{ {cvs.height}}px;" canvas-id="canvas"></canvas>
//index.js
//獲取應用實例
import Mcaptcha from "../../utils/mcaptcha.js";
function getRanNum() {
var result = [];
for (var i = 0; i < 4; i++) {
var ranNum = Math.ceil(Math.random() * 25); //生成一個0到25的數(shù)字
//大寫字母'A'的ASCII是65,A~Z的ASCII碼就是65 + 0~25;然后調(diào)用String.fromCharCode()傳入ASCII值返回相應的字符并push進數(shù)組里
result.push(String.fromCharCode(65 + ranNum));
}
return result.join("");
}
Page({
data: {
cvs: {
width: 80,
height: 35
},
},
onLoad: function () {
const temp = getRanNum();
this.setData({
tuyzm: temp
});
this.mcaptcha = new Mcaptcha({
el: "canvas",
width: this.data.cvs.width,
height: this.data.cvs.height,
code: temp
})
},
//刷新圖形驗證碼
onTap() {
const temp = getRanNum();
this.setData({
tuyzm: temp
});
this.mcaptcha.refresh(temp);
},
})
參考:
canvas代碼>mcaptcha
隨機生成字母代碼
demo