identify

这是一个vue的插件,使用canvas来生成图形验证码。

具体参数如下:

identify.vue组件(主要用于定义参数和方法)

<template><div class="s-canvas"><canvas id="s-canvas" :width="contentWidth" :height="contentHeight"></canvas></div>
</template>
<script>export default{name: 'SIdentify',props: {identifyCode: {type: String,default: '1234'},fontSizeMin: {type: Number,default: 16},fontSizeMax: {type: Number,default: 40},backgroundColorMin: {type: Number,default: 180},backgroundColorMax: {type: Number,default: 240},colorMin: {type: Number,default: 50},colorMax: {type: Number,default: 160},lineColorMin: {type: Number,default: 40},lineColorMax: {type: Number,default: 180},dotColorMin: {type: Number,default: 0},dotColorMax: {type: Number,default: 255},contentWidth: {type: Number,default: 112},contentHeight: {type: Number,default: 38}},methods: {// 生成一个随机数
      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 () {let canvas = document.getElementById('s-canvas')let ctx = canvas.getContext('2d')ctx.textBaseline = 'bottom'// 绘制背景ctx.fillStyle = this.randomColor(this.backgroundColorMin, this.backgroundColorMax)ctx.fillRect(0, 0, this.contentWidth, this.contentHeight)// 绘制文字for (let i = 0; i < this.identifyCode.length; i++) {this.drawText(ctx, this.identifyCode[i], i)}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'let x = (i + 1) * (this.contentWidth / (this.identifyCode.length + 1))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 < 8; i++) {ctx.strokeStyle = this.randomColor(this.lineColorMin, this.lineColorMax)ctx.beginPath()ctx.moveTo(this.randomNum(0, this.contentWidth), this.randomNum(0, this.contentHeight))ctx.lineTo(this.randomNum(0, this.contentWidth), this.randomNum(0, this.contentHeight))ctx.stroke()}},drawDot (ctx) {// 绘制干扰点for (let i = 0; i < 100; i++) {ctx.fillStyle = this.randomColor(0, 255)ctx.beginPath()ctx.arc(this.randomNum(0, this.contentWidth), this.randomNum(0, this.contentHeight), 1, 0, 2 * Math.PI)ctx.fill()}}},watch: {identifyCode () {this.drawPic()}},mounted () {this.drawPic()}}
</script>

login.vue(在页面进行使用):

<template><div class="code" @click="refreshCode"><s-identify :identifyCode="identifyCode"></s-identify></div>
</template><script>import SIdentify    from '@/components/public/identify/identify'
export default {name: "codetest",

  components: {
    Footer,
    SIdentify
  },

  data() {return {identifyCodes: "1234567890",identifyCode: ""};},mounted() {this.identifyCode = "";this.makeCode(this.identifyCodes, 4);},methods: {randomNum(min, max) {return Math.floor(Math.random() * (max - min) + min);},refreshCode() {this.identifyCode = "";this.makeCode(this.identifyCodes, 4);},makeCode(o, l) {for (let i = 0; i < l; i++) {this.identifyCode += this.identifyCodes[this.randomNum(0, this.identifyCodes.length)];}console.log(this.identifyCode);}}
};
</script><style>
.code {margin: 400px auto;width: 114px;height: 40px;border: 1px solid red;
}
</style>

注意图片验证码的宽高是由参数 contentWidth 和 contentHeight 决定的,所以更改宽高的时候,直接在声明的参数中进行更改就可。

测试效果:

转载于:https://www.cnblogs.com/haonanZhang/p/9396087.html

vue系列---identify(生成图片验证码)插件相关推荐

  1. vue中identify(生成图片验证码)插件的配置和使用

    效果图 1.在vue项目的 src/ components文件夹下新建 identify.vue 文件,写入如下代码 <template><div class="s-can ...

  2. VUE系列-Vue中组件的应用(三)

    ​ 大家好,我是前端岚枫,一枚二线城市的程序媛,上篇主要跟分享了<VUE系列-Vue核心应用(二)>,今天主要跟大家分享我整理的vue的组件相关的,主要包括组件声明.组件种类.组件间的几种 ...

  3. 从零开发一款轻量级滑动验证码插件(深度复盘)

    关注并将「趣谈前端」设为星标 每天定时分享技术干货/优秀开源/技术思维 github地址: https://github.com/MrXujiang/react-slider-vertify 之前一直 ...

  4. vue开源项目(各大插件,gitup源码)

    vue开源项目(各大插件,gitup源码) 目录 UI组件 开发框架 实用库 服务端 辅助工具 应用实例 Demo示例 UI组件 element★31142 - 饿了么出品的Vue2的web UI工具 ...

  5. 【前端vue——系列6】vue连接摄像头并实现摄像头暂停,计时,截图到本地等功能

    系列文章总结 [前端vue--系列1]vue的路由 [前端vue--系列2]vue中的data是函数而不是对象与computed的实现原理 [前端vue--系列3]vue框架的优缺点:vue实现双向绑 ...

  6. 极验接入教程纯html,VUE接入 腾讯验证码,极验验证码

    VUE接入 腾讯验证码,极验验证码 安装 $ npm install vue-social-captcha -S 使用 在 main.js 文件中引入插件并注册 # main.js import ca ...

  7. CheckCode.js 前端验证码插件

    CheckCode.js 前端验证码插件 效果截图 插件使用方法 CheckCode.js 本插件的参考示例 效果截图 插件使用方法 // 在html页面引入CheckCode.js <scri ...

  8. 开源Vue表格组件,表格插件源码

    开源Vue表格组件,表格插件源码 前言: 关于html里面原生的table,通常满足不了程序员的要求.所以开发了一款表格插件,其功 能有: 1 导入json格式数据后,自动填充表格.表格长宽自适应.排 ...

  9. 实战:vue项目中导入swiper插件

    版本选择 swiper是个常用的插件,现在已经迭代到了第四代:swiper4. 常用的版本是swiper3和swiper4,我选择的是swiper3. 安装 安装swiper3的最新版本3.4.2: ...

  10. vue分页+spring boot +分页插件pagehelper

    vue分页+spring boot +分页插件pagehelper https://blog.csdn.net/baidu_38603246/article/details/98854013

最新文章

  1. [CTO札记]高效能辅导(Coach)转摘
  2. 连环清洁工之特殊任务--java资源如何关闭?
  3. 浙大通讯与计算机网络离线作业,浙大2015年 通信与计算机网络离线作业
  4. wxWidgets:wxRichTextCtrl类用法
  5. pringMVC“Ambiguous mapping found. Cannot map 'XXXController' bean method”解决方法
  6. 定时器timerfd
  7. Windows环境下多个tomcat启动,CATALINA_HOME配置(大坑)
  8. Android和Linux kernel发展史
  9. hackerrank初级篇之Plus Minus
  10. pano2vr输出的HTML手机可以看吗,Pano2VR怎么导出手机可看, Pano2VR导出手机查看教程...
  11. T9 PDF如何转存为高清图片
  12. 仿网易评论盖楼的留言板插件demo
  13. Qt自定义Combobox实现列表上拉展示
  14. uboot利用uEnv.txt文件实现灵活功能(加载PL侧bit,修改uenvcmd,配置bootargs,配置bootm,配置bootz)
  15. 免费公共DNS的IP地址
  16. npm 和 cnpm 区别
  17. Linux 查看文件和文件夹大小,隐藏文件的大小方法总结
  18. 企业微信机器人发送消息
  19. 使用Appium爬取淘宝App数据
  20. 月薪2w+的测试,到底强在哪里?

热门文章

  1. 普式变换,仿射变换,人脸对齐
  2. 2021-06-14 七大参数,四种拒绝策略
  3. ubuntu命令行打开vscode-insider_系统小技巧:迁移Wubi安装的ubuntu到Windows 10
  4. 复选框选中的值怎么传到mysql_PHP中如何将复选框选中的内容提交到mysql的一个字段中...
  5. 怎么判断噎到没噎到_怎么判定股市是在走牛市还是走熊市呢?两种最简单的判定方法...
  6. 数据结构和算法——八种常用的排序算法----选择排序
  7. flashcs3java_Flash CS3组件开发图文教程
  8. php无法写入json,php json解析不出来怎么办
  9. 十二、Object类
  10. Oracle 视图、序列、索引、游标、触发器、事务