当前位置:   article > 正文

vue3中使用canvas_vue3 canvas

vue3 canvas

1、先封装一个canvas.ts,通过两种方式获取画布看个人需求

  1. export default function canvasFunctions() {
  2. //画圆 ref获取画笔
  3. const drawCircle = (
  4. x: any,
  5. y: any,
  6. r: any,
  7. color: any,
  8. width: any,
  9. canvas: any
  10. ) => {
  11. const ctx = canvas.value.getContext("2d");
  12. ctx.beginPath();
  13. ctx.arc(x, y, r, 0, 2 * Math.PI);
  14. ctx.strokeStyle = color;
  15. ctx.fillStyle = color;
  16. ctx.lineWidth = width;
  17. ctx.fill();
  18. ctx.stroke();
  19. ctx.closePath();
  20. };
  21. //画圆 id获取画笔
  22. const drawCircleById = (
  23. x: any,
  24. y: any,
  25. r: any,
  26. color: any,
  27. width: any,
  28. ctx: any
  29. ) => {
  30. ctx.beginPath();
  31. ctx.arc(x, y, r, 0, 2 * Math.PI);
  32. ctx.strokeStyle = color;
  33. ctx.fillStyle = color;
  34. ctx.lineWidth = width;
  35. ctx.fill();
  36. ctx.stroke();
  37. ctx.closePath();
  38. };
  39. // 画直线 ref获取画笔
  40. const drawLine = (
  41. x: any,
  42. y: any,
  43. x1: any,
  44. y1: any,
  45. color: any,
  46. width: any,
  47. canvas: any
  48. ) => {
  49. const ctx = canvas.value.getContext("2d");
  50. ctx.beginPath();
  51. ctx.setLineDash([]);
  52. ctx.moveTo(x, y);
  53. ctx.lineTo(x1, y1);
  54. ctx.strokeStyle = color; //在着色之前设置颜色
  55. ctx.lineWidth = width; //在着色之前设置线宽
  56. ctx.stroke();
  57. ctx.closePath();
  58. };
  59. // 画直线 id获取画笔
  60. const drawLineById = (
  61. x: any,
  62. y: any,
  63. x1: any,
  64. y1: any,
  65. color: any,
  66. width: any,
  67. ctx: any
  68. ) => {
  69. ctx.beginPath();
  70. ctx.setLineDash([]);
  71. ctx.moveTo(x, y);
  72. ctx.lineTo(x1, y1);
  73. ctx.strokeStyle = color; //在着色之前设置颜色
  74. ctx.lineWidth = width; //在着色之前设置线宽
  75. ctx.stroke();
  76. ctx.closePath();
  77. };
  78. // 画箭头 ref获取画笔
  79. const drawArrowLine = (
  80. x: any,
  81. y: any,
  82. x1: any,
  83. y1: any,
  84. x2: any,
  85. y2: any,
  86. color: any,
  87. width: any,
  88. canvas: any
  89. ) => {
  90. const ctx = canvas.value.getContext("2d");
  91. ctx.beginPath();
  92. ctx.moveTo(x, y);
  93. ctx.lineTo(x1, y1);
  94. ctx.lineTo(x2, y2);
  95. ctx.lineTo(x, y);
  96. ctx.fillStyle = color; //在着色之前设置颜色
  97. ctx.lineWidth = width; //在着色之前设置线宽
  98. ctx.strokeStyle = color;
  99. ctx.fill();
  100. ctx.stroke();
  101. ctx.closePath();
  102. };
  103. // 画箭头 id获取画笔
  104. const drawArrowLineById = (
  105. x: any,
  106. y: any,
  107. x1: any,
  108. y1: any,
  109. x2: any,
  110. y2: any,
  111. color: any,
  112. width: any,
  113. ctx: any
  114. ) => {
  115. ctx.beginPath();
  116. ctx.moveTo(x, y);
  117. ctx.lineTo(x1, y1);
  118. ctx.lineTo(x2, y2);
  119. ctx.lineTo(x, y);
  120. ctx.fillStyle = color; //在着色之前设置颜色
  121. ctx.lineWidth = width; //在着色之前设置线宽
  122. ctx.strokeStyle = color;
  123. ctx.fill();
  124. ctx.stroke();
  125. ctx.closePath();
  126. };
  127. // 填充文字 ref获取画笔
  128. const write = (
  129. text: any,
  130. fontsize: any,
  131. x: any,
  132. y: any,
  133. color: any,
  134. canvas: any
  135. ) => {
  136. const ctx = canvas.value.getContext("2d");
  137. ctx.fillStyle = color;
  138. ctx.font = fontsize;
  139. ctx.fillText(text, x, y);
  140. };
  141. // 填充文字 id获取画笔
  142. const writeById = (
  143. text: any,
  144. fontsize: any,
  145. x: any,
  146. y: any,
  147. color: any,
  148. ctx: any
  149. ) => {
  150. ctx.fillStyle = color;
  151. ctx.font = fontsize;
  152. ctx.fillText(text, x, y);
  153. };
  154. return {
  155. drawCircle,
  156. drawCircleById,
  157. drawLine,
  158. drawLineById,
  159. drawArrowLine,
  160. drawArrowLineById,
  161. write,
  162. writeById,
  163. };
  164. }

2、定义画布并获取ref

  1. const canvas = ref();
  2. <canvas ref="canvas" id="canvas" height="400" width="1600"></canvas>

3、第二种通过js获取dom

let ctx: HTMLElement | null = document.getElementById(“canvas”).getContext("2d");

4、把封装好的方法解构出来

  1. import canvasFunctions from "@/utils/canvas";
  2. let {
  3. drawCircle,
  4. drawCircleById,
  5. drawLine,
  6. drawLineById,
  7. drawArrowLine,
  8. drawArrowLineById,
  9. write,
  10. writeById,
  11. } = canvasFunctions();

5、使用ref调用 传入canvas(见第二条)

drawCircle(x, y, 20, “red”, 1, canvas);

6、使用dom调用 传入ctx(见第三条)

  1. drawCircleById(
  2. x,
  3. y,
  4. 20,
  5. "red",
  6. 1,
  7. ctx
  8. );

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/凡人多烦事01/article/detail/254438
推荐阅读
相关标签
  

闽ICP备14008679号