Skip to content

Commit d3ff84c

Browse files
committed
refactor: improve code formatting and consistency in coastline rendering functions
1 parent d300d5e commit d3ff84c

3 files changed

Lines changed: 81 additions & 29 deletions

File tree

src/controllers/coastline-editor.ts

Lines changed: 38 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,10 @@ function drawRoughnessGraph(canvas: HTMLCanvasElement): void {
9090
const rand = Alea(PREVIEW_SEED);
9191
const profile = makeRoughnessProfile(rand, coastSettings.roughnessContrast);
9292

93-
const pl = 2, pr = 2, pt = 6, pb = 6;
93+
const pl = 2,
94+
pr = 2,
95+
pt = 6,
96+
pb = 6;
9497
const gW = W - pl - pr;
9598
const gH = H - pt - pb;
9699
const thresh = Math.min(Math.max(coastSettings.smoothThreshold, 0), 1);
@@ -125,7 +128,11 @@ function drawRoughnessGraph(canvas: HTMLCanvasElement): void {
125128
};
126129

127130
// Helper: stroke curve clipped to a horizontal band
128-
const strokeBand = (clipTop: number, clipBot: number, color: string): void => {
131+
const strokeBand = (
132+
clipTop: number,
133+
clipBot: number,
134+
color: string,
135+
): void => {
129136
const h = clipBot - clipTop;
130137
if (h <= 0) return;
131138
ctx.save();
@@ -186,27 +193,41 @@ function drawShapePreview(canvas: HTMLCanvasElement): void {
186193

187194
// Generate at canvas scale so all setting changes are immediately visible.
188195
const basePts: [number, number][] = [
189-
[cx, cy - r], // top
190-
[cx + r, cy ], // right
191-
[cx, cy + r], // bottom
192-
[cx - r, cy ], // left
196+
[cx, cy - r], // top
197+
[cx + r, cy], // right
198+
[cx, cy + r], // bottom
199+
[cx - r, cy], // left
193200
];
194201

195202
const shape = fractalize(basePts, Alea(PREVIEW_SEED), coastSettings);
196203
const path = new Path2D(`${buildCoastlinePath(shape)}Z`);
197204

198205
// Ocean background — radial gradient, lighter at centre
199-
const bgGrad = ctx.createRadialGradient(cx, cy, 0, cx, cy, Math.max(W, H) * 0.85);
206+
const bgGrad = ctx.createRadialGradient(
207+
cx,
208+
cy,
209+
0,
210+
cx,
211+
cy,
212+
Math.max(W, H) * 0.85,
213+
);
200214
bgGrad.addColorStop(0, "#cce5f5");
201215
bgGrad.addColorStop(1, "#6aa4cb");
202216
ctx.fillStyle = bgGrad;
203217
ctx.fillRect(0, 0, W, H);
204218

205219
// Land fill with drop shadow
206-
const landGrad = ctx.createRadialGradient(cx - r * 0.1, cy - r * 0.1, r * 0.05, cx, cy, r * 1.1);
207-
landGrad.addColorStop(0, "#d8c87a");
220+
const landGrad = ctx.createRadialGradient(
221+
cx - r * 0.1,
222+
cy - r * 0.1,
223+
r * 0.05,
224+
cx,
225+
cy,
226+
r * 1.1,
227+
);
228+
landGrad.addColorStop(0, "#d8c87a");
208229
landGrad.addColorStop(0.5, "#9cbc60");
209-
landGrad.addColorStop(1, "#5c8e40");
230+
landGrad.addColorStop(1, "#5c8e40");
210231

211232
ctx.save();
212233
ctx.shadowColor = "rgba(0,20,60,0.35)";
@@ -223,7 +244,7 @@ function drawShapePreview(canvas: HTMLCanvasElement): void {
223244
ctx.stroke(path);
224245

225246
// Original polygon skeleton — shows the raw 4-vertex input before fractalization
226-
const origPts = shape.origIndices.map(i => shape.points[i]);
247+
const origPts = shape.origIndices.map((i) => shape.points[i]);
227248
ctx.beginPath();
228249
for (let j = 0; j < origPts.length; j++) {
229250
const [x, y] = origPts[j];
@@ -256,7 +277,7 @@ function updatePreviews(): void {
256277
}
257278

258279
function buildDialogHTML(): string {
259-
const rows = SLIDER_DEFS.map(({id, label, tip, min, max, step, key}) => {
280+
const rows = SLIDER_DEFS.map(({ id, label, tip, min, max, step, key }) => {
260281
const val = coastSettings[key] as number;
261282
return /* html */ `
262283
<tr data-tip="${tip}">
@@ -300,10 +321,12 @@ function setupCoastlineEditor(): void {
300321
document.body.insertAdjacentHTML("beforeend", buildDialogHTML());
301322
}
302323

303-
for (const {id, key} of SLIDER_DEFS) {
324+
for (const { id, key } of SLIDER_DEFS) {
304325
const slider = document.getElementById(id) as HTMLInputElement | null;
305326
const output = document.getElementById(`${id}Out`) as HTMLElement | null;
306-
const resetBtn = document.getElementById(`${id}Reset`) as HTMLElement | null;
327+
const resetBtn = document.getElementById(
328+
`${id}Reset`,
329+
) as HTMLElement | null;
307330

308331
if (!slider || !output || !resetBtn) continue;
309332

@@ -333,7 +356,7 @@ function setupCoastlineEditor(): void {
333356
title: "Coastline Advanced Settings",
334357
resizable: false,
335358
width: "auto",
336-
position: {my: "center", at: "center", of: "svg"},
359+
position: { my: "center", at: "center", of: "svg" },
337360
});
338361
};
339362
}

src/renderers/coastline-fractal.ts

Lines changed: 43 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,25 @@ export const coastSettings: CoastlineSettings = {
1515
amplitudeDecay: 0.9,
1616
minEdge: 1,
1717
smoothThreshold: 0.25,
18-
roughnessContrast: 1.5
18+
roughnessContrast: 1.5,
1919
};
2020

2121
export const PROFILE_SIZE = 256;
2222

2323
// Build a smooth closed roughness envelope via sum-of-cosine harmonics.
2424
// Intrinsically seam-free; result raised to `contrast` power for calm/rough contrast.
25-
export function makeRoughnessProfile(rand: () => number, contrast: number): Float32Array {
25+
export function makeRoughnessProfile(
26+
rand: () => number,
27+
contrast: number,
28+
): Float32Array {
2629
const profile = new Float32Array(PROFILE_SIZE);
2730
const numHarmonics = 3 + Math.floor(rand() * 3); // 3, 4 or 5
2831
for (let k = 1; k <= numHarmonics; k++) {
2932
const amp = rand();
3033
const phase = rand() * Math.PI * 2;
3134
for (let i = 0; i < PROFILE_SIZE; i++) {
32-
profile[i] += amp * Math.cos((2 * Math.PI * k * i) / PROFILE_SIZE + phase);
35+
profile[i] +=
36+
amp * Math.cos((2 * Math.PI * k * i) / PROFILE_SIZE + phase);
3337
}
3438
}
3539
let min = Infinity,
@@ -74,7 +78,7 @@ function subdivideEdge(
7478
profile: Float32Array,
7579
rand: () => number,
7680
resultPts: [number, number][],
77-
settings: CoastlineSettings
81+
settings: CoastlineSettings,
7882
): void {
7983
const dx = x1 - x0;
8084
const dy = y1 - y0;
@@ -92,9 +96,35 @@ function subdivideEdge(
9296
const my = (y0 + y1) / 2 + py * disp;
9397

9498
const nextAmp = amplitude * settings.amplitudeDecay;
95-
subdivideEdge(x0, y0, mx, my, t0, tm, depth - 1, nextAmp, profile, rand, resultPts, settings);
99+
subdivideEdge(
100+
x0,
101+
y0,
102+
mx,
103+
my,
104+
t0,
105+
tm,
106+
depth - 1,
107+
nextAmp,
108+
profile,
109+
rand,
110+
resultPts,
111+
settings,
112+
);
96113
resultPts.push([mx, my]);
97-
subdivideEdge(mx, my, x1, y1, tm, t1, depth - 1, nextAmp, profile, rand, resultPts, settings);
114+
subdivideEdge(
115+
mx,
116+
my,
117+
x1,
118+
y1,
119+
tm,
120+
t1,
121+
depth - 1,
122+
nextAmp,
123+
profile,
124+
rand,
125+
resultPts,
126+
settings,
127+
);
98128
}
99129

100130
export interface FractalizedShape {
@@ -105,7 +135,7 @@ export interface FractalizedShape {
105135
export function fractalize(
106136
points: [number, number][],
107137
rand: () => number,
108-
settings: CoastlineSettings
138+
settings: CoastlineSettings,
109139
): FractalizedShape {
110140
const profile = makeRoughnessProfile(rand, settings.roughnessContrast);
111141

@@ -147,19 +177,20 @@ export function fractalize(
147177
profile,
148178
rand,
149179
resultPts,
150-
settings
180+
settings,
151181
);
152182
}
153183

154-
return {points: resultPts, origIndices};
184+
return { points: resultPts, origIndices };
155185
}
156186

157187
export function fractalizeCoastline(
158188
points: [number, number][],
159189
featureIndex: number,
160-
settings: CoastlineSettings = coastSettings
190+
settings: CoastlineSettings = coastSettings,
161191
): FractalizedShape {
162-
if (points.length < 3) return {points, origIndices: points.map((_, i) => i)};
192+
if (points.length < 3)
193+
return { points, origIndices: points.map((_, i) => i) };
163194
const rand = Alea(`${seed}_c${featureIndex}`);
164195
return fractalize(points, rand, settings);
165196
}
@@ -170,7 +201,7 @@ export function fractalizeCoastline(
170201
* Jagged span: centripetal Catmull-Rom (α=0.5) through every fractal sub-point. Rounds sharp kinks into gentle curves.
171202
*/
172203
export function buildCoastlinePath(shape: FractalizedShape) {
173-
const {points: pts, origIndices} = shape;
204+
const { points: pts, origIndices } = shape;
174205
const N = pts.length;
175206
const M = origIndices.length;
176207
if (M < 3) return "";
@@ -229,4 +260,3 @@ export function buildCoastlinePath(shape: FractalizedShape) {
229260

230261
return d.join("");
231262
}
232-

src/renderers/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,3 @@ import "./draw-relief-icons";
1313
import "./draw-scalebar";
1414
import "./draw-state-labels";
1515
import "./draw-temperature";
16-

0 commit comments

Comments
 (0)