js随机验证码

function randomColor() {
var colorValue = "0,1,2,3,4,5,6,7,8,9,a,b,c,d,e,f";
var colorArray = colorValue.split(",");
var color = "#";
for (var i = 0; i < 6; i++) {
color += colorArray[Math.floor(Math.random() * 16)];
}
return color;
}

function getRandomNum(n) {
return parseInt(Math.random() * n);
}

function getVerification() {
var ctx = document.getElementById("canvas").getContext("2d");
ctx.clearRect(0, 0, 400, 400);
// 设置字体
ctx.font = "128px bold 黑体";
// 设置垂直对齐方式
ctx.textBaseline = "top";
// 设置颜色
ctx.fillStyle = randomColor();
// 绘制文字(参数:要写的字,x坐标,y坐标)
ctx.fillText(getRandomNum(10), 0, getRandomNum(50));
ctx.fillStyle = randomColor();
ctx.fillText(getRandomNum(10), 50, getRandomNum(50));
ctx.fillStyle = randomColor();
ctx.fillText(getRandomNum(10), 100, getRandomNum(50));
ctx.fillStyle = randomColor();
ctx.fillText(getRandomNum(10), 150, getRandomNum(50));
}
getVerification();

posted @ 2020-08-30 18:34  文采呱呱  阅读(118)  评论(0编辑  收藏  举报