b5 is a React-based visual programming environment that uses a node-based interface for creative coding. This guide explains the project’s architecture and how different components work together.Documentation Index
Fetch the complete documentation index at: https://mintlify.com/peilingjiang/b5/llms.txt
Use this file to discover all available pages before exploring further.
High-Level Overview
The application follows a component-based architecture with clear separation between the editor interface and the viewer/renderer:Project Structure
The source code is organized in/src with the following structure:
Core Components
App.js
The root component manages state and data flow between Editor and Viewer:src/App.js:1
Editor Component
The main programming environment where users create visual programs. Key responsibilities:- Managing editor state (blocks, wires, sections)
- Handling user interactions (drag, click, search)
- Coordinating Playground and Factory sections
- Session storage for persistence
src/components/editor/editor.js:1
The editor state structure:
Playground vs Factory
Viewer Component
Renders the live preview of the visual program:- Receives data from Editor via bridge
- Displays canvas output
- Controls for pause/play, refresh, capture
- Minimizable to corner
src/components/viewer/viewer.js:1
Block Rendering System
The block rendering system handles the visual representation of code blocks:- blockRenderer/ - Main block rendering logic
- blockRendererLite.js - Lightweight block renderer
- node.js - Input/output node rendering
- wireRenderer.js - Connection wire rendering
- specialBlocks/ - Special block types:
- Color picker
- Input fields
- Sliders
- Comments
- Inline blocks
src/components/blockRenderer/
Block Search
Fuzzy search system for discovering and adding blocks:- Uses Fuse.js for fuzzy searching
- Searches by name, type, or description
- Activated by double-clicking empty block spaces
src/components/blockSearch/
b5.js Submodule
The b5.js submodule (src/b5.js/) is a separate library that:
- Interprets and executes block-based programs
- Converts blocks/wires to executable code
- Will eventually run standalone to execute b5 JSON files on other websites
Data Flow
- User Input: User interacts with Editor (drag blocks, create wires)
- State Update: Editor updates internal state
- Bridge: Editor sends data to Viewer via bridge function
- Rendering: Viewer receives data and updates canvas
- Persistence: Editor state saved to session storage
Execution Model
Sequential Execution
Unlike most node-based visual programming languages (e.g., Grasshopper, Max), b5 executes blocks sequentially based on grid position.
- Factory sections run once (like p5.js
setup()) - Playground runs 60 times per second (like p5.js
draw()) - Within Playground: left to right, top to bottom (line by line)
Effect Blocks
Special blocks that affect subsequent blocks through context rather than wire connections:- Similar to p5.js functions like
fill(),stroke(),scale() - Affect blocks based on positional relationships
- Visual feedback shows effective range when selected
Key Technologies
- React 18 - UI framework
- React Hooks - State management (
useHooks.js) - Fuse.js - Fuzzy search for blocks
- React Color - Color picker component
- file-saver - Export/save functionality
- uuid - Unique identifiers for blocks
- PostCSS - CSS processing with plugins
- Bun - JavaScript runtime and package manager
File Persistence
Current implementation:- Session Storage: Automatic save of editor state
- JSON Export: Save projects as JSON files (
Cmd/Ctrl + S) - JSON Import: Drag JSON files into editor to load
Testing
Test files located insrc/__test__/:
- Component tests using React Testing Library
- Jest as test runner
- Run with:
bun run test
src/__test__/components/editor/editor.test.js
Build Process
Development
Production
- Gulp tasks for CSS optimization
- React production build
- Output to
build/directory
Constants and Configuration
Shared constants defined insrc/components/constants.js:
lineNumberWidth- Grid line number widthblockAlphabetHeight- Block heightgap- Spacing between blocks
src/components/editor/defaultValue.js
Next Steps
- Review the Contributing Guide to start contributing
- Check out the Development Setup for environment setup
- Read the example files in
src/examples/to understand b5 project structure