Atlaslog
Atlaslog / Examples / OpenLayers / Iiif
OpenLayers · Runnable Example

Iiif

Runnable OpenLayers example: Iiif. 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-iiif.htmlLIVE
single HTML file · CDN dependencies onlyinterface may contain Chinese labels

Source

demos/openlayers-iiif.html
<!--
  示例:IIIF 图像 — OpenLayers iiif
  核心逻辑: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>IIIF 图像</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:3px 9px;border:1px solid #ccd2da;background:#fff;
       border-radius:3px;cursor:pointer;color:#2b3140}
  #map{flex:1 1 auto;min-height:0;background:#eef0f3}
</style>
</head>
<body>
<div class="bar">
  <button id="zoomIn">放大扫描件</button>
  <button id="reset">适合窗口</button>
  <span>双击图像可继续放大</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;
  }

  /* 画一张“古籍扫描件”:纸色、竖排文字、朱印 */
  function makeSheet() {
    var w = 800, h = 1100;
    var c = document.createElement('canvas');
    c.width = w;
    c.height = h;
    var ctx = c.getContext('2d');

    var paper = ctx.createLinearGradient(0, 0, w, h);
    paper.addColorStop(0, '#f3e7cd');
    paper.addColorStop(0.5, '#ecd9b5');
    paper.addColorStop(1, '#e2cba3');
    ctx.fillStyle = paper;
    ctx.fillRect(0, 0, w, h);

    ctx.strokeStyle = 'rgba(120,90,55,0.35)';
    ctx.lineWidth = 2;
    ctx.strokeRect(40, 40, w - 80, h - 80);
    for (var i = 1; i < 9; i++) {
      ctx.beginPath();
      ctx.moveTo(40 + i * (w - 80) / 9, 40);
      ctx.lineTo(40 + i * (w - 80) / 9, h - 40);
      ctx.stroke();
    }

    ctx.fillStyle = 'rgba(50,35,20,0.82)';
    ctx.font = '26px "PingFang SC","Microsoft YaHei",serif';
    var text = '山河千里 城阙九重 风云际会 岁月无声';
    for (var k = 0; k < text.length; k++) {
      ctx.fillText(text.charAt(k), w - 90 - (k % 4) * 90, 110 + Math.floor(k / 4) * 120);
    }

    ctx.fillStyle = 'rgba(178,34,34,0.75)';
    ctx.beginPath();
    ctx.arc(w - 150, h - 150, 52, 0, Math.PI * 2);
    ctx.fill();
    ctx.fillStyle = 'rgba(255,245,235,0.9)';
    ctx.font = '600 26px "PingFang SC",serif';
    ctx.textAlign = 'center';
    ctx.fillText('藏', w - 150, h - 140);

    return c.toDataURL();
  }

  var imageExtent = ol.proj.transformExtent(
    [116.36, 39.88, 116.42, 39.93], 'EPSG:4326', 'EPSG:3857'
  );

  var sheetSource = new ol.source.ImageStatic({
    url: makeSheet(),
    projection: 'EPSG:3857',
    imageExtent: imageExtent,
    crossOrigin: 'anonymous',
    /* 放大时让浏览器插值,扫描件边缘更柔和 */
    interpolate: true
  });

  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'}), opacity: 0.55 }),
      new ol.layer.Image({ source: sheetSource })
    ],
    view: new ol.View({
      center: ol.proj.fromLonLat([116.39, 39.905]),
      zoom: 15,
      minZoom: 2,
      maxZoom: 22
    }),
    controls: ol.control.defaults.defaults({ attribution: false })
  });

  function fitSheet() {
    map.getView().fit(imageExtent, { padding: [40, 40, 40, 40], duration: 400 });
  }

  document.getElementById('zoomIn').addEventListener('click', function () {
    var view = map.getView();
    view.animate({ zoom: (view.getZoom() || 15) + 1, duration: 300 });
  });

  document.getElementById('reset').addEventListener('click', fitSheet);

  fitSheet();
})();
</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 · EXAMPLEImage Filter OpenLayers · EXAMPLEImage Load Events OpenLayers · EXAMPLEImage Vector Layer