Canvas Gradient Pattern
Runnable OpenLayers example: Canvas Gradient Pattern. 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 渐变与图案填充
要点:ol.style.Fill 的 color 可以直接接 CanvasGradient 与 CanvasPattern
依赖: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>Canvas 渐变与图案填充</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:12px;align-items:center;font-size:12px;color:#58606e;flex-wrap:wrap}
.bar label{display:flex;gap:5px;align-items:center;cursor:pointer}
#map{flex:1 1 auto;min-height:0;background:#eef0f3}
</style>
</head>
<body>
<div class="bar">
<label><input type="radio" name="fill" value="gradient" checked> 线性渐变</label>
<label><input type="radio" name="fill" value="pattern"> 点阵图案</label>
<label><input type="radio" name="fill" value="solid"> 纯色</label>
</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;
/* 渐变:在 256×256 的画布上建一个对角渐变对象 */
var gradCanvas = document.createElement('canvas');
gradCanvas.width = gradCanvas.height = 256;
var gctx = gradCanvas.getContext('2d');
var gradient = gctx.createLinearGradient(0, 0, 256, 256);
gradient.addColorStop(0, 'rgba(31,95,208,0.85)');
gradient.addColorStop(0.5, 'rgba(122,208,200,0.85)');
gradient.addColorStop(1, 'rgba(250,193,86,0.85)');
/* 图案:8×8 单元内画一个圆点,再用 createPattern 平铺 */
var patCanvas = document.createElement('canvas');
patCanvas.width = patCanvas.height = 8;
var pctx = patCanvas.getContext('2d');
pctx.fillStyle = '#e8f0fb';
pctx.fillRect(0, 0, 8, 8);
pctx.fillStyle = '#1f5fd0';
pctx.beginPath();
pctx.arc(4, 4, 1.8, 0, Math.PI * 2);
pctx.fill();
var pattern = pctx.createPattern(patCanvas, 'repeat');
var mode = 'gradient';
function makePolygon(ring, name) {
return new ol.Feature({
geometry: new ol.Polygon([ring.map(function (p) { return fromLonLat(p); })]),
name: name
});
}
var features = [
makePolygon([[116.30, 39.94], [116.38, 39.94], [116.38, 39.88], [116.30, 39.88]], '西城'),
makePolygon([[116.40, 39.95], [116.47, 39.95], [116.47, 39.89], [116.40, 39.89]], '东城'),
makePolygon([[116.42, 39.87], [116.50, 39.87], [116.50, 39.81], [116.42, 39.81]], '丰台')
];
function fillColor() {
if (mode === 'gradient') return gradient;
if (mode === 'pattern') return pattern;
return 'rgba(31,95,208,0.5)';
}
function styleFn() {
return new ol.style.Style({
fill: new ol.style.Fill({ color: fillColor() }),
stroke: new ol.style.Stroke({ color: '#0f3f8f', width: 1.5 }),
text: new ol.style.Text({
font: '500 12px sans-serif',
fill: new ol.style.Fill({ color: '#16181d' }),
stroke: new ol.style.Stroke({ color: '#ffffff', width: 3 })
})
});
}
var layer = new ol.layer.Vector({
source: new ol.source.Vector({ features: features }),
style: styleFn
});
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'}) }), layer],
view: new ol.View({ center: fromLonLat([116.40, 39.90]), zoom: 11 }),
controls: ol.control.defaults.defaults({ rotate: false })
});
/* 标注文字跟着要素名走,需要在样式函数里拿到 feature */
layer.setStyle(function (feature) {
var s = styleFn();
if (feature) s.getText().setText(feature.get('name'));
return s;
});
document.querySelectorAll('input[name=fill]').forEach(function (el) {
el.addEventListener('change', function () {
mode = this.value;
layer.changed(); /* 样式对象变了,通知图层重绘 */
});
});
})();
</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.