Skip to content

Is this canvas resize hook a useful addition? #205

@petervdn

Description

@petervdn

I'm using this in a personal project, was wondering if it could be of use here.

type Props = {
  width: number;
  height: number;
  canvasRef: RefObject<HTMLCanvasElement | null>;
};

export function useSizedCanvas({ canvasRef, height, width }: Props) {
  useEffect(() => {
    if (!canvasRef.current) {
      return;
    }

    canvasRef.current.style.width = `${width}px`;
    canvasRef.current.style.height = `${height}px`;
    canvasRef.current.width = width * window.devicePixelRatio;
    canvasRef.current.height = height * window.devicePixelRatio;
  }, [width, height, canvasRef]);
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions