Fractal Flame
別名: flame fractal、flame algorithm
初出 1992 · Scott Draves — Created by Scott Draves in 1992 as an open-source iterated-function-system variant; later ported widely (e.g. the Apophysis editor). · 年表 ↗
発明者による命名 名称 · 命名 Scott Draves (1992) — 考案者による命名。Scott Draves が 1992 年に「fractal flame」と命名。
ひとことで 反復関数系(IFS)を一般化したフラクタルの一種。非線形の「バリエーション」関数、対数濃度(トーンマッピング)による蓄積、構造的な彩色を加え、Apophysis などで知られる滑らかで発光する炎や煙のような形を生み出す。
コード
上のデモの実際のスケッチ(サンドボックスで再現可能)。これで全部です。
fractal-flame/index.html
// Fractal flame (Scott Draves, 1992) — a chaos game over an Iterated Function
// System whose maps are affine transforms composed with non-linear "variations"
// (sinusoidal, spherical, swirl, horseshoe…). Each iterate lands in a bin; we
// accumulate hit DENSITY (+ a colour coordinate per function), then tone-map
// with a logarithmic transfer so dense cores glow without clipping. Vanilla canvas.
const SEED = (Math.random() * 1e9) | 0;
let s = SEED || 1;
const rnd = () => (s = (s * 1103515245 + 12345) & 0x7fffffff) / 0x7fffffff;
const cv = document.getElementById('c');
const ctx = cv.getContext('2d');
const DIM = Math.floor(Math.min(window.innerWidth, window.innerHeight));
const RES = Math.min(DIM, 560);
cv.width = cv.height = RES;
cv.style.width = cv.style.height = DIM + 'px';
// palette (colour by which function fired)
const PAL = [[255,90,60],[255,190,40],[80,200,255],[190,90,255],[60,255,160]];
// variations
function V(k, x, y) {
const r2 = x*x + y*y, r = Math.sqrt(r2) || 1e-9, th = Math.atan2(y, x);
switch (k) {
case 0: return [x, y]; // linear
case 1: return [Math.sin(x), Math.sin(y)]; // sinusoidal
case 2: return [x / r2, y / r2]; // spherical
case 3: return [x*Math.sin(r2) - y*Math.cos(r2), x*Math.cos(r2) + y*Math.sin(r2)]; // swirl
case 4: return [(x - y)*(x + y)/r, 2*x*y/r]; // horseshoe
case 5: return [Math.sin(th)*Math.cos(r), Math.cos(th)*Math.sin(r)]; // handkerchief-ish
}
return [x, y];
}
// build 3–4 random functions: affine + a chosen variation + colour
const NF = 3 + (rnd() * 2 | 0);
const F = [];
for (let i = 0; i < NF; i++) {
// contractive affine maps (rotation+scale < 1, modest translation) so the
// attractor stays bounded and dense — reliably flame-like across seeds
const ang = rnd() * Math.PI * 2;
const sc = 0.35 + rnd() * 0.5; // scale 0.35–0.85 (contractive)
const co = Math.cos(ang) * sc, si = Math.sin(ang) * sc;
F.push({
m: [co, -si, (rnd()*2-1)*0.9, si, co, (rnd()*2-1)*0.9], // rotate-scale + translate
v: (rnd() * 6) | 0,
col: PAL[i % PAL.length],
w: 0.4 + rnd(),
});
}
const wsum = F.reduce((s, f) => s + f.w, 0);
// accumulation buffers
const R = new Float32Array(RES * RES), G = new Float32Array(RES * RES), B = new Float32Array(RES * RES), A = new Float32Array(RES * RES);
let px = rnd() * 2 - 1, py = rnd() * 2 - 1;
let cr = 0, cg = 0, cb = 0;
const SCALE = RES / 4.2, OX = RES / 2, OY = RES / 2;
function iterate(n) {
for (let i = 0; i < n; i++) {
// choose a function by weight
let t = rnd() * wsum, k = 0; while ((t -= F[k].w) > 0 && k < NF - 1) k++;
const f = F[k], m = f.m;
const ax = m[0]*px + m[1]*py + m[2], ay = m[3]*px + m[4]*py + m[5];
const [vx, vy] = V(f.v, ax, ay);
px = vx; py = vy;
// drift the running colour toward this function's colour
cr = (cr + f.col[0]) / 2; cg = (cg + f.col[1]) / 2; cb = (cb + f.col[2]) / 2;
if (i < 20) continue; // discard the transient
const sx = (px * SCALE + OX) | 0, sy = (py * SCALE + OY) | 0;
if (sx >= 0 && sx < RES && sy >= 0 && sy < RES) {
const idx = sy * RES + sx;
R[idx] += cr; G[idx] += cg; B[idx] += cb; A[idx] += 1;
}
}
}
const img = ctx.createImageData(RES, RES);
function render() {
let max = 1;
for (let i = 0; i < A.length; i++) if (A[i] > max) max = A[i];
const lm = 1 / Math.log(max + 1);
const d = img.data;
for (let i = 0; i < A.length; i++) {
const a = A[i];
if (a === 0) { d[i*4+3] = 255; continue; }
const bright = Math.log(a + 1) * lm; // logarithmic density tone-map
const inv = 1 / a;
d[i*4] = Math.min(255, R[i] * inv * bright * 1.6);
d[i*4+1] = Math.min(255, G[i] * inv * bright * 1.6);
d[i*4+2] = Math.min(255, B[i] * inv * bright * 1.6);
d[i*4+3] = 255;
}
ctx.putImageData(img, 0, 0);
}
function loop() { iterate(120000); render(); requestAnimationFrame(loop); }
loop();仕組み
アフィン変換群でカオスゲームを回しつつ、各変換の出力を非線形「バリエーション」関数に通して混ぜ(炎の有機的な曲線の源)、ヒット数と色座標を蓄積バッファに記録し、濃度の対数でトーンマッピングして彩色する。画像は変換・バリエーション・重み・色のパラメータ群(約100以上の数値=ゲノム)で完全に決まる。
パラメータ
variations: array — The nonlinear variation functions blended each iteration.iterations: number — Points plotted via the chaos game; more iterations reduce noise.gamma: number — Tone-mapping gamma applied to the accumulated log-density buffer.symmetry: number — Rotational/dihedral symmetry imposed on the function set.
系譜
よく組み合わされるアルゴリズム
このアルゴリズムを使う作品 (2)
最終更新 2026-07-16