其他分享
首页 > 其他分享> > vue图片验证码

vue图片验证码

作者:互联网

先看效果

1.验证码组件

<template>
  <div class="s-canvas">
    <canvas id="s-canvas" :width="contentWidth" :height="contentHeight" @click="Refresh"></canvas>
  </div>
</template>
<script>
export default {
  name: 'SIdentify',
  props: {
    identifyCode: {
      type: String,
      default: ["a", "v", "q", "p", "y", "b", "c", "d", "e", "f", "h", "x", "z", "t", "j", "k", "l", "C", "D", "E", "F", "G", "H", "T", "Y", "U", "I", "O", "K", "L", "M", "N", "S", "A", "B"]
    },

  },
  data() {
    return {
      //定义容器接收验证码
      nums: '',
      fontSizeMin: 45,
      fontSizeMax: 35,
      backgroundColorMin: 180,
      backgroundColorMax: 240,
      colorMin: 50,
      colorMax: 160,
      lineColorMin: 40,
      lineColorMax: 180,
      dotColorMin: 0,
      dotColorMax: 255,
      contentWidth: 120,
      contentHeight: 40
    }
  },
  methods: {
    //组件通信
    getData() {
      this.$emit("getData", this.nums);
    },
    // 生成一个随机数
    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() {
      var arr = [] //定义一个数组用来接收产生的随机数

      let canvas = document.getElementById('s-canvas')
      let ctx = canvas.getContext('2d')
      // ctx.textBaseline = 'bottom'
      // 绘制背景
      ctx.fillStyle = this.randomColor(this.backgroundColorMin, this.backgroundColorMax) //图形填充颜色设置
      ctx.strokeStyle = this.randomColor(this.backgroundColorMin, this.backgroundColorMax) //图形轮廓的颜色设置
      ctx.fillRect(0, 0, this.contentWidth, this.contentHeight) //绘制一个填充的矩形 0 0 width height x起点 y起点  宽 高
      ctx.strokeRect(0, 0, this.contentWidth, this.contentHeight) // 绘制一个矩形边框 0 0 width height x起点 y起点  宽 高
      // ctx.clearRect(50,0,this.contentWidth,this.contentHeight)  //清除指定矩形区域,清除部分完全透明
      // 绘制文字
      for (let i = 0; i < 4; i++) {
        var index = Math.floor(Math.random() * 34);//随机索引值
        // var txt = identifyCode[index];
        this.drawText(ctx, this.identifyCode[index], i)
        arr[i] = this.identifyCode[index] //接收产生的随机数
      }
      this.nums = arr[0] + arr[1] + arr[2] + arr[3] //将产生的验证码放入num
      this.getData();
      this.drawLine(ctx)
      this.drawDot(ctx)
    },
    drawText(ctx, txt, i) {
      ctx.fillStyle = this.randomColor(this.colorMin, this.colorMax)
      ctx.font = this.randomNum(this.fontSizeMin, this.fontSizeMax) + 'px SimHei' //字体大小
      ctx.textBaseline = 'alphabetic' //基线对齐
      let x = 20 + i * 25;
      // let y = 20 + 10 * Math.random();
      let y = this.randomNum(this.fontSizeMax, this.contentHeight - 5)
      var deg = this.randomNum(-45, 45)
      // 修改坐标原点和旋转角度
      ctx.translate(x, y)//移动不同位置  参数偏移量
      ctx.rotate(deg * Math.PI / 180) //旋转 参数角度
      ctx.fillText(txt, 0, 0)
      // 恢复坐标原点和旋转角度
      ctx.rotate(-deg * Math.PI / 180)
      ctx.translate(-x, -y)
    },
    drawLine(ctx) {
      // 绘制干扰线
      for (let i = 0; i < 10; i++) {
        ctx.strokeStyle = this.randomColor(this.lineColorMin, this.lineColorMax)
        ctx.beginPath() //新建一条路径
        ctx.moveTo(this.randomNum(0, this.contentWidth), this.randomNum(0, this.contentHeight)) //设置起点x,y
        ctx.lineTo(this.randomNum(0, this.contentWidth), this.randomNum(0, this.contentHeight)) //绘制直线 x,y 一条当前位置到x,y点的直线
        ctx.stroke() // 通过线条绘制图形轮廓
        // ctx.closePath() //结束闭合路径
      }
    },
    drawDot(ctx) {
      // 绘制干扰点
      for (let i = 0; i < 120; i++) {
        ctx.fillStyle = this.randomColor(0, 255)
        ctx.beginPath()
        // 绘制圆弧或圆,x,y,radius,startAngle,endAngle,anticlockwise // x,y 圆心点,radius 半径,从startAngle开始到endAngle结束
        ctx.arc(this.randomNum(0, this.contentWidth), this.randomNum(0, this.contentHeight), 1, 0, 2 * Math.PI)
        ctx.fill() //通过填充路径的内容区域生成实心的图形。
      }
    },
    Refresh() {

      this.drawPic();

    }
  },

  watch: {
    identifyCode() {
      this.drawPic()
    }
  },
  mounted() {
    this.drawPic()
  }
}
</script>


2.登录组件 

	
<template>	
<div>
  <el-form-item prop="sidentify">
     <el-col :span="14">
     <el-input placeholder="请输入验证码" type="text" v-model="yzm" ></el-input>
     </el-col>
     <el-col :span="8">
     <v-sidentify @getData="getData"></v-sidentify>
     </el-col>
  </el-form-item>
</div>
</template>

<script>
	
	  import Sidentify from './yzm.vue'  //**引入验证码组件**
	export default {
		name: 'Login',
		data() {
			return {
				ruleForm: {
					usr_name: '',
					usr_password: '',
				}
			}
		},
		 components: {
            'v-sidentify':Sidentify ,
			
        },
     methods: {
        getData(date){
      
        },
     }
</script>

 

标签:vue,ctx,验证码,randomNum,let,contentWidth,contentHeight,Math,图片
来源: https://blog.csdn.net/weixin_58670730/article/details/122820248