Effect picture:
Ideas: (1) Create a canvas drawing area // template <div class="canvas-container"> <canvas ref="canvas" width="300" height="200">Your browser does not support canvas, please upgrade your browser. </canvas> </div> //scss .canvas-container { background: #fafafa; canvas { background: #fff; border: 1px solid #000; } } (2) Obtaining the initial horizontal and vertical coordinates data() { return { initX: 0, // initial horizontal coordinate initY: 0, // initial vertical coordinate} }, mounted() { this.initBound() }, methods: { // Initialize canvas position initBound() { let bound = this.$refs.canvas.getBoundingClientRect() this.initX = bound.x this.initY = bound.y } } (3) Add mouse click events, move events, and release events // template <div class="canvas-container"> <canvas ref="canvas" width="300" height="200" @mousedown="onMouseDown" @mousemove="onMouseMove" @mouseup="onMouseUp">Your browser does not support canvas, please upgrade your browser. </canvas> </div> // script data() { return { // ... lastX: 0, // previous horizontal coordinatelastY: 0, // previous vertical coordinateisHandWrite: false, // whether to start handwritingpointsXY: [], // single strokeallPointsXY: [], // all strokes} }, methods: { onMouseDown(e) { this.pointsXY = [] let cx = e.clientX - this.initX let 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) { let cx = e.clientX - this.initX let cy = e.clientY - this.initY this.pointsXY.push(cx - this.lastX) this.pointsXY.push(cy - this.lastY) // Get the 2d context object let ctx = this.$refs.canvas.getContext('2d') // Create a new path ctx.beginPath() ctx.strokeStyle = '#000' ctx.fillStyle = '#000' ctx.lineWidth = 8 ctx.lineCap = 'round' ctx.moveTo(this.lastX, this.lastY) ctx.lineTo(cx, cy) ctx.stroke() this.lastX = cx this.lastY = cy } }, onMouseUp(e) { if (this.isHandWrite) { this.isHandWrite = false this.allPointsXY.push(this.pointsXY.join(',')) this.queryText() // Recognize text} }, } (4) Add text recognition interface and jsonp callback function. Vue-jsonp is used for cross-domain requests. For specific usage, please refer to the usage of jsonp in vue. // script data() { return { // ... write_result: [], // Return similar words} }, mounted() { // ... let _this = this // Add jsonp callback function, qq input method specific window['QQShuru'] = { HWPanel: ajax_callback: function (res) { _this.write_result = res.cand }, }, } }, methods: { queryText() { let track_str = this.allPointsXY.join(',eb,') this.$jsonp( `https://handwriting.shuru.qq.com/cloud/cgi-bin/cloud_hw_pub.wsgi?track_str=${track_str}&cmd=0` ).catch(err => { console.log(err) }) }, } (5) Finally, add a rewrite button to clear the canvas. // template <div> <button @click="onReload">Rewrite</button> </div> // script onReload() { if (!this.$refs.canvas) return this.pointsXY = [] this.allPointsXY = [] let ctx = this.$refs.canvas.getContext('2d') ctx.clearRect(0, 0, 300, 200) } The full code is as follows: <template> <div id="app"> <div class="canvas-container"> <canvas ref="canvas" width="300" height="200" @mousedown="onMouseDown" @mousemove="onMouseMove" @mouseup="onMouseUp">Your browser does not support canvas, please upgrade your browser. </canvas> </div> <div>[{{ lastX + ', ' + lastY }}]</div> <div> <button @click="onReload">Rewrite</button> </div> <div>Return similar words: {{ write_result }}</div> </div> </template> <script> export default { name: 'App', data() { return { initX: 0, // Initial horizontal coordinateinitY: 0, // Initial vertical coordinatelastX: 0, // Previous horizontal coordinatelastY: 0, // Previous vertical coordinateisHandWrite: false, // Whether to start handwritingpointsXY: [], // Single strokeallPointsXY: [], // All strokeswrite_result: [], // Return similar characters} }, mounted() { this.initBound() let _this = this // Add jsonp callback function, qq input method specific window['QQShuru'] = { HWPanel: ajax_callback: function (res) { _this.write_result = res.cand }, }, } }, methods: { // Initialize canvas position initBound() { let bound = this.$refs.canvas.getBoundingClientRect() this.initX = bound.x this.initY = bound.y }, onMouseDown(e) { console.log('onDown', e) this.pointsXY = [] let cx = e.clientX - this.initX let 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) { let cx = e.clientX - this.initX let cy = e.clientY - this.initY this.pointsXY.push(cx - this.lastX) this.pointsXY.push(cy - this.lastY) // Get the 2d context object let ctx = this.$refs.canvas.getContext('2d') // Create a new path ctx.beginPath() ctx.strokeStyle = '#000' ctx.fillStyle = '#000' ctx.lineWidth = 8 ctx.lineCap = 'round' ctx.moveTo(this.lastX, this.lastY) ctx.lineTo(cx, cy) ctx.stroke() this.lastX = cx this.lastY = cy } }, onMouseUp(e) { if (this.isHandWrite) { this.isHandWrite = false this.allPointsXY.push(this.pointsXY.join(',')) this.queryText() } }, // Recognize text queryText() { let track_str = this.allPointsXY.join(',eb,') this.$jsonp( `https://handwriting.shuru.qq.com/cloud/cgi-bin/cloud_hw_pub.wsgi?track_str=${track_str}&cmd=0` ).catch(err => { console.log(err) }) }, onReload() { if (!this.$refs.canvas) return this.pointsXY = [] this.allPointsXY = [] let ctx = this.$refs.canvas.getContext('2d') ctx.clearRect(0, 0, 300, 200) }, }, } </script> <style lang="scss"> #app { font-family: Avenir, Helvetica, Arial, sans-serif; -webkit-font-smoothing: antialiased; -moz-osx-font-smoothing: grayscale; text-align: center; color: #2c3e50; .canvas-container { background: #fafafa; canvas { background: #fff; border: 1px solid #000; } } } </style> This is the end of this article about vue using canvas handwriting input to recognize Chinese. For more relevant vue using canvas handwriting input to recognize Chinese content, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: Web page text design should be like smart girls wearing clothes
>>: How to pass parameters to JS via CSS
<br />The official version of Baidu Encyclop...
The full name of Blog should be Web log, which mea...
Application scenarios: One of the new requirement...
1. Environmental Description (1) CentOS-7-x86_64,...
Table of contents Preface 1. Recursive components...
What Beautiful HTML Code Looks Like How to write ...
1. Download the alpine image [root@docker43 ~]# d...
To write a drop-down menu, click the button. The ...
1. Analytical thinking 1. Eliminate the machine...
This article shares the encapsulation code of Jav...
Logpoint-based replication 1. Create a dedicated ...
1. Before configuring the IP address, first use i...
Table of contents 1. What is a window function? 1...
The experimental code is as follows: </head>...
Understand this Perhaps you have seen this in oth...