canvasSensorModelA.vue 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <template>
  2. <div class="canvasSensorModelAPanel">
  3. <canvas id="canvasSensorModelA" width="1000" height="500"></canvas>
  4. <canvas id="canvasSensorModelABg" width="1000" height="500"></canvas>
  5. </div>
  6. </template>
  7. <script>
  8. //import from '';
  9. export default {
  10. name: "canvasSensorModelA", //
  11. components: {},
  12. data() {
  13. return {
  14. modelImgSrc: require("@/assets/common/image/others/carTopView.png"),
  15. ctx: null,
  16. ctxBg: null,
  17. imgBg: null,
  18. rate: 70, // 换算比例
  19. };
  20. },
  21. props: {
  22. coordinate: {
  23. type: Object,
  24. default: { r: 0, n: 0, hl: 60, hr: 60 },
  25. },
  26. },
  27. watch: {
  28. coordinate(val) {
  29. this.draw(val);
  30. },
  31. },
  32. methods: {
  33. draw(data) {
  34. if (
  35. data.r === "" ||
  36. data.n === "" ||
  37. data.hl === "" ||
  38. data.hr === ""
  39. )
  40. return;
  41. this.ctx.clearRect(-250, -220, 500, 1000);
  42. this.ctx.beginPath();
  43. let rate = this.rate;
  44. let hr1 = +data.hl;
  45. let hr2 = +data.hr;
  46. // if (hr1 > 180) hr1 = 180;
  47. // if (hr2 > 180) hr2 = 180;
  48. //定义起点
  49. this.ctx.moveTo(6, 0);
  50. this.ctx.arc(
  51. 6,
  52. 0,
  53. +data.r * rate,
  54. 0.5 * Math.PI,
  55. (hr1 / 180 + 0.5) * Math.PI
  56. );
  57. this.ctx.closePath();
  58. this.ctx.fillStyle = "rgba(252,222,147,0.54)";
  59. this.ctx.fill();
  60. this.ctx.beginPath();
  61. //定义起点
  62. this.ctx.moveTo(6, 0);
  63. this.ctx.arc(
  64. 6,
  65. 0,
  66. +data.r * rate,
  67. 0.5 * Math.PI,
  68. (0.5 - hr2 / 180) * Math.PI,
  69. true
  70. );
  71. this.ctx.closePath();
  72. this.ctx.fillStyle = "rgba(252,222,147,0.54)";
  73. this.ctx.fill();
  74. this.clearArcFun(6, 0, +data.n * rate, this.ctx);
  75. },
  76. clearArcFun(x, y, r, cxt) {
  77. //(x,y)为要清除的圆的圆心,r为半径,cxt为context
  78. var stepClear = 1; //别忘记这一步
  79. clearArc(x, y, r);
  80. function clearArc(x, y, radius) {
  81. var calcWidth = radius - stepClear;
  82. var calcHeight = Math.sqrt(
  83. radius * radius - calcWidth * calcWidth
  84. );
  85. var posX = x - calcWidth;
  86. var posY = y - calcHeight;
  87. var widthX = 2 * calcWidth;
  88. var heightY = 2 * calcHeight;
  89. if (stepClear <= radius) {
  90. cxt.clearRect(posX, posY, widthX, heightY);
  91. stepClear += 1;
  92. clearArc(x, y, radius);
  93. }
  94. }
  95. },
  96. clear() {
  97. this.ctx.clearRect(-250, -220, 500, 1000);
  98. },
  99. },
  100. mounted() {
  101. $(document).ready(() => {
  102. this.ctx = document
  103. .getElementById("canvasSensorModelA")
  104. .getContext("2d");
  105. this.ctxBg = document
  106. .getElementById("canvasSensorModelABg")
  107. .getContext("2d");
  108. this.ctx.translate(780, 250);
  109. this.ctx.rotate(Math.PI / 2);
  110. this.imgBg = new Image();
  111. this.imgBg.src = this.modelImgSrc;
  112. this.imgBg.onload = () => {
  113. this.ctxBg.drawImage(this.imgBg, 0, 0, 1000, 500);
  114. };
  115. });
  116. },
  117. };
  118. </script>
  119. <style lang="less" scoped>
  120. .canvasSensorModelAPanel {
  121. position: relative;
  122. canvas {
  123. width: 100%;
  124. }
  125. #canvasSensorModelA {
  126. position: relative;
  127. z-index: 1;
  128. }
  129. #canvasSensorModelABg {
  130. position: absolute;
  131. top: 0;
  132. left: 0;
  133. z-index: 0;
  134. }
  135. }
  136. </style>