Fractal
Runnable OpenLayers example: Fractal. Open the live demo, read the complete source, and copy the file into your project. No build step or API key.
single HTML file · CDN dependencies onlyinterface may contain Chinese labels
Source
<!--
说明:分形栅格渲染
核心逻辑:Canvas 计算曼德博集合,toDataURL 作为 ImageStatic 源并指定 imageExtent
依赖:OpenLayers 10.3.1 · jsDelivr CDN
-->
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta name="robots" content="noindex,follow">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>分形栅格渲染</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/ol@10.3.1/ol.css">
<style>
html,body{height:100%;margin:0}
body{display:flex;flex-direction:column;overflow:hidden;
font:13px/1.6 -apple-system,"Segoe UI","PingFang SC",sans-serif}
.bar{flex:0 0 auto;padding:8px 12px;background:#f6f7f9;border-bottom:1px solid #e3e6ea;
display:flex;gap:12px;align-items:center;font-size:12px;color:#58606e;flex-wrap:wrap}
.bar button{font:inherit;padding:4px 10px;border:1px solid #c9d0da;border-radius:5px;
background:#fff;color:#1f2a37;cursor:pointer}
.bar input[type=range]{width:120px}
#info{margin-left:auto}
#map{flex:1 1 auto;min-height:0;background:#eef0f3}
</style>
</head>
<body>
<div class="bar">
<label>迭代 <input type="range" id="iter" min="20" max="200" step="10" value="80"></label>
<span id="iv">80</span>
<button id="regen">重新生成</button>
<label>透明度 <input type="range" id="op" min="20" max="100" step="5" value="85"></label>
<span id="info">—</span>
</div>
<div id="map"></div>
<script src="https://cdn.jsdelivr.net/npm/ol@10.3.1/dist/ol.js"></script>
<script>
(function () {
'use strict';
if (typeof ol === 'undefined') {
document.getElementById('map').innerHTML =
'<div style="padding:20px;color:#b42318">OpenLayers 加载失败,请检查能否访问 cdn.jsdelivr.net</div>';
return;
}
var fromLonLat = ol.proj.fromLonLat;
var W = 480;
var H = 320;
function render(maxIter) {
var canvas = document.createElement('canvas');
canvas.width = W;
canvas.height = H;
var ctx = canvas.getContext('2d');
var img = ctx.createImageData(W, H);
for (var py = 0; py < H; py++) {
var ci = (py / H) * 2.4 - 1.2;
for (var px = 0; px < W; px++) {
var cr = (px / W) * 3.2 - 2.2;
var zr = 0;
var zi = 0;
var n = 0;
while (n < maxIter && zr * zr + zi * zi < 4) {
var t = zr * zr - zi * zi + cr;
zi = 2 * zr * zi + ci;
zr = t;
n++;
}
var idx = (py * W + px) * 4;
if (n === maxIter) {
img.data[idx] = 12;
img.data[idx + 1] = 20;
img.data[idx + 2] = 48;
} else {
var k = n / maxIter;
img.data[idx] = Math.round(30 + 210 * k);
img.data[idx + 1] = Math.round(120 + 100 * Math.sin(k * 3.1));
img.data[idx + 2] = Math.round(200 - 150 * k);
}
img.data[idx + 3] = 255;
}
}
ctx.putImageData(img, 0, 0);
return canvas.toDataURL();
}
var extent = ol.proj.transformExtent([100, 28, 122, 42], 'EPSG:4326', 'EPSG:3857');
var imageLayer = new ol.layer.Image({
source: new ol.source.ImageStatic({
url: render(80),
imageExtent: extent,
projection: 'EPSG:3857'
}),
opacity: 0.85
});
var map = new ol.Map({
target: 'map',
layers: [new ol.layer.Tile({ source: new ol.source.XYZ({url: 'https://server.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer/tile/{z}/{y}/{x}', attributions: 'Tiles © Esri'}) }), imageLayer],
view: new ol.View({ center: fromLonLat([111, 35]), zoom: 4, minZoom: 2, maxZoom: 19 }),
controls: ol.control.defaults.defaults({ attribution: false, rotate: false })
});
var info = document.getElementById('info');
document.getElementById('iter').addEventListener('input', function () {
document.getElementById('iv').textContent = this.value;
});
document.getElementById('regen').addEventListener('click', function () {
var iter = Number(document.getElementById('iter').value);
var url = render(iter);
imageLayer.setSource(new ol.source.ImageStatic({
url: url,
imageExtent: extent,
projection: 'EPSG:3857'
}));
info.textContent = '已按 ' + iter + ' 次迭代重新生成 ' + W + '×' + H + ' 影像';
});
document.getElementById('op').addEventListener('input', function () {
imageLayer.setOpacity(Number(this.value) / 100);
});
info.textContent = '初始影像 ' + W + '×' + H + ' · 迭代 80 次';
})();
</script>
</body>
</html>
Language: the code and this page are in English; the full tutorial and API notes for this example are in Chinese. The demo itself is the authoritative version — it runs exactly as shown.