Skip to content

Commit

Permalink
Make sure we have a context before fetching canvas
Browse files Browse the repository at this point in the history
  • Loading branch information
kirkegaard committed Mar 16, 2024
1 parent 0bc19ba commit d0d62df
Showing 1 changed file with 17 additions and 15 deletions.
32 changes: 17 additions & 15 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,27 +58,29 @@ export const useCanvas = ({ setup, draw, options = {} }) => {
useEffect(() => {
if (canvasRef.current) {
const context = getContext();
const { canvas } = context;
if (context) {
const { canvas } = context;

if (contextType === "2d") {
const ratio = window.devicePixelRatio || 1;
if (contextType === "2d") {
const ratio = window.devicePixelRatio || 1;

canvas.setAttribute("width", width * ratio);
canvas.setAttribute("height", height * ratio);
canvas.setAttribute("width", width * ratio);
canvas.setAttribute("height", height * ratio);

context.scale(ratio, ratio);
} else {
canvas.setAttribute("width", width);
canvas.setAttribute("height", height);
}
context.scale(ratio, ratio);
} else {
canvas.setAttribute("width", width);
canvas.setAttribute("height", height);
}

canvas.style.width = width + "px";
canvas.style.height = height + "px";
canvas.style.width = width + "px";
canvas.style.height = height + "px";

if (typeof setup === "function") {
setup({ context, height, width });
if (typeof setup === "function") {
setup({ context, height, width });
}
render();
}
render();
}

return () => window.cancelAnimationFrame(animationFrameIdRef.current);
Expand Down

0 comments on commit d0d62df

Please sign in to comment.