# JSON Canvas Viewer JSON Canvas Viewer is an extensible web-based viewer for JSON Canvas files (`.canvas`) from Obsidian. It provides interactive pan and zoom functionality, supports 100% of the JSON Canvas 1.0 specification, offers responsive design with mobile and touchpad adaptation, and features modern aesthetics with light and dark mode support. The library is TypeScript-native and supports lazy loading for performance optimization. The library is available as a monorepo with framework adapters for vanilla JavaScript/TypeScript, React, Preact, Vue 3, and a Vite plugin for build-time canvas parsing. It features a modular architecture allowing optional features like minimap navigation, zoom controls, mistouch prevention, and debug panels. The "chimp" version provides a bundled solution for quick trials via CDN, while the "full" version offers complete customizability and tree-shaking support for production use. ## JSONCanvasViewer Class The main viewer class that renders JSON Canvas content with interactive pan/zoom capabilities. ```typescript import { JSONCanvasViewer, Minimap, Controls } from 'json-canvas-viewer'; import canvasData from './demo.canvas'; // Basic instantiation const viewer = new JSONCanvasViewer({ container: document.getElementById('canvas-container'), canvas: canvasData, theme: 'dark', loading: 'lazy', attachmentDir: './attachments/', markdownParser: (md) => md, colors: { dark: { background: '#1a1a1a', text: '#ffffff', } } }, [Minimap, Controls]); // Optional modules // Control the viewer programmatically viewer.zoom(1.5); // Zoom by factor viewer.zoomToScale(2.0); // Zoom to exact scale viewer.pan({ x: 100, y: 50 }); // Pan by delta viewer.panToCoords({ x: 0, y: 0 }); // Pan to coordinates viewer.resetView(); // Fit all content in view viewer.changeTheme('light'); // Switch theme viewer.toggleFullscreen('enter'); // Enter fullscreen // Subscribe to lifecycle hooks viewer.onNodeActive.subscribe((node) => { console.log('Node selected:', node.id); }); viewer.onRefresh.subscribe(() => { console.log('Canvas rerendered'); }); // Load a different canvas viewer.load({ canvas: newCanvasData, attachmentDir: './new-attachments/' }); // Cleanup when done viewer.dispose(); ``` ## fetchCanvas Function Async function to fetch and parse canvas files at runtime (primarily for chimp version usage). ```typescript import { JSONCanvasViewer, fetchCanvas, parser } from 'json-canvas-viewer'; // Fetch canvas from URL const canvas = await fetchCanvas('./path/to/demo.canvas'); new JSONCanvasViewer({ container: document.body, canvas: canvas, markdownParser: parser, // Built-in sanitized markdown parser }); ``` ## Vite Plugin Configuration Build-time canvas parsing plugin that enables direct `.canvas` file imports. ```typescript // vite.config.ts import { defineConfig } from 'vite'; import canvas from 'vite-plugin-json-canvas'; import { marked } from 'marked'; export default defineConfig({ plugins: [ canvas({ // Optional: custom markdown parser parser: (md) => marked(md), }), // Or disable markdown parsing canvas({ parser: (md) => md, }), ], }); // In your application import canvasData from './diagram.canvas'; // TypeScript-aware import new JSONCanvasViewer({ container: document.body, canvas: canvasData, // Already parsed at build time }); ``` ## React Component React wrapper component with reactive props and custom node rendering support. ```tsx import { JSONCanvasViewerComponent, Minimap, Controls } from '@json-canvas-viewer/react'; import { useRef, useEffect } from 'react'; import canvas from './demo.canvas'; function CanvasPage() { const viewerRef = useRef(null); useEffect(() => { // Access viewer instance via ref if (viewerRef.current?.viewer) { viewerRef.current.viewer.resetView(); } }, []); return ( (
{content}
)} link={({ content, node }) => (