component.tsx 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. import { defineComponent, onMounted , ref, effect} from "vue";
  2. import { string } from "vue-types";
  3. import { useCompData } from ".";
  4. import { useEditor } from "../../../..";
  5. import { DesignComp } from "../../../../objects/DesignTemp/DesignComp";
  6. import { View } from "../View";
  7. import { CompUI } from "../..";
  8. export const Component = defineComponent({
  9. props: {
  10. compId: string().isRequired,
  11. },
  12. setup(props) {
  13. const { helper, controls , store} = useEditor();
  14. const data = useCompData(props.compId);
  15. onMounted(()=>{
  16. // draw();
  17. effect(draw);
  18. })
  19. const canvasRef = ref<HTMLCanvasElement>();
  20. function draw() {
  21. const canvas = canvasRef.value as HTMLCanvasElement;
  22. if (!canvas) return;
  23. const ctx = canvasRef.value?.getContext("2d") as CanvasRenderingContext2D;
  24. const width = data.layout.size[0];
  25. const height = data.layout.size[1];
  26. canvas.width = Math.ceil(Math.max(1, width));
  27. canvas.height = Math.ceil(Math.max(1, height));
  28. ctx.clearRect(0, 0, canvas.width, canvas.height);
  29. ctx.lineWidth = data.value.lineWidth;
  30. ctx.strokeStyle = data.value.lineColor;
  31. if (data.value.reverseFill) {
  32. ctx.fillStyle = data.value.fillColor;
  33. ctx.fillRect(0, 0, canvas.width, canvas.height);
  34. }
  35. const padding = Math.max(10, data.value.lineWidth);
  36. ctx.beginPath();
  37. ctx.arc(width/2.0, height / 2.0, (Math.min(width,height) - padding) / 2.0, Math.PI / 180.0 * data.value.start, Math.PI / 180.0 * data.value.end)
  38. if (data.value.isFill) {
  39. ctx.closePath();
  40. }
  41. ctx.stroke();
  42. if (data.value.isFill) {
  43. let bColor = data.value.fillColor;
  44. if (!bColor) bColor = data.value.lineColor;
  45. ctx.fillStyle = bColor;
  46. if (data.value.reverseFill) {
  47. ctx.clip();
  48. // 将当前路径指定的区域颜色扣除
  49. ctx.globalCompositeOperation = "destination-out";
  50. }
  51. ctx.fill();
  52. }
  53. }
  54. return () => (
  55. <View compId={props.compId}>
  56. <canvas ref={canvasRef} style={{width:"100%", height: "100%"}}> </canvas>
  57. </View>
  58. );
  59. },
  60. });