You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在OpenSeadragon的getTileUrl中构建指定格式的瓦片URL?

Building Custom Tile URLs for OpenSeadragon

Alright, let's break this down step by step to build that custom tile URL in OpenSeadragon's getTileUrl callback.

First, let's parse the example URL you provided: jpg/0,0,500,800/105,/0/default.jpg. From this, we can identify the structure we need to replicate:
[base-prefix]/[left,top,right,bottom]/[static-or-dynamic-value]/[level]/default.jpg

Step 1: Define Key Tile Parameters

First, you need to know two critical values from your tile server:

  • tileWidth: The width of each tile (in pixels, e.g., 500 from your example)
  • tileHeight: The height of each tile (in pixels, e.g., 800 from your example)

These must match exactly what the server expects—if they're wrong, your tile requests will return incorrect or missing images.

Step 2: Implement the getTileUrl Callback

Here's a complete example of how to calculate the required URL components and assemble the final string:

const viewer = OpenSeadragon({
    // Your viewer configuration (container, etc.)
    tileSources: {
        // Required metadata for OpenSeadragon to understand the tile set
        width: 5000, // Total width of the image at the highest zoom level
        height: 8000, // Total height of the image at the highest zoom level
        tileSize: 500, // Match your tileWidth here
        minLevel: 0, // Minimum zoom level supported by the server
        maxLevel: 3, // Maximum zoom level supported by the server

        getTileUrl: function(level, x, y) {
            // Calculate the pixel boundaries of the current tile
            const tileWidth = 500;
            const tileHeight = 800;
            
            // Basic calculation (assuming tile Y-axis starts at the top of the image)
            const left = x * tileWidth;
            const top = y * tileHeight;
            const right = left + tileWidth;
            const bottom = top + tileHeight;

            // If your tile server uses a reversed Y-axis (tiles start at the bottom), use this instead:
            // const top = this.height - (y + 1) * tileHeight;
            // const bottom = this.height - y * tileHeight;

            // Assemble the final URL
            // Replace "105," with the correct value from your server (could be static or dynamic)
            return `jpg/${left},${top},${right},${bottom}/105,/${level}/default.jpg`;
        }
    }
});

Key Notes & Adjustments

  • Reversed Y-Axis: Some tile servers use a coordinate system where y=0 starts at the bottom of the image instead of the top. If your tiles are loading upside down or in the wrong position, switch to the reversed Y calculation commented in the code.
  • The "105," Segment: In your example URL, this part looks like a static value, but if it's actually tied to tile properties (e.g., tile height or resolution), replace it with the corresponding variable (e.g., ${tileHeight},).
  • Level Alignment: Double-check that OpenSeadragon's level value matches what your server expects. If the server uses 1-based indexing instead of 0-based, adjust the level in the URL to level + 1.
  • Total Image Dimensions: The width and height properties in the tile source must match the total pixel dimensions of your image at the highest zoom level. This tells OpenSeadragon how many tiles exist per level.

内容的提问来源于stack exchange,提问作者Jack Sparrow

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 06:36:12