Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

2.1 KB
Raw Blame
import { useEffect, useRef } from 'react';

// DevicePixelRatio-aware canvas sizing shared by the piano-roll renderers
// (<PianoRoll>, <MidiPianoRoll>). Sizes the canvas bitmap to the container
// width × the given CSS height at the device pixel ratio, keeps the CSS size
// in plain px, applies the DPR transform, and redraws — on mount and on every
// container resize (ResizeObserver).
//
// `drawRef` is a ref to the latest draw() (not the function itself) so the
// observer is created once per height and never torn down just because the
// draw closure was recreated.
//
// `height` is a fixed CSS px value; pass `undefined` to follow the container's
// own height instead (the canvas then keeps its CSS size from its classes and
// only the bitmap is resized — the Core Assembly avatar fills an aspect box).
//
// Returns a ref holding the current CSS width — the draw code reads it
// instead of measuring the DOM per frame.
export default function useCanvasDprSize(wrapRef, canvasRef, height, drawRef) {
  const widthRef = useRef(0);

  useEffect(() => {
    const el = wrapRef.current;
    if (!el) return undefined;
    const resize = () => {
      const w = Math.floor(el.clientWidth);
      const h = height ?? Math.floor(el.clientHeight);
      if (!w || !h) return;
      widthRef.current = w;
      const canvas = canvasRef.current;
      if (!canvas) return;
      const dpr = (typeof window !== 'undefined' && window.devicePixelRatio) || 1;
      canvas.width = Math.round(w * dpr);
      canvas.height = Math.round(h * dpr);
      if (height !== undefined) {
        canvas.style.width = `${w}px`;
        canvas.style.height = `${height}px`;
      }
      const ctx = canvas.getContext('2d');
      if (ctx) ctx.setTransform(dpr, 0, 0, dpr, 0, 0);
      // A caller that installs its draw in a LATER effect (the Core Assembly
      // avatar's rAF loop) has nothing to paint yet on the mount resize; its
      // own loop paints the first frame a tick later.
      drawRef.current?.();
    };
    resize();
    const ro = new ResizeObserver(resize);
    ro.observe(el);
    return () => ro.disconnect();
  }, [height, wrapRef, canvasRef, drawRef]);

  return widthRef;
}