この記事の例では、グラフィック検証コードを実装するためのVueの具体的なコードを参考までに共有しています。具体的な内容は次のとおりです。 効果画像:または または または コード:検証コードのコンポーネント: src/common/sIdentify.vue <テンプレート> <div class="s-canvas"> <canvas id="s-canvas" :width="contentWidth" :height="contentHeight"></canvas> </div> </テンプレート> <スクリプト> エクスポートデフォルト{ 名前: 'SIdentify', 小道具: { 識別コード: タイプ: 文字列、 デフォルト: '1234' }, フォントサイズ最小: { タイプ: 数値、 デフォルト: 18 }, フォントサイズ最大: { タイプ: 数値、 デフォルト: 40 }, 背景色最小: { タイプ: 数値、 デフォルト: 180 }, 背景色最大: { タイプ: 数値、 デフォルト: 240 }, 色最小: タイプ: 数値、 デフォルト: 50 }, 色最大値: { タイプ: 数値、 デフォルト: 160 }, ラインカラー最小: { タイプ: 数値、 デフォルト: 40 }, ラインカラー最大: { タイプ: 数値、 デフォルト: 180 }, ドットカラー最小: { タイプ: 数値、 デフォルト: 0 }, ドットカラーマックス: { タイプ: 数値、 デフォルト: 255 }, コンテンツ幅: { タイプ: 数値、 デフォルト: 111 }, コンテンツの高さ: { タイプ: 数値、 デフォルト: 38 } }, メソッド: { // 乱数を生成する randomNum(min, max) { Math.floor(Math.random() * (max - min) + min) を返します。 }, // ランダムな色を生成する randomColor(min, max) { r = this.randomNum(最小値, 最大値) とします。 g = this.randomNum(min, max) とします。 b = this.randomNum(最小値, 最大値) とします。 'rgb(' + r + ',' + g + ',' + b + ')' を返します }, 描画画像() { キャンバス = document.getElementById('s-canvas') とします。 ctx = canvas.getContext('2d') とします。 ctx.textBaseline = '下' // 背景を描画します 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' x = (i + 1) * (this.contentWidth / (this.identifyCode.length + 1)) とします。 y = this.randomNum(this.fontSizeMax, this.contentHeight - 5) とします。 var deg = this.randomNum(-30, 30) // 文字の回転角度(45度以下が望ましい) // 座標原点と回転角度を変更する ctx.translate(x, y) ctx.rotate(度 * Math.PI / 180) です。 ctx.fillText(txt, 0, 0) // 座標原点と回転角度を復元します ctx.rotate(-deg * Math.PI / 180) ctx.translate(-x, -y) }, 描画線(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.ストローク() } }, 描画ドット(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() } } }, 時計: 識別コード() { この.drawPic() } }, マウント() { この.drawPic() } } </スクリプト> 親コンポーネント <テンプレート> <div> <div>検証コードテスト</div> <div @click="refreshCode()" class="code" style="cursor:pointer;" title="クリックして確認コードを切り替えます"> <s-identify :identifyCode="identifyCode"></s-identify> </div> </div> </テンプレート> <スクリプト> 'vue' から defineComponent をインポートします。 "@/common/sIdentify.vue" から sIdentify をインポートします。 // 'axios' から axios をインポート エクスポートデフォルトdefineComponent({ 名前: 'WatermarkTest'、 コンポーネント: { sIdentify }, データ() { 戻る { 識別コード: "", identifyCodes: ['0','1','2','3','4','5','6','7','8','9','a','b','c','d'], //実際のニーズに応じて必要な文字を追加します} }, マウント() { this.refreshCode() }, アンマウント() { }, メソッド: { // 乱数を生成する randomNum(min, max) { 最大値 = 最大値 + 1 Math.floor(Math.random() * (max - min) + min) を返します。 }, // 検証コードを更新する refreshCode() { this.identifyCode = ""; this.makeCode(this.identifyCodes, 4); console.log('現在の検証コード:',this.identifyCode); }, // 検証コードをランダムに生成する string makeCode(data, len) { console.log('データ, 長さ:', データ, 長さ) (i = 0; i < len; i++) の場合 { this.identifyCode += this.identifyCodes[this.randomNum(0, this.identifyCodes.length-1)] } }, }, }); </スクリプト> 以上がこの記事の全内容です。皆様の勉強のお役に立てれば幸いです。また、123WORDPRESS.COM を応援していただければ幸いです。 以下もご興味があるかもしれません:
|
>>: 画像の盗難を防ぐために Nginx で Referer を設定する方法
今日、私はブログサイト shoptalkshow を閲覧していて、非常に興味深いこのインターフェース...
Linux では、ハードディスクの追加やパーティションの再マウントといった状況に頻繁に遭遇します。こ...
Mac 最新バージョンの MySQL 8.0.22 パスワード回復問題の説明:昨日、突然、Macで最...
MySQLに何がインストールされているか確認する rpm -qa | grep -i mysql n...
目次序文動的プロパティとは何ですか?値のコピー値の種類を決定する要約する序文これは JavaScri...
本日、MySQL 8.0 をアップデートしました。最初の問題: Navicatがデータベースに接続で...
MySQL 5.x と MySQL 8.0.X のいくつかの違いapplication.proper...
1. 背景DockerでRabbitMQをデプロイする際に、次の2つの問題が発生します。問題1: ス...
<Head>……</head> は <HTML> のファイル ヘ...
1. シナリオの説明:私たちの環境ではマイクロサービスを使用しています。各プログラムには個別のプロセ...
1. 時間差関数(TIMESTAMPDIFF、DATEDIFF) MySQLを使用して時間差を計算...
軽量のオープンソース データベースである MySQL は、エンタープライズ レベルのアプリケーション...
ウェブサイトのナビゲーションを設計することは、家の基礎を築くようなものです。基礎がしっかりしていなけ...
1. コマンドの紹介時間は、コマンドの実行に費やされた時間や関連するシステム リソース、その他の情報...
コンピュータ サイエンスで最も一般的に使用され、議論されているデータ構造の 1 つは、二分探索木です...