地形等值线图案
Topographic Isoline Art
把高度相同的位置连成等值线,用层叠曲线展现峰顶、斜坡和鞍部。
适用场景
适合编辑封面、标识系统或高程信息图,表达地形结构。 文字附近可适当降低线条密度。需要表达实际高程时,用真实地形数据,并标注相邻等高线的高度差。
应用示例
徒步指南在路线图上每隔 20 米高程绘制一条等高线。读者看到山口前的线条明显变密,就能提前判断那里有一段陡坡。
AI 提示词
为【区域】绘制地形等值线图案。采样高度场,用 Marching Squares 沿网格边插值求交点,对有歧义的鞍点单元采用一致规则。按高度级别区分线条强调程度,允许调整密度并保持前景文字可读。
示例代码
保存为 HTML 文件后,用浏览器打开即可运行。代码与页面里的演示一致。
查看 HTML
<!doctype html>
<html lang="zh">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>地形等值线图案</title>
<style>
:root {
color-scheme: dark;
--demo-bg: #171629;
--demo-ink: #f8f5ff;
--demo-accent: #b9a6ff;
--demo-muted: #bfbbd3;
font-family: system-ui, sans-serif;
color: var(--demo-ink);
background: var(--demo-bg);
}
*, *::before, *::after { box-sizing: border-box; }
body { margin: 0; min-width: 0; }
button, input { font: inherit; }
.demo {
display: grid;
place-items: center;
align-content: center;
min-height: 100vh;
padding: 14px;
overflow: hidden;
background: radial-gradient(ellipse at 50% 24%, #28233f, var(--demo-bg) 78%);
}
.demo-button {
border: 1px solid #ffffff3d;
border-radius: 7px;
background: #ffffff12;
color: var(--demo-ink);
padding: 6px 11px;
font-size: 12px;
cursor: pointer;
}
:focus-visible {
outline: 2px solid var(--demo-accent);
outline-offset: 3px;
}
.specimen {
display: grid;
width: min(100%, 356px);
gap: 8px;
margin: 0;
justify-items: center;
}
.specimen canvas {
display: block;
width: 100%;
aspect-ratio: 2;
max-height: max(72px, calc(100vh - 65px));
border-radius: 11px;
background: linear-gradient(145deg, #232943, #14172c);
box-shadow: 0 8px 22px #090a16a8;
}
.specimen label {
display: grid;
width: 100%;
min-height: 25px;
grid-template-columns: minmax(0, auto) minmax(56px, 1fr);
align-items: center;
gap: 8px;
color: var(--demo-ink);
font-size: 12px;
}
.specimen input {
width: 100%;
margin: 0;
accent-color: #b9a6ff;
}
@media (prefers-reduced-motion: reduce) {
.specimen [data-loop-toggle] { display: none; }
}
</style>
</head>
<body>
<main class="demo">
<figure class="specimen">
<canvas role="img" aria-label="地形等值线图案" width="288" height="144">地形等值线图案</canvas>
<label for="specimen-setting">等值线密度<input id="specimen-setting" type="range" min="0" max="100" value="52"></label>
</figure>
</main>
<script>
(() => {
const canvas = document.querySelector('.specimen canvas');
const ctx = canvas.getContext('2d');
if (!ctx) {
document.querySelector('.specimen :is(input, button)').disabled = true;
return;
}
const width = 288;
const height = 144;
const turn = Math.PI * 2;
let elapsed = 0;
let variation = 0.52;
function dot(x, y, radius, ink) {
ctx.fillStyle = ink;
ctx.beginPath();
ctx.arc(x, y, radius, 0, turn);
ctx.fill();
}
function mix(a, b, amount) {
return a + (b - a) * amount;
}
const cell = 4;
const columns = width / cell;
const rows = height / cell;
function elevation(x, y) {
function bump(cx, cy, spreadX, spreadY, strength) {
return strength * Math.exp(-(((x - cx) / spreadX) ** 2 + ((y - cy) / spreadY) ** 2));
}
return bump(79, 49, 53, 35, 1) + bump(205, 91, 54, 39, 1.12) + bump(171, 37, 40, 30, .34) - bump(144, 78, 32, 25, .35) + .036 * Math.cos(x * .12 + y * .069);
}
const samples = Array.from({ length: rows + 1 }, (_, row) =>
Array.from({ length: columns + 1 }, (_, column) => elevation(column * cell, row * cell))
);
const edges = [[0, 1], [1, 2], [2, 3], [3, 0]];
function draw(_time, amount) {
const dusk = ctx.createLinearGradient(0, 0, width, height);
dusk.addColorStop(0, '#27414c');
dusk.addColorStop(.6, '#1e293d');
dusk.addColorStop(1, '#292745');
ctx.fillStyle = dusk;
ctx.fillRect(0, 0, width, height);
const levels = Math.round(5 + amount * 20);
for (let index = 1; index <= levels; index++) {
const level = index / (levels + 1) * 1.19;
ctx.lineWidth = index % 4 === 0 ? 1.1 : .63;
ctx.strokeStyle = index % 4 === 0 ? '#f7d6a9cc' : '#b6dcf08c';
ctx.beginPath();
for (let row = 0; row < rows; row++) {
for (let column = 0; column < columns; column++) {
const x = column * cell;
const y = row * cell;
const corners = [[x, y], [x + cell, y], [x + cell, y + cell], [x, y + cell]];
const values = [samples[row][column], samples[row][column + 1], samples[row + 1][column + 1], samples[row + 1][column]];
const crossings = [];
for (const [start, end] of edges) {
if ((values[start] >= level) === (values[end] >= level)) continue;
const factor = (level - values[start]) / (values[end] - values[start]);
crossings.push([
mix(corners[start][0], corners[end][0], factor),
mix(corners[start][1], corners[end][1], factor),
]);
}
if (crossings.length === 2) {
ctx.moveTo(...crossings[0]);
ctx.lineTo(...crossings[1]);
} else if (crossings.length === 4) {
const centerAbove = values.reduce((sum, value) => sum + value, 0) / 4 >= level;
const adjacent = centerAbove === (values[0] >= level);
ctx.moveTo(...crossings[0]);
ctx.lineTo(...crossings[adjacent ? 1 : 3]);
ctx.moveTo(...crossings[2]);
ctx.lineTo(...crossings[adjacent ? 3 : 1]);
}
}
}
ctx.stroke();
}
}
function render() {
const ratio = Math.min(devicePixelRatio || 1, 2);
const pixelsWide = Math.max(1, Math.round(canvas.clientWidth * ratio));
const pixelsTall = Math.max(1, Math.round(canvas.clientHeight * ratio));
if (canvas.width !== pixelsWide || canvas.height !== pixelsTall) {
canvas.width = pixelsWide;
canvas.height = pixelsTall;
}
ctx.setTransform(pixelsWide / width, 0, 0, pixelsTall / height, 0, 0);
ctx.clearRect(0, 0, width, height);
ctx.save();
draw(elapsed, variation);
ctx.restore();
}
render();
new ResizeObserver(render).observe(canvas);
const input = document.querySelector('#specimen-setting');
input.addEventListener('input', () => {
variation = Number(input.value) / 100;
render();
});
})();
</script>
</body>
</html>UI Skills 原创示例