import NiiVue from '@niivue/niivue'
const cardCheck = document.getElementById('cardCheck')
const colorBtn = document.getElementById('colorBtn')
const fmriCheck = document.getElementById('fmriCheck')
const frameLabel = document.getElementById('frameLabel')
const gl1 = document.getElementById('gl1')
const headerBtn = document.getElementById('headerBtn')
const lineOpacity = document.getElementById('lineOpacity')
const lineSize = document.getElementById('lineSize')
const nextFrame = document.getElementById('nextFrame')
const prevFrame = document.getElementById('prevFrame')
const respCheck = document.getElementById('respCheck')
const sliceType = document.getElementById('sliceType')
const webgpuCheck = document.getElementById('webgpuCheck')
// A task-fMRI run (TR 2 s) with its BIDS physio recordings. The cardiac (200 Hz)
// and respiratory (50 Hz) traces are loaded as signals attached to the BOLD
// volume (attachToId), so the graph shows the crosshair BOLD time-course (one
// point per TR) alongside both physio traces on a shared Time (s) axis - each at
// its native sampling rate, no resampling. Physio logged before/after the scan
// is clamped out (window = imaging period). A marker tracks the current frame;
// move the crosshair to update the BOLD trace, or click the graph to scrub
// frames. (See physio.html for the standalone cardiac/respiratory viewer.)
const BOLD = '/signals/bold.nii.gz'
backgroundColor: [0.1, 0.1, 0.1, 1],
await nv1.attachToCanvas(gl1)
nv1.sliceType = 0 // axial (single view; A+C+S+R also available in the menu)
await nv1.loadVolumes([{ url: BOLD }])
const volId = nv1.volumes[0].id
// This sample was scaled to uint8 over the 2-98% range, so some voxels (incl.
// the origin) saturate and read flat across the run. Open on an unclamped deep
// white-matter voxel ~20 mm left of the origin so the BOLD trace actually moves.
nv1.setCrosshairPos([-20, 0, 10])
url: `${STEM}respiratory.tsv.gz`,
display: { selectedColumns: [0] }, // column 0 is the recording (not trigger)
url: `${STEM}cardiac.tsv.gz`,
display: { selectedColumns: [0] },
// The whole run squeezes ~50k cardiac samples into the plot width; open on the
// first 15 s. The window follows the frame marker, and the graph's - button
nv1.setGraphRange([0, 15])
// Signals load in order: [respiratory, cardiac]. Toggle a physio trace by
// showing column 0 or no column (selectedColumns []); toggle the fMRI/BOLD
// trace via the association graph's volume-timecourse flag; hide the whole
// graph when nothing is selected.
function showPhysio(idx, on) {
nv1.setSignal(idx, { display: { selectedColumns: on ? [0] : [] } })
nv1.graphShowVolumeTimecourse = fmriCheck.checked
showPhysio(RESP_IDX, respCheck.checked)
showPhysio(CARD_IDX, cardCheck.checked)
// No plot when every trace is unchecked.
fmriCheck.checked || respCheck.checked || cardCheck.checked
fmriCheck.onchange = syncTraces
respCheck.onchange = syncTraces
cardCheck.onchange = syncTraces
// Line style: relative width multiplier + opacity. Translucent thin lines let
// the dense, overlapping physio traces be read where they intersect.
function applyLineStyle() {
nv1.graphLineWidth = parseFloat(lineSize.value)
nv1.graphLineAlpha = parseFloat(lineOpacity.value)
lineSize.onchange = applyLineStyle
lineOpacity.onchange = applyLineStyle
applyLineStyle() // apply the initial dropdown selections
function updateFrameLabel() {
frameLabel.textContent = `frame ${nv1.getFrame4D(volId)}`
async function stepFrame(delta) {
await nv1.setFrame4D(volId, nv1.getFrame4D(volId) + delta)
console.error('setFrame4D failed', e)
prevFrame.onclick = () => stepFrame(-1)
nextFrame.onclick = () => stepFrame(1)
sliceType.onchange = () => {
nv1.sliceType = parseInt(sliceType.value, 10)
headerBtn.onclick = () => {
/** @type {any} hdr: nifti-reader-js types omit toFormattedString */
const hdr = nv1.volumes[0]?.hdr
typeof hdr.toFormattedString === 'function'
? hdr.toFormattedString()
: JSON.stringify(hdr, null, 2),
colorBtn.addEventListener('input', (event) => {
const hex = event.target.value
parseInt(hex.slice(1, 3), 16) / 255,
parseInt(hex.slice(3, 5), 16) / 255,
parseInt(hex.slice(5, 7), 16) / 255,
webgpuCheck.onclick = function () {
const want = this.checked
// reinitializeView resolves false when the requested backend is unavailable
// (e.g. a webgl2-only build); revert the checkbox so it matches the renderer.
nv1.reinitializeView({ backend: want ? 'webgpu' : 'webgl2' }),
if (ok === false) webgpuCheck.checked = !want
nv1.addEventListener('signalLocationChange', (e) => {
document.getElementById('location').textContent = ` ${e.detail.string}`