-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmycelium_network.html
More file actions
742 lines (671 loc) · 26.3 KB
/
mycelium_network.html
File metadata and controls
742 lines (671 loc) · 26.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1,viewport-fit=cover">
<style>
*{margin:0;padding:0}
body{background:#06050a;overflow:hidden;touch-action:none;cursor:none}
canvas{display:block}
</style>
</head>
<body>
<canvas id="c"></canvas>
<script>
const cv=document.getElementById('c');
const ctx=cv.getContext('2d');
let CELL=10;
let W,H,cols,rows;
let trail,age,dir,density,pulse;
let mx=0.5,my=0.5;
let t=0;
let hyphae=[];
let started=false;
let isMobile=false,maxHyphae=1600,initialSpores=6,clickSpores=4,grainCountScale=1,mobileSpreadScale=1,mobileStereoScale=1,mobileGainScale=1;
let actx,masterGain,tipBus,threadBus,branchBus,nodeBus,pulseBus,audioReady=false;
let delaySend,spaceDelay,delayFeedback,delayFilter,delayWet;
let driveHP,drivePre,driveShaper,driveLP,driveWet;
let nextGrainTime=0,lastNodeVoice=0,lastVoiceBloom=0;
let prevPulseNorm=0,prevNodeNorm=0,audioPan=0;
let sampleBuffers={},samplesLoadingPromise=null,samplesReady=false;
// voice sample palette — different "eras" of the-line narration
// chosen so each mycelium state has its own texture.
// Longer grain durations at node tier let actual phonemes emerge.
const SAMPLE_PATHS={
tip: 'the-line-v3-daisy-00-cave.ogg', // ancient, breathy
thread:'the-line-v3-daisy-06-telegraph.ogg', // short pulses
branch:'the-line-v3-daisy-04-concrete.ogg', // spoken, clean
node: 'the-line-v3-daisy-14-circuit.ogg', // denser articulation — voice bloom source
pulse: 'the-line-v3-daisy-13-glitch.ogg' // corrupted
};
// per-tier grain profile. `durMin/Max` is grain length in seconds;
// `attack` is the fade-in time (smaller = more click);
// `releaseRatio` is the fade-out as a fraction of grain length
// (higher = longer sustain -> phonemes hold together).
const GRAIN_PROFILES={
tip: {durMin:0.05,durMax:0.16, attack:0.006,releaseRatio:0.42,rateMin:0.76, rateMax:1.44, filter:'bandpass',fMin:800, fMax:2400,q:2.2,vol:0.022},
thread: {durMin:0.10,durMax:0.34, attack:0.012,releaseRatio:0.58,rateMin:0.72,rateMax:1.26,filter:'bandpass',fMin:600, fMax:1800,q:1.8,vol:0.028},
branch: {durMin:0.28,durMax:0.62, attack:0.024,releaseRatio:0.66,rateMin:0.64,rateMax:1.18,filter:'bandpass',fMin:500, fMax:1400,q:1.4,vol:0.036},
// node: long grains, gentle attack, low-pass — words almost coherent
node: {durMin:0.58,durMax:1.22, attack:0.055,releaseRatio:0.54,rateMin:0.78,rateMax:1.12,filter:'lowpass', fMin:1400,fMax:3200,q:0.7,vol:0.044},
pulse: {durMin:0.04,durMax:0.12, attack:0.003,releaseRatio:0.32,rateMin:0.58, rateMax:1.72, filter:'highpass',fMin:900, fMax:2200,q:1.0,vol:0.024}
};
function clamp(v,a,b){return Math.max(a,Math.min(b,v));}
function makeDriveCurve(amount){
const n=2048;
const curve=new Float32Array(n);
const k=Math.max(2,amount);
for(let i=0;i<n;i++){
const x=i*2/(n-1)-1;
curve[i]=(1+k)*x/(1+k*Math.abs(x));
}
return curve;
}
async function loadSampleBuffers(){
if(samplesLoadingPromise)return samplesLoadingPromise;
samplesLoadingPromise=(async()=>{
const fallback='the-line-v3-daisy.ogg';
const entries=await Promise.all(Object.entries(SAMPLE_PATHS).map(async([key,url])=>{
for(const candidate of [url,fallback]){
try{
const res=await fetch(new URL(candidate,location.href));
if(!res.ok)continue;
const arr=await res.arrayBuffer();
const buf=await actx.decodeAudioData(arr.slice(0));
if(buf)return [key,buf];
}catch(err){}
}
return [key,null];
}));
sampleBuffers=Object.fromEntries(entries.filter(([,buf])=>!!buf));
samplesReady=Object.keys(sampleBuffers).length>0;
})().catch(()=>{sampleBuffers={};samplesReady=false;});
return samplesLoadingPromise;
}
function initAudio(){
actx=new (window.AudioContext||window.webkitAudioContext)();
masterGain=actx.createGain();
masterGain.gain.value=0.82*mobileGainScale;
const comp=actx.createDynamicsCompressor();
comp.threshold.value=isMobile?-36:-32;
comp.knee.value=26;
comp.ratio.value=isMobile?10:8;
comp.attack.value=0.004;
comp.release.value=isMobile?0.18:0.15;
// tier buses — each has its own baseline gain so the mix stays balanced
tipBus=actx.createGain(); tipBus.gain.value=0.62*mobileGainScale;
threadBus=actx.createGain(); threadBus.gain.value=0.82*mobileGainScale;
branchBus=actx.createGain(); branchBus.gain.value=0.90*mobileGainScale;
nodeBus=actx.createGain(); nodeBus.gain.value=1.04*mobileGainScale; // node tier loudest so voice bleeds through
pulseBus=actx.createGain(); pulseBus.gain.value=0.76*mobileGainScale;
// shared space (delay) send so voice fragments hang
delaySend=actx.createGain(); delaySend.gain.value=(isMobile?0.22:0.28)*mobileGainScale;
spaceDelay=actx.createDelay(1.6);
spaceDelay.delayTime.value=0.28;
delayFeedback=actx.createGain();delayFeedback.gain.value=isMobile?0.3:0.38;
delayFilter=actx.createBiquadFilter();
delayFilter.type='lowpass';
delayFilter.frequency.value=2200;
delayWet=actx.createGain(); delayWet.gain.value=(isMobile?0.2:0.26)*mobileGainScale;
driveHP=actx.createBiquadFilter();
driveHP.type='highpass';
driveHP.frequency.value=180;
drivePre=actx.createGain();
drivePre.gain.value=1.05;
driveShaper=actx.createWaveShaper();
driveShaper.curve=makeDriveCurve(8);
driveShaper.oversample='4x';
driveLP=actx.createBiquadFilter();
driveLP.type='lowpass';
driveLP.frequency.value=4200;
driveWet=actx.createGain();
driveWet.gain.value=(isMobile?0.01:0.015)*mobileGainScale;
// routing
[tipBus,threadBus,branchBus,nodeBus,pulseBus].forEach(b=>{
b.connect(masterGain);
b.connect(delaySend);
});
delaySend.connect(spaceDelay);
spaceDelay.connect(delayFilter);
delayFilter.connect(delayWet);
delayFilter.connect(delayFeedback);
delayFeedback.connect(spaceDelay);
delayWet.connect(masterGain);
masterGain.connect(comp);
masterGain.connect(driveHP);
driveHP.connect(drivePre);
drivePre.connect(driveShaper);
driveShaper.connect(driveLP);
driveLP.connect(driveWet);
driveWet.connect(comp);
comp.connect(actx.destination);
nextGrainTime=actx.currentTime+0.06;
lastNodeVoice=0;
lastVoiceBloom=0;
audioReady=true;
loadSampleBuffers();
}
function bootAudio(){
try{
if(!actx)initAudio();
if(actx.state!=='running')actx.resume();
}catch(err){
audioReady=false;
}
}
function tierBus(tier){
switch(tier){
case 'tip':return tipBus;
case 'thread':return threadBus;
case 'branch':return branchBus;
case 'node':return nodeBus;
case 'pulse':return pulseBus;
default:return threadBus;
}
}
// schedule a single voice grain from the given tier's sample buffer.
// `override` can tweak dur/attack/releaseRatio/rate/filterFreq/vol for blooms.
function scheduleVoiceGrain(tier,at,amp,pan,override){
if(!audioReady||!samplesReady)return;
const buf=sampleBuffers[tier];
if(!buf)return;
const prof=GRAIN_PROFILES[tier];
const o=override||{};
const dur=o.dur!=null?o.dur:(prof.durMin+Math.random()*(prof.durMax-prof.durMin));
const attack=Math.min(o.attack!=null?o.attack:prof.attack,dur*0.45);
const releaseRatio=o.releaseRatio!=null?o.releaseRatio:prof.releaseRatio;
const release=Math.max(0.01,dur*releaseRatio);
const rate=o.rate!=null?o.rate:(prof.rateMin+Math.random()*(prof.rateMax-prof.rateMin));
const fCenter=o.filterFreq!=null?o.filterFreq:(prof.fMin+Math.random()*(prof.fMax-prof.fMin));
const vol=(o.volScale!=null?o.volScale:1)*prof.vol;
const src=actx.createBufferSource();
src.buffer=buf;
src.playbackRate.value=rate;
const filter=actx.createBiquadFilter();
filter.type=prof.filter;
filter.frequency.value=fCenter;
filter.Q.value=prof.q;
const gain=actx.createGain();
const sustainStart=at+attack;
const end=at+dur;
const releaseStart=Math.max(sustainStart,end-release);
gain.gain.setValueAtTime(0.0001,at);
gain.gain.linearRampToValueAtTime(amp*vol,sustainStart);
if(releaseStart>sustainStart)gain.gain.setValueAtTime(amp*vol,releaseStart);
gain.gain.exponentialRampToValueAtTime(0.0001,end);
src.connect(filter);
filter.connect(gain);
const bus=tierBus(tier);
if(actx.createStereoPanner){
const p=actx.createStereoPanner();
p.pan.value=clamp(pan,-1,1);
gain.connect(p);
p.connect(bus);
}else{
gain.connect(bus);
}
// pick grain offset inside the sample — bias by time so palette drifts
const playDur=dur*rate;
const maxOffset=Math.max(0,buf.duration-playDur-0.02);
const drift=(t*0.023)%1;
const offsetSpread=o.offsetSpread!=null?o.offsetSpread:0.7;
const offset=(drift+Math.random()*offsetSpread)%1*maxOffset;
src.start(at,offset,dur+0.02);
src.stop(end+0.05);
}
// longer grain at dense nodes — where voice fragments can emerge
function scheduleVoiceBloom(at,amp,pan){
if(!audioReady||!samplesReady||!sampleBuffers.node)return;
const dur=0.82+Math.random()*0.4;
scheduleVoiceGrain('node',at,amp,pan,{
dur,
attack:0.08,
releaseRatio:0.6,
rate:0.92+Math.random()*0.1,
filterFreq:1600+Math.random()*1400,
volScale:1.3
});
}
function triggerSporeBurst(sx,sy){
if(!audioReady)return;
const now=actx.currentTime+0.02;
const pan=clamp(sx*2-1,-0.9,0.9);
for(let i=0;i<3;i++){
scheduleVoiceGrain('tip',now+i*0.028,0.9-i*0.12,pan+(Math.random()-0.5)*0.3);
}
scheduleVoiceGrain('branch',now+0.04,0.8,pan*0.7);
if(Math.random()<0.45)scheduleVoiceBloom(now+0.08,0.7,pan*0.5);
}
function triggerPulseCrackle(strength,pan,cy){
if(!audioReady)return;
const now=actx.currentTime+0.01;
const count=3+Math.floor(strength*6);
for(let i=0;i<count;i++){
const at=now+i*(0.009+Math.random()*0.016);
const amp=0.7+strength*0.5-Math.min(i*0.05,0.35);
scheduleVoiceGrain('pulse',at,amp,clamp(pan+(Math.random()-0.5)*0.6,-1,1));
}
}
function updateAudio(stats){
if(!audioReady||!actx)return;
const now=actx.currentTime;
const cells=Math.max(1,cols*rows);
const growth=clamp(stats.liveCells/(cells*0.06),0,1);
const nodes=clamp(stats.nodeCells/120,0,1);
const tips=clamp(stats.tipCells/90,0,1);
const pulses=clamp(stats.pulseEnergy/240,0,1);
const centerX=stats.weight>0?stats.sumX/stats.weight:0.5;
const centerY=stats.weight>0?stats.sumY/stats.weight:0.5;
const branchiness=clamp(stats.branchCells/220,0,1);
const freshness=clamp(stats.freshEnergy/180,0,1);
const activity=clamp(growth*0.28+tips*0.2+nodes*0.14+pulses*0.2+branchiness*0.18,0,1);
const pulseRise=Math.max(0,pulses-prevPulseNorm);
const nodeRise=Math.max(0,nodes-prevNodeNorm);
const mousePan=clamp(mx*2-1,-1,1);
const driveCorner=Math.pow(clamp(mx*(1-my),0,1),1.4);
const sparseCorner=Math.pow(clamp((1-mx)*my,0,1),1.35);
const shortCorner=Math.pow(clamp((1-mx)*(1-my),0,1),1.35);
const highCorner=Math.pow(clamp(mx*my,0,1),1.35);
const stereoWidth=(0.16+Math.abs(mousePan)*0.9)*mobileStereoScale;
const grainStretch=Math.max(0.18,0.38+my*2.25-shortCorner*1.3);
const grainScatter=0.06+(1-my)*0.58+activity*0.12;
const rateTilt=(1.42-my*0.74)*(1+highCorner*0.16);
audioPan+=(((centerX*2-1)*0.45+mousePan*0.55)-audioPan)*0.18;
// tier-bus levels track state so voice bleeds in/out with the colony
tipBus.gain.setTargetAtTime(0.24+tips*0.58+freshness*0.32,now,0.14);
threadBus.gain.setTargetAtTime(0.30+growth*0.62+activity*0.24,now,0.16);
branchBus.gain.setTargetAtTime(0.24+branchiness*0.72+my*0.14,now,0.16);
nodeBus.gain.setTargetAtTime(0.18+nodes*0.92+pulses*0.18+my*0.18,now,0.18);
pulseBus.gain.setTargetAtTime(0.12+pulses*0.74+(1-my)*0.12,now,0.1);
masterGain.gain.setTargetAtTime(0.54+growth*0.16+nodes*0.12+pulses*0.08+my*0.1,now,0.18);
drivePre.gain.setTargetAtTime(1.02+driveCorner*3.4,now,0.12);
driveLP.frequency.setTargetAtTime(5000-driveCorner*1800,now,0.12);
driveWet.gain.setTargetAtTime(0.012+driveCorner*0.2,now,0.1);
driveShaper.curve=makeDriveCurve(8+driveCorner*34);
// dispatch grains in clusters per tick — each tier fires if its metric crosses a threshold
if(now>=nextGrainTime&&samplesReady){
const pan=audioPan;
// TIP: short clicky fragments — fire often when there are fresh growing tips
if(tips>0.06||freshness>0.1){
const n=Math.max(1,Math.floor((1+Math.floor(tips*3+freshness*2+Math.random()*1.5))*(1-sparseCorner*0.72)*grainCountScale));
for(let i=0;i<n;i++){
const dur=(GRAIN_PROFILES.tip.durMin+Math.random()*(GRAIN_PROFILES.tip.durMax-GRAIN_PROFILES.tip.durMin))*(0.45+grainStretch*0.62);
scheduleVoiceGrain('tip',now+i*(0.008+Math.random()*0.014),0.86+tips*0.62,clamp(pan+(Math.random()-0.5)*stereoWidth*1.08,-1,1),{
dur,
rate:(GRAIN_PROFILES.tip.rateMin+Math.random()*(GRAIN_PROFILES.tip.rateMax-GRAIN_PROFILES.tip.rateMin))*rateTilt,
offsetSpread:grainScatter*mobileSpreadScale,
volScale:1.25
});
}
}
// THREAD: the backbone texture — moderate rate, always some
const threadN=Math.max(1,Math.floor((1+Math.floor(growth*3+activity*1.5))*(1-sparseCorner*0.76)*grainCountScale));
for(let i=0;i<threadN;i++){
const dur=(GRAIN_PROFILES.thread.durMin+Math.random()*(GRAIN_PROFILES.thread.durMax-GRAIN_PROFILES.thread.durMin))*(0.54+grainStretch*0.72);
scheduleVoiceGrain('thread',now+i*(0.02+Math.random()*0.026),0.8+growth*0.62,clamp(pan+(Math.random()-0.5)*(0.42+stereoWidth*0.75),-1,1),{
dur,
rate:(GRAIN_PROFILES.thread.rateMin+Math.random()*(GRAIN_PROFILES.thread.rateMax-GRAIN_PROFILES.thread.rateMin))*rateTilt,
offsetSpread:grainScatter*1.18*mobileSpreadScale,
volScale:1.22
});
}
// BRANCH: spoken, longer — fire when branchiness is high
if(branchiness>0.12&&Math.random()<0.4+branchiness*0.6){
const n=Math.max(1,Math.floor((1+Math.floor(branchiness*2))*(1-sparseCorner*0.78)*grainCountScale));
for(let i=0;i<n;i++){
const dur=(GRAIN_PROFILES.branch.durMin+Math.random()*(GRAIN_PROFILES.branch.durMax-GRAIN_PROFILES.branch.durMin))*(0.72+grainStretch*0.88);
scheduleVoiceGrain('branch',now+i*(0.05+Math.random()*0.034),0.84+branchiness*0.5,clamp(pan*0.65+(Math.random()-0.5)*(0.34+stereoWidth*0.62),-1,1),{
dur,
rate:(GRAIN_PROFILES.branch.rateMin+Math.random()*(GRAIN_PROFILES.branch.rateMax-GRAIN_PROFILES.branch.rateMin))*(1.24-my*0.42),
offsetSpread:grainScatter*1.28*mobileSpreadScale,
volScale:1.28
});
}
}
// NODE: long grains — THE voice tier. Throttle so long grains don't mush.
if(nodes>0.08&&now-lastNodeVoice>(0.55-nodes*0.2)){
if(Math.random()<0.35+nodes*0.5+nodeRise*1.4){
const dur=(GRAIN_PROFILES.node.durMin+Math.random()*(GRAIN_PROFILES.node.durMax-GRAIN_PROFILES.node.durMin))*(0.82+grainStretch*1.08);
scheduleVoiceGrain('node',now+0.018,0.92+nodes*0.58,clamp(pan*0.55+(Math.random()-0.5)*(0.28+stereoWidth*0.52),-0.95,0.95),{
dur,
rate:(0.82+Math.random()*0.2)*(1.18-my*0.26),
offsetSpread:grainScatter*1.52*mobileSpreadScale,
volScale:1.34
});
lastNodeVoice=now;
}
}
// VOICE BLOOM: rare, long, gentle — where a word could almost emerge
if(nodes>0.18&&now-lastVoiceBloom>1.8-nodes*0.6){
if(Math.random()<0.25+nodes*0.4+pulses*0.3){
scheduleVoiceGrain('node',now+0.05,0.65+nodes*0.4,clamp(pan*0.45+(Math.random()-0.5)*(0.22+stereoWidth*0.22),-0.75,0.75),{
dur:(0.92+Math.random()*0.54)*(0.96+grainStretch*1.18),
attack:0.08,
releaseRatio:0.68,
rate:(0.8+Math.random()*0.18)*(1.12-my*0.22),
filterFreq:1600+Math.random()*1400,
volScale:1.52,
offsetSpread:grainScatter*1.72*mobileSpreadScale
});
lastVoiceBloom=now;
}
}
nextGrainTime=now+clamp(0.2-activity*0.09-pulses*0.05-branchiness*0.04-(1-my)*0.06+sparseCorner*0.08+(isMobile?0.04:0),0.035,0.34);
}
if(pulseRise>0.025||nodeRise>0.018){
const cracklePan=clamp(audioPan+(Math.random()-0.5)*(0.4+stereoWidth*0.45),-1,1);
triggerPulseCrackle(clamp(pulseRise*5+nodeRise*6+pulses*0.4,0.18,1),cracklePan,centerY);
}
prevPulseNorm+=(pulses-prevPulseNorm)*0.3;
prevNodeNorm+=(nodes-prevNodeNorm)*0.26;
}
// directional line-drawing characters
const HDIR={
0:'\u2500',1:'\u2571',2:'\u2502',3:'\u2572',
4:'\u2500',5:'\u2571',6:'\u2502',7:'\u2572'
};
const BRANCH='\u251C\u2524\u252C\u2534\u253C\u256C\u2560\u2563';
const ALIVE='\u2219\u2022\u25CF\u25C9\u25CE';
const SPORE='\u00B7\u2027\u2059\u205A\u205B';
const DECAY='\u2591\u2592\u2593\u2588\u2593\u2592\u2591';
const FRUIT='\u2740\u2741\u273F\u2744\u2746\u2749\u274B';
const NODE='\u25C6\u25C7\u2B22\u2B23\u2726\u2727\u2728\u2729';
function resize(){
W=window.innerWidth;H=window.innerHeight;
isMobile=window.matchMedia('(pointer: coarse)').matches||Math.min(W,H)<700;
CELL=isMobile?12:10;
maxHyphae=isMobile?900:1600;
initialSpores=isMobile?4:6;
clickSpores=isMobile?2:4;
grainCountScale=isMobile?0.62:1;
mobileSpreadScale=isMobile?0.8:1;
mobileStereoScale=isMobile?0.42:1;
mobileGainScale=isMobile?0.7:1;
cv.width=W;cv.height=H;
cols=Math.floor(W/(CELL*0.58));
rows=Math.floor(H/CELL);
const sz=cols*rows;
trail=new Float32Array(sz);
age=new Float32Array(sz);
dir=new Int8Array(sz).fill(-1);
density=new Float32Array(sz);
pulse=new Float32Array(sz);
ctx.font=CELL+'px Courier New';
ctx.textBaseline='top';
hyphae=[];
// initial spores
for(let i=0;i<initialSpores;i++) spawnSpore(Math.random(),Math.random());
}
function spawnSpore(sx,sy){
const count=2+Math.floor(Math.random()*4);
for(let i=0;i<count;i++){
hyphae.push({
x:sx*cols,
y:sy*rows,
angle:Math.random()*Math.PI*2,
energy:95+Math.random()*150,
speed:0.22+Math.random()*0.28,
branchProb:0.005+Math.random()*0.008,
age:0,
gen:0
});
}
// mark origin
const gc=Math.floor(sx*cols),gr=Math.floor(sy*rows);
if(gc>=0&&gc<cols&&gr>=0&&gr<rows){
const idx=gr*cols+gc;
density[idx]=10;
trail[idx]=5;
}
}
function frame(){
if(!started){
ctx.fillStyle='#06050a';
ctx.fillRect(0,0,W,H);
ctx.fillStyle='rgba(180,240,200,0.78)';
ctx.fillText(isMobile?'tap to germinate':'click to germinate',Math.max(18,W*0.5-(isMobile?72:84)),Math.max(18,H*0.5-8));
requestAnimationFrame(frame);
return;
}
t+=0.02;
const cw=CELL*0.58;
const mxg=mx*cols,myg=my*rows;
const growthRamp=clamp(t/18,0.28,1);
// GROW HYPHAE
const newHyphae=[];
for(let h=0;h<hyphae.length;h++){
const hy=hyphae[h];
if(hy.energy<=0)continue;
hy.age++;
hy.energy-=0.34+growthRamp*0.24;
// chemotaxis: bend toward mouse
const dx=mxg-hy.x,dy=myg-hy.y;
const dist=Math.sqrt(dx*dx+dy*dy);
if(dist>1){
const targetAngle=Math.atan2(dy,dx);
let diff=targetAngle-hy.angle;
while(diff>Math.PI)diff-=2*Math.PI;
while(diff<-Math.PI)diff+=2*Math.PI;
const attraction=Math.min(0.03+growthRamp*0.05,2/dist);
hy.angle+=diff*attraction;
}
// random wander
hy.angle+=( Math.random()-0.5)*(0.3+growthRamp*0.3);
// avoid edges gently
if(hy.x<3)hy.angle+= 0.1;
if(hy.x>cols-3)hy.angle-=0.1;
if(hy.y<3)hy.angle=Math.abs(hy.angle)<Math.PI/2?hy.angle:hy.angle+0.1;
if(hy.y>rows-3)hy.angle+=-0.1;
// follow existing trails slightly (stigmergy)
const lookX=Math.floor(hy.x+Math.cos(hy.angle)*2);
const lookY=Math.floor(hy.y+Math.sin(hy.angle)*2);
if(lookX>=0&&lookX<cols&&lookY>=0&&lookY<rows){
const li=lookY*cols+lookX;
if(trail[li]>0.5&&density[li]<3){
hy.angle+=(Math.random()-0.5)*0.3;
hy.energy+=0.2;
}
}
// move
const travel=hy.speed*(0.45+growthRamp*0.55);
hy.x+=Math.cos(hy.angle)*travel;
hy.y+=Math.sin(hy.angle)*travel;
const gc=Math.floor(hy.x),gr=Math.floor(hy.y);
if(gc<0||gc>=cols||gr<0||gr>=rows){hy.energy=0;continue;}
const idx=gr*cols+gc;
// deposit trail
trail[idx]=Math.min(trail[idx]+1.2,8);
age[idx]=0;
density[idx]+=0.12;
// direction for character selection
const d8=Math.round(((hy.angle%(2*Math.PI))+(2*Math.PI))%(2*Math.PI)/(Math.PI/4))%8;
dir[idx]=d8;
// fusion: hitting dense area = create node, die, release energy
if(density[idx]>4&&hy.age>20){
density[idx]+=3;
trail[idx]=8;
pulse[idx]=3;
hy.energy=0;
// spawn new directions from node
if(hyphae.length+newHyphae.length<maxHyphae){
for(let b=0;b<2;b++){
newHyphae.push({
x:hy.x,y:hy.y,
angle:hy.angle+Math.PI/2*(b===0?1:-1)+Math.random()*0.5,
energy:(40+Math.random()*50)*(0.65+growthRamp*0.35),
speed:(0.18+Math.random()*0.22)*(0.55+growthRamp*0.45),
branchProb:0.0035+growthRamp*0.0025,age:0,gen:hy.gen+1
});
}
}
continue;
}
// branching
if(Math.random()<hy.branchProb*(0.35+growthRamp*0.65)&&hy.energy>30&&hyphae.length+newHyphae.length<maxHyphae&&hy.gen<8){
const side=Math.random()<0.5?1:-1;
newHyphae.push({
x:hy.x,y:hy.y,
angle:hy.angle+side*(0.4+Math.random()*0.8),
energy:hy.energy*0.4,
speed:hy.speed*0.85,
branchProb:hy.branchProb*0.7,
age:0,gen:hy.gen+1
});
hy.energy*=0.6;
}
if(hy.energy>0)newHyphae.push(hy);
}
hyphae=newHyphae;
// respawn if population drops
if(hyphae.length<50&&Math.random()<0.02+growthRamp*0.03){
spawnSpore(Math.random(),Math.random());
}
// spawn near mouse occasionally
if(Math.random()<0.002+growthRamp*0.008){
spawnSpore(mx+Math.random()*0.05-0.025,my+Math.random()*0.05-0.025);
}
// age and decay
for(let i=0;i<cols*rows;i++){
age[i]+=0.01;
trail[i]*=0.994;
density[i]*=0.9975;
// pulse propagation
if(pulse[i]>0.1){
pulse[i]*=0.92;
// spread to neighbors
const r2=Math.floor(i/cols),c2=i%cols;
const spread=pulse[i]*0.02;
if(c2>0&&trail[i-1]>0.3)pulse[i-1]=Math.max(pulse[i-1],spread);
if(c2<cols-1&&trail[i+1]>0.3)pulse[i+1]=Math.max(pulse[i+1],spread);
if(r2>0&&trail[i-cols]>0.3)pulse[i-cols]=Math.max(pulse[i-cols],spread);
if(r2<rows-1&&trail[i+cols]>0.3)pulse[i+cols]=Math.max(pulse[i+cols],spread);
}
}
// periodic nutrient pulses from nodes
if(Math.floor(t*10)%30===0){
for(let i=0;i<cols*rows;i++){
if(density[i]>5)pulse[i]=Math.max(pulse[i],2);
}
}
// RENDER
ctx.fillStyle='#06050a';
ctx.fillRect(0,0,W,H);
let liveCells=0,nodeCells=0,tipCells=0,pulseEnergy=0,branchCells=0,freshEnergy=0;
let sumX=0,sumY=0,weight=0;
for(let r=0;r<rows;r++){
for(let c=0;c<cols;c++){
const idx=r*cols+c;
const v=trail[idx];
if(v<0.08)continue;
liveCells++;
const a=age[idx];
const d=density[idx];
const p=pulse[idx];
const norm=Math.min(v/6,1);
pulseEnergy+=Math.min(p,1.5);
if(d>6)nodeCells++;
else if(d>3)branchCells++;
if(a<0.05)tipCells++;
if(a<0.25)freshEnergy+=1-a*2.4;
const w=norm*(1+p*0.35+(a<0.12?0.8:0)+(d>6?0.55:0));
sumX+=(c/Math.max(cols-1,1))*w;
sumY+=(r/Math.max(rows-1,1))*w;
weight+=w;
// CHARACTER based on type
let ch;
if(d>6){
// dense node
ch=NODE[Math.floor((t*2+c*0.3)%NODE.length)];
}else if(d>3){
// junction
ch=BRANCH[Math.floor(d)%BRANCH.length];
}else if(a<0.05){
// fresh tip
ch=ALIVE[Math.floor(norm*ALIVE.length)%ALIVE.length];
}else if(a>2&&norm<0.3){
// decaying
ch=SPORE[Math.floor(a)%SPORE.length];
}else if(a>3&&Math.random()<0.01){
// rare fruiting body
ch=FRUIT[Math.floor(Math.random()*FRUIT.length)];
}else{
// normal hypha - directional
const dd=dir[idx];
ch=dd>=0?HDIR[dd]:'\u2500';
}
// COLOR
let cr,cg,cb;
if(a<0.1){
// living tip: bright bioluminescent
cr=180+p*30;cg=240+p*15;cb=200+p*20;
}else if(d>5){
// node: warm amber pulse
const pp=Math.sin(t*4+c*0.2+r*0.3)*0.3+0.7;
cr=200*pp+p*40;cg=140*pp+p*20;cb=60*pp+p*30;
}else if(a<1){
// young: pale green-blue
const fade=1-a;
cr=40+fade*60+p*50;cg=90+fade*100+p*40;cb=80+fade*80+p*35;
}else if(a<3){
// mature: muted earth tones
const fade=1-(a-1)/2;
cr=60*fade+p*40;cg=50*fade+p*30;cb=35*fade+p*25;
}else{
// old: fading to near-black
const fade=Math.max(0,1-(a-3)/4);
cr=25*fade+p*20;cg=18*fade+p*15;cb=15*fade+p*12;
}
// brightness from trail intensity
const bright=0.3+norm*0.7;
cr=Math.min(255,Math.floor(cr*bright));
cg=Math.min(255,Math.floor(cg*bright));
cb=Math.min(255,Math.floor(cb*bright));
if(cr<3&&cg<3&&cb<3)continue;
ctx.fillStyle=`rgb(${cr},${cg},${cb})`;
ctx.fillText(ch,c*cw,r*CELL);
}
}
// cursor: spore halo
const curX=mx*W,curY=my*H;
const sporeChars='\u00B7\u2219\u2022\u25CF';
ctx.fillStyle='rgba(180,240,200,0.4)';
for(let i=0;i<8;i++){
const a2=t*1.5+i*Math.PI/4;
const r2=18+Math.sin(t*3+i*1.1)*6;
ctx.fillText(sporeChars[i%4],curX+Math.cos(a2)*r2-3,curY+Math.sin(a2)*r2-4);
}
ctx.fillStyle='rgba(200,255,220,0.7)';
ctx.fillText('\u2316',curX-4,curY-5);
if(audioReady)updateAudio({liveCells,nodeCells,tipCells,pulseEnergy,branchCells,freshEnergy,sumX,sumY,weight});
requestAnimationFrame(frame);
}
document.addEventListener('mousemove',e=>{mx=e.clientX/W;my=e.clientY/H});
document.addEventListener('touchmove',e=>{e.preventDefault();mx=e.touches[0].clientX/W;my=e.touches[0].clientY/H},{passive:false});
document.addEventListener('pointerdown',()=>{started=true;bootAudio();});
document.addEventListener('keydown',e=>{if(e.key==='Enter'||e.key===' '){started=true;bootAudio();}});
document.addEventListener('touchstart',()=>{started=true;bootAudio();},{passive:true});
// click: drop a cluster of spores
document.addEventListener('click',e=>{
bootAudio();
const sx=e.clientX/W,sy=e.clientY/H;
for(let i=0;i<clickSpores;i++){
spawnSpore(sx+(Math.random()-0.5)*0.03,sy+(Math.random()-0.5)*0.03);
}
// pulse burst
const gc=Math.floor(sx*cols),gr=Math.floor(sy*rows);
for(let dy=-5;dy<=5;dy++){
for(let dx=-5;dx<=5;dx++){
const r2=gr+dy,c2=gc+dx;
if(r2<0||r2>=rows||c2<0||c2>=cols)continue;
if(Math.sqrt(dx*dx+dy*dy)>5)continue;
pulse[r2*cols+c2]=3;
}
}
triggerSporeBurst(sx,sy);
});
window.addEventListener('resize',resize);
resize();
frame();
</script>
</body>
</html>