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
Serious MySQL optimization! If the amount of MySQ...
Inserting Data insert into table name (column nam...
Phenomenon When using Apache Spark 2.x, you may e...
There is a task process on the server. When we us...
The detailed process of configuring the MySQL dat...
Preface In MySQL, multi-table join query is a ver...
Win10 installs mysql5.7 decompressed version, for...
Download address: https://dev.mysql.com/downloads...
1. Introduction to Layer 4 Load Balancing What is...
1.device-width Definition: Defines the screen vis...
What I want to share today is to use native JS to...
Table of contents 1. Vue3 component communication...
<table id=" <%=var1%>">, the...
Table of contents Creating OAuth Apps Get the cod...
HTML 4 HTML (not XHTML), MIME type is text/html, ...