ログイン ページのグラフィック キャプチャ プラグイン

identify は、キャンバスを使用してグラフィック検証コードを生成するvue プラグインです。

npm 識別 --save

グローバルにインポートできます


   
   
    
    
  1. './components/page/identify'からSIdentifyをインポートします。
  2. ビュー 使用(SIdentify)

現地輸入も可能、本稿では現地紹介を利用 

 

実装されたコードは次のとおりです。

サブアセンブリ


   
   
    
    
  1. < テンプレート>
  2. < divクラス= "s-キャンバス" >
  3. < キャンバス id = "s-canvas" :width = "contentWidth" :height = "contentHeight" > < /キャンバス>
  4. < / div >
  5. < / テンプレート>
  6. < スクリプト>
  7. デフォルト エクスポート{
  8. 名前 : 'SIdentify' ,
  9. 小道具 : {
  10. 識別コード: {
  11. タイプ :文字列
  12. デフォルト : '1234'
  13. }、
  14. fontSizeMin: {
  15. タイプ :数値,
  16. デフォルト : 18
  17. }、
  18. fontSizeMax: {
  19. タイプ :数値,
  20. デフォルト : 40
  21. }、
  22. backgroundColorMin: {
  23. タイプ :数値,
  24. デフォルト : 180
  25. }、
  26. backgroundColorMax: {
  27. タイプ :数値,
  28. デフォルト : 240
  29. }、
  30. colorMin: {
  31. タイプ :数値,
  32. デフォルト : 50
  33. }、
  34. カラーマックス: {
  35. タイプ :数値,
  36. デフォルト : 160
  37. }、
  38. lineColorMin: {
  39. タイプ :数値,
  40. デフォルト : 40
  41. }、
  42. lineColorMax: {
  43. タイプ :数値,
  44. デフォルト : 180
  45. }、
  46. dotColorMin: {
  47. タイプ :数値,
  48. デフォルト : 0
  49. }、
  50. dotColorMax: {
  51. タイプ :数値,
  52. デフォルト : 255
  53. }、
  54. contentWidth: {
  55. タイプ :数値,
  56. デフォルト : 111
  57. }、
  58. コンテンツの高さ: {
  59. タイプ :数値,
  60. デフォルト : 38
  61. }
  62. }、
  63. 私の やり方: {
  64. // 乱数 を生成する
  65. randomNum(最小、最大) {
  66. return Math.floor(Math.random ( ) * (最大-最小) +最小)
  67. }、
  68. // ランダムな 色を生成
  69. randomColor(最小、最大) {
  70. let r = this.randomNum(min, max)
  71. g = this.randomNum(最小、最大)
  72. let b = this.randomNum(min, max)
  73. return 'rgb(' + r + ',' + g + ',' + b + ')'
  74. }、
  75. drawPic() {
  76. let canvas = document.getElementById( 's-canvas' )
  77. let ctx = canvas.getContext( '2d' )
  78. ctx.textBaseline = '下部'
  79. // 背景 を描く
  80. ctx.fillStyle = this.randomColor(this.backgroundColorMin, this.backgroundColorMax)
  81. ctx.fillRect( 0 , 0 , this.contentWidth, this.contentHeight)
  82. // テキスト を描画
  83. for (let i = 0 ; i < this.identifyCode. length ; i + + ) {
  84. this.drawText(ctx, this.identifyCode[i], i)
  85. }
  86. // this.drawLine (ctx) //干渉線を描画
  87. // this.drawDot (ctx) //干渉点を描画
  88. }、
  89. // テキスト を描画する
  90. drawText(ctx, txt, i) {
  91. ctx.fillStyle = this.randomColor(this.colorMin, this.colorMax)
  92. ctx.font = this.randomNum(this.fontSizeMin, this.fontSizeMax) + 'px SimHei'
  93. let x = (i + 1 ) * (this.contentWidth / (this.identifyCode. length + 1 ))
  94. let y = this.randomNum(this.fontSizeMax, this.contentHeight - 5 )
  95. var deg = this.randomNum(- 30 , 30 ) //キャラクターの回転角度 ( 45度以下が良い)
  96. //座標原点 回転角度を修正
  97. ctx.translate(x, y)
  98. ctx.rotate(deg * Math.PI / 180 )
  99. ctx.fillText(txt, 0 , 0 )
  100. //座標原点 回転角度を復元
  101. ctx.rotate(-deg * Math.PI / 180 )
  102. ctx.translate(-x, -y)
  103. }、
  104. drawLine(ctx) {
  105. // 干渉 線を引く
  106. for (let i = 0 ; i < 8 ; i + + ) {
  107. ctx.strokeStyle = this.randomColor(this.lineColorMin, this.lineColorMax)
  108. ctx.beginPath()
  109. ctx.moveTo(this.randomNum( 0 , this.contentWidth), this.randomNum( 0 , this.contentHeight))
  110. ctx.lineTo(this.randomNum( 0 , this.contentWidth), this.randomNum( 0 , this.contentHeight))
  111. ctx.ストローク()
  112. }
  113. }、
  114. drawDot(ctx) {
  115. // 干渉 点を描く
  116. for (let i = 0 ; i < 100 ; i + + ) {
  117. ctx.fillStyle = this.randomColor( 0 , 255 )
  118. ctx.beginPath()
  119. ctx.arc(this.randomNum( 0 , this.contentWidth), this.randomNum( 0 , this.contentHeight), 1 , 0 , 2 * Math.PI)
  120. ctx.fill()
  121. }
  122. }
  123. }、
  124. 見る : {
  125. 識別コード() {
  126. this.drawPic()
  127. }
  128. }、
  129. モウンテッド () {
  130. this.drawPic()
  131. }
  132. }
  133. < / スクリプト>

親コンポーネント


   
   
    
    
  1. < テンプレート>
  2. < 分割>
  3. <div> キャプチャテスト</div> _ _ _ _
  4. < div @click = "refreshCode()" class = "code" style = "cursor:pointer;" title = "クリックして確認コードを切り替える" >
  5. < s-identify :identifyCode = "identifyCode" > < / s-identify >
  6. < / div >
  7. < / div >
  8. < / テンプレート>
  9. < スクリプト>
  10. import { defineComponent } from 'vue' ;
  11. "../../src/components/Sidentify.vue"からsIdentify をインポートします
  12. // 'axios'からaxios を インポート
  13. デフォルト defineComponent({
  14. 名前 「リスト」
  15. コンポーネント : { sIdentify },
  16. データ () {
  17. 戻る {
  18. 識別コード: "" ,
  19. IdentifyCodes: [ '0' , '1' , '2' , '3' , ' 4' , '5' , '6' , '7' , '8' , '9' , 'a' , 'b' , 'c' , 'd' ], //実際の必要に応じて必要な文字を追加します
  20. }
  21. }、
  22. マウント された() {
  23. 番目は .refreshCode() です
  24. }、
  25. マウントされていない() {
  26. }、
  27. メソッド : {
  28. // 乱数 を生成
  29. ランダム 数(最小、最大) {
  30. 最大=最大+ 1
  31. return Math.floor(Math.random ( ) * (max - min) + min);
  32. }、
  33. // 確認コード を更新
  34. re freshCode() {
  35. this.identifyCode = "" ;
  36. this.makeCode(this.identifyCodes, 4 );
  37. console.log( '現在の確認コード:' ,this.identifyCode);
  38. }、
  39. // 検証 コード文字列をランダムに生成
  40. makeCode (データ, レン) {
  41. console.log( 「データ、長さ:」データ、長さ)
  42. for (let i = 0 ; i < len; i + + ) {
  43. this.identifyCode + = this.identifyCodes[this.randomNum( 0 , this.identifyCodes. length - 1 )]
  44. }
  45. }、
  46. }、
  47. });
  48. < / スクリプト>
  49. < style lang = "less"スコープ>
  50. . コード{
  51. 幅: 500ピクセル;
  52. 高さ: 31.25レム;
  53. ボーダー: 1 px ソリッド #ddd;
  54. }
  55. < / スタイル>

レンダリング

 

 

 

 

 

identify は、キャンバスを使用してグラフィック検証コードを生成するvue プラグインです。

npm 識別 --save

グローバルにインポートできます


   
   
  
  
  1. './components/page/identify'からSIdentifyをインポートします。
  2. ビュー 使用(SIdentify)

現地輸入も可能、本稿では現地紹介を利用 

 

実装されたコードは次のとおりです。

サブアセンブリ


   
   
  
  
  1. < テンプレート>
  2. < divクラス= "s-キャンバス" >
  3. < キャンバス id = "s-canvas" :width = "contentWidth" :height = "contentHeight" > < /キャンバス>
  4. < / div >
  5. < / テンプレート>
  6. < スクリプト>
  7. デフォルト エクスポート{
  8. 名前 : 'SIdentify' ,
  9. 小道具 : {
  10. 識別コード: {
  11. タイプ :文字列
  12. デフォルト : '1234'
  13. }、
  14. fontSizeMin: {
  15. タイプ :数値,
  16. デフォルト : 18
  17. }、
  18. fontSizeMax: {
  19. タイプ :数値,
  20. デフォルト : 40
  21. }、
  22. backgroundColorMin: {
  23. タイプ :数値,
  24. デフォルト : 180
  25. }、
  26. backgroundColorMax: {
  27. タイプ :数値,
  28. デフォルト : 240
  29. }、
  30. colorMin: {
  31. タイプ :数値,
  32. デフォルト : 50
  33. }、
  34. カラーマックス: {
  35. タイプ :数値,
  36. デフォルト : 160
  37. }、
  38. lineColorMin: {
  39. タイプ :数値,
  40. デフォルト : 40
  41. }、
  42. lineColorMax: {
  43. タイプ :数値,
  44. デフォルト : 180
  45. }、
  46. dotColorMin: {
  47. タイプ :数値,
  48. デフォルト : 0
  49. }、
  50. dotColorMax: {
  51. タイプ :数値,
  52. デフォルト : 255
  53. }、
  54. contentWidth: {
  55. タイプ :数値,
  56. デフォルト : 111
  57. }、
  58. コンテンツの高さ: {
  59. タイプ :数値,
  60. デフォルト : 38
  61. }
  62. }、
  63. 私の やり方: {
  64. // 乱数 を生成する
  65. randomNum(最小、最大) {
  66. return Math.floor(Math.random ( ) * (最大-最小) +最小)
  67. }、
  68. // ランダムな 色を生成
  69. randomColor(最小、最大) {
  70. let r = this.randomNum(min, max)
  71. g = this.randomNum(最小、最大)
  72. let b = this.randomNum(min, max)
  73. return 'rgb(' + r + ',' + g + ',' + b + ')'
  74. }、
  75. drawPic() {
  76. let canvas = document.getElementById( 's-canvas' )
  77. let ctx = canvas.getContext( '2d' )
  78. ctx.textBaseline = '下部'
  79. // 背景 を描く
  80. ctx.fillStyle = this.randomColor(this.backgroundColorMin, this.backgroundColorMax)
  81. ctx.fillRect( 0 , 0 , this.contentWidth, this.contentHeight)
  82. // テキスト を描画
  83. for (let i = 0 ; i < this.identifyCode. length ; i + + ) {
  84. this.drawText(ctx, this.identifyCode[i], i)
  85. }
  86. // this.drawLine (ctx) //干渉線を描画
  87. // this.drawDot (ctx) //干渉点を描画
  88. }、
  89. // テキスト を描画する
  90. drawText(ctx, txt, i) {
  91. ctx.fillStyle = this.randomColor(this.colorMin, this.colorMax)
  92. ctx.font = this.randomNum(this.fontSizeMin, this.fontSizeMax) + 'px SimHei'
  93. let x = (i + 1 ) * (this.contentWidth / (this.identifyCode. length + 1 ))
  94. let y = this.randomNum(this.fontSizeMax, this.contentHeight - 5 )
  95. var deg = this.randomNum(- 30 , 30 ) //キャラクターの回転角度 ( 45度以下が良い)
  96. //座標原点 回転角度を修正
  97. ctx.translate(x, y)
  98. ctx.rotate(deg * Math.PI / 180 )
  99. ctx.fillText(txt, 0 , 0 )
  100. //座標原点 回転角度を復元
  101. ctx.rotate(-deg * Math.PI / 180 )
  102. ctx.translate(-x, -y)
  103. }、
  104. drawLine(ctx) {
  105. // 干渉 線を引く
  106. for (let i = 0 ; i < 8 ; i + + ) {
  107. ctx.strokeStyle = this.randomColor(this.lineColorMin, this.lineColorMax)
  108. ctx.beginPath()
  109. ctx.moveTo(this.randomNum( 0 , this.contentWidth), this.randomNum( 0 , this.contentHeight))
  110. ctx.lineTo(this.randomNum( 0 , this.contentWidth), this.randomNum( 0 , this.contentHeight))
  111. ctx.ストローク()
  112. }
  113. }、
  114. drawDot(ctx) {
  115. // 干渉 点を描く
  116. for (let i = 0 ; i < 100 ; i + + ) {
  117. ctx.fillStyle = this.randomColor( 0 , 255 )
  118. ctx.beginPath()
  119. ctx.arc(this.randomNum( 0 , this.contentWidth), this.randomNum( 0 , this.contentHeight), 1 , 0 , 2 * Math.PI)
  120. ctx.fill()
  121. }
  122. }
  123. }、
  124. 見る : {
  125. 識別コード() {
  126. this.drawPic()
  127. }
  128. }、
  129. モウンテッド () {
  130. this.drawPic()
  131. }
  132. }
  133. < / スクリプト>

親コンポーネント


   
   
  
  
  1. < テンプレート>
  2. < 分割>
  3. <div> キャプチャテスト</div> _ _ _ _
  4. < div @click = "refreshCode()" class = "code" style = "cursor:pointer;" title = "クリックして確認コードを切り替える" >
  5. < s-identify :identifyCode = "identifyCode" > < / s-identify >
  6. < / div >
  7. < / div >
  8. < / テンプレート>
  9. < スクリプト>
  10. import { defineComponent } from 'vue' ;
  11. "../../src/components/Sidentify.vue"からsIdentify をインポートします
  12. // 'axios'からaxios を インポート
  13. デフォルト defineComponent({
  14. 名前 「リスト」
  15. コンポーネント : { sIdentify },
  16. データ () {
  17. 戻る {
  18. 識別コード: "" ,
  19. IdentifyCodes: [ '0' , '1' , '2' , '3' , ' 4' , '5' , '6' , '7' , '8' , '9' , 'a' , 'b' , 'c' , 'd' ], //実際の必要に応じて必要な文字を追加します
  20. }
  21. }、
  22. マウント された() {
  23. 番目は .refreshCode() です
  24. }、
  25. マウントされていない() {
  26. }、
  27. メソッド : {
  28. // 乱数 を生成
  29. ランダム 数(最小、最大) {
  30. 最大=最大+ 1
  31. return Math.floor(Math.random ( ) * (max - min) + min);
  32. }、
  33. // 確認コード を更新
  34. re freshCode() {
  35. this.identifyCode = "" ;
  36. this.makeCode(this.identifyCodes, 4 );
  37. console.log( '現在の確認コード:' ,this.identifyCode);
  38. }、
  39. // 検証 コード文字列をランダムに生成
  40. makeCode (データ, レン) {
  41. console.log( 「データ、長さ:」データ、長さ)
  42. for (let i = 0 ; i < len; i + + ) {
  43. this.identifyCode + = this.identifyCodes[this.randomNum( 0 , this.identifyCodes. length - 1 )]
  44. }
  45. }、
  46. }、
  47. });
  48. < / スクリプト>
  49. < style lang = "less"スコープ>
  50. . コード{
  51. 幅: 500ピクセル;
  52. 高さ: 31.25レム;
  53. ボーダー: 1 px ソリッド #ddd;
  54. }
  55. < / スタイル>

レンダリング

 

 

 

 

 

おすすめ

転載: blog.csdn.net/weixin_64310738/article/details/129034206