ui.vue 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <template>
  2. <div id="fabricContainer" class="fabric-container">
  3. <div id="canvas-container">
  4. <canvas id="myCanvas"></canvas>
  5. </div>
  6. </div>
  7. </template>
  8. <script>
  9. export default {
  10. data() {
  11. return {
  12. boxWidth: 100,
  13. boxHeight: 100,
  14. canvas: null,
  15. position: [],
  16. backgroundImageUrl: 'https://file-node.oss-cn-shanghai.aliyuncs.com/youji/01a0120395b249ecbae565600f2fb3ce'
  17. }
  18. },
  19. beforeDestroy() {
  20. let _self = this;
  21. window.removeEventListener('resize', function() {
  22. _self.initResize();
  23. });
  24. },
  25. mounted() {
  26. let _self = this;
  27. _self.init();
  28. // 监听窗口大小变化
  29. window.addEventListener('resize', function() {
  30. _self.initResize();
  31. });
  32. },
  33. methods: {
  34. init() {
  35. let box = document.getElementById('fabricContainer');
  36. if (!box) return;
  37. let boxWidth = box.offsetWidth,
  38. boxHeight = box.offsetHeight;
  39. let imgObj = new Image();
  40. imgObj.src = this.backgroundImageUrl;
  41. imgObj.onload = () => {
  42. let imgWidth = imgObj.width,
  43. imgHeight = imgObj.height;
  44. let scale = imgWidth / imgHeight;
  45. let canvasWidth = boxWidth >= boxHeight ? boxHeight * scale : boxWidth,
  46. canvasHeight = boxWidth < boxHeight ? boxWidth / scale : boxHeight;
  47. this.canvas = new fabric.Canvas('myCanvas', {
  48. fireRightClick: true, // 启用右键,button的数字为3
  49. stopContextMenu: true, // 禁止默认右键菜单
  50. controlsAboveOverlay: true, // 超出clipPath后仍然展示控制条
  51. preserveObjectStacking: true, // 当选择画布中的对象时,让对象不在顶层。
  52. width: canvasWidth, // 设置画布宽度
  53. height: canvasHeight, // 设置画布高度
  54. });
  55. document.getElementById('canvas-container').style.width = canvasWidth;
  56. document.getElementById('canvas-container').style.hight = canvasWidth;
  57. this.initBackgroundImage();
  58. this.clickFunction();
  59. }
  60. },
  61. initResize() {
  62. let box = document.getElementById('fabricContainer');
  63. if (!box) return;
  64. let boxWidth = box.offsetWidth,
  65. container = document.getElementById('myCanvas').offsetWidth;
  66. let scale = boxWidth / container >= 1 ? 1 : boxWidth / container;
  67. document.getElementById('canvas-container').style.transform = 'scale(' + scale + ')';
  68. },
  69. clickFunction() {
  70. let _self = this;
  71. // 监听点击事件
  72. _self.canvas.on('mouse:down', function(options) {
  73. console.log(options);
  74. // // 获取点击位置的坐标
  75. // const pointer = _self.canvas.getPointer(options.e);
  76. // const x = pointer.x;
  77. // const y = pointer.y;
  78. // _self.position.push({
  79. // x: x,
  80. // y: y
  81. // })
  82. // console.log(_self.position);
  83. // // 在点击位置添加一个圆形标记
  84. // const circle = new fabric.Circle({
  85. // radius: 5,
  86. // fill: 'red',
  87. // left: x,
  88. // top: y,
  89. // selectable: false // 禁止选中
  90. // });
  91. // _self.canvas.add(circle);
  92. // _self.canvas.renderAll();
  93. });
  94. },
  95. initBackgroundImage() {
  96. let _self = this;
  97. let bg = 'https://file-node.oss-cn-shanghai.aliyuncs.com/youji/01a0120395b249ecbae565600f2fb3ce'
  98. fabric.Image.fromURL(bg, function(img) {
  99. // 获取画布的宽度和高度
  100. const canvasWidth = _self.canvas.getWidth();
  101. const canvasHeight = _self.canvas.getHeight();
  102. // 计算图片的缩放比例
  103. const scaleX = canvasWidth / img.width;
  104. const scaleY = canvasHeight / img.height;
  105. const scale = scaleX < scaleY ? scaleX : scaleY;
  106. // 设置图片的缩放比例
  107. img.scale(scale);
  108. img.set({
  109. left: 0,
  110. top: 0,
  111. selectable: false
  112. })
  113. _self.canvas.add(img);
  114. _self.addPolygon();
  115. });
  116. },
  117. addPolygon() {
  118. const starPoints = [{
  119. "x": 705,
  120. "y": 213
  121. }, {
  122. "x": 855,
  123. "y": 216
  124. }, {
  125. "x": 855,
  126. "y": 354
  127. }, {
  128. "x": 705,
  129. "y": 354
  130. }];
  131. const star = new fabric.Polygon(starPoints, {
  132. fill: 'rgba(113, 179, 255, 0.6)',
  133. stroke: 'rgba(113, 179, 255, 1.0)',
  134. strokeWidth: 1,
  135. selectable: false,
  136. id:1
  137. });
  138. star.on('click', function(e) {
  139. console.log(e);
  140. });
  141. this.canvas.add(star);
  142. this.canvas.renderAll();
  143. }
  144. }
  145. }
  146. </script>
  147. <style lang="scss">
  148. .fabric-container {
  149. width: 100%;
  150. height: 100%;
  151. overflow: hidden;
  152. #canvas-container {
  153. transform-origin: 0% 0%;
  154. }
  155. }
  156. </style>