当前位置:网站首页>微信小程序 图片验证码展示
微信小程序 图片验证码展示
2022-06-23 22:18:00 【坚持与努力的成果】
废话不多说,上代码
一.html页面
<canvas canvas-id="canvas" bindtap='change' style="width:90px;height: 30px;"></canvas>二,js逻辑
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);
},
/**生成一个随机色**/
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 + ")";
},
/**绘制验证码图片**/
drawPic(that) {
ctx = wx.createCanvasContext('canvas');
/**绘制背景色**/
ctx.fillStyle = this.randomColor(180, 240); //颜色若太深可能导致看不清
ctx.fillRect(0, 0, 90, 28)
/**绘制文字**/
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); //随机生成字体颜色
ctx.font = this.randomNum(20, 26) + 'px SimHei'; //随机生成字体大小
var x = 5 + i * 20;
var y = this.randomNum(20, 25);
var deg = this.randomNum(-20, 20);
//修改坐标原点和旋转角度
ctx.translate(x, y);
ctx.rotate(deg * Math.PI / 180);
ctx.fillText(txt, 5, 0);
text = text + txt;
//恢复坐标原点和旋转角度
ctx.rotate(-deg * Math.PI / 180);
ctx.translate(-x, -y);
}
/**绘制干扰线**/
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();
}
/**绘制干扰点**/
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
})
});
}
})
边栏推荐
- Kotlin coroutine asynchronous flow
- To ensure the safety of groups with special difficulties, Guangzhou Civil Affairs made every effort to do a good job in the three prevention work
- 【HackTheBox】 meow
- 格林公式挖洞法中内曲线顺时针的直观解释
- Avoid confusion when switching between arouter components
- What kind of automated test is used for H5 mobile terminal
- 文言文能编程???
- iNFTnews | 创造者经济的未来在Web3世界中该去向何处?
- 思考(八十七):协议加密与压缩
- Postman返回值中文乱码????
猜你喜欢

STM32-------外部中断

MySQL索引底层为什么用B+树?看完这篇文章,轻松应对面试。

6月25日PMP考试敏捷怎么考?替你分忧解难

一个人竟然撸了一个网易云音乐云村

19 MySQL optimizations commonly used in projects

一个人竟然撸了一个微博 APP

Develop synergy and efficiently manage | community essay solicitation

“山大地纬杯”第十二届山东省ICPC大学生程序设计竞赛

Solve the problem that slf4j logs are not printed

老龄化下背景下,综合能效管理平台为医院保驾护航
随机推荐
Math. Max() method obtains the maximum value in the array and returns Nan problem analysis
Install using snap in opencloudos NET 6
laravel之任务队列
一个人竟然撸了一个微博 APP
1004. number of maximum consecutive 1 III ●●
Grpc security -2: fast implementation of server-side JWT authentication
三维向量场中的通量
ORB_ Slam3 environment setup and demo demonstration
“山大地纬杯”第十二届山东省ICPC大学生程序设计竞赛
2022 Shandong Health Expo, Jinan International Health Industry Expo, China Nutrition and Health Exhibition
图论(树的直径)
STM32 ------ external interrupt
工作中一些常用的工具函数
2022 information security engineer examination knowledge point: access control
PLC数据操作系列之构造不同应用场景的缓存栈FIFO(算法详解)
Acrel-3000WEB电能管理系统在都巴高速的应用
7、STM32——LCD
Kotlin set list, set, map operation summary
【Proteus仿真】T6963C驱动PG12864示例(带中英文显示)
为实现“双碳”目标,应如何实现节能、合理的照明管控