効果画像:
アイデア: (1)キャンバス描画領域を作成する // テンプレート <div class="canvas-container"> <canvas ref="canvas" width="300" height="200">ブラウザはキャンバスをサポートしていません。ブラウザをアップグレードしてください。 </キャンバス> </div> // SCSS で .キャンバスコンテナ{ 背景: #fafafa; キャンバス { 背景: #fff; 境界線: 1px実線 #000; } } (2)初期の水平座標と垂直座標の取得 データ() { 戻る { initX: 0, // 初期水平座標 initY: 0, // 初期垂直座標} }, マウント() { このメソッドは、 }, メソッド: { // キャンバスの位置を初期化する initBound() { バインド = this.$refs.canvas.getBoundingClientRect() this.initX = bound.x this.initY = 境界.y } } (3)マウスクリックイベント、移動イベント、リリースイベントを追加する // テンプレート <div class="canvas-container"> <canvas ref="canvas" width="300" height="200" @mousedown="onMouseDown" @mousemove="onMouseMove" @mouseup="onMouseUp">ブラウザはキャンバスをサポートしていません。ブラウザをアップグレードしてください。 </キャンバス> </div> // スクリプト データ() { 戻る { // ... lastX: 0, // 前の水平座標lastY: 0, // 前の垂直座標isHandWrite: false, // 手書きを開始するかどうかpointsXY: [], // 単一のストロークallPointsXY: [], // すべてのストローク} }, メソッド: { マウスダウン時(e) { this.pointsXY = [] cx = e.clientX - this.initX とします。 cy = e.clientY - this.initY とします。 this.lastX = cx this.lastY = cy this.pointsXY.push(cx) this.pointsXY.push(cy) this.isHandWrite = true }, マウスを動かすと(e) { if (this.isHandWrite) { cx = e.clientX - this.initX とします。 cy = e.clientY - this.initY とします。 this.pointsXY.push(cx - this.lastX) this.pointsXY.push(cy - this.lastY) // 2D コンテキスト オブジェクトを取得します。let ctx = this.$refs.canvas.getContext('2d') // 新しいパスを作成する ctx.beginPath() ctx.strokeStyle = '#000' ctx.fillStyle = '#000' ctx.lineWidth = 8 ctx.lineCap = '丸' ctx.moveTo(this.lastX, this.lastY) ctx.lineTo(cx, cy) ctx.ストローク() this.lastX = cx this.lastY = cy } }, マウスを離すと(e) { if (this.isHandWrite) { this.isHandWrite = false this.allPointsXY.push(this.pointsXY.join(',')) this.queryText() // テキストを認識する} }, } (4) テキスト認識インターフェースとjsonpコールバック関数を追加します。Vue-jsonpはクロスドメインリクエストに使用されます。具体的な使用方法については、vueでのjsonpの使用を参照してください。 // スクリプト データ() { 戻る { // ... write_result: [], // 類似の単語を返す} }, マウント() { // ... _this = this とします // jsonpコールバック関数を追加、qq入力メソッド固有 window['QQShuru'] = { HWPanel: ajax_callback: 関数 (res) { _this.write_result = res.cand }, }, } }, メソッド: { クエリテキスト() { track_str = this.allPointsXY.join(',eb,') とします。 これ.$jsonp( `https://handwriting.shuru.qq.com/cloud/cgi-bin/cloud_hw_pub.wsgi?track_str=${track_str}&cmd=0` ).catch(エラー => { コンソール.log(エラー) }) }, } (5)最後に、キャンバスをクリアするための書き換えボタンを追加します。 // テンプレート <div> <button @click="onReload">書き換え</button> </div> // スクリプト onReload() { if (!this.$refs.canvas) 戻り値 this.pointsXY = [] this.allPointsXY = [] ctx = this.$refs.canvas.getContext('2d') とします。 ctx.clearRect(0, 0, 300, 200) } 完全なコードは次のとおりです。 <テンプレート> <div id="アプリ"> <div class="canvas-container"> <canvas ref="canvas" width="300" height="200" @mousedown="onMouseDown" @mousemove="onMouseMove" @mouseup="onMouseUp">ブラウザはキャンバスをサポートしていません。ブラウザをアップグレードしてください。 </キャンバス> </div> <div>[{{ lastX + ', ' + lastY }}]</div> <div> <button @click="onReload">書き換え</button> </div> <div>類似の単語を返す: {{ write_result }}</div> </div> </テンプレート> <スクリプト> エクスポートデフォルト{ 名前: 'アプリ'、 データ() { 戻る { initX: 0, // 初期水平座標initY: 0, // 初期垂直座標lastX: 0, // 前の水平座標lastY: 0, // 前の垂直座標isHandWrite: false, // 手書きを開始するかどうかpointsXY: [], // 単一のストロークallPointsXY: [], // すべてのストロークwrite_result: [], // 類似の文字を返す} }, マウント() { このメソッドは、 _this = this とします // jsonpコールバック関数を追加、qq入力メソッド固有 window['QQShuru'] = { HWPanel: ajax_callback: 関数 (res) { _this.write_result = res.cand }, }, } }, メソッド: { // キャンバスの位置を初期化する initBound() { バインド = this.$refs.canvas.getBoundingClientRect() this.initX = bound.x this.initY = 境界.y }, マウスダウン時(e) { コンソールログ('onDown', e) this.pointsXY = [] cx = e.clientX - this.initX とします。 cy = e.clientY - this.initY とします。 this.lastX = cx this.lastY = cy this.pointsXY.push(cx) this.pointsXY.push(cy) this.isHandWrite = true }, onMouseMove(e) { if (this.isHandWrite) { cx = e.clientX - this.initX とします。 cy = e.clientY - this.initY とします。 this.pointsXY.push(cx - this.lastX) this.pointsXY.push(cy - this.lastY) // 2D コンテキスト オブジェクトを取得します。let ctx = this.$refs.canvas.getContext('2d') // 新しいパスを作成する ctx.beginPath() ctx.strokeStyle = '#000' ctx.fillStyle = '#000' ctx.lineWidth = 8 ctx.lineCap = '丸' ctx.moveTo(this.lastX, this.lastY) ctx.lineTo(cx, cy) ctx.ストローク() this.lastX = cx this.lastY = cy } }, マウスを離すと(e) { if (this.isHandWrite) { this.isHandWrite = false this.allPointsXY.push(this.pointsXY.join(',')) this.queryText() } }, // テキストを認識する queryText() { track_str = this.allPointsXY.join(',eb,') とします。 これ.$jsonp( `https://handwriting.shuru.qq.com/cloud/cgi-bin/cloud_hw_pub.wsgi?track_str=${track_str}&cmd=0` ).catch(エラー => { コンソール.log(エラー) }) }, onReload() { if (!this.$refs.canvas) 戻り値 this.pointsXY = [] this.allPointsXY = [] ctx = this.$refs.canvas.getContext('2d') とします。 ctx.clearRect(0, 0, 300, 200) }, }, } </スクリプト> <スタイル lang="scss"> #アプリ { フォントファミリー: Avenir、Helvetica、Arial、sans-serif; -webkit-font-smoothing: アンチエイリアス; -moz-osx-font-smoothing: グレースケール; テキスト配置: 中央; 色: #2c3e50; .キャンバスコンテナ{ 背景: #fafafa; キャンバス { 背景: #fff; 境界線: 1px実線 #000; } } } </スタイル> これで、キャンバスの手書き入力を使用して中国語を認識する vue に関するこの記事は終了です。キャンバスの手書き入力を使用して中国語を認識する vue に関するより関連性の高いコンテンツについては、123WORDPRESS.COM の以前の記事を検索するか、次の関連記事を引き続き参照してください。今後とも 123WORDPRESS.COM を応援していただければ幸いです。 以下もご興味があるかもしれません:
|
この記事では、MySQL データベースの基礎を学ぶためによく使用されるコマンドを例を使って説明します...
google.htmlインターフェースは図の通りですコードは図のとおりです: (比較的シンプルで、入...
1. はじめにVagrant は、仮想マシン (VirtualBox) を構築および管理するためのツ...
元のコードは次のとおりです。 <div class='コントロールグループ'&...
序文MySQL を扱ったことがある人なら、テーブル メタデータ ロックの待機についてよく知っているは...
1. 設置環境Windows7 で MySQL5.6 データベースの解凍バージョンをアンインストー...
リバースプロキシリバースプロキシとは、プロキシサーバーを介してユーザーのアクセス要求を受信し、ユーザ...
最近、会社でたまたま生放送をしていたのですが、今日は私が遭遇した落とし穴を記録します。会社のサーバー...
ページの主要部分: <body> <ul id="メニュー"&...
目次1. シナリオの説明2. 解決策オプションが多すぎる el-select コンポーネントの解決策...
この記事では、画像の一時停止を実現するためのjQueryプラグインの具体的なコードを参考までに共有し...
MySQL レプリケーション テーブルの詳細な説明テーブル構造、インデックス、デフォルト値などを含む...
具体的なコードは次のとおりです。 /*スクロールバーの幅*/ ::-webkit-スクロールバー{ ...
目次getApp()ページエントリファイルの先頭に変数を定義しますwx.createSelector...
まず、ページ分割クエリを使用する理由を明確にする必要があります。データが膨大なため、すべてのデータを...