Plots

Fisheye heatmap with an animated grid

Drag a handle over a heatmap: the row and column under it grow, the effect fades over the neighbours, and the grid glides to each new focus.

import {
    VisQuill, Reactive, Boxes, Segments, Points, Grids, Animate,
    type Grid, type GridCell,
} from "@visquill/visquill-gdk"
import './styles.css'

// ─── Layout ───────────────────────────────────────────────────────────────
const columns     = 24
const rows        = 14
const chartWidth  = 600
const chartHeight = 360
const focusFactor = 4
const falloff     = 3
const followAlpha = 0.15
const startPoint  = [480, 300]

// ─── Data ─────────────────────────────────────────────────────────────────
// A smooth field of values between 0 and 1, one per cell.
const peaks = [
    { x: 0.25, y: 0.30, width: 0.12, height: 1.0 },
    { x: 0.70, y: 0.65, width: 0.18, height: 0.8 },
    { x: 0.85, y: 0.20, width: 0.06, height: 0.9 },
]
function valueAt(u: number, v: number) {
    const sum = peaks.reduce((s, p) =>
        s + p.height * Math.exp(-((u - p.x) ** 2 + (v - p.y) ** 2) / (2 * p.width ** 2)), 0)
    return Math.min(1, sum)
}

export function run(div: HTMLDivElement) {
    const rvg    = VisQuill.create(div, "demos-heatmap-fisheye-")
    const canvas = rvg.canvas

    // ─── Instruction label ────────────────────────────────────────────
    const instructions = canvas.text.label("Drag the handle over the heatmap to enlarge the cells around it", "instructions")
    Reactive.do([rvg.frame], () => {
        Points.moveDown(Segments.midPoint(Boxes.topSide(rvg.frame)), 25, instructions)
    })

    // ─── Cells, shaded once by their value ────────────────────────────
    const cells = Array.from({ length: columns * rows }, () => canvas.visuals.box("cell"))
    const base: Grid = { x: [], y: [] }
    const chart = canvas.visuals.box("chart")
    Reactive.do([rvg.frame], () => {
        Boxes.centeredBox(Boxes.center(rvg.frame), chartWidth, chartHeight, chart)
        Grids.divided(chart, columns, rows, base)
    })

    cells.forEach((cell, i) => {
        const { row, column } = Grids.cellAt(base, i)
        const value = valueAt((column + 0.5) / columns, (row + 0.5) / rows)
        cell.applyStyleDirective(`@style fill-opacity: ${value.toFixed(3)}`)
    })

    // ─── Animated grid ────────────────────────────────────────────────
    // `shown` is drawn. On every new `goal`, the shown grid is copied into `from`,
    // t starts again at 0 and follows to 1; lerp blends from → goal by t.
    const from:  Grid = { x: [], y: [] }
    const goal:  Grid = { x: [], y: [] }
    const shown: Grid = { x: [], y: [] }
    const t = canvas.values.real(1)
    const goalCell: GridCell = { row: -2, column: -2 }   // the focused cell of `goal`

    // A new frame: start from the even grid and let the handle focus again.
    Reactive.do([chart], () => {
        shown.x = [...base.x]
        shown.y = [...base.y]
        goalCell.row = goalCell.column = -2
    })

    // ─── Focus follows the handle ─────────────────────────────────────
    const handle  = canvas.handles.disk("handle", startPoint)
    const focused = canvas.visuals.box("focused")
    const label   = canvas.text.label("", "value")

    Reactive.do([handle, chart], () => {
        // focusAt: the cell that contains the handle once the grid has settled.
        const cell: GridCell = { row: -1, column: -1 }
        if (Grids.containsPoint(base, handle)) {
            Grids.focusAt(base, handle, focusFactor, falloff, goal)
            Grids.cellOf(goal, handle, cell)
        }
        if (cell.row === goalCell.row && cell.column === goalCell.column) return
        goalCell.row    = cell.row
        goalCell.column = cell.column
        if (cell.row < 0) Grids.focus(base, cell, 1, 0, goal)

        from.x = [...shown.x]
        from.y = [...shown.y]
        Animate.real.snap(t, 0)
        Animate.real.follow(t, 1, { alpha: followAlpha })
    })

    // ─── Cells follow the animated grid ───────────────────────────────
    Reactive.do([t], () => {
        Grids.lerp(from, goal, t, shown)
        Grids.cells(shown, cells)
    })

    // ─── Outline: the cell under the handle, as shown right now ───────
    // While the grid glides this can be a neighbour of the focused cell;
    // once it has settled, it is the focused cell.
    Reactive.do([t, handle], () => {
        const under  = Grids.cellOf(shown, handle)
        const inside = Grids.containsCell(shown, under)
        focused.visible.value = inside
        label.visible.value   = inside
        if (!inside) return
        Grids.cell(shown, under, focused)
        Boxes.center(focused, label)
        label.value = valueAt((under.column + 0.5) / columns, (under.row + 0.5) / rows).toFixed(2)
    })
}

A heatmap whose cells grow around a draggable handle, fading out over three neighbours. Grids.focusAt computes the focused grid, and Grids.lerp with Animate.real.follow blends the grid on screen towards it.

How it works

Grids.focusAt enlarges the column and row that contain the handle once the grid has settled, fading out over falloff neighbours.

On every new focus, the grid on screen (shown) is copied into from, and t follows from 0 to 1. Grids.lerp blends from into goal:

from.x = [...shown.x]
from.y = [...shown.y]
Animate.real.snap(t, 0)
Animate.real.follow(t, 1, { alpha: followAlpha })
Reactive.do([t], () => {
    Grids.lerp(from, goal, t, shown)
    Grids.cells(shown, cells)
    // …
})

A focus change halfway starts the next animation from where the grid is.

The outline and the value show the cell under the handle in the grid as drawn right now, so the handle stays inside the outline while the grid glides:

const under  = Grids.cellOf(shown, handle)
const inside = Grids.containsCell(shown, under)

API

  • Grids.focusAt: enlarge the column and row under a point, with falloff to the neighbours
  • Grids.lerp: blend two grids line by line
  • Animate.real.follow: move a value towards a target every frame
  • Grids.cells: all cells of a grid as boxes
  • Grids.cellOf, Grids.containsCell: the cell under a point, and whether there is one

See also

  • plots/heatmap-01-binning
  • animations/04-waveform

API used

Also used (22)