1
1

annotated-image.vue 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. <annotation-box v-for="(result, index) in predictions"
  10. :type="result.origin"
  11. :key="index"
  12. :id="result.id"
  13. :image="image"
  14. :position="result"
  15. :socket="socket"
  16. :box-url="mediaUrl + '/' + result.id"
  17. @resize="resize"/>
  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. fixed: false,
  53. current: false,
  54. callback: false
  55. }
  56. },
  57. computed: {
  58. mediaUrl: function () {
  59. return this.socket.media(this.project.id, this.data.id);
  60. },
  61. mediaUrlSet: function () {
  62. return this.sizes.map(e => this.mediaUrl + '/' + e + ' ' + e + 'w').join(',');
  63. },
  64. mediaUrlSizes: function () {
  65. return this.sizes.map(e => '(max-width: ' + e + 'px) ' + e + 'px').join(',');
  66. },
  67. predictions: function () {
  68. return Object.keys(this.data.predictionResults).map(k => this.data.predictionResults[k]);
  69. },
  70. events: function () {
  71. return {
  72. 'touchstart': this.press,
  73. 'touchmove': this.track,
  74. 'touchend': this.release,
  75. 'mousedown': this.press,
  76. 'mousemove': this.track,
  77. 'mouseup': this.release,
  78. 'dragstart': e => e.stopPropagation()
  79. }
  80. }
  81. },
  82. methods: {
  83. resizeEvent: function () {
  84. const element = this.$refs.image.getBoundingClientRect();
  85. const parent = this.$refs.image.parentElement.getBoundingClientRect();
  86. this.image.left = element.x - parent.x;
  87. this.image.top = element.y - parent.y;
  88. this.image.width = element.width;
  89. this.image.height = element.height;
  90. },
  91. get: function (event) {
  92. if ('clientX' in event)
  93. return event;
  94. if ('touches' in event && event.touches.length > 0)
  95. return event.touches[0];
  96. if ('changedTouches' in event && event.changedTouches.length > 0)
  97. return event.changedTouches[0];
  98. },
  99. getX: function (event) {
  100. const bcr = this.$refs.image.getBoundingClientRect();
  101. return (this.get(event).clientX - bcr.left) / bcr.width;
  102. },
  103. getY: function (event) {
  104. const bcr = this.$refs.image.getBoundingClientRect();
  105. return (this.get(event).clientY - bcr.top) / bcr.height;
  106. },
  107. buildRectangle: function (x1, y1, x2, y2) {
  108. const lx = this.fixed && 'lx' in this.fixed ? this.fixed.lx : Math.max(Math.min(x1, x2), 0);
  109. const hx = this.fixed && 'hx' in this.fixed ? this.fixed.hx : Math.min(Math.max(x1, x2), 1);
  110. const ly = this.fixed && 'ly' in this.fixed ? this.fixed.ly : Math.max(Math.min(y1, y2), 0);
  111. const hy = this.fixed && 'hy' in this.fixed ? this.fixed.hy : Math.min(Math.max(y1, y2), 1);
  112. return {
  113. x: lx,
  114. y: ly,
  115. w: hx - lx,
  116. h: hy - ly
  117. }
  118. },
  119. press: function (event) {
  120. this.start = {
  121. x: this.getX(event),
  122. y: this.getY(event)
  123. };
  124. },
  125. track: function (event) {
  126. if (this.start) {
  127. const x = this.getX(event);
  128. const y = this.getY(event);
  129. this.current = this.buildRectangle(this.start.x, this.start.y, x, y);
  130. }
  131. },
  132. release: function () {
  133. if (this.start) {
  134. if (this.callback)
  135. this.callback(this.current);
  136. else
  137. this.socket.post(this.mediaUrl, this.current);
  138. this.start = false;
  139. this.fixed = false;
  140. this.current = false;
  141. this.callback = false;
  142. }
  143. },
  144. resize: function (position, mode, callback) {
  145. this.callback = callback;
  146. switch (mode) {
  147. case 'nw':
  148. this.start = {x: position.x + position.w, y: position.y + position.h};
  149. this.fixed = false;
  150. break;
  151. case 'ne':
  152. this.start = {x: position.x, y: position.y + position.h};
  153. this.fixed = false;
  154. break;
  155. case 'sw':
  156. this.start = {x: position.x + position.w, y: position.y};
  157. this.fixed = false;
  158. break;
  159. case 'se':
  160. this.start = {x: position.x, y: position.y};
  161. this.fixed = false;
  162. break;
  163. case 'nn':
  164. this.start = {x: position.x, y: position.y + position.h};
  165. this.fixed = {lx: position.x, hx: position.x + position.w};
  166. break;
  167. case 'ww':
  168. this.start = {x: position.x + position.w, y: position.y};
  169. this.fixed = {ly: position.y, hy: position.y + position.h};
  170. break;
  171. case 'ee':
  172. this.start = {x: position.x, y: position.y};
  173. this.fixed = {ly: position.y, hy: position.y + position.h};
  174. break;
  175. case 'ss':
  176. this.start = {x: position.x, y: position.y};
  177. this.fixed = {lx: position.x, hx: position.x + position.w};
  178. break;
  179. }
  180. }
  181. }
  182. }
  183. </script>
  184. <style scoped>
  185. img {
  186. max-width: 100%;
  187. max-height: 100%;
  188. }
  189. </style>