Identify.vue 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <template>
  2. <canvas id="s-canvas" :width="contentWidth" :height="contentHeight"></canvas>
  3. </template>
  4. <script>
  5. export default{
  6. name: 'SIdentify',
  7. props: {
  8. identifyCode: {
  9. type: String,
  10. default: '1234'
  11. },
  12. fontSizeMin: {
  13. type: Number,
  14. default: 20
  15. },
  16. fontSizeMax: {
  17. type: Number,
  18. default: 35
  19. },
  20. backgroundColorMin: {
  21. type: Number,
  22. default: 180
  23. },
  24. backgroundColorMax: {
  25. type: Number,
  26. default: 240
  27. },
  28. colorMin: {
  29. type: Number,
  30. default: 50
  31. },
  32. colorMax: {
  33. type: Number,
  34. default: 160
  35. },
  36. lineColorMin: {
  37. type: Number,
  38. default: 40
  39. },
  40. lineColorMax: {
  41. type: Number,
  42. default: 180
  43. },
  44. dotColorMin: {
  45. type: Number,
  46. default: 0
  47. },
  48. dotColorMax: {
  49. type: Number,
  50. default: 255
  51. },
  52. contentWidth: {
  53. type: Number,
  54. default: 90
  55. },
  56. contentHeight: {
  57. type: Number,
  58. default: 38
  59. }
  60. },
  61. methods: {
  62. // 生成一个随机数
  63. randomNum (min, max) {
  64. return Math.floor(Math.random() * (max - min) + min)
  65. },
  66. // 生成一个随机的颜色
  67. randomColor (min, max) {
  68. let r = this.randomNum(min, max)
  69. let g = this.randomNum(min, max)
  70. let b = this.randomNum(min, max)
  71. return 'rgb(' + r + ',' + g + ',' + b + ')'
  72. },
  73. drawPic () {
  74. let canvas = document.getElementById('s-canvas')
  75. let ctx = canvas.getContext('2d')
  76. ctx.textBaseline = 'bottom'
  77. // 绘制背景
  78. ctx.fillStyle = this.randomColor(this.backgroundColorMin, this.backgroundColorMax)
  79. ctx.fillRect(0, 0, this.contentWidth, this.contentHeight)
  80. // 绘制文字
  81. for (let i = 0; i < this.identifyCode.length; i++) {
  82. this.drawText(ctx, this.identifyCode[i], i)
  83. }
  84. this.drawLine(ctx)
  85. this.drawDot(ctx)
  86. },
  87. drawText (ctx, txt, i) {
  88. ctx.fillStyle = this.randomColor(this.colorMin, this.colorMax)
  89. ctx.font = this.randomNum(this.fontSizeMin, this.fontSizeMax) + 'px SimHei'
  90. let x = (i + 1) * (this.contentWidth / (this.identifyCode.length + 1))
  91. let y = this.randomNum(this.fontSizeMax, this.contentHeight - 5)
  92. var deg = this.randomNum(-45, 45)
  93. // 修改坐标原点和旋转角度
  94. ctx.translate(x, y)
  95. ctx.rotate(deg * Math.PI / 180)
  96. ctx.fillText(txt, 0, 0)
  97. // 恢复坐标原点和旋转角度
  98. ctx.rotate(-deg * Math.PI / 180)
  99. ctx.translate(-x, -y)
  100. },
  101. drawLine (ctx) {
  102. // 绘制干扰线
  103. for (let i = 0; i < 3; i++) {
  104. ctx.strokeStyle = this.randomColor(this.lineColorMin, this.lineColorMax)
  105. ctx.beginPath()
  106. ctx.moveTo(this.randomNum(0, this.contentWidth), this.randomNum(0, this.contentHeight))
  107. ctx.lineTo(this.randomNum(0, this.contentWidth), this.randomNum(0, this.contentHeight))
  108. ctx.stroke()
  109. }
  110. },
  111. drawDot (ctx) {
  112. // 绘制干扰点
  113. for (let i = 0; i < 30; i++) {
  114. ctx.fillStyle = this.randomColor(0, 255)
  115. ctx.beginPath()
  116. ctx.arc(this.randomNum(0, this.contentWidth), this.randomNum(0, this.contentHeight), 1, 0, 2 * Math.PI)
  117. ctx.fill()
  118. }
  119. }
  120. },
  121. watch: {
  122. identifyCode () {
  123. this.drawPic()
  124. }
  125. },
  126. mounted () {
  127. this.drawPic()
  128. },
  129. updated () {
  130. this.drawPic()
  131. }
  132. }
  133. </script>
  134. <style scoped>
  135. canvas{
  136. cursor: pointer;
  137. }
  138. </style>