Advertisement
vitareinforce

kotretan

May 2nd, 2024
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.88 KB | None | 0 0
  1. Sources: codeproject.com (1) developer.mozilla.org (2) ipycanvas.readthedocs.io (3) github.com (4)
  2.  
  3. Here is an example code to achieve what you described using Vite:
  4.  
  5. First, create a new Vite project by running npm init vite@latest and follow the prompts.
  6.  
  7. Then, create a new file main.js and add the following code:
  8.  
  9. js
  10. Edit
  11. Full Screen
  12. Copy code
  13. import { createApp } from 'vue'
  14. import App from './App.vue'
  15.  
  16. createApp(App).use().mount('#app')
  17. Create a new file App.vue and add the following code:
  18.  
  19. html
  20. Edit
  21. Run
  22. Full Screen
  23. Copy code
  24. <template>
  25. <div>
  26. <input type="file" @change="handleImageUpload" />
  27. <canvas id="canvas" width="400" height="400"></canvas>
  28. </div>
  29. </template>
  30.  
  31. <script>
  32. export default {
  33. data() {
  34. return {
  35. image: null,
  36. polygons: [],
  37. currentPolygon: null,
  38. }
  39. },
  40. methods: {
  41. handleImageUpload(event) {
  42. const file = event.target.files[0]
  43. const reader = new FileReader()
  44. reader.onload = () => {
  45. this.image = new Image()
  46. this.image.src = reader.result
  47. this.image.onload = () => {
  48. const canvas = document.getElementById('canvas')
  49. const ctx = canvas.getContext('2d')
  50. ctx.drawImage(this.image, 0, 0, canvas.width, canvas.height)
  51. }
  52. }
  53. reader.readAsDataURL(file)
  54. },
  55. handleMouseDown(event) {
  56. if (!this.currentPolygon) {
  57. this.currentPolygon = []
  58. }
  59. this.currentPolygon.push([event.offsetX, event.offsetY])
  60. },
  61. handleMouseMove(event) {
  62. if (this.currentPolygon) {
  63. this.currentPolygon.push([event.offsetX, event.offsetY])
  64. }
  65. },
  66. handleMouseUp() {
  67. if (this.currentPolygon) {
  68. this.polygons.push(this.currentPolygon)
  69. this.currentPolygon = null
  70. }
  71. },
  72. sendPolygonsToAPI() {
  73. // Send the polygons to your API with their corresponding IDs
  74. console.log(this.polygons)
  75. // Replace with your API call
  76. // fetch('/api/polygons', {
  77. // method: 'POST',
  78. // headers: { 'Content-Type': 'application/json' },
  79. // body: JSON.stringify(this.polygons),
  80. // })
  81. },
  82. },
  83. mounted() {
  84. const canvas = document.getElementById('canvas')
  85. canvas.addEventListener('mousedown', this.handleMouseDown)
  86. canvas.addEventListener('mousemove', this.handleMouseMove)
  87. canvas.addEventListener('mouseup', this.handleMouseUp)
  88. },
  89. }
  90. </script>
  91. This code allows the user to upload an image, draw polygons on top of the image, and send the polygons to an API with their corresponding IDs.
  92.  
  93. Note that you'll need to replace the sendPolygonsToAPI method with your actual API call.
  94.  
  95. Also, this is just a basic example, and you may want to add more features, such as:
  96.  
  97. Handling multiple images
  98. Allowing the user to edit or delete polygons
  99. Displaying the polygon IDs on top of the image
  100. etc.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement