vue最常用的图片验证码(canvas绘图)

使用数字和字母生成随机的四位数验证码,代码注释很清楚。

<template>
  <div class="img-canvas">
    <canvas
      id="canvas"
      :width="width"
      :height="height"
      @click="handleDraw"
    ></canvas>
  </div>
</template>

<script>
export default {
  data() {
    return {
      allTexts: "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890", // 字符串
      width: 120,
      height: 40,
      imgCode: "",
    };
  },
  mounted() {
    // 初始化绘制图片验证码
    this.imgCode = this.draw();
  },
  methods: {
    //todo 点击图片刷新
    handleDraw() {
      this.imgCode = this.draw();
    },
    //todo 产生随机数
    randomNum(min, max) {
      return parseInt(Math.random() * (max - min) + min);
    },
    //todo 产生随机颜色
    randomColor(min, max) {
      const r = this.randomNum(min, max);
      const g = this.randomNum(min, max);
      const b = this.randomNum(min, max);
      return `rgb(${r},${g},${b})`;
    },
    //todo 绘图
    draw() {
        const canvas = document.getElementById("canvas");
        const ctx = canvas.getContext("2d");
        //!! 1.填充背景颜色,背景颜色要浅一点
        ctx.fillStyle = this.randomColor(180, 230);
        // 填充的位置
        ctx.fillRect(0, 0, this.width, this.height);
        let imgCode = "";
        //!! 2.随机产生字符串,并且随机旋转
        for (let i = 0; i < 4; i++) {
          // 随机的四个字
          const text = this.allTexts[this.randomNum(0, this.allTexts.length)];
          imgCode += text;
          // 随机的字体大小
          const fontSize = this.randomNum(18, 40);
          // 字体随机的旋转角度
          const deg = this.randomNum(-30, 30);
          ctx.font = fontSize + "px Arial";
          ctx.textBaseline = "top";
          ctx.fillStyle = this.randomColor(80, 150);
          ctx.save();
          ctx.translate(30 * i + 15, 15);
          ctx.rotate((deg * Math.PI) / 180);
          ctx.fillText(text, -15 + 5, -15);
          ctx.restore();
        }
        //!! 3.随机产生5条干扰线
        for (let i = 0; i < 5; i++) {
          ctx.beginPath();
          ctx.moveTo(
            this.randomNum(0, this.width),
            this.randomNum(0, this.height)
          );
          ctx.lineTo(
            this.randomNum(0, this.width),
            this.randomNum(0, this.height)
          );
          ctx.strokeStyle = this.randomColor(180, 230);
          ctx.closePath();
          ctx.stroke();
        }
        //!! 4.随机产生40个干扰的小点
        for (let i = 0; i < 40; i++) {
          ctx.beginPath();
          ctx.arc(
            this.randomNum(0, this.width),
            this.randomNum(0, this.height),
            1,
            0,
            2 * Math.PI
          );
          ctx.closePath();
          ctx.fillStyle = this.randomColor(150, 200);
          ctx.fill();
        }
        return imgCode;
    },
  },
};
</script>

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Vue 中实现图片验证码的过程与普通的实现方式类似,需要后端生成验证码图片并返回给前端,前端将图片显示在页面上并在提交时将用户输入的验证码与后端生成的验证码进行比对。 以下是一个简单的实现过程: 1. 后端生成验证码图片并返回给前端,可以使用第三方库如`captcha`或自行编写生成函数。 2. 前端使用`<img>`标签显示验证码图片。可以使用`src`属性将后端返回的验证码图片地址设置为`<img>`标签的`src`属性值,也可以将图片的二进制数据作为 URL 数据(data URI)嵌入`<img>`标签的`src`属性中。 3. 用户输入验证码,将用户输入的验证码与后端生成的验证码进行比对。 下面是一个示例代码: ```html <template> <div> <img :src="captchaUrl" alt="验证码"> <input v-model="captchaInput" type="text" placeholder="请输入验证码"> <button @click="submit">提交</button> </div> </template> <script> export default { data() { return { captchaUrl: '', // 验证码图片地址 captchaInput: '', // 用户输入的验证码 } }, mounted() { this.refreshCaptcha() }, methods: { // 刷新验证码 refreshCaptcha() { this.captchaUrl = `/api/captcha?_=${Date.now()}` }, // 提交表单 submit() { // 将用户输入的验证码与后端生成的验证码进行比对 // 如果验证码正确,则提交表单 }, }, } </script> ``` 在上面的示例代码中,`captchaUrl`是图片的地址,`captchaInput`是用户输入的验证码。`refreshCaptcha`方法用于刷新验证码,`submit`方法用于提交表单时验证验证码是否正确。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

liuwenjie_

感谢打赏,问题留言~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值