2024-11-29 23:08:47 -05:00
|
|
|
import React, {useCallback, useEffect, useState, createContext} from "react";
|
2024-11-24 00:06:22 -05:00
|
|
|
import {useColorMode} from "@docusaurus/theme-common";
|
|
|
|
|
2024-11-29 23:08:47 -05:00
|
|
|
export interface PainterProps {
|
|
|
|
readonly width: number;
|
|
|
|
readonly height: number;
|
|
|
|
readonly setPainter: (painter: Iterator<ImageData>) => void;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Context provider for child elements to submit image iterator functions
|
|
|
|
* (painters) for rendering
|
|
|
|
*/
|
|
|
|
export const PainterContext = createContext<PainterProps>(null);
|
|
|
|
|
|
|
|
interface CanvasProps {
|
2024-11-30 18:01:29 -05:00
|
|
|
width?: number;
|
|
|
|
height?: number;
|
2024-12-01 15:16:30 -05:00
|
|
|
hidden?: boolean;
|
2024-11-24 00:06:22 -05:00
|
|
|
children?: React.ReactNode;
|
|
|
|
}
|
2024-11-24 18:59:11 -05:00
|
|
|
|
2024-11-29 23:08:47 -05:00
|
|
|
/**
|
|
|
|
* Draw fractal flames to a canvas.
|
|
|
|
*
|
|
|
|
* This component is a bit involved because it attempts to solve
|
2024-12-01 15:16:30 -05:00
|
|
|
* a couple problems at once:
|
2024-11-29 23:08:47 -05:00
|
|
|
* - Incrementally drawing an image to the canvas
|
2024-12-01 15:16:30 -05:00
|
|
|
* - Interrupting drawing with new parameters
|
2024-11-29 23:08:47 -05:00
|
|
|
* - Dark mode
|
|
|
|
*
|
2024-12-01 15:16:30 -05:00
|
|
|
* Running a full render is labor-intensive, so we model it
|
|
|
|
* as an iterator that yields an image of the current system.
|
|
|
|
* Internally, that iterator is re-queued on each new image;
|
|
|
|
* so long as that image is returned quickly, we keep
|
|
|
|
* the main loop running even with CPU-heavy code.
|
2024-11-29 23:08:47 -05:00
|
|
|
*
|
2024-12-01 15:16:30 -05:00
|
|
|
* To interrupt drawing, children set the active iterator
|
|
|
|
* through the context provider. This component doesn't care
|
|
|
|
* about which iterator is in progress, it exists only
|
|
|
|
* to fetch the next image and paint it to our canvas.
|
2024-11-29 23:08:47 -05:00
|
|
|
*
|
2024-12-01 15:16:30 -05:00
|
|
|
* Finally, we make a distinction between "render" and "paint" buffers.
|
|
|
|
* The render image is provided by the iterator, and then:
|
|
|
|
* - If light mode is active, draw it to the canvas as-is
|
|
|
|
* - If dark mode is active, copy the "render" buffer to the "paint" buffer,
|
|
|
|
* invert colors, and then draw the image
|
2024-11-29 23:08:47 -05:00
|
|
|
*
|
2024-12-01 15:16:30 -05:00
|
|
|
* TODO(bspeice): Can we make this "re-queueing iterator" pattern generic?
|
|
|
|
* It would be nice to have iterators returning arbitrary objects,
|
|
|
|
* but we rely on contexts to manage the iterator, and there's
|
|
|
|
* no good way to make those generic.
|
2024-11-29 23:08:47 -05:00
|
|
|
*
|
|
|
|
* @param width Canvas draw width
|
|
|
|
* @param height Canvas draw height
|
2024-12-01 15:16:30 -05:00
|
|
|
* @param hidden Hide the canvas
|
2024-11-29 23:08:47 -05:00
|
|
|
* @param children Child elements
|
|
|
|
*/
|
2024-12-01 15:16:30 -05:00
|
|
|
export default function Canvas({width, height, hidden, children}: CanvasProps) {
|
2024-11-24 18:59:11 -05:00
|
|
|
const [canvasCtx, setCanvasCtx] = useState<CanvasRenderingContext2D>(null);
|
2024-11-24 00:06:22 -05:00
|
|
|
const canvasRef = useCallback(node => {
|
|
|
|
if (node !== null) {
|
|
|
|
setCanvasCtx(node.getContext("2d"));
|
|
|
|
}
|
|
|
|
}, []);
|
|
|
|
|
2024-11-29 23:08:47 -05:00
|
|
|
// Holder objects are used to force re-painting even if the iterator
|
|
|
|
// returns a modified image with the same reference
|
|
|
|
type ImageHolder = { image?: ImageData };
|
2024-11-30 12:39:54 -05:00
|
|
|
|
|
|
|
const [paintImage, setPaintImage] = useState<ImageHolder>({ image: null });
|
2024-11-29 23:08:47 -05:00
|
|
|
useEffect(() => {
|
2024-11-30 12:39:54 -05:00
|
|
|
if (paintImage.image && canvasCtx) {
|
|
|
|
canvasCtx.putImageData(paintImage.image, 0, 0);
|
2024-11-29 23:08:47 -05:00
|
|
|
}
|
2024-11-30 12:39:54 -05:00
|
|
|
}, [paintImage, canvasCtx]);
|
2024-11-29 23:08:47 -05:00
|
|
|
|
2024-11-30 12:39:54 -05:00
|
|
|
const {colorMode} = useColorMode();
|
|
|
|
const [renderImage, setRenderImage] = useState<ImageHolder>({ image: null });
|
|
|
|
useEffect(() => {
|
|
|
|
const image = renderImage.image;
|
|
|
|
if (!image) {
|
2024-11-24 00:06:22 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-11-29 23:08:47 -05:00
|
|
|
// If light mode is active, paint the image as-is
|
|
|
|
if (colorMode === 'light') {
|
2024-11-30 12:39:54 -05:00
|
|
|
setPaintImage({ image });
|
2024-11-29 23:08:47 -05:00
|
|
|
return;
|
2024-11-24 00:06:22 -05:00
|
|
|
}
|
|
|
|
|
2024-11-29 23:08:47 -05:00
|
|
|
// If dark mode is active, copy the image into a new buffer
|
|
|
|
// and invert colors prior to painting.
|
|
|
|
// Copy alpha values as-is.
|
|
|
|
const paintImage = new ImageData(image.width, image.height);
|
|
|
|
image.data.forEach((value, index) => {
|
|
|
|
const isAlpha = index % 4 === 3;
|
|
|
|
paintImage.data[index] = isAlpha ? value : 255 - value;
|
|
|
|
})
|
2024-11-30 12:39:54 -05:00
|
|
|
setPaintImage({ image: paintImage });
|
|
|
|
}, [colorMode, renderImage]);
|
2024-11-24 18:59:11 -05:00
|
|
|
|
2024-11-29 23:08:47 -05:00
|
|
|
// Image iterators (painters) are also in a holder; this allows
|
|
|
|
// re-submitting the existing iterator to draw the next frame,
|
|
|
|
// and also allows child components to over-write the iterator
|
|
|
|
// if a new set of parameters becomes available
|
|
|
|
// TODO(bspeice): Potential race condition?
|
|
|
|
// Not sure if it's possible for painters submitted by children
|
|
|
|
// to be over-ridden as a result re-submitting the
|
|
|
|
// existing iterator
|
|
|
|
type PainterHolder = { painter?: Iterator<ImageData> };
|
|
|
|
const [animHolder, setAnimHolder] = useState<PainterHolder>({ painter: null });
|
|
|
|
useEffect(() => {
|
|
|
|
const painter = animHolder.painter;
|
2024-11-24 22:37:53 -05:00
|
|
|
if (!painter) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-11-29 23:08:47 -05:00
|
|
|
if (!canvasCtx) {
|
|
|
|
setAnimHolder({ painter });
|
|
|
|
return;
|
2024-11-24 18:59:11 -05:00
|
|
|
}
|
2024-11-29 23:08:47 -05:00
|
|
|
|
|
|
|
const image = painter.next().value;
|
|
|
|
if (image) {
|
2024-11-30 12:39:54 -05:00
|
|
|
setRenderImage({ image });
|
2024-11-29 23:08:47 -05:00
|
|
|
setAnimHolder({ painter });
|
|
|
|
} else {
|
|
|
|
setAnimHolder({ painter: null });
|
|
|
|
}
|
|
|
|
}, [animHolder, canvasCtx]);
|
|
|
|
|
|
|
|
// Finally, child elements submit painters through a context provider
|
|
|
|
const [painter, setPainter] = useState<Iterator<ImageData>>(null);
|
|
|
|
useEffect(() => setAnimHolder({ painter }), [painter]);
|
2024-11-24 00:06:22 -05:00
|
|
|
|
2024-11-30 18:01:29 -05:00
|
|
|
width = width ?? 500;
|
|
|
|
height = height ?? 500;
|
2024-11-24 00:06:22 -05:00
|
|
|
return (
|
2024-11-24 18:59:11 -05:00
|
|
|
<>
|
2024-11-29 23:08:47 -05:00
|
|
|
<center>
|
|
|
|
<canvas
|
|
|
|
ref={canvasRef}
|
|
|
|
width={width}
|
|
|
|
height={height}
|
2024-12-01 15:16:30 -05:00
|
|
|
hidden={hidden ?? false}
|
2024-11-29 23:08:47 -05:00
|
|
|
style={{
|
|
|
|
aspectRatio: width / height,
|
|
|
|
width: '80%'
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
</center>
|
|
|
|
<PainterContext.Provider value={{width, height, setPainter}}>
|
|
|
|
{children}
|
|
|
|
</PainterContext.Provider>
|
2024-11-24 18:59:11 -05:00
|
|
|
</>
|
2024-11-24 00:06:22 -05:00
|
|
|
)
|
|
|
|
}
|