当前位置:网站首页>Wechat applet picture verification code display
Wechat applet picture verification code display
2022-06-23 23:52:00 【Results of persistence and effort】
I don't say much nonsense , Code up
One .html page
<canvas canvas-id="canvas" bindtap='change' style="width:90px;height: 30px;"></canvas>Two ,js Logic
var app = getApp();
var baseUrl = getApp().baseUrl;
var ctx;
Page({
data:{
text:""
},
onLoad: function(options) {
var that = this;
this.drawPic(that);
},
change: function() {
var that = this;
this.drawPic(that);
},
mobile(e) {
this.setData({
mobile: e.detail.value
})
},
randomNum(min, max) {
return Math.floor(Math.random() * (max - min) + min);
},
/** Generate a random color **/
randomColor(min, max) {
let r = this.randomNum(min, max);
let g = this.randomNum(min, max);
let b = this.randomNum(min, max);
return "rgb(" + r + "," + g + "," + b + ")";
},
/** Draw verification code picture **/
drawPic(that) {
ctx = wx.createCanvasContext('canvas');
/** Draw the background color **/
ctx.fillStyle = this.randomColor(180, 240); // If the color is too dark, it may make you can't see clearly
ctx.fillRect(0, 0, 90, 28)
/** Draw text **/
var arr;
var text = '';
var str = 'ABCEFGHJKLMNPQRSTWXY123456789';
for (var i = 0; i < 4; i++) {
var txt = str[this.randomNum(0, str.length)];
ctx.fillStyle = this.randomColor(50, 160); // Randomly generate font colors
ctx.font = this.randomNum(20, 26) + 'px SimHei'; // Randomly generate font size
var x = 5 + i * 20;
var y = this.randomNum(20, 25);
var deg = this.randomNum(-20, 20);
// Modify the origin and rotation Angle
ctx.translate(x, y);
ctx.rotate(deg * Math.PI / 180);
ctx.fillText(txt, 5, 0);
text = text + txt;
// Restore the origin of coordinates and rotation Angle
ctx.rotate(-deg * Math.PI / 180);
ctx.translate(-x, -y);
}
/** Draw interference line **/
for (var i = 0; i < 4; i++) {
ctx.strokeStyle = this.randomColor(40, 180);
ctx.beginPath();
ctx.moveTo(this.randomNum(0, 90), this.randomNum(0, 28));
ctx.lineTo(this.randomNum(0, 90), this.randomNum(0, 28));
ctx.stroke();
}
/** Plot interference point **/
for (var i = 0; i < 20; i++) {
ctx.fillStyle = this.randomColor(0, 255);
ctx.beginPath();
ctx.arc(this.randomNum(0, 90), this.randomNum(0, 28), 1, 0, 2 * Math.PI);
ctx.fill();
}
ctx.draw(false, function() {
that.setData({
text: text
})
});
}
})
边栏推荐
猜你喜欢

How to take the PMP Exam agile on June 25? Share your troubles

Revit API:明细表 ViewSchedule

微信录制视频转圈效果如何实现?

Docker deploy redis

Under the background of aging, the comprehensive energy efficiency management platform escorts hospitals
![Restore IP address [standard backtracking + standard pruning]](/img/e6/5f9d2a5af973b6c7051ed434a4b93d.png)
Restore IP address [standard backtracking + standard pruning]

Web site SSL certificate

产线工控安全有什么好的解决方案

Niuke.com: the double pointer problem of receiving rainwater

Stm32-------adc (voltage detection)
随机推荐
CPU取指到发出控制、微程序控制原理详细过程
Chrome plug-in features and case analysis of actual combat scenarios
Quelques fonctions d'outils couramment utilisées au travail
点乘和叉乘
Digital property management has become a trend. How can traditional property companies achieve digital butterfly change through transformation?
matlab实现对图像批量重命名
2018/gan:self attention generating adversarial networks
High imitation Book flag novel flutter edition, learn it
High imitation Betta app
网站ssl证书
ACM. HJ89 24点运算 ●●●
Postman return value Chinese garbled????
The input parameter is object, but it was passed as [object object] because it needs to be converted to JSON format
Facebook open source shimmer effect
Thinking (87): Protocol encryption and compression
完整开源项目之诗词吧 APP
暑假第一周
How to achieve the turning effect of wechat video recording?
Notepad++实用功能分享(正则行尾行首替换常用方法、文本比对功能等)
Golang type assertion