annotated-image.vue 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <template>
  2. <div class="annotated-image" v-on="events">
  3. <img alt="media" ref="image" draggable="false"
  4. :src="mediaUrl" :srcset="mediaUrlSet" :sizes="mediaUrlSizes"/>
  5. <div style="position: absolute; top: 0.5rem; left: 0.5rem">{{ data }}</div>
  6. <annotation-box v-if="current"
  7. :image="image"
  8. :position="current"
  9. :immutable="true"/>
  10. <annotation-box v-for="(result, index) in predictions"
  11. type="server"
  12. :key="index"
  13. :image="image"
  14. :position="result"
  15. :socket="socket"
  16. :immutable="false"
  17. :box-url="mediaUrl + '/' + result.id"/>
  18. </div>
  19. </template>
  20. <script>
  21. import AnnotationBox from "@/components/media/annotation-box";
  22. export default {
  23. name: "annotated-image",
  24. components: {AnnotationBox},
  25. props: ['project', 'data', 'socket'],
  26. mounted: function () {
  27. window.addEventListener("resize", this.resizeEvent);
  28. if (this.$refs.image.complete)
  29. this.resizeEvent();
  30. else
  31. this.$refs.image.addEventListener('load', this.resizeEvent);
  32. },
  33. destroyed() {
  34. window.removeEventListener("resize", this.resizeEvent);
  35. },
  36. watch: {
  37. data: function () {
  38. this.current = false;
  39. this.resizeEvent();
  40. }
  41. },
  42. data: function () {
  43. return {
  44. sizes: [600, 800, 1200, 1600, 2000, 3000],
  45. image: {
  46. left: 0,
  47. top: 0,
  48. width: 0,
  49. height: 0
  50. },
  51. start: false,
  52. current: false
  53. }
  54. },
  55. computed: {
  56. mediaUrl: function () {
  57. return this.socket.media(this.project.id, this.data.id);
  58. },
  59. mediaUrlSet: function () {
  60. return this.sizes.map(e => this.mediaUrl + '/' + e + ' ' + e + 'w').join(',');
  61. },
  62. mediaUrlSizes: function () {
  63. return this.sizes.map(e => '(max-width: ' + e + 'px) ' + e + 'px').join(',');
  64. },
  65. predictions: function () {
  66. return Object.keys(this.data.predictionResults).map(k => this.data.predictionResults[k]);
  67. },
  68. events: function () {
  69. return {
  70. 'touchstart': this.press,
  71. 'touchmove': this.track,
  72. 'touchend': this.release,
  73. 'mousedown': this.press,
  74. 'mousemove': this.track,
  75. 'mouseup': this.release,
  76. 'dragstart': e => e.stopPropagation()
  77. }
  78. }
  79. },
  80. methods: {
  81. resizeEvent: function () {
  82. const element = this.$refs.image.getBoundingClientRect();
  83. const parent = this.$refs.image.parentElement.getBoundingClientRect();
  84. this.image.left = element.x - parent.x;
  85. this.image.top = element.y - parent.y;
  86. this.image.width = element.width;
  87. this.image.height = element.height;
  88. },
  89. get: function (event) {
  90. if ('clientX' in event)
  91. return event;
  92. if ('touches' in event && event.touches.length > 0)
  93. return event.touches[0];
  94. if ('changedTouches' in event && event.changedTouches.length > 0)
  95. return event.changedTouches[0];
  96. },
  97. getX: function (event) {
  98. const bcr = this.$refs.image.getBoundingClientRect();
  99. return (this.get(event).clientX - bcr.left) / bcr.width;
  100. },
  101. getY: function (event) {
  102. const bcr = this.$refs.image.getBoundingClientRect();
  103. return (this.get(event).clientY - bcr.top) / bcr.height;
  104. },
  105. buildRectangle: function (x1, y1, x2, y2) {
  106. const lx = Math.max(Math.min(x1, x2), 0);
  107. const hx = Math.min(Math.max(x1, x2), 1);
  108. const ly = Math.max(Math.min(y1, y2), 0);
  109. const hy = Math.min(Math.max(y1, y2), 1);
  110. return {
  111. x: lx,
  112. y: ly,
  113. w: hx - lx,
  114. h: hy - ly
  115. }
  116. },
  117. press: function (event) {
  118. this.start = {
  119. x: this.getX(event),
  120. y: this.getY(event)
  121. };
  122. },
  123. track: function (event) {
  124. if (this.start) {
  125. const x = this.getX(event);
  126. const y = this.getY(event);
  127. this.current = this.buildRectangle(this.start.x, this.start.y, x, y);
  128. }
  129. },
  130. release: function () {
  131. if (this.start) {
  132. this.socket.post(this.mediaUrl, this.current);
  133. this.start = false;
  134. this.current = false;
  135. }
  136. }
  137. }
  138. }
  139. </script>
  140. <style scoped>
  141. img {
  142. max-width: 100%;
  143. max-height: 100%;
  144. }
  145. </style>