import React, {useCallback, useEffect, useState, createContext} from "react"; import {useColorMode} from "@docusaurus/theme-common"; import BrowserOnly from "@docusaurus/BrowserOnly"; function invertImage(sourceImage: ImageData): ImageData { const image = new ImageData(sourceImage.width, sourceImage.height); image.data.forEach((value, index) => image.data[index] = index % 4 === 3 ? value : 0xff - value) return image; } type InvertibleCanvasProps = { width: number, height: number, hidden?: boolean, // NOTE: Images are provided as a single-element array //so we can allow re-painting with the same (modified) ImageData reference. image?: [ImageData], } /** * Draw images to a canvas, automatically inverting colors as needed. * * @param width Canvas width * @param height Canvas height * @param hidden Hide the canvas element * @param image Image data to draw on the canvas */ const InvertibleCanvas: React.FC = ({width, height, hidden, image}) => { const [canvasCtx, setCanvasCtx] = useState(null); const canvasRef = useCallback(node => { if (node !== null) { setCanvasCtx(node.getContext("2d")); } }, []); const [paintImage, setPaintImage] = useState<[ImageData]>(null); useEffect(() => { if (canvasCtx && paintImage) { canvasCtx.putImageData(paintImage[0], 0, 0); } }, [canvasCtx, paintImage]); const {colorMode} = useColorMode(); useEffect(() => { if (image) { setPaintImage(colorMode === 'light' ? image : [invertImage(image[0])]); } }, [image, colorMode]); return (