Whole-slide tiles (NVSlide, HTTP range)
Tile Viewer
Code
{ const toggle = document.getElementById('hudToggle') const key = `hud-off:${location.pathname}` const setHud = (off) => { document.body.classList.toggle('hud-off', off) toggle.setAttribute('aria-pressed', String(!off)) toggle.textContent = off ? 'HUD: off' : 'HUD: on' try { localStorage.setItem(key, off ? '1' : '') } catch {} } try { setHud(localStorage.getItem(key) === '1') } catch {} toggle.onclick = () => setHud(!document.body.classList.contains('hud-off')) // I (info): NiiVue already binds V, A, C, H/J/K/L, U and D. addEventListener('keydown', (e) => { if (e.repeat || e.key?.toUpperCase() !== 'I' || e.ctrlKey || e.metaKey || e.altKey) return if (/^(INPUT|SELECT|TEXTAREA)$/.test(e.target.tagName)) return toggle.click() })}// Whole-slide-image tile viewer built on the NVSlide core class.//// NVSlide owns the manifest/levels/tiles model, the viewport (pan/zoom), HTTP// Range-request tile streaming, the tile cache (bitmaps, plus samples for monochrome) and request stats. This// demo just wires DOM controls to it and draws it in a `createSlideView`// (renderer, frame loop, gestures, overlay). WebGPU by default; add// `?backend=webgl2` for WebGL2, `?aa=on|off` to override the anti-aliasing// default. TIFF/SVS slides live in apps/demo-ext-tile.//// The synthetic pyramid (public/tile-range-poc/) streams one tile's worth of// bytes at a time over HTTP 206 range requests, so a multi-gigabyte slide never// has to be downloaded whole.
import { createFrameDecoder } from '@niivue/dcm2niix'import { buildDrawingLut, createSlideView, DRAG_MODE, DziSource, drawingBitmapToRGBA, formatSlideSample, lookupColorMap, NVSlide, SlideDrawing, SlideVectorLayer, slideStatsSummary,} from '@niivue/niivue'import { backendFromUrl, isAntiAlias } from './displayControls.js'import { urlParamSelect } from './urlParamSelect.js'
// DZI sources construct via NVSlide.fromSource(DziSource.fromUrl(...)) rather// than a manifest URL. This OpenSeadragon example (overlap 2) is CORS-enabled.const DZI_URLS = { 'dzi-highsmith': 'https://openseadragon.github.io/example-images/highsmith/highsmith.dzi',}
// NVSlide core ships no JPEG 2000 or JPEG-LS codec: dcm2niix's WASM worker pool// decodes those tiles, following each level's photometric and BitsStored.// Monochrome tiles return samples, which NVSlide windows. Started on the first tile.let frameDecoder = null// dcm2niix never respawns a dead worker, so a worker failure (a plain Error,// unlike a bad frame, an abort or 'frame decoder busy') replaces the pool, and// the tile retries once on the new one: a death masked by an earlier abort// surfaces only on the next tile. The old pool is terminated once its other// in-flight tiles (on a worker that may still be live) settle.async function withFrameDecoder(signal, run, isRetry = false) { frameDecoder ??= { decoder: createFrameDecoder({ workers: 2 }), active: 0 } const p = frameDecoder p.active++ try { return await run(p.decoder) } catch (err) { if ( err !== signal?.reason && err?.name === 'Error' && !err.message.startsWith('frame decoder ') ) { p.retired = true if (frameDecoder === p) frameDecoder = null if (!isRetry && !signal?.aborted) return withFrameDecoder(signal, run, true) } throw err } finally { if (--p.active === 0 && p.retired) p.decoder.terminate() }}/** @type {import('@niivue/niivue').SlideTileDecoder} */async function decodeDicomTile(bytes, { level, signal }) { const options = { photometric: level.photometric, bitsStored: level.bitsStored, signal, } if (level.photometric?.startsWith('MONOCHROME')) { return withFrameDecoder(signal, (d) => d.decode(bytes, { ...options, output: 'samples' }), ) } const f = await withFrameDecoder(signal, (d) => d.decode(bytes, options)) return createImageBitmap(new ImageData(f.rgba, f.width, f.height))}NVSlide.registerTileDecoder('image/jp2', decodeDicomTile)NVSlide.registerTileDecoder('image/jls', decodeDicomTile)// ICC colour management: dcm2niix's LittleCMS bakes each slide's profile into a// 33^3 sRGB table.NVSlide.registerIccTransform(async (profile, signal) => { const data = await withFrameDecoder(signal, (d) => d.iccLut(profile, { size: 33, signal }), ) return data && { size: 33, data }})
const SYNTHETIC_MANIFEST_PATH = 'tile-range-poc/tiles.json'// DICOM-WSI manifests use dicom-wsi-range-v1: per-level JPEG tiles addressed// by byte offset, which NVSlide fetches over HTTP Range and decodes in-browser.// Manifests and binaries resolve from the streaming bases below.const DICOM_WSI_PATH = 'dicom-wsi/cptac-brca/manifest.json'// Manifest path per source-dropdown value. The OpenSlide DICOM-WSI archives// and manifests are hosted with Git LFS; only JPEG TILED_FULL sources are// loadable in-browser (see scripts/fetch-openslide-dicom.ts).const MANIFEST_PATHS = { synthetic: SYNTHETIC_MANIFEST_PATH, 'dicom-wsi': DICOM_WSI_PATH, 'openslide-3dhistech-1': 'dicom-wsi/3dhistech-1/manifest.json', 'openslide-jp2k-33003-1': 'dicom-wsi/jp2k-33003-1/manifest.json', // Monochrome JPEG-LS test slides (3dhistech-1 luminance): window, BitsStored, MONOCHROME1. 'mono12-jls': 'dicom-wsi/mono12-jls/manifest.json', 'mono8-jls-m1': 'dicom-wsi/mono8-jls-m1/manifest.json', // OpenSlide DICOM test slides showing what the DICOM-WSI index handles. 'openslide-3dhistech-2': 'dicom-wsi/3dhistech-2/manifest.json', 'openslide-leica-4': 'dicom-wsi/leica-4/manifest.json', 'openslide-hamamatsu-2': 'dicom-wsi/hamamatsu-2/manifest.json', 'openslide-jp2k-rct': 'dicom-wsi/cmu-1-jp2k-rct/manifest.json',}const MAX_CACHE_BYTES = 96 * 1024 * 1024const TARGET_SCREEN_PIXELS_PER_TILE_PIXEL = 0.75const RANGE_LOG_LENGTH = 24
function el(id) { const node = document.getElementById(id) if (!node) throw new Error(`Missing #${id}`) return node}
const els = { backendSelect: el('backendSelect'), aaSelect: el('aaSelect'), slideFilter: el('slideFilter'), retinaControl: el('retinaControl'), retina: el('retina'), source: el('source'), levelMode: el('levelMode'), windowControl: el('windowControl'), window: el('window'), zoomOut: el('zoomOut'), zoomIn: el('zoomIn'), fit: el('fit'), showGrid: el('showGrid'), scaleBar: el('scaleBar'), canvas: el('tileCanvas'), overlay: el('drawOverlay'), drawBtn: el('drawBtn'), drawTool: el('drawTool'), wandTol: el('wandTol'), penValue: el('penValue'), drawUndo: el('drawUndo'), drawClear: el('drawClear'), exportSvg: el('exportSvg'), hud: el('hud'), rangeLog: el('rangeLog'), fallback: el('fallback'),}
// Wired before anything loads, so the menu works even if loading fails (#488).urlParamSelect(els.backendSelect, 'backend', backendFromUrl())urlParamSelect(els.aaSelect, 'aa', isAntiAlias ? 'on' : 'off')
// The slide view (renderer, frame loop, gestures), made in main().let view = nulllet slide = null// The slide pixel under the pointer, for the HUD's value readout.let hoverCss = null // the pointer in canvas CSS px: a pan moves the slide under it// Slide-space drawing: a SlideDrawing raster (painted with the shared pen tools,// in slide pixels) shown via a stacked Canvas2D overlay mapped through NVSlide's// screen<->slide transform, so the annotation stays registered under pan/zoom.const DRAW_RASTER_CAP = 1536let drawing = nulllet drawMode = falselet drawStroke = null // { pointerId } while a stroke is in progresslet lastRasterPt = nulllet overlayDirty = falselet rasterCanvas = nulllet rasterCtx = nulllet rasterImageData = null// Vector annotation layer (slide-pixel coords) + in-progress freehand polygon.let vector = nulllet vecPoints = nulllet penValue = 1let drawTool = 'pen' // pen | eraser | bucket | filled | wandlet wandTolerance = 40// The shared "draw" label colormap, so annotation colors match the 3D demo and// each pen value paints a distinct label color. Resolved lazily (Vite glob).let drawLut = nullconst DRAW_OPACITY = 0.85
function resolveDemoUrl(path) { const base = import.meta.env.BASE_URL || '/' const normalized = base.endsWith('/') ? base : `${base}/` return new URL(`${normalized}${path}`, window.location.href).toString()}
// Streaming slides (DICOM-WSI, OpenSlide DICOM) come from the Git LFS// repository the Pages build uses, so `bun run dev` loads them with no fetch// step: manifests from raw, binaries from the LFS media endpoint (raw returns// LFS pointers). VITE_STREAMING_ASSET_BASE=/ serves local copies instead// (scripts/fetch-dicom-wsi.ts, scripts/fetch-openslide-dicom.ts).const STREAMING_REPO = 'niivue/niivue-demo-images-lfs/main/streaming/'const STREAMING_ASSET_BASE = import.meta.env.VITE_STREAMING_ASSET_BASE || `https://raw.githubusercontent.com/${STREAMING_REPO}`const STREAMING_MEDIA_BASE = import.meta.env.VITE_STREAMING_MEDIA_BASE || import.meta.env.VITE_STREAMING_ASSET_BASE || `https://media.githubusercontent.com/media/${STREAMING_REPO}`const isLocalStreaming = !/^https?:/.test(STREAMING_ASSET_BASE)
function resolveStreamingUrl(path) { const base = STREAMING_ASSET_BASE const normalized = base.endsWith('/') ? base : `${base}/` return new URL(`${normalized}${path}`, window.location.href).toString()}
function resolveStreamingMediaUrl(path) { const base = STREAMING_MEDIA_BASE const normalized = base.endsWith('/') ? base : `${base}/` return new URL(`${normalized}${path}`, window.location.href).toString()}
function manifestUrl() { const path = MANIFEST_PATHS[els.source.value] ?? SYNTHETIC_MANIFEST_PATH return els.source.value === 'synthetic' ? resolveDemoUrl(path) : resolveStreamingUrl(path)}
function manifestBinaryBaseUrl() { const path = MANIFEST_PATHS[els.source.value] ?? SYNTHETIC_MANIFEST_PATH return els.source.value === 'synthetic' ? resolveDemoUrl(path) : resolveStreamingMediaUrl(path)}
async function slideFromManifest(opts) { const url = manifestUrl() const binaryBaseUrl = manifestBinaryBaseUrl() if (binaryBaseUrl === url) return NVSlide.fromManifestUrl(url, opts)
const response = await fetch(url) if (!response.ok) throw new Error(`Slide manifest HTTP ${response.status}`) return new NVSlide(await response.json(), { ...opts, manifestUrl: binaryBaseUrl, })}
function html(value) { return value .replaceAll('&', '&') .replaceAll('<', '<') .replaceAll('>', '>') .replaceAll('"', '"') .replaceAll("'", ''')}
const screenForCanvas = () => view.screen()const requestRender = () => view?.requestRender()
// After each frame: the drawing overlay, HUD, range log and export button.function afterRender(screen) { // Retina is offered only on a HiDPI display (another monitor, browser zoom). els.retinaControl.hidden = window.devicePixelRatio <= 1 drawOverlay(screen) updateHud(screen) updateRangeLog() updateExportEnabled()}
// The SVG export only has content when the VECTOR tool has drawn shapes (a raster// pen drawing is not exportable as SVG). Disable the button otherwise so clicking// it isn't a silent no-op that looks broken mid-demo. Cheap to re-evaluate per// frame; the state tracks add/undo/clear/source-switch without extra wiring.function updateExportEnabled() { els.exportSvg.disabled = !slide || !vector || vector.shapes.length === 0}
// Build (or rebuild) the slide-space drawing raster for the active slide.function ensureDrawing() { if (!slide) { drawing = null return } const sw = slide.manifest.width const sh = slide.manifest.height const scale = Math.min(1, DRAW_RASTER_CAP / Math.max(1, sw, sh)) const rw = Math.max(1, Math.round(sw * scale)) const rh = Math.max(1, Math.round(sh * scale)) drawing = new SlideDrawing(rw, rh) vector = new SlideVectorLayer() vecPoints = null lastRasterPt = null rasterCanvas = document.createElement('canvas') rasterCanvas.width = rw rasterCanvas.height = rh rasterCtx = rasterCanvas.getContext('2d') rasterImageData = rasterCtx.createImageData(rw, rh) overlayDirty = true}
// Repaint the offscreen raster from the label bitmap via the "draw" LUT, so// each pen value shows its label color (same palette as the 3D slide demo).function rebuildRaster() { if (!drawing || !rasterCtx || !rasterImageData) return if (!drawLut) { const cm = lookupColorMap('draw') if (cm) drawLut = buildDrawingLut(cm) } const data = rasterImageData.data if (drawLut) { const rgba = drawingBitmapToRGBA( drawing.img, drawLut.lut, drawLut.min ?? 0, DRAW_OPACITY, ) data.set(rgba) } else { // Fallback if the colormap isn't available: single color. const img = drawing.img for (let i = 0; i < img.length; i++) { const o = i * 4 if (img[i]) { data[o] = 235 data[o + 1] = 45 data[o + 2] = 55 data[o + 3] = 210 } else { data[o + 3] = 0 } } } rasterCtx.putImageData(rasterImageData, 0, 0)}
// Inverse of NVSlide.screenToSlide: slide base pixel -> canvas CSS coords.function slideToCss(sx, sy, screen) { const vp = slide.viewport const sc = vp.scale const leftSlide = vp.centerX - screen.widthCss / (2 * sc) const xCss = (sx - leftSlide) * sc const yCss = slide.isYAxisUp() ? screen.heightCss / 2 - (sy - vp.centerY) * sc : (sy - (vp.centerY - screen.heightCss / (2 * sc))) * sc return [xCss, yCss]}
// Draw the annotation raster over the tiles, mapped to the current viewport.function drawOverlay(screen) { const ov = els.overlay if (ov.width !== els.canvas.width || ov.height !== els.canvas.height) { ov.width = els.canvas.width ov.height = els.canvas.height } const octx = ov.getContext('2d') octx.clearRect(0, 0, ov.width, ov.height) if (!slide || !drawing) return if (overlayDirty) { rebuildRaster() overlayDirty = false } const dpr = screen.devicePixelRatio ?? 1 const sw = slide.manifest.width const sh = slide.manifest.height const [x0, y0] = slideToCss(0, 0, screen) const [x1, y1] = slideToCss(sw, sh, screen) const dx = Math.min(x0, x1) * dpr const dy = Math.min(y0, y1) * dpr const dW = Math.abs(x1 - x0) * dpr const dH = Math.abs(y1 - y0) * dpr octx.imageSmoothingEnabled = false if (y0 > y1) { // yAxis up: slide row 0 is at the bottom — flip vertically. octx.save() octx.translate(0, dy + dH) octx.scale(1, -1) octx.drawImage(rasterCanvas, dx, 0, dW, dH) octx.restore() } else { octx.drawImage(rasterCanvas, dx, dy, dW, dH) } // Vector annotations (slide-space polygons) stroked over the raster. const strokePath = (pts, color, closed) => { if (!pts || pts.length < 2) return octx.beginPath() for (let i = 0; i < pts.length; i++) { const [cx, cy] = slideToCss(pts[i][0], pts[i][1], screen) if (i === 0) octx.moveTo(cx * dpr, cy * dpr) else octx.lineTo(cx * dpr, cy * dpr) } if (closed) octx.closePath() octx.lineWidth = Math.max(1, 2 * dpr) octx.lineJoin = 'round' octx.lineCap = 'round' octx.strokeStyle = color octx.stroke() } if (vector) { for (const s of vector.shapes) { strokePath(s.points, s.color, s.kind === 'polygon') } } if (vecPoints) strokePath(vecPoints, penColorCss(), false) // in-progress}
// Convert a pointer event to a slide-drawing raster pixel, or null if off-slide.function eventToRaster(event) { if (!slide || !drawing) return null const rect = els.canvas.getBoundingClientRect() const sp = slide.screenToSlide( event.clientX - rect.left, event.clientY - rect.top, screenForCanvas(), ) const rx = Math.round((sp.x / slide.manifest.width) * drawing.width) const ry = Math.round((sp.y / slide.manifest.height) * drawing.height) if (rx < 0 || ry < 0 || rx >= drawing.width || ry >= drawing.height) { return null } return [rx, ry]}
function penSizeForRaster() { return Math.max(2, Math.round(drawing.width / 300))}
// Pointer event -> slide base-pixel coords (resolution-independent; for vectors).function eventToSlide(event) { if (!slide) return null const rect = els.canvas.getBoundingClientRect() return slide.screenToSlide( event.clientX - rect.left, event.clientY - rect.top, screenForCanvas(), )}
// CSS hex for the current pen label, from the "draw" LUT (matches the raster).function penColorCss() { if (!drawLut) { const cm = lookupColorMap('draw') if (cm) drawLut = buildDrawingLut(cm) } const lut = drawLut?.lut const min = drawLut?.min ?? 0 const i = (penValue - min) * 4 if (!lut || i < 0 || i + 2 >= lut.length) return '#e62d37' const hex = (n) => n.toString(16).padStart(2, '0') return `#${hex(lut[i])}${hex(lut[i + 1])}${hex(lut[i + 2])}`}
// Build an RGBA colour reference for the magic wand at the drawing-raster// resolution by compositing the cached tiles of the most detailed pyramid level// that fits the raster. Rebuilt per use so tiles that stream in are picked up;// returns whatever is cached (partial = some black, improves on a later click).function buildWandReference() { if (!slide || !drawing) return null const levels = slide.manifest.levels let level = levels[levels.length - 1] for (const lv of levels) { if (lv.width <= drawing.width && lv.width > level.width) level = lv } const c = document.createElement('canvas') c.width = drawing.width c.height = drawing.height const cx = c.getContext('2d') // Requests missing tiles (a sharper re-click); scalar tiles composite on the CPU. const bmp = slide.thumbnailBitmap(level, Math.max(c.width, c.height)) if (bmp) cx.drawImage(bmp, 0, 0, c.width, c.height) return cx.getImageData(0, 0, c.width, c.height).data}
function updateHud(screen) { // The drawn level: a pinned level too large for the tile cache shows a coarser one. const level = slide?.lastVisibleTiles?.level if (!slide || !level) { els.hud.innerHTML = '<div class="title">Loading tile manifest</div>' return } const s = slide.stats const cls = { requests: s.fullFileFallbacks > 0 ? 'warn' : 'ok', failures: s.failures > 0 ? 'bad' : '', } const rows = [ ['backend', view.backend === 'webgpu' ? 'WebGPU' : 'WebGL2'], ['source', slide.sourceLabelForLevel(level)], ...slideStatsSummary(slide, screen).filter(([key]) => key !== 'slide'), ] els.hud.innerHTML = ` <div class="title">${html(slide.manifest.name)}</div> ${rows.map(([key, text]) => `<div class="row"><span class="key">${key}</span><span class="${cls[key] ?? ''}">${html(text)}</span></div>`).join('')} ${hoverRow()} `}
// Per-channel values under the pointer, from cached tiles only (render runs// again when a tile lands, so a finer level shows up without a new move).function hoverRow() { if (!hoverCss) return '' const p = slide.screenToSlide(hoverCss.xCss, hoverCss.yCss, screenForCanvas()) const text = formatSlideSample(p.x, p.y, slide.sampleAt(p.x, p.y)) return text ? `<div class="row"><span class="key">value</span><span>${html(text)}</span></div>` : ''}
function updateRangeLog() { const events = slide?.stats.lastRequests ?? [] const spans = [] for (let i = 0; i < RANGE_LOG_LENGTH - events.length; i++) { spans.push(document.createElement('span')) } for (const event of events) { const span = document.createElement('span') span.className = event.status === 'hit' ? 'hit' : event.status span.title = event.label spans.push(span) } els.rangeLog.replaceChildren(...spans)}
function populateLevels(manifest) { els.levelMode.replaceChildren() const auto = document.createElement('option') auto.value = 'auto' auto.textContent = 'auto LOD' els.levelMode.appendChild(auto) for (const level of manifest.levels) { const option = document.createElement('option') option.value = String(level.index) option.textContent = `L${level.index} ${level.width}x${level.height}` els.levelMode.appendChild(option) } els.levelMode.value = 'auto'}
function showFallback(message) { els.fallback.textContent = message els.fallback.setAttribute('aria-hidden', 'false')}
// Let the user drag a floating panel (the HUD) out of the way of a tile.// Switches the element to left/top positioning and clamps it to the viewport.function makeDraggable(node) { let dragging = null node.addEventListener('pointerdown', (event) => { if (event.button !== 0) return const rect = node.getBoundingClientRect() dragging = { pointerId: event.pointerId, offsetX: event.clientX - rect.left, offsetY: event.clientY - rect.top, } node.setPointerCapture(event.pointerId) node.classList.add('dragging') }) node.addEventListener('pointermove', (event) => { if (!dragging || dragging.pointerId !== event.pointerId) return const width = node.offsetWidth const height = node.offsetHeight const maxX = Math.max(0, window.innerWidth - width) const maxY = Math.max(0, window.innerHeight - height) const x = Math.min(maxX, Math.max(0, event.clientX - dragging.offsetX)) const y = Math.min(maxY, Math.max(0, event.clientY - dragging.offsetY)) node.style.left = `${x}px` node.style.top = `${y}px` node.style.right = 'auto' node.style.bottom = 'auto' }) const end = (event) => { if (!dragging || dragging.pointerId !== event.pointerId) return dragging = null node.classList.remove('dragging') } node.addEventListener('pointerup', end) node.addEventListener('pointercancel', end)}
function zoomCentered(factor) { if (!slide) return const screen = screenForCanvas() slide.zoomBy(factor, screen.widthCss / 2, screen.heightCss / 2, screen)}
// Wired in main() on view.canvas (a failed WebGPU start can replace the canvas).function onDrawDown(event) { // Draw strokes take no pointer capture: a captured click can swallow the next // click elsewhere (a header link needing two). Pan and zoom go through the // view's controller, which captures only once a press becomes a drag. // Draw mode paints the slide with the left button (the controller leaves it free). if (event.button === 0 && drawMode && drawing) { // Vector tool draws a freehand polygon in slide coords (not the raster). if (drawTool === 'vector') { const sp = eventToSlide(event) vecPoints = sp ? [[sp.x, sp.y]] : [] drawStroke = { pointerId: event.pointerId, tool: 'vector' } requestRender() return } const pt = eventToRaster(event) drawing.beginStroke() // snapshot for undo (all raster tools) drawStroke = { pointerId: event.pointerId, tool: drawTool, points: [] } if (drawTool === 'bucket') { if (pt) { drawing.bucketFill(pt[0], pt[1], penValue, true) overlayDirty = true requestRender() } return } if (drawTool === 'wand') { if (pt) { const ref = buildWandReference() if (ref) { drawing.magicWand(ref, pt[0], pt[1], wandTolerance, penValue, true) overlayDirty = true requestRender() } } return } // pen / eraser / filled: paint the first dab (filled also accrues points). const pv = drawTool === 'eraser' ? 0 : penValue if (pt) { drawing.point(pt[0], pt[1], pv, penSizeForRaster(), true) drawStroke.points.push(pt) lastRasterPt = pt overlayDirty = true requestRender() } return }}
function onDrawMove(event) { if (drawStroke && drawStroke.pointerId === event.pointerId) { if (drawStroke.tool === 'bucket' || drawStroke.tool === 'wand') return // single click if (drawStroke.tool === 'vector') { const sp = eventToSlide(event) if (sp && vecPoints) { vecPoints.push([sp.x, sp.y]) requestRender() } return } const pt = eventToRaster(event) if (pt) { const pv = drawStroke.tool === 'eraser' ? 0 : penValue if (lastRasterPt) { drawing.line( lastRasterPt[0], lastRasterPt[1], pt[0], pt[1], pv, penSizeForRaster(), true, ) } else { drawing.point(pt[0], pt[1], pv, penSizeForRaster(), true) } // Filled pen also records the outline to fill on release. if (drawStroke.tool === 'filled') drawStroke.points.push(pt) lastRasterPt = pt overlayDirty = true requestRender() } return }}
// On window (not the canvas) so a release outside the canvas still ends a// stroke, which takes no pointer capture.window.addEventListener('pointerup', (event) => { if (drawStroke?.pointerId === event.pointerId) { // Filled pen: close the recorded outline and fill its interior on release. if (drawStroke.tool === 'filled' && drawStroke.points.length >= 2) { drawing.fillPen(drawStroke.points, penValue, true) overlayDirty = true requestRender() } // Vector: commit the freehand polygon to the vector layer. if (drawStroke.tool === 'vector' && vecPoints) { vector?.addPolygon(vecPoints, penColorCss()) vecPoints = null requestRender() } drawStroke = null lastRasterPt = null }})
window.addEventListener('pointercancel', (event) => { if (drawStroke?.pointerId === event.pointerId) { drawStroke = null lastRasterPt = null }})
els.drawBtn.addEventListener('click', () => { drawMode = !drawMode if (view?.controller) view.controller.primaryDragMode = drawMode ? DRAG_MODE.none : DRAG_MODE.pan els.drawBtn.textContent = `Draw: ${drawMode ? 'on' : 'off'}` els.canvas.style.cursor = drawMode ? 'crosshair' : 'grab'})els.drawTool.addEventListener('change', () => { drawTool = els.drawTool.value})els.wandTol.addEventListener('change', () => { wandTolerance = Number(els.wandTol.value) || 40})els.penValue.addEventListener('change', () => { penValue = Number(els.penValue.value) || 1})els.drawUndo.addEventListener('click', () => { // Vector tool undoes the last polygon; other tools undo the raster. if (drawTool === 'vector') { if (vector?.removeLast()) requestRender() return } if (drawing?.undo()) { overlayDirty = true requestRender() }})els.drawClear.addEventListener('click', () => { drawing?.clear() vector?.clear() overlayDirty = true requestRender()})els.exportSvg.addEventListener('click', () => { if (!slide || !vector || vector.shapes.length === 0) return const svg = vector.toSVG(slide.manifest.width, slide.manifest.height) const blob = new Blob([svg], { type: 'image/svg+xml' }) const url = URL.createObjectURL(blob) const a = document.createElement('a') a.href = url a.download = `${slide.manifest.name || 'slide'}-annotations.svg` a.click() URL.revokeObjectURL(url)})
els.levelMode.addEventListener('change', () => { slide?.setLevelChoice( els.levelMode.value === 'auto' ? 'auto' : Number(els.levelMode.value), )})els.showGrid.addEventListener('change', () => { slide?.setTileGridVisible(els.showGrid.checked)})els.scaleBar.addEventListener('change', () => { if (view) view.overlayStyle = { isRulerVisible: els.scaleBar.checked }})els.fit.addEventListener('click', () => view?.fit())els.zoomIn.addEventListener('click', () => zoomCentered(1.45))els.zoomOut.addEventListener('click', () => zoomCentered(1 / 1.45))els.source.addEventListener('change', () => { void loadSlide()})els.retina.addEventListener('change', () => { if (view) view.hidpi = els.retina.checked})
// Bumped on each load so a slower source (e.g. DZI) that started earlier cannot// resolve after a newer selection and desync the visible slide from the dropdown.let loadSeq = 0
// Turn a load failure into an actionable hint. Most demo sources need a// gitignored fixture fetched by a script, or an external host to be reachable;// without this a missing manifest or an offline DZI shows only a raw fetch error.function hintForSource(src) { if (src === 'dzi-highsmith') { return ` Fetched from ${new URL(DZI_URLS[src]).host}; check your network/CORS (this external DZI must be online).` } if (src === 'synthetic') return '' if (!isLocalStreaming) { return ` Fetched from ${new URL(STREAMING_ASSET_BASE, location.href).host}; check your network.` } if (src.startsWith('openslide-')) { return ` Did you run scripts/fetch-openslide-dicom.ts --slide=${src.replace('openslide-', '')}?` } if (src === 'dicom-wsi') return ' Did you run scripts/fetch-dicom-wsi.ts?' return ''}
// Monochrome slides get a calMin,calMax window, shared by every tile and level.function showWindowControl(s) { const level = s.manifest.levels[0] const mono = level?.photometric?.startsWith('MONOCHROME') ?? false els.windowControl.hidden = !mono if (mono) els.window.value = els.window.defaultValue = `0,${2 ** (level.bitsStored ?? 8) - 1}`}
els.window.addEventListener('change', () => { if (!slide) return const [lo, hi] = els.window.value.split(',').map(Number) if (Number.isFinite(lo) && Number.isFinite(hi) && lo < hi) { slide.setChannel(0, { calMin: lo, calMax: hi }) return } // Restore the window in force. const { calMin, calMax } = slide.channels[0] els.window.value = Number.isFinite(calMin) && Number.isFinite(calMax) ? `${calMin},${calMax}` : els.window.defaultValue})
async function loadSlide() { // A source picked before main() has a view: main() loads the current pick. if (!view) return const seq = ++loadSeq els.fallback.setAttribute('aria-hidden', 'true') els.fallback.textContent = '' // Release the outgoing slide before loading the next: the view disposes it // (its cache and in-flight fetches) and frees the renderer's tile textures. slide = null view.setSlide(null) try { const opts = { maxCacheBytes: MAX_CACHE_BYTES, targetScreenPixelsPerTilePixel: TARGET_SCREEN_PIXELS_PER_TILE_PIXEL, showTileGrid: els.showGrid.checked, } const dziUrl = DZI_URLS[els.source.value] let next if (dziUrl) { next = NVSlide.fromSource(await DziSource.fromUrl(dziUrl), opts) } else { next = await slideFromManifest(opts) } if (seq !== loadSeq) { // A newer selection superseded this load; discard it. next.dispose?.() return } slide = next view.setSlide(next) // fits it on the first sized frame ensureDrawing() populateLevels(slide.manifest) showWindowControl(slide) } catch (err) { if (seq !== loadSeq) return console.error(err) showFallback( (err instanceof Error ? err.message : String(err)) + hintForSource(els.source.value), ) }}
async function main() { makeDraggable(els.hud) view = await createSlideView(els.canvas, { backend: backendFromUrl(), antialias: isAntiAlias, hidpi: els.retina.checked, overlayStyle: { isRulerVisible: els.scaleBar.checked }, // Left drag pans (painted instead in draw mode), middle drag pans, right // drag zooms to a box, the wheel zooms about the pointer. controller: { primaryDragMode: drawMode ? DRAG_MODE.none : DRAG_MODE.pan, secondaryDragMode: DRAG_MODE.roiSelection, onHover: (point) => { hoverCss = point && slide?.slideToScreen(point.x, point.y, screenForCanvas()) requestRender() }, }, onRender: afterRender, }) els.canvas = view.canvas // After the view's controller: in draw mode it leaves the left button free. els.canvas.addEventListener('pointerdown', onDrawDown) els.canvas.addEventListener('pointermove', onDrawMove) els.backendSelect.value = view.backend const applySlideFilter = () => { view.renderer.isNearestInterpolation = els.slideFilter.value === 'nearest' requestRender() } applySlideFilter() els.slideFilter.addEventListener('change', applySlideFilter) await loadSlide()}
main().catch((err) => { console.error(err) showFallback(err instanceof Error ? err.message : String(err))})