component.tsx 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. const padding = Math.max(10, data.value.lineWidth);
  32. ctx.beginPath();
  33. ctx.ellipse(width/2.0, height / 2.0, (width-padding)/2.0, (height-padding) / 2.0, 0, Math.PI / 180.0 * data.value.start, Math.PI / 180.0 * data.value.end)
  34. if (data.value.isFill) {
  35. ctx.closePath();
  36. }
  37. ctx.stroke();
  38. if (data.value.isFill) {
  39. let bColor = data.value.fillColor;
  40. if (!bColor) bColor = data.value.lineColor;
  41. ctx.fillStyle = bColor;
  42. ctx.fill();
  43. }
  44. }
  45. return () => (
  46. <View compId={props.compId}>
  47. <canvas ref={canvasRef} style={{width:"100%", height: "100%"}}> </canvas>
  48. </View>
  49. );
  50. },
  51. });