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