Basic function weights

This commit is contained in:
2024-11-24 22:37:53 -05:00
parent 4c3f4246a4
commit 7759b58dbe
24 changed files with 172 additions and 62 deletions

View File

@ -39,13 +39,18 @@ export default function Canvas({width, height, painter, children}: Props) {
useEffect(paint, [colorMode, image]);
const animate = () => {
if (!painter) {
return;
}
console.log("Animating");
const nextImage = painter.next().value;
if (nextImage) {
setImage([nextImage])
requestAnimationFrame(animate);
}
}
useEffect(animate, [canvasCtx]);
useEffect(animate, [painter, canvasCtx]);
return (
<>

View File

@ -1,5 +1,5 @@
// hidden-start
import { Variation } from './variations'
import { Variation } from './variation'
// hidden-end
export const julia: Variation = (x, y) => {
const r = Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2));

View File

@ -1,4 +1,4 @@
// hidden-start
import {Variation} from "./variations"
import {Variation} from "./variation"
//hidden-end
export const linear: Variation = (x, y) => [x, y];

View File

@ -4,7 +4,6 @@
*/
import { Coefs } from './coefs';
import { Transform } from './transform';
import { linear } from './linear'
import { julia } from './julia'
import { popcorn } from './popcorn'

View File

@ -1,5 +1,5 @@
// hidden-start
import { Variation } from './variations'
import { Variation } from './variation'
//hidden-end
export function pdj(a: number, b: number, c: number, d: number): Variation {
return (x, y) => [

View File

@ -1,6 +1,6 @@
// hidden-start
import {Coefs} from './coefs'
import {Variation} from './variations'
import {Variation} from './variation'
// hidden-end
export function popcorn({c, f}: Coefs): Variation {
return (x, y) => [

View File

@ -0,0 +1,3 @@
export function randomBiUnit() {
return Math.random() * 2 - 1;
}

View File

@ -0,0 +1,15 @@
export function randomChoice<T>(choices: [number, T][]): [number, T] {
const weightSum = choices.reduce((sum, [weight, _]) => sum + weight, 0);
let choice = Math.random() * weightSum;
for (const [index, element] of choices.entries()) {
const [weight, t] = element;
if (choice < weight) {
return [index, t];
}
choice -= weight;
}
const index = choices.length - 1;
return [index, choices[index][1]];
}

View File

@ -0,0 +1,3 @@
export function randomInteger(min: number, max: number) {
return Math.floor(Math.random() * (max - min)) + min;
}

View File

@ -1,5 +1,5 @@
import { Coefs } from './coefs'
import { Variation } from './variations'
import { Variation } from './variation'
export interface Transform {
coefs: Coefs,