speice.io/blog/2024-11-15-playing-with-fire/src/Canvas.tsx

100 lines
3.0 KiB
TypeScript
Raw Normal View History

import React, {useEffect, useState, createContext, useRef} from "react";
import {useColorMode} from "@docusaurus/theme-common";
import BrowserOnly from "@docusaurus/BrowserOnly";
type PainterProps = {
width: number;
height: number;
setPainter: (painter: Iterator<ImageData>) => void;
}
export const PainterContext = createContext<PainterProps>(null)
type CanvasProps = {
style?: any;
children?: React.ReactElement
}
export const Canvas: React.FC<CanvasProps> = ({style, children}) => {
const canvasRef = useRef<HTMLCanvasElement>(null);
2024-11-24 18:59:11 -05:00
2024-12-08 16:17:21 -05:00
const [isVisible, setIsVisible] = useState(false);
useEffect(() => {
if (!canvasRef.current) {
2024-12-08 16:17:21 -05:00
return;
}
const observer = new IntersectionObserver((entries) => {
const [entry] = entries;
2024-12-08 16:17:21 -05:00
if (entry.isIntersecting) {
setIsVisible(true);
}
});
observer.observe(canvasRef.current);
2024-12-08 16:17:21 -05:00
return () => {
if (canvasRef.current) {
observer.unobserve(canvasRef.current);
2024-12-08 16:17:21 -05:00
}
}
}, [canvasRef]);
const [width, setWidth] = useState(0);
const [height, setHeight] = useState(0);
useEffect(() => {
if (canvasRef.current) {
setWidth(canvasRef.current.offsetWidth);
setHeight(canvasRef.current.offsetHeight);
}
}, [canvasRef]);
const [imageHolder, setImageHolder] = useState<[ImageData]>(null);
useEffect(() => {
if (canvasRef.current && imageHolder) {
canvasRef.current.getContext("2d").putImageData(imageHolder[0], 0, 0);
}
}, [canvasRef, imageHolder]);
2024-12-08 16:17:21 -05:00
const [painterHolder, setPainterHolder] = useState<[Iterator<ImageData>]>(null);
useEffect(() => {
2024-12-08 16:17:21 -05:00
if (!isVisible || !painterHolder) {
return;
}
2024-11-24 22:37:53 -05:00
const painter = painterHolder[0];
const nextImage = painter.next().value;
if (nextImage) {
setImageHolder([nextImage]);
setPainterHolder([painter]);
} else {
setPainterHolder(null);
}
2024-12-08 16:17:21 -05:00
}, [isVisible, painterHolder]);
const [painter, setPainter] = useState<Iterator<ImageData>>(null);
useEffect(() => {
if (painter) {
setPainterHolder([painter]);
}
}, [painter]);
const {colorMode} = useColorMode();
return (
2024-11-24 18:59:11 -05:00
<>
<canvas
ref={canvasRef}
width={width}
height={height}
style={{
filter: colorMode === 'dark' ? 'invert(1)' : '',
...style
}}
/>
<PainterContext.Provider value={{width, height, setPainter}}>
<BrowserOnly>{() => children}</BrowserOnly>
</PainterContext.Provider>
2024-11-24 18:59:11 -05:00
</>
)
}
export const SquareCanvas: React.FC<CanvasProps> = ({style, children}) => {
return <Canvas style={{width: '100%', aspectRatio: '1/1', ...style}} children={children}/>
}