Atlaslog
Atlaslog / Examples / OpenLayers / WMTS Hidpi
OpenLayers · Runnable Example

WMTS Hidpi

Runnable OpenLayers example: WMTS Hidpi. Open the live demo, read the complete source, and copy the file into your project. No build step or API key.

Open standalone → Chinese notes All OpenLayers examples
/demos/openlayers-wmts-hidpi.htmlLIVE
single HTML file · CDN dependencies onlyinterface may contain Chinese labels

Source

demos/openlayers-wmts-hidpi.html
<!--
  示例:高 DPI 瓦片 — OpenLayers wmts-hidpi
  核心逻辑:TileImage 设 tilePixelRatio 为 2,一张 512 像素的 canvas 按 256 像素瓦片投放
  依赖: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>高 DPI 瓦片</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 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="checkbox" id="osm" checked> 叠加 OSM 底图</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 generated = 0;
  var SIZE = 256;

  /* devicePixelRatio 为 2 的客户端应当拿到 512 像素的瓦片 */
  function tileUrl(z, x, y) {
    generated++;
    var px = SIZE * 2;
    var c = document.createElement('canvas');
    c.width = px;
    c.height = px;
    var ctx = c.getContext('2d');

    ctx.fillStyle = '#f2f0e6';
    ctx.fillRect(0, 0, px, px);

    ctx.strokeStyle = '#b9c3cf';
    ctx.lineWidth = 2;
    ctx.strokeRect(1, 1, px - 2, px - 2);

    /* 细密网格只有高 DPI 下才看得清 */
    ctx.strokeStyle = 'rgba(31,95,208,0.18)';
    ctx.lineWidth = 1;
    for (var i = 1; i < 8; i++) {
      ctx.beginPath();
      ctx.moveTo(i * px / 8, 0);
      ctx.lineTo(i * px / 8, px);
      ctx.stroke();
      ctx.beginPath();
      ctx.moveTo(0, i * px / 8);
      ctx.lineTo(px, i * px / 8);
      ctx.stroke();
    }

    ctx.fillStyle = '#1f5fd0';
    ctx.font = '600 26px sans-serif';
    ctx.fillText('z' + z, 18, 46);
    ctx.font = '500 22px sans-serif';
    ctx.fillStyle = '#58606e';
    ctx.fillText(x + ' / ' + y, 18, 78);

    ctx.beginPath();
    ctx.arc(px - 40, px - 40, 14, 0, Math.PI * 2);
    ctx.fillStyle = '#c92a2a';
    ctx.fill();
    ctx.strokeStyle = '#fff';
    ctx.lineWidth = 4;
    ctx.stroke();

    return c.toDataURL();
  }

  var hidpiSource = new ol.source.TileImage({
    projection: 'EPSG:3857',
    tileGrid: ol.tilegrid.createXYZ({ maxZoom: 19, tileSize: SIZE }),
    /* 关键:逻辑瓦片 256,物理像素 512 */
    tilePixelRatio: 2,
    tileUrlFunction: function (tileCoord) {
      return tileUrl(tileCoord[0], tileCoord[1], tileCoord[2]);
    }
  });

  var hidpiLayer = new ol.layer.Tile({ source: hidpiSource, opacity: 0.92 });

  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'}) }),
      hidpiLayer
    ],
    view: new ol.View({
      center: ol.proj.fromLonLat([116.391, 39.907]),
      zoom: 13,
      minZoom: 2,
      maxZoom: 18
    }),
    controls: ol.control.defaults.defaults({ attribution: false })
  });

  hidpiSource.on('tileloadend', function () {
    document.getElementById('info').textContent =
      '窗格像素比 ' + (window.devicePixelRatio || 1) +
      ' · 已生成 ' + generated + ' 张 512px 瓦片';
  });

  document.getElementById('osm').addEventListener('change', function () {
    map.getLayers().item(0).setVisible(this.checked);
  });
})();
</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.

More OpenLayers examples

OpenLayers · EXAMPLEWMTS Ign OpenLayers · EXAMPLEWMTS Layer From Capabilities OpenLayers · EXAMPLEXYZ