speice.io/blog/2024-11-15-playing-with-fire/2-transforms/FlameBlend.tsx

71 lines
3.0 KiB
TypeScript
Raw Normal View History

import {useContext, useEffect, useState} from "react";
2024-11-29 19:25:29 -05:00
import {Transform} from "../src/transform";
2024-12-01 21:57:10 -05:00
import * as params from "../src/params"
import {PainterContext} from "../src/Canvas"
2024-11-30 17:35:42 -05:00
import {chaosGameFinal} from "./chaosGameFinal"
import {VariationEditor, VariationProps} from "./VariationEditor"
2024-12-01 21:57:10 -05:00
import {xform1Weight} from "../src/params";
import {applyTransform} from "@site/blog/2024-11-15-playing-with-fire/src/applyTransform";
import {buildBlend} from "@site/blog/2024-11-15-playing-with-fire/2-transforms/buildBlend";
2024-11-29 19:25:29 -05:00
export default function FlameBlend() {
const {width, height, setPainter} = useContext(PainterContext);
const xform1VariationsDefault: VariationProps = {
2024-11-29 19:25:29 -05:00
linear: 0,
julia: 1,
popcorn: 0,
pdj: 0,
}
const [xform1Variations, setXform1Variations] = useState(xform1VariationsDefault)
const resetXform1Variations = () => setXform1Variations(xform1VariationsDefault);
2024-11-29 19:25:29 -05:00
const xform2VariationsDefault: VariationProps = {
2024-11-29 19:25:29 -05:00
linear: 1,
julia: 0,
popcorn: 1,
pdj: 0
}
const [xform2Variations, setXform2Variations] = useState(xform2VariationsDefault)
const resetXform2Variations = () => setXform2Variations(xform2VariationsDefault);
2024-11-29 19:25:29 -05:00
const xform3VariationsDefault: VariationProps = {
2024-11-29 19:25:29 -05:00
linear: 0,
julia: 0,
popcorn: 0,
pdj: 1
}
const [xform3Variations, setXform3Variations] = useState(xform3VariationsDefault)
const resetXform3Variations = () => setXform3Variations(xform3VariationsDefault);
2024-11-29 19:25:29 -05:00
2024-11-30 17:35:42 -05:00
// Cheating a bit here; for purposes of code re-use, use the post- and final-transform-enabled chaos game,
// and swap in identity components for each
const identityXform: Transform = (x, y) => [x, y];
2024-11-29 19:25:29 -05:00
2024-12-01 21:57:10 -05:00
useEffect(() => {
const transforms: [number, Transform][] = [
[params.xform1Weight, applyTransform(params.xform1Coefs, buildBlend(params.xform1Coefs, xform1Variations))],
[params.xform2Weight, applyTransform(params.xform2Coefs, buildBlend(params.xform2Coefs, xform2Variations))],
[params.xform3Weight, applyTransform(params.xform3Coefs, buildBlend(params.xform3Coefs, xform3Variations))]
]
const gameParams = {
width,
height,
transforms,
final: identityXform
}
setPainter(chaosGameFinal(gameParams));
}, [xform1Variations, xform2Variations, xform3Variations]);
2024-11-29 19:25:29 -05:00
return (
2024-11-30 17:35:42 -05:00
<>
<VariationEditor title={"Transform 1"} variations={xform1Variations} setVariations={setXform1Variations}
resetVariations={resetXform1Variations}/>
<VariationEditor title={"Transform 2"} variations={xform2Variations} setVariations={setXform2Variations}
resetVariations={resetXform2Variations}/>
<VariationEditor title={"Transform 3"} variations={xform3Variations} setVariations={setXform3Variations}
resetVariations={resetXform3Variations}/>
2024-11-30 17:35:42 -05:00
</>
2024-11-29 19:25:29 -05:00
)
}