diff --git a/blog/2024-11-15-playing-with-fire/3-log-density/FlameColor.tsx b/blog/2024-11-15-playing-with-fire/3-log-density/FlameColor.tsx index b46418e..88453f4 100644 --- a/blog/2024-11-15-playing-with-fire/3-log-density/FlameColor.tsx +++ b/blog/2024-11-15-playing-with-fire/3-log-density/FlameColor.tsx @@ -2,14 +2,32 @@ import React, {useContext, useEffect, useMemo, useRef, useState} from "react"; import * as params from "../src/params"; import {PainterContext} from "../src/Canvas"; import {colorFromPalette} from "./paintColor"; -import {chaosGameColor, ChaosGameColorProps, TransformColor} from "./chaosGameColor"; +import {chaosGameColor, Props as ChaosGameColorProps, TransformColor} from "./chaosGameColor"; import styles from "../src/css/styles.module.css"; import {histIndex} from "../src/camera"; import {useColorMode} from "@docusaurus/theme-common"; -const paletteBarPainter = (palette: number[]) => - (width: number, height: number) => { +type PaletteBarProps = { + height: number; + palette: number[]; + children?: React.ReactNode; +} +const PaletteBar: React.FC = ({height, palette, children}) => { + const sizingRef = useRef(null); + const [width, setWidth] = useState(0); + useEffect(() => { + if (sizingRef) { + setWidth(sizingRef.current.offsetWidth); + } + }, [sizingRef]); + + const canvasRef = useRef(null); + const paletteImage = useMemo(() => { + if (width === 0) { + return; + } + const image = new ImageData(width, height); for (let x = 0; x < width; x++) { const colorIndex = x / width; @@ -23,21 +41,20 @@ const paletteBarPainter = (palette: number[]) => image.data[pixelIndex + 3] = 0xff; } } - return image; - } -type PaletteBarProps = { - height: number; - palette: number[]; - children?: React.ReactNode; -} -const PaletteBar: React.FC = ({height, palette, children}) => { - const painter = useMemo(() => paletteBarPainter(palette), [palette]); + return image; + }, [width, height, palette]); + + useEffect(() => { + if (canvasRef && paletteImage) { + canvasRef.current.getContext("2d").putImageData(paletteImage, 0, 0); + } + }, [canvasRef, paletteImage]); return ( <> -
- {/**/} +
+ {width > 0 ? : null}
{children} @@ -115,7 +132,6 @@ export default function FlameColor({quality, children}: Props) { height, transforms: params.xforms, final: params.xformFinal, - quality, palette: params.palette, colors: [xform1Color, xform2Color, xform3Color], finalColor: xformFinalColor diff --git a/blog/2024-11-15-playing-with-fire/3-log-density/FlameHistogram.tsx b/blog/2024-11-15-playing-with-fire/3-log-density/FlameHistogram.tsx index d0b11de..3ac3a86 100644 --- a/blog/2024-11-15-playing-with-fire/3-log-density/FlameHistogram.tsx +++ b/blog/2024-11-15-playing-with-fire/3-log-density/FlameHistogram.tsx @@ -4,11 +4,10 @@ import {PainterContext} from "../src/Canvas"; import {chaosGameHistogram} from "./chaosGameHistogram"; type Props = { - quality?: number; - paint: (width: number, histogram: Uint32Array) => ImageData; + paint: (width: number, height: number, histogram: number[]) => ImageData; children?: React.ReactElement; } -export default function FlameHistogram({quality, paint, children}: Props) { +export default function FlameHistogram({paint, children}: Props) { const {width, height, setPainter} = useContext(PainterContext); useEffect(() => { @@ -17,11 +16,10 @@ export default function FlameHistogram({quality, paint, children}: Props) { height, transforms, final, - quality, paint } setPainter(chaosGameHistogram(gameParams)); - }, []); + }, [width, height]); return children; } \ No newline at end of file diff --git a/blog/2024-11-15-playing-with-fire/3-log-density/chaosGameColor.ts b/blog/2024-11-15-playing-with-fire/3-log-density/chaosGameColor.ts index 4d35735..58b253b 100644 --- a/blog/2024-11-15-playing-with-fire/3-log-density/chaosGameColor.ts +++ b/blog/2024-11-15-playing-with-fire/3-log-density/chaosGameColor.ts @@ -17,7 +17,7 @@ function mixColor(color1: number, color2: number, colorSpeed: number) { return color1 * (1 - colorSpeed) + color2 * colorSpeed; } -type Props = ChaosGameFinalProps & { +export type Props = ChaosGameFinalProps & { palette: number[]; colors: TransformColor[]; finalColor: TransformColor; diff --git a/blog/2024-11-15-playing-with-fire/src/Canvas.tsx b/blog/2024-11-15-playing-with-fire/src/Canvas.tsx index 0b262d7..012c540 100644 --- a/blog/2024-11-15-playing-with-fire/src/Canvas.tsx +++ b/blog/2024-11-15-playing-with-fire/src/Canvas.tsx @@ -14,8 +14,17 @@ type CanvasProps = { children?: React.ReactElement } export const Canvas: React.FC = ({style, children}) => { - const canvasRef = useRef(null); + const sizingRef = useRef(null); + const [width, setWidth] = useState(0); + const [height, setHeight] = useState(0); + useEffect(() => { + if (sizingRef.current) { + setWidth(sizingRef.current.offsetWidth); + setHeight(sizingRef.current.offsetHeight); + } + }, [sizingRef]); + const canvasRef = useRef(null); const [isVisible, setIsVisible] = useState(false); useEffect(() => { if (!canvasRef.current) { @@ -35,16 +44,7 @@ export const Canvas: React.FC = ({style, children}) => { observer.unobserve(canvasRef.current); } } - }, [canvasRef]); - - const [width, setWidth] = useState(0); - const [height, setHeight] = useState(0); - useEffect(() => { - if (canvasRef.current) { - setWidth(canvasRef.current.offsetWidth); - setHeight(canvasRef.current.offsetHeight); - } - }, [canvasRef]); + }, [canvasRef.current]); const [imageHolder, setImageHolder] = useState<[ImageData]>(null); useEffect(() => { @@ -76,25 +76,22 @@ export const Canvas: React.FC = ({style, children}) => { } }, [painter]); - const {colorMode} = useColorMode(); + const filter = useColorMode().colorMode === 'dark' ? 'invert(1)' : ''; + return ( <> - +
+
+ {width > 0 ? : null} +
+
- {() => children} + {width > 0 ? children : null} ) } export const SquareCanvas: React.FC = ({style, children}) => { - return + return
} \ No newline at end of file