mirror of
https://github.com/bspeice/speice.io
synced 2025-07-29 03:25:03 -04:00
Basic function weights
This commit is contained in:
@ -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 (
|
||||
<>
|
||||
|
@ -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));
|
||||
|
@ -1,4 +1,4 @@
|
||||
// hidden-start
|
||||
import {Variation} from "./variations"
|
||||
import {Variation} from "./variation"
|
||||
//hidden-end
|
||||
export const linear: Variation = (x, y) => [x, y];
|
@ -4,7 +4,6 @@
|
||||
*/
|
||||
|
||||
import { Coefs } from './coefs';
|
||||
import { Transform } from './transform';
|
||||
import { linear } from './linear'
|
||||
import { julia } from './julia'
|
||||
import { popcorn } from './popcorn'
|
||||
|
@ -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) => [
|
||||
|
@ -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) => [
|
||||
|
3
blog/2024-11-15-playing-with-fire/src/randomBiUnit.ts
Normal file
3
blog/2024-11-15-playing-with-fire/src/randomBiUnit.ts
Normal file
@ -0,0 +1,3 @@
|
||||
export function randomBiUnit() {
|
||||
return Math.random() * 2 - 1;
|
||||
}
|
15
blog/2024-11-15-playing-with-fire/src/randomChoice.ts
Normal file
15
blog/2024-11-15-playing-with-fire/src/randomChoice.ts
Normal 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]];
|
||||
}
|
3
blog/2024-11-15-playing-with-fire/src/randomInteger.ts
Normal file
3
blog/2024-11-15-playing-with-fire/src/randomInteger.ts
Normal file
@ -0,0 +1,3 @@
|
||||
export function randomInteger(min: number, max: number) {
|
||||
return Math.floor(Math.random() * (max - min)) + min;
|
||||
}
|
@ -1,5 +1,5 @@
|
||||
import { Coefs } from './coefs'
|
||||
import { Variation } from './variations'
|
||||
import { Variation } from './variation'
|
||||
|
||||
export interface Transform {
|
||||
coefs: Coefs,
|
||||
|
Reference in New Issue
Block a user