import {useState} from "react"; import { blend } from "./blend"; import { applyCoefs, Coefs } from "../src/coefs" import {randomBiUnit} from "../src/randomBiUnit"; import {linear} from "../src/linear"; import {julia} from "../src/julia"; import {popcorn} from "../src/popcorn"; import {pdj} from "../src/pdj"; import {Variation} from "../src/variation"; import {Transform} from "../src/transform"; import { pdjParams, xform1Coefs, xform1Weight, xform2Coefs, xform2Weight, xform3Coefs, xform3Weight } from "../src/params"; import {randomChoice} from "../src/randomChoice"; import {plotBinary} from "../src/plotBinary" import Canvas from "../src/Canvas" import styles from "../src/css/styles.module.css" type VariationBlend = { linear: number, julia: number, popcorn: number, pdj: number } export default function FlameBlend() { const image = new ImageData(400, 400); const quality = 2; const step = 5000; const xform1Default: VariationBlend = { linear: 0, julia: 1, popcorn: 0, pdj: 0, } const [xform1Variations, setXform1Variations] = useState(xform1Default) const xform2Default: VariationBlend = { linear: 1, julia: 0, popcorn: 1, pdj: 0 } const [xform2Variations, setXform2Variations] = useState(xform2Default) const xform3Default: VariationBlend = { linear: 0, julia: 0, popcorn: 0, pdj: 1 } const [xform3Variations, setXform3Variations] = useState(xform3Default) function buildTransform(coefs: Coefs, variations: VariationBlend): Transform { return (x: number, y: number) => { const [varX, varY] = applyCoefs(x, y, coefs); const varFunctions: [number, Variation][] = [ [variations.linear, linear], [variations.julia, julia], [variations.popcorn, popcorn(coefs)], [variations.pdj, pdj(pdjParams)] ] return blend(varX, varY, varFunctions); } } function* chaosGame() { let [x, y] = [randomBiUnit(), randomBiUnit()]; const transforms: [number, Transform][] = [ [xform1Weight, buildTransform(xform1Coefs, xform1Variations)], [xform2Weight, buildTransform(xform2Coefs, xform2Variations)], [xform3Weight, buildTransform(xform3Coefs, xform3Variations)] ] const iterations = quality * image.width * image.height; for (let i = 0; i < iterations; i++) { let [_, transform] = randomChoice(transforms); [x, y] = transform(x, y); if (i > 20) plotBinary(x, y, image); if (i % step === 0) { console.log(`Checking in; iterations=${i}`) yield image; } } yield image; } const variationEditor = (title, variations, setVariations) => { return ( <>
{title}:
Linear: {variations.linear}
setVariations({...variations, linear: Number(e.currentTarget.value)})}/>Julia: {variations.julia}
setVariations({...variations, julia: Number(e.currentTarget.value)})}/>Popcorn: {variations.popcorn}
setVariations({...variations, popcorn: Number(e.currentTarget.value)})}/>PDJ: {variations.pdj}
setVariations({...variations, pdj: Number(e.currentTarget.value)})}/>