import NiiVue from '@niivue/niivue'
const downBtn = document.getElementById('downBtn')
const gl1 = document.getElementById('gl1')
const sliceType = document.getElementById('sliceType')
const toBottomBtn = document.getElementById('toBottomBtn')
const toTopBtn = document.getElementById('toTopBtn')
const upBtn = document.getElementById('upBtn')
const volumeSelect = document.getElementById('volumeSelect')
// --- Populate the volume dropdown from the current volume list ---
function refreshVolumeList() {
const prev = volumeSelect.value
volumeSelect.innerHTML = ''
for (let i = 0; i < nv1.volumes.length; i++) {
const vol = nv1.volumes[i]
const label = vol.name || vol.url || `Volume ${i}`
const opt = document.createElement('option')
opt.textContent = `[${i}] ${label}`
volumeSelect.appendChild(opt)
// Try to keep the same index selected
if (prev !== '' && parseInt(prev, 10) < nv1.volumes.length) {
volumeSelect.value = prev
// --- Reorder buttons ---
upBtn.onclick = async () => {
const idx = parseInt(volumeSelect.value, 10)
await nv1.moveVolumeUp(idx)
downBtn.onclick = async () => {
const idx = parseInt(volumeSelect.value, 10)
await nv1.moveVolumeDown(idx)
toTopBtn.onclick = async () => {
const idx = parseInt(volumeSelect.value, 10)
await nv1.moveVolumeToTop(idx)
toBottomBtn.onclick = async () => {
const idx = parseInt(volumeSelect.value, 10)
await nv1.moveVolumeToBottom(idx)
sliceType.onchange = () => {
nv1.sliceType = parseInt(sliceType.value, 10)
function handleLocationChange(data) {
document.getElementById('location').innerHTML = ` ${data.string}`
backgroundColor: [0.1, 0.1, 0.1, 1],
nv1.addEventListener('locationChange', (e) => handleLocationChange(e.detail))
nv1.addEventListener('volumeOrderChanged', () => refreshVolumeList())
await nv1.attachToCanvas(gl1)
url: '/volumes/mni152.nii.gz',
isColorbarVisible: false,
url: '/volumes/spmMotor.nii.gz',
colormapNegative: 'winter',
// Apply the same initial thresholds as vox.stats.html
// (slider defaults: minNeg=30, rangeNeg=30, min=30, range=30)
calMaxNeg: 30 * -0.1 + 30 * -0.1,
calMax: 30 * 0.1 + 30 * 0.1,