Offscreen Canvas
Runnable OpenLayers example: Offscreen Canvas. 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
<!--
示例:离屏画布生成图标
要点:OffscreenCanvas 绘制后转成 ImageBitmap,交给 ol.style.Icon 使用
依赖: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;color:#16181d}
.bar{flex:0 0 auto;padding:8px 12px;background:#f6f7f9;border-bottom:1px solid #e3e6ea;
display:flex;gap:16px;align-items:center;font-size:12px;color:#58606e;flex-wrap:wrap}
.bar b{color:#16181d;font-weight:600}
#map{flex:1 1 auto;min-height:0;background:#eef0f3}
</style>
</head>
<body>
<div class="bar">
<span>OffscreenCanvas:<b id="sup">—</b></span>
<span>图标由 48×48 画布绘制,同一份位图复用到多个要素</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 (!window.ol) {
document.getElementById('map').textContent = 'OpenLayers 加载失败,请检查 cdn.jsdelivr.net 是否可访问';
return;
}
var fromLonLat = ol.proj.fromLonLat;
var hasOffscreen = typeof OffscreenCanvas !== 'undefined';
document.getElementById('sup').textContent = hasOffscreen ? '可用' : '不可用,退回普通画布';
function drawPin(ctx, size, fill) {
ctx.clearRect(0, 0, size, size);
ctx.beginPath();
ctx.arc(size / 2, size * 0.36, size * 0.3, Math.PI, 0);
ctx.lineTo(size / 2, size * 0.96);
ctx.closePath();
ctx.fillStyle = fill;
ctx.fill();
ctx.lineWidth = 2.5;
ctx.strokeStyle = '#ffffff';
ctx.stroke();
ctx.beginPath();
ctx.arc(size / 2, size * 0.36, size * 0.11, 0, Math.PI * 2);
ctx.fillStyle = '#ffffff';
ctx.fill();
}
/* 每种颜色只画一次,位图缓存在 map 里重复使用 */
var iconCache = {};
function iconFor(color) {
if (iconCache[color]) return iconCache[color];
var size = 48;
var source;
if (hasOffscreen) {
var oc = new OffscreenCanvas(size, size);
drawPin(oc.getContext('2d'), size, color);
source = oc.transferToImageBitmap ? oc.transferToImageBitmap() : oc;
} else {
var c = document.createElement('canvas');
c.width = c.height = size;
drawPin(c.getContext('2d'), size, color);
source = c;
}
var icon = new ol.style.Icon({
img: source,
anchor: [0.5, 0.96],
anchorXUnits: 'fraction',
anchorYUnits: 'fraction'
});
iconCache[color] = icon;
return icon;
}
var spots = [
[116.32, 39.93, '枢纽', '#1f5fd0'],
[116.40, 39.905, '中心', '#b42318'],
[116.47, 39.945, '仓储', '#2f9e6e'],
[116.44, 39.862, '南站', '#a9700f'],
[116.36, 39.875, '西片', '#6b4fa0']
];
var features = spots.map(function (s) {
return new ol.Feature({
geometry: new ol.geom.Point(fromLonLat([s[0], s[1]])),
name: s[2],
style: new ol.style.Style({
image: iconFor(s[3]),
text: new ol.style.Text({
text: s[2],
offsetY: 6,
font: '600 12px sans-serif',
fill: new ol.style.Fill({ color: '#16181d' }),
stroke: new ol.style.Stroke({ color: '#ffffff', width: 3 })
})
})
});
});
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'}) }),
new ol.layer.Vector({ source: new ol.source.Vector({ features: features }) })
],
view: new ol.View({ center: fromLonLat([116.40, 39.905]), zoom: 12 }),
controls: ol.control.defaults.defaults({ rotate: false })
});
})();
</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.