//生成随机颜色RGB分量
export function randomColor(min, max) {
  let _r = randomNum(min, max);
  let _g = randomNum(min, max);
  let _b = randomNum(min, max);
  return "rgb(" + _r + "," + _g + "," + _b + ")";
}

//生成随机数
export function randomNum(min, max) {
  return Math.floor(Math.random() * (max - min) + min);
}

export function drawPic() {
  //获取到元素canvas
  let $canvas = document.getElementById("validCode");
  //  let _str = "0123456789";//设置随机数库
  let _str = "2345789ABCDEFGHJKLMNPQRSTUVWXYZacdefhijklmnprstuvwxzy";//去掉容易混淆的字符：0、1、6、I、O、b、g、o、q
  let _picTxt = "";//随机数
  let _num = 4;//4个随机数字
  let _width = $canvas.width;
  let _height = $canvas.height;
  let ctx = $canvas.getContext("2d");//获取 context 对象
  ctx.textBaseline = "bottom";//文字上下对齐方式--底部对齐
  //ctx.fillStyle = randomColor(180, 240);//填充画布颜色
  ctx.fillStyle = "rgb(255,255,255)";
  //console.log("canvas:" + _width + "*" + _height);
  ctx.fillRect(0, 0, _width, _height);//填充矩形--画画
  for (let i = 0; i < _num; i++) {
    let x = (_width - 10) / _num * i + 25;
    let y = randomNum(_height / 2, _height);
    //console.log("x:" + x + " y:" + y);
    let deg = randomNum(-45, 45);
    let txt = _str[randomNum(0, _str.length)];
    _picTxt += txt;//获取一个随机数
    ctx.fillStyle = randomColor(10, 100);//填充随机颜色
    ctx.font = randomNum(_height/2.5, _height/1.5) + "px SimHei";//设置随机数大小，字体为SimHei
    //ctx.font = "40px";
    ctx.translate(x, y);//将当前xy坐标作为原始坐标
    ctx.rotate(deg * Math.PI / 180);//旋转随机角度
    ctx.fillText(txt, 0, 0);//绘制填色的文本
    ctx.rotate(-deg * Math.PI / 180);
    ctx.translate(-x, -y);
  }
  for (let i = 0; i < _num; i++) {
    //定义笔触颜色
    ctx.strokeStyle = randomColor(90, 180);
    ctx.beginPath();
    //随机划线--4条路径
    ctx.moveTo(randomNum(0, _width), randomNum(0, _height));
    ctx.lineTo(randomNum(0, _width), randomNum(0, _height));
    ctx.stroke();
  }
  for (let i = 0; i < _num * 10; i++) {
    ctx.fillStyle = randomColor(0, 255);
    ctx.beginPath();
    //随机画原，填充颜色
    ctx.arc(randomNum(0, _width), randomNum(0, _height), 1, 0, 2 * Math.PI);
    ctx.fill();
  }
  //console.log("验证码：" + _picTxt);
  // checkCodes = _picTxt;
  // $("#checkCodes").val(_picTxt);
  return _picTxt;//返回随机数字符串
}