Repository navigation
Expand file tree
/
Copy pathindex.html
More file actions
900 lines (791 loc) · 35.8 KB
/
Copy pathindex.html
File metadata and controls
900 lines (791 loc) · 35.8 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
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>densekernel</title>
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Geist:wght@100..900&display=swap" rel="stylesheet">
<style>
@font-face {
font-family: 'Geist Pixel';
src: url('fonts/GeistPixel-Square.woff2') format('woff2');
font-display: swap;
}
:root {
--bg: #fafafa;
--fg: #0a0a0a;
--fg-muted: #666;
--fg-subtle: #888;
--border: #ddd;
}
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: 'Geist', sans-serif;
min-height: 100vh;
background: var(--bg);
color: var(--fg);
transition: background 0.3s, color 0.3s;
}
.visualization-viewport {
width: 100%;
overflow: hidden;
margin-bottom: 2rem;
}
canvas {
display: block;
width: 2200px;
max-width: none;
height: 48vh;
min-height: 340px;
max-height: 560px;
position: relative;
left: 50%;
transform: translateX(-50%);
}
.container {
max-width: 1000px;
margin: 0 auto;
padding: 0 2rem;
}
nav {
padding: 1.5rem 0;
}
.logo {
font-family: 'Geist Pixel', sans-serif;
font-weight: 600;
font-size: 1.1rem;
letter-spacing: 0.08em;
color: var(--fg);
text-decoration: none;
}
.logo:hover {
opacity: 0.7;
}
main {
padding: 5rem 0 4rem;
}
section {
margin-bottom: 3rem;
}
section:last-child {
margin-bottom: 0;
}
.about {
max-width: 520px;
}
.intro {
font-size: 1rem;
font-weight: 400;
line-height: 1.7;
color: var(--fg-subtle);
}
.intro + .intro {
margin-top: 1rem;
}
h2 {
font-family: 'Geist Pixel', sans-serif;
font-weight: 500;
font-size: 0.8rem;
letter-spacing: 0.15em;
text-transform: uppercase;
color: var(--fg-muted);
margin-bottom: 1.25rem;
}
.projects {
display: flex;
flex-direction: column;
border-top: 1px solid var(--border);
}
.project {
display: flex;
align-items: baseline;
justify-content: space-between;
gap: 1rem;
padding: 1.25rem 0;
border-bottom: 1px solid var(--border);
text-decoration: none;
color: inherit;
transition: opacity 0.2s;
}
.project::before {
content: '';
width: 0.7rem;
height: 0.7rem;
flex: 0 0 0.7rem;
margin-top: 0.25rem;
background: var(--project-accent);
}
.project:nth-child(1) {
--project-accent: rgb(238, 50, 38);
}
.project:nth-child(2) {
--project-accent: rgb(255, 135, 34);
}
.project:nth-child(3) {
--project-accent: rgb(25, 220, 230);
}
.project:nth-child(4) {
--project-accent: rgb(90, 45, 210);
}
.project:hover {
opacity: 0.7;
}
.project-name {
font-weight: 400;
font-size: 1rem;
}
.project-info {
flex: 1;
}
.project-desc {
font-size: 0.875rem;
color: var(--fg-subtle);
margin-top: 0.25rem;
}
.project-arrow {
color: var(--fg-muted);
font-size: 0.9rem;
transition: transform 0.2s;
}
.project:hover .project-arrow {
transform: translateX(3px);
}
.social {
margin-top: 3rem;
padding-bottom: 3rem;
}
.social-link {
display: inline-block;
color: var(--fg-muted);
transition: color 0.2s;
margin-right: 1rem;
}
.social-link:hover {
color: var(--fg);
}
.social-link svg {
width: 18px;
height: 18px;
fill: currentColor;
}
</style>
</head>
<body>
<nav>
<div class="container">
<a href="/" class="logo">densekernel</a>
</div>
</nav>
<div class="visualization-viewport">
<canvas id="kde"></canvas>
</div>
<main class="container">
<section>
<h2>Projects</h2>
<div class="projects">
<a href="https://associateai.com/" target="_blank" rel="noopener" class="project">
<div class="project-info">
<span class="project-name">Associate AI</span>
<p class="project-desc">Integrity layer for financial documents</p>
</div>
<span class="project-arrow">→</span>
</a>
<a href="https://www.checkstep.com/" target="_blank" rel="noopener" class="project">
<div class="project-info">
<span class="project-name">Checkstep</span>
<p class="project-desc">Raised $10m+ to build a platform to scan 1b+ pieces of UGC for online harms</p>
</div>
<span class="project-arrow">→</span>
</a>
<a href="https://abcdle.com/" target="_blank" rel="noopener" class="project">
<div class="project-info">
<span class="project-name">Abcdle</span>
<p class="project-desc">My Wordle spin-off based on binary search</p>
</div>
<span class="project-arrow">→</span>
</a>
<a href="http://isostarbase.com/" target="_blank" rel="noopener" class="project">
<div class="project-info">
<span class="project-name">Iso Starbase</span>
<p class="project-desc">Isometric map of Starbase</p>
</div>
<span class="project-arrow">→</span>
</a>
</div>
</section>
<section class="about">
<h2>About</h2>
<p class="intro">Jonathan Manfield. Co-founder of Checkstep, building trust and safety infrastructure. Previously at JP Morgan on Chase UK Card Payments and Morgan Stanley on Realtime Risk systems.</p>
</section>
<section class="about">
<h2>What is Dense Kernel</h2>
<p class="intro">A concentration of core knowledge. From kernel density estimation in statistics, to the kernel at the heart of an operating system, to the dense kernel of a seed.</p>
<p class="intro">Kernel density estimation turns scattered data points into a smooth probability surface. The animation above borrows that idea: contours and gradients suggest a model moving across a landscape. In machine learning and AI, gradient descent is one of the core ways systems learn, iteratively following slope to reduce error.</p>
</section>
<div class="social">
<a href="https://x.com/densekernel" target="_blank" rel="noopener" class="social-link" aria-label="X">
<svg viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path d="M18.244 2.25h3.308l-7.227 8.26 8.502 11.24H16.17l-5.214-6.817L4.99 21.75H1.68l7.73-8.835L1.254 2.25H8.08l4.713 6.231zm-1.161 17.52h1.833L7.084 4.126H5.117z"/></svg>
</a>
<a href="https://www.linkedin.com/in/jonathanmanfield/" target="_blank" rel="noopener" class="social-link" aria-label="LinkedIn">
<svg viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path d="M20.447 20.452h-3.554v-5.569c0-1.328-.027-3.037-1.852-3.037-1.853 0-2.136 1.445-2.136 2.939v5.667H9.351V9h3.414v1.561h.046c.477-.9 1.637-1.85 3.37-1.85 3.601 0 4.267 2.37 4.267 5.455v6.286zM5.337 7.433a2.062 2.062 0 01-2.063-2.065 2.064 2.064 0 112.063 2.065zm1.782 13.019H3.555V9h3.564v11.452zM22.225 0H1.771C.792 0 0 .774 0 1.729v20.542C0 23.227.792 24 1.771 24h20.451C23.2 24 24 23.227 24 22.271V1.729C24 .774 23.2 0 22.222 0h.003z"/></svg>
</a>
</div>
</main>
<script>
const canvas = document.getElementById('kde');
const ctx = canvas.getContext('2d');
let width, height, grid, morphGrid, animatedGrid, maxD, minD, gridCols, gridRows, projectedGrid;
const peaks = [];
const ridgeFields = [];
const contourLevels = 10;
const terrainOverscanX = 1.15;
const terrainOverscanY = 0.68;
const terrainGridCols = 112;
const terrainGridRows = 76;
const fillSubdivisions = 3;
const contourSubdivisions = 3;
const contourCurveTension = 0.36;
const heightSmoothing = 0.44;
const contourStrokeColor = 'rgba(0, 0, 0, 0.2)';
const driftingBlobs = [
{ x: 0.22, y: 0.34, radius: 0.24, amplitude: 0.058, speedX: 0.72, speedY: 0.43, phase: 0.4 },
{ x: 0.68, y: 0.3, radius: 0.2, amplitude: 0.048, speedX: 0.48, speedY: 0.67, phase: 2.1 },
{ x: 0.42, y: 0.72, radius: 0.26, amplitude: -0.052, speedX: 0.36, speedY: 0.58, phase: 4.2 },
{ x: 0.78, y: 0.68, radius: 0.22, amplitude: 0.044, speedX: 0.62, speedY: 0.31, phase: 5.4 }
];
const searchParams = new URLSearchParams(window.location.search);
const renderSurfaceShading = searchParams.get('shading') !== 'off';
const colorPalette = searchParams.get('palette') || 'elevation';
const renderGrid = searchParams.get('grid') === 'on';
function createRandom(seed) {
let value = seed;
return () => {
value = (value * 1664525 + 1013904223) % 4294967296;
return value / 4294967296;
};
}
function resize() {
const dpr = window.devicePixelRatio || 1;
width = canvas.offsetWidth;
height = canvas.offsetHeight;
canvas.width = width * dpr;
canvas.height = height * dpr;
ctx.scale(dpr, dpr);
}
function terrainX(i) {
return (i / gridCols) * (1 + terrainOverscanX * 2) - terrainOverscanX;
}
function terrainY(j) {
return (j / gridRows) * (1 + terrainOverscanY * 2) - terrainOverscanY;
}
function initPeaks() {
peaks.length = 0;
ridgeFields.length = 0;
const random = createRandom(1337);
const numPeaks = 18;
peaks.push(
{ x: 0.2, y: 0.34, sigmaX: 0.16, sigmaY: 0.24, rotation: 0.7, amplitude: 0.86 },
{ x: 0.58, y: 0.48, sigmaX: 0.18, sigmaY: 0.12, rotation: -0.45, amplitude: 0.72 },
{ x: 0.82, y: 0.28, sigmaX: 0.13, sigmaY: 0.22, rotation: 1.1, amplitude: 0.62 },
{ x: 0.34, y: 0.76, sigmaX: 0.24, sigmaY: 0.12, rotation: -0.8, amplitude: 0.52 },
{ x: 0.7, y: 0.75, sigmaX: 0.2, sigmaY: 0.16, rotation: 0.55, amplitude: -0.34 },
{ x: 0.43, y: 0.18, sigmaX: 0.18, sigmaY: 0.11, rotation: 0.1, amplitude: -0.22 }
);
for (let i = 0; i < numPeaks; i++) {
peaks.push({
x: -0.14 + random() * 1.28,
y: -0.14 + random() * 1.28,
sigmaX: 0.045 + random() * 0.16,
sigmaY: 0.05 + random() * 0.18,
rotation: random() * Math.PI,
amplitude: (random() > 0.24 ? 1 : -1) * (0.18 + random() * 0.38)
});
}
for (let i = 0; i < 7; i++) {
ridgeFields.push({
angle: random() * Math.PI,
offset: -0.45 + random() * 1.9,
width: 0.045 + random() * 0.13,
frequency: 2.2 + random() * 6.2,
phase: random() * Math.PI * 2,
amplitude: (random() > 0.3 ? 1 : -1) * (0.07 + random() * 0.15)
});
}
}
function densityAt(x, y) {
let d = 0;
const warpX = x + 0.055 * Math.sin((y * 2.8 + x * 0.7) * Math.PI + 0.6) + 0.025 * Math.sin((x + y) * 8.5);
const warpY = y + 0.06 * Math.cos((x * 2.4 - y * 0.6) * Math.PI + 1.1) + 0.02 * Math.cos((x - y) * 7.7);
for (const p of peaks) {
const dx = warpX - p.x;
const dy = warpY - p.y;
const cos = Math.cos(p.rotation);
const sin = Math.sin(p.rotation);
const rx = dx * cos - dy * sin;
const ry = dx * sin + dy * cos;
d += p.amplitude * Math.exp(-((rx * rx) / (2 * p.sigmaX * p.sigmaX) + (ry * ry) / (2 * p.sigmaY * p.sigmaY)));
}
for (const ridge of ridgeFields) {
const axis = warpX * Math.cos(ridge.angle) + warpY * Math.sin(ridge.angle);
const across = warpX * -Math.sin(ridge.angle) + warpY * Math.cos(ridge.angle);
const band = Math.exp(-Math.pow(across - ridge.offset, 2) / (2 * ridge.width * ridge.width));
d += Math.sin(axis * Math.PI * ridge.frequency + ridge.phase) * ridge.amplitude * band;
}
const ringA = Math.hypot(warpX - 0.63, warpY - 0.42);
const ringB = Math.hypot(warpX - 0.18, warpY - 0.72);
d += 0.18 * Math.exp(-Math.pow(ringA - 0.18, 2) / 0.004) - 0.16 * Math.exp(-(ringA * ringA) / 0.028);
d += 0.12 * Math.exp(-Math.pow(ringB - 0.13, 2) / 0.003) - 0.1 * Math.exp(-(ringB * ringB) / 0.018);
d += 0.08 * Math.tanh((warpX - warpY - 0.08) * 8);
d += 0.07 * Math.sin((warpX * 3.4 + warpY * 1.8) * Math.PI);
d += 0.05 * Math.cos((warpX * 1.2 - warpY * 4.2) * Math.PI);
return d;
}
function buildGrid() {
gridCols = terrainGridCols;
gridRows = terrainGridRows;
grid = [];
morphGrid = [];
maxD = 0;
minD = Infinity;
let maxMorphD = 0;
let minMorphD = Infinity;
for (let j = 0; j <= gridRows; j++) {
const row = [];
const morphRow = [];
for (let i = 0; i <= gridCols; i++) {
const x = terrainX(i);
const y = terrainY(j);
const d = densityAt(x, y);
const morphD = densityAt(x * 0.92 + 0.16, y * 1.08 - 0.1) + 0.08 * Math.sin((x * 1.7 + y * 2.2) * Math.PI);
if (d > maxD) maxD = d;
if (d < minD) minD = d;
if (morphD > maxMorphD) maxMorphD = morphD;
if (morphD < minMorphD) minMorphD = morphD;
row.push(d);
morphRow.push(morphD);
}
grid.push(row);
morphGrid.push(morphRow);
}
const range = maxD - minD || 1;
const morphRange = maxMorphD - minMorphD || 1;
for (let j = 0; j <= gridRows; j++) {
for (let i = 0; i <= gridCols; i++) {
grid[j][i] = (grid[j][i] - minD) / range;
morphGrid[j][i] = (morphGrid[j][i] - minMorphD) / morphRange;
}
}
minD = 0;
maxD = 1;
}
function animatedValueAt(i, j, time) {
const x = terrainX(i);
const y = terrainY(j);
const t = time * 0.00005;
const morph = 0.5 - 0.5 * Math.cos(time * 0.000052);
const base = grid[j][i] * (1 - morph) + morphGrid[j][i] * morph;
const ridge = Math.sin((x * 2.7 - y * 1.4 + t * 2.2) * Math.PI);
const cross = Math.cos((x * 0.9 + y * 3.1 - t * 1.6) * Math.PI);
const swirl = Math.sin((Math.hypot(x - 0.62, y - 0.44) * 7.4 - t * 5.2) * Math.PI);
const activity = 0.35 + 0.65 * Math.sin(base * Math.PI);
const driftX = x + 0.035 * Math.sin((y * 2.1 + t * 1.7) * Math.PI);
const driftY = y + 0.03 * Math.cos((x * 1.8 - t * 1.4) * Math.PI);
let blobRelief = 0;
for (const blob of driftingBlobs) {
const cx = blob.x + Math.sin(t * blob.speedX + blob.phase) * 0.16;
const cy = blob.y + Math.cos(t * blob.speedY + blob.phase * 1.37) * 0.13;
const dx = driftX - cx;
const dy = driftY - cy;
blobRelief += blob.amplitude * Math.exp(-(dx * dx + dy * dy) / (2 * blob.radius * blob.radius));
}
const movingRelief = (ridge * 0.009 + cross * 0.007 + swirl * 0.006 + blobRelief) * activity;
return Math.max(0, Math.min(1, base + movingRelief));
}
function buildAnimatedGrid(time) {
const rawGrid = [];
for (let j = 0; j <= gridRows; j++) {
const row = [];
for (let i = 0; i <= gridCols; i++) {
row.push(animatedValueAt(i, j, time));
}
rawGrid.push(row);
}
animatedGrid = [];
for (let j = 0; j <= gridRows; j++) {
const row = [];
for (let i = 0; i <= gridCols; i++) {
const left = rawGrid[j][Math.max(0, i - 1)];
const right = rawGrid[j][Math.min(gridCols, i + 1)];
const up = rawGrid[Math.max(0, j - 1)][i];
const down = rawGrid[Math.min(gridRows, j + 1)][i];
const neighborAverage = (left + right + up + down) * 0.25;
row.push(rawGrid[j][i] * (1 - heightSmoothing) + neighborAverage * heightSmoothing);
}
animatedGrid.push(row);
}
}
function projectPoint(i, j, value) {
const x = terrainX(i) - 0.5;
const y = terrainY(j) - 0.5;
const z = value / maxD;
const scale = height * 1.48;
const isoX = (x - y) * scale * 0.96;
const isoY = (x + y) * scale * 0.44 - z * height * 0.48;
return {
x: width * 0.5 + isoX,
y: height * 0.62 + isoY,
z
};
}
function buildProjectedGrid(time) {
buildAnimatedGrid(time);
projectedGrid = [];
for (let j = 0; j <= gridRows; j++) {
const row = [];
for (let i = 0; i <= gridCols; i++) {
row.push(projectPoint(i, j, animatedGrid[j][i]));
}
projectedGrid.push(row);
}
}
function sampleAnimatedGrid(x, y) {
const x0 = Math.max(0, Math.min(gridCols - 1, Math.floor(x)));
const y0 = Math.max(0, Math.min(gridRows - 1, Math.floor(y)));
const x1 = Math.min(gridCols, x0 + 1);
const y1 = Math.min(gridRows, y0 + 1);
const tx = x - x0;
const ty = y - y0;
const top = animatedGrid[y0][x0] * (1 - tx) + animatedGrid[y0][x1] * tx;
const bottom = animatedGrid[y1][x0] * (1 - tx) + animatedGrid[y1][x1] * tx;
return top * (1 - ty) + bottom * ty;
}
function projectSampledPoint(x, y, value) {
return projectPoint(x, y, value);
}
function expandPolygon(points, amount) {
const center = points.reduce((acc, point) => ({
x: acc.x + point.x / points.length,
y: acc.y + point.y / points.length
}), { x: 0, y: 0 });
return points.map((point) => ({
x: center.x + (point.x - center.x) * amount,
y: center.y + (point.y - center.y) * amount
}));
}
function drawFilledPatch(x0, y0, x1, y1, depth) {
const v1 = sampleAnimatedGrid(x0, y0);
const v2 = sampleAnimatedGrid(x1, y0);
const v3 = sampleAnimatedGrid(x1, y1);
const v4 = sampleAnimatedGrid(x0, y1);
const [p1, p2, p3, p4] = expandPolygon([
projectPoint(x0, y0, v1),
projectPoint(x1, y0, v2),
projectPoint(x1, y1, v3),
projectPoint(x0, y1, v4)
], 1.028);
const average = (v1 + v2 + v3 + v4) / 4;
const slope = Math.abs(v1 - v3) + Math.abs(v2 - v4);
ctx.beginPath();
ctx.moveTo(p1.x, p1.y);
ctx.lineTo(p2.x, p2.y);
ctx.lineTo(p3.x, p3.y);
ctx.lineTo(p4.x, p4.y);
ctx.closePath();
ctx.fillStyle = shadeCell(average, depth, slope);
ctx.fill();
}
function drawCellUnderfill(i, j, depth) {
const [p1, p2, p3, p4] = expandPolygon([
projectedGrid[j][i],
projectedGrid[j][i + 1],
projectedGrid[j + 1][i + 1],
projectedGrid[j + 1][i]
], 1.035);
const average = (animatedGrid[j][i] + animatedGrid[j][i + 1] + animatedGrid[j + 1][i + 1] + animatedGrid[j + 1][i]) / 4;
const slope = Math.abs(animatedGrid[j][i] - animatedGrid[j + 1][i + 1]) + Math.abs(animatedGrid[j][i + 1] - animatedGrid[j + 1][i]);
ctx.beginPath();
ctx.moveTo(p1.x, p1.y);
ctx.lineTo(p2.x, p2.y);
ctx.lineTo(p3.x, p3.y);
ctx.lineTo(p4.x, p4.y);
ctx.closePath();
ctx.fillStyle = shadeCell(average, depth, slope);
ctx.fill();
}
function mixColor(a, b, amount) {
return a.map((channel, index) => channel + (b[index] - channel) * amount);
}
function shadeCell(value, depth, slope) {
const t = maxD === 0 ? 0 : value / maxD;
if (colorPalette === 'lava' || colorPalette === 'elevation') {
const stops = [
...(colorPalette === 'elevation'
? [
[90, 45, 210],
[45, 105, 240],
[25, 220, 230],
[95, 245, 145],
[205, 250, 72],
[255, 222, 48],
[255, 135, 34],
[238, 50, 38]
]
: [
[42, 16, 36],
[124, 24, 58],
[218, 38, 88],
[255, 102, 42],
[255, 214, 92]
])
];
const scaled = Math.min(stops.length - 1.001, Math.max(0, t * (stops.length - 1)));
const index = Math.floor(scaled);
const amount = scaled - index;
const color = mixColor(stops[index], stops[index + 1], amount);
const shadeLimit = colorPalette === 'elevation' ? 0.18 : 0.28;
const shade = 1 - Math.min(shadeLimit, depth * 0.06 + slope * 0.48);
const lift = colorPalette === 'elevation' ? 4 : 10 + t * 6;
const r = Math.round(color[0] * shade + lift);
const g = Math.round(color[1] * shade + lift);
const b = Math.round(color[2] * shade + lift);
return `rgb(${r}, ${g}, ${b})`;
}
const hue = 202 - t * 82 + depth * 26;
const light = 96 - t * 18 - depth * 5 - Math.min(10, slope * 48);
return `hsl(${hue}, ${24 + t * 38 + slope * 42}%, ${light}%)`;
}
function drawContourSegmentsForCell(i, j) {
const lerp = (threshold, va, vb) => {
if (Math.abs(vb - va) < 0.0001) return 0.5;
return (threshold - va) / (vb - va);
};
const areClose = (p1, p2) => {
const dx = p1.x - p2.x;
const dy = p1.y - p2.y;
return dx * dx + dy * dy < 1;
};
const drawSmoothedLine = (points) => {
if (points.length < 2) return;
ctx.moveTo(points[0].x, points[0].y);
if (points.length === 2) {
ctx.lineTo(points[1].x, points[1].y);
return;
}
for (let pointIndex = 1; pointIndex < points.length - 1; pointIndex++) {
const current = points[pointIndex];
const next = points[pointIndex + 1];
const mid = {
x: (current.x + next.x) * 0.5,
y: (current.y + next.y) * 0.5
};
const previous = points[pointIndex - 1];
const softControl = {
x: mid.x + (current.x - (previous.x + next.x) * 0.5) * contourCurveTension,
y: mid.y + (current.y - (previous.y + next.y) * 0.5) * contourCurveTension
};
ctx.quadraticCurveTo(softControl.x, softControl.y, mid.x, mid.y);
}
const last = points[points.length - 1];
ctx.lineTo(last.x, last.y);
};
const drawSmoothedSegments = (segments) => {
while (segments.length) {
const segment = segments.pop();
const line = [segment[0], segment[1]];
let extended = true;
while (extended) {
extended = false;
for (let segmentIndex = segments.length - 1; segmentIndex >= 0; segmentIndex--) {
const [start, end] = segments[segmentIndex];
if (areClose(line[line.length - 1], start)) {
line.push(end);
} else if (areClose(line[line.length - 1], end)) {
line.push(start);
} else if (areClose(line[0], end)) {
line.unshift(start);
} else if (areClose(line[0], start)) {
line.unshift(end);
} else {
continue;
}
segments.splice(segmentIndex, 1);
extended = true;
}
}
drawSmoothedLine(line);
}
};
for (let level = 1; level <= contourLevels; level++) {
const threshold = (level / (contourLevels + 1)) * maxD;
const segments = [];
for (let sy = 0; sy < contourSubdivisions; sy++) {
for (let sx = 0; sx < contourSubdivisions; sx++) {
const x0 = i + sx / contourSubdivisions;
const y0 = j + sy / contourSubdivisions;
const x1 = i + (sx + 1) / contourSubdivisions;
const y1 = j + (sy + 1) / contourSubdivisions;
const v1 = sampleAnimatedGrid(x0, y0);
const v2 = sampleAnimatedGrid(x1, y0);
const v3 = sampleAnimatedGrid(x1, y1);
const v4 = sampleAnimatedGrid(x0, y1);
const tl = v1 >= threshold ? 1 : 0;
const tr = v2 >= threshold ? 1 : 0;
const br = v3 >= threshold ? 1 : 0;
const bl = v4 >= threshold ? 1 : 0;
const config = tl * 8 + tr * 4 + br * 2 + bl;
if (config === 0 || config === 15) continue;
const top = projectSampledPoint(x0 + (x1 - x0) * lerp(threshold, v1, v2), y0, threshold);
const right = projectSampledPoint(x1, y0 + (y1 - y0) * lerp(threshold, v2, v3), threshold);
const bottom = projectSampledPoint(x0 + (x1 - x0) * lerp(threshold, v4, v3), y1, threshold);
const left = projectSampledPoint(x0, y0 + (y1 - y0) * lerp(threshold, v1, v4), threshold);
switch (config) {
case 1: case 14: segments.push([left, bottom]); break;
case 2: case 13: segments.push([bottom, right]); break;
case 3: case 12: segments.push([left, right]); break;
case 4: case 11: segments.push([top, right]); break;
case 5: segments.push([left, top], [bottom, right]); break;
case 6: case 9: segments.push([top, bottom]); break;
case 7: case 8: segments.push([left, top]); break;
case 10: segments.push([top, right], [left, bottom]); break;
}
}
}
drawSmoothedSegments(segments);
}
}
function draw(time) {
buildProjectedGrid(time);
ctx.fillStyle = '#fafafa';
ctx.fillRect(0, 0, width, height);
ctx.save();
ctx.lineCap = 'round';
ctx.lineJoin = 'round';
if (renderSurfaceShading) {
const cells = [];
for (let j = 0; j < gridRows; j++) {
for (let i = 0; i < gridCols; i++) {
const p1 = projectedGrid[j][i];
const p2 = projectedGrid[j][i + 1];
const p3 = projectedGrid[j + 1][i + 1];
const p4 = projectedGrid[j + 1][i];
cells.push({ i, j, depth: i + j });
}
}
cells.sort((a, b) => a.depth - b.depth);
for (const cell of cells) {
const { i, j } = cell;
const p1 = projectedGrid[j][i];
const p2 = projectedGrid[j][i + 1];
const p3 = projectedGrid[j + 1][i + 1];
const p4 = projectedGrid[j + 1][i];
const depth = (j + i) / (gridCols + gridRows);
drawCellUnderfill(i, j, depth);
for (let sy = 0; sy < fillSubdivisions; sy++) {
for (let sx = 0; sx < fillSubdivisions; sx++) {
const x0 = i + sx / fillSubdivisions;
const y0 = j + sy / fillSubdivisions;
const x1 = i + (sx + 1) / fillSubdivisions;
const y1 = j + (sy + 1) / fillSubdivisions;
drawFilledPatch(x0, y0, x1, y1, depth);
}
}
if (renderGrid) {
ctx.strokeStyle = 'rgba(0, 0, 0, 0.045)';
ctx.lineWidth = 1;
ctx.beginPath();
ctx.moveTo(p1.x, p1.y);
ctx.lineTo(p2.x, p2.y);
ctx.lineTo(p3.x, p3.y);
ctx.lineTo(p4.x, p4.y);
ctx.closePath();
ctx.stroke();
}
ctx.beginPath();
ctx.strokeStyle = contourStrokeColor;
ctx.lineWidth = 1;
drawContourSegmentsForCell(i, j);
ctx.stroke();
}
}
if (!renderSurfaceShading) {
if (renderGrid) {
ctx.strokeStyle = 'rgba(0, 0, 0, 0.075)';
ctx.lineWidth = 1;
for (let j = 0; j <= gridRows; j += 2) {
ctx.beginPath();
for (let i = 0; i <= gridCols; i++) {
const p = projectedGrid[j][i];
if (i === 0) ctx.moveTo(p.x, p.y);
else ctx.lineTo(p.x, p.y);
}
ctx.stroke();
}
for (let i = 0; i <= gridCols; i += 2) {
ctx.beginPath();
for (let j = 0; j <= gridRows; j++) {
const p = projectedGrid[j][i];
if (j === 0) ctx.moveTo(p.x, p.y);
else ctx.lineTo(p.x, p.y);
}
ctx.stroke();
}
}
ctx.strokeStyle = contourStrokeColor;
ctx.lineWidth = 1;
for (let level = 1; level <= contourLevels; level++) {
const threshold = (level / (contourLevels + 1)) * maxD;
ctx.beginPath();
for (let j = 0; j < gridRows; j++) {
for (let i = 0; i < gridCols; i++) {
const tl = animatedGrid[j][i] >= threshold ? 1 : 0;
const tr = animatedGrid[j][i + 1] >= threshold ? 1 : 0;
const br = animatedGrid[j + 1][i + 1] >= threshold ? 1 : 0;
const bl = animatedGrid[j + 1][i] >= threshold ? 1 : 0;
const config = tl * 8 + tr * 4 + br * 2 + bl;
if (config === 0 || config === 15) continue;
const lerp = (va, vb) => {
if (Math.abs(vb - va) < 0.0001) return 0.5;
return (threshold - va) / (vb - va);
};
const top = projectPoint(i + lerp(animatedGrid[j][i], animatedGrid[j][i + 1]), j, threshold);
const right = projectPoint(i + 1, j + lerp(animatedGrid[j][i + 1], animatedGrid[j + 1][i + 1]), threshold);
const bottom = projectPoint(i + lerp(animatedGrid[j + 1][i], animatedGrid[j + 1][i + 1]), j + 1, threshold);
const left = projectPoint(i, j + lerp(animatedGrid[j][i], animatedGrid[j + 1][i]), threshold);
const drawLine = (p1, p2) => {
ctx.moveTo(p1.x, p1.y);
ctx.lineTo(p2.x, p2.y);
};
switch (config) {
case 1: case 14: drawLine(left, bottom); break;
case 2: case 13: drawLine(bottom, right); break;
case 3: case 12: drawLine(left, right); break;
case 4: case 11: drawLine(top, right); break;
case 5: drawLine(left, top); drawLine(bottom, right); break;
case 6: case 9: drawLine(top, bottom); break;
case 7: case 8: drawLine(left, top); break;
case 10: drawLine(top, right); drawLine(left, bottom); break;
}
}
}
ctx.stroke();
}
}
ctx.restore();
requestAnimationFrame(draw);
}
window.addEventListener('resize', () => {
resize();
});
resize();
initPeaks();
buildGrid();
requestAnimationFrame(draw);
</script>
</body>
</html>