index.vue 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <template>
  2. <div class="canvas-wrap">
  3. <canvas ref="whiteboard" width="920" height="520"></canvas>
  4. </div>
  5. </template>
  6. <script setup>
  7. import { ref, onMounted } from 'vue';
  8. import { paintData } from './data';
  9. const whiteboard = ref(null);
  10. function paint() {
  11. let context = whiteboard.value.getContext('2d');
  12. paintData.forEach(({ action, value }) => {
  13. if (action !== 'draw') return;
  14. const { fileName, data } = value;
  15. if (fileName !== 'WhiteBorad') return;
  16. const { color: color16, thickness, type, draw, width, height } = data;
  17. // 将十进制的颜色,转为十六进制
  18. let color = Number(color16).toString(16);
  19. color = `#${color.length < 6 ? '0'.repeat(6 - color.length) : ''}${color}`;
  20. if (draw.length <= 0) return;
  21. context.beginPath();
  22. context.moveTo(width * draw[0].x, height * draw[0].y);
  23. if (type === 10) {
  24. context.lineWidth = (3 * 2 * (thickness * width)) / (draw[0].ppt_width || 1295);
  25. } else {
  26. context.lineWidth = (thickness * width) / (draw[0].ppt_width || 1295);
  27. }
  28. context.lineJoin = 'round';
  29. context.strokeStyle = type === 10 ? '#fff' : color;
  30. draw.forEach(({ x, y }) => {
  31. context.lineTo(width * x, height * y);
  32. });
  33. context.stroke();
  34. });
  35. }
  36. onMounted(() => {
  37. paint();
  38. });
  39. </script>
  40. <style lang="scss" scoped>
  41. .canvas-wrap {
  42. width: 922px;
  43. height: 522px;
  44. margin: 0 auto;
  45. margin-top: 200px;
  46. border: 1px solid #ccc;
  47. canvas {
  48. position: relative;
  49. z-index: 9999;
  50. display: block;
  51. }
  52. }
  53. </style>