You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

31 lines
904 B

5 months ago
  1. import { useCallback, useEffect, useRef } from "react";
  2. export default function RealtimeLayer(props: {
  3. width: number;
  4. height: number;
  5. leftPadding: number;
  6. rightPadding: number;
  7. topPadding: number;
  8. bottomPadding: number;
  9. }) {
  10. const canvasRef = useRef<HTMLCanvasElement | null>(null);
  11. const draw = useCallback((ctx: CanvasRenderingContext2D) => {
  12. // 使用context对象进行绘图
  13. ctx.fillStyle = "skyblue"; // 设置填充颜色
  14. ctx.fillRect(50, 50, 150, 100); // 绘制一个矩形
  15. }, []);
  16. useEffect(() => {
  17. const canvas = canvasRef.current;
  18. if (!canvas) return;
  19. const context = canvas.getContext("2d");
  20. if (!context) return;
  21. draw(context);
  22. }, [draw]);
  23. return (
  24. <div>
  25. <canvas ref={canvasRef} width={props.width} height={props.height}></canvas>
  26. </div>
  27. );
  28. }