@jiujue/react-canvas-fiber 2.0.2 → 2.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/AI_GUIDE.md +142 -0
  2. package/package.json +3 -2
package/AI_GUIDE.md ADDED
@@ -0,0 +1,142 @@
1
+ # AI Guide for react-canvas-fiber
2
+
3
+ This document provides a comprehensive guide for AI agents to understand, use, and extend the `react-canvas-fiber` project. It covers the architecture, core concepts, usage patterns, and development guidelines.
4
+
5
+ ## 1. Project Overview
6
+
7
+ `react-canvas-fiber` is a custom React Renderer that renders React components to an HTML5 Canvas. It leverages `react-reconciler` for state management and updates, and integrates **Yoga Layout** (via `yoga-layout-prebuilt`) for a web-like Flexbox layout system.
8
+
9
+ ### Key Characteristics
10
+
11
+ - **Renderer**: Custom Host Config implementation mapping React Virtual DOM to Canvas Scene Graph.
12
+ - **Layout**: Full Flexbox support powered by Yoga.
13
+ - **Rendering**: Canvas 2D API based rendering pipeline.
14
+ - **Events**: Synthetic event system simulating DOM events (bubbling, capturing) within the Canvas.
15
+ - **Runtime**: Supports high-performance updates using `requestAnimationFrame` loop (batched updates).
16
+
17
+ ## 2. Directory Structure
18
+
19
+ - `src/components/`: Public React components (e.g., `<Canvas />`).
20
+ - `src/jsx/`: JSX type definitions.
21
+ - `src/layout/`: Layout calculation logic bridging internal nodes and Yoga.
22
+ - `src/render/`: Painting logic (draw calls).
23
+ - `src/runtime/`: Core runtime logic.
24
+ - `reconciler.ts`: `react-reconciler` HostConfig implementation.
25
+ - `nodes.ts`: Scene graph node definitions (`ViewNode`, `RectNode`, `TextNode`).
26
+ - `root.ts`: Root container managing the render loop and event dispatching.
27
+ - `src/types/`: TypeScript type definitions.
28
+ - `src/utils/`: Utility functions.
29
+
30
+ ## 3. Usage & Supported Elements
31
+
32
+ ### Entry Point
33
+
34
+ The entry point is the `<Canvas />` component.
35
+
36
+ ```tsx
37
+ import { Canvas } from 'react-canvas-fiber'
38
+
39
+ function App() {
40
+ return (
41
+ <Canvas width={800} height={600} style={{ border: '1px solid black' }}>
42
+ <View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
43
+ <Text text="Hello World" style={{ fontSize: 20 }} color="black" />
44
+ </View>
45
+ </Canvas>
46
+ )
47
+ }
48
+ ```
49
+
50
+ ### Intrinsic Elements
51
+
52
+ #### `<View />`
53
+
54
+ Container element for layout and grouping.
55
+
56
+ - **Props**: `ViewProps`
57
+ - **Key Attributes**:
58
+ - `style`: `YogaStyle` (flex, padding, margin, position, etc.)
59
+ - `background`: Background color string.
60
+ - `borderRadius`: Number.
61
+ - `scrollX`, `scrollY`: Booleans to enable scrolling.
62
+ - `onScroll`, `onScrollX`: Scroll callbacks.
63
+ - `pointerEvents`: `'auto' | 'none'`.
64
+ - Event handlers: `onClick`, `onPointerDown`, etc.
65
+
66
+ #### `<Text />`
67
+
68
+ Text rendering element.
69
+
70
+ - **Props**: `TextProps`
71
+ - **Key Attributes**:
72
+ - `text`: **Mandatory** string content (do not use children).
73
+ - `style`: `YogaStyle` (supports `fontSize`, `fontFamily`, `fontWeight`, `lineHeight`).
74
+ - `color`: Text color string.
75
+ - `maxWidth`: Max width for wrapping.
76
+
77
+ #### `<Rect />`
78
+
79
+ Basic rectangle shape.
80
+
81
+ - **Props**: `RectProps`
82
+ - **Key Attributes**:
83
+ - `style`: `YogaStyle`.
84
+ - `fill`: Fill color.
85
+ - `stroke`: Stroke color.
86
+ - `lineWidth`: Stroke width.
87
+ - `borderRadius`: Corner radius.
88
+
89
+ ### Style Properties (`YogaStyle`)
90
+
91
+ Supports standard Flexbox properties:
92
+
93
+ - Layout: `width`, `height`, `flexDirection`, `justifyContent`, `alignItems`, `flexWrap`, `flexGrow`, `padding`, `margin`.
94
+ - Positioning: `position` ('relative' | 'absolute'), `top`, `left`, `right`, `bottom`.
95
+
96
+ ## 4. Internal Architecture
97
+
98
+ ### The Render Loop
99
+
100
+ 1. **React Updates**: React commits changes via `reconciler.ts`.
101
+ 2. **Invalidation**: `commitUpdate` triggers `container.invalidate()`.
102
+ 3. **rAF Loop**: `requestAnimationFrame` calls `renderFrame` in `root.ts`.
103
+ 4. **Layout Pass**:
104
+ - Syncs Scene Graph props to Yoga Nodes.
105
+ - Calculates layout via `yogaNode.calculateLayout()`.
106
+ - Writes computed layout (x, y, w, h) back to Scene Graph nodes.
107
+ 5. **Draw Pass**:
108
+ - Clears canvas.
109
+ - Recursively calls `drawNode` (in `src/render/drawTree.ts`).
110
+
111
+ ### Event System
112
+
113
+ - **Hit Testing**: `hitTest` function in `root.ts` traverses the Scene Graph (accounting for scroll offsets) to find the target node.
114
+ - **Dispatch**: Simulates `capture` and `bubble` phases.
115
+ - **Pointer Capture**: Supports implicit and explicit pointer capture for drag operations (e.g., scrollbars).
116
+
117
+ ## 5. Development & Extension
118
+
119
+ ### Adding a New Node Type
120
+
121
+ 1. **Define Node**: Add a new type in `src/types/nodes.ts` (e.g., `ImageNode`).
122
+ 2. **Define Props**: Add props interface in `src/types/jsx.ts`.
123
+ 3. **Register Intrinsic**: Add to `JSX.IntrinsicElements` in `src/intrinsics.d.ts`.
124
+ 4. **Implement Creation**: Update `createInstance` in `src/runtime/reconciler.ts` to instantiate the new node.
125
+ 5. **Implement Drawing**: Update `drawNode` in `src/render/drawTree.ts` to handle the new node type.
126
+
127
+ ### Debugging
128
+
129
+ - Nodes have a `debugId`.
130
+ - Layout issues usually stem from Yoga configuration or `syncYogaTree` logic in `src/layout/layoutTree.ts`.
131
+ - Rendering issues are handled in `src/render/drawTree.ts`.
132
+
133
+ ### Build & Watch
134
+
135
+ - **Build**: `npm run build` (uses tsup)
136
+ - **Watch**: `npm run dev`
137
+
138
+ ## 6. Common Pitfalls for AI
139
+
140
+ - **Text Children**: `<Text>Content</Text>` is **NOT** supported. Must use `<Text text="Content" />`.
141
+ - **Z-Index**: Currently determined by tree order (painting order). No explicit `zIndex` support in styles yet.
142
+ - **Scrolling**: Requires `scrollX` or `scrollY` prop on `<View>` AND fixed dimensions (or flex constraints) to work.
package/package.json CHANGED
@@ -1,13 +1,14 @@
1
1
  {
2
2
  "name": "@jiujue/react-canvas-fiber",
3
- "version": "2.0.2",
3
+ "version": "2.0.3",
4
4
  "private": false,
5
5
  "type": "module",
6
6
  "license": "MIT",
7
7
  "description": "A React custom renderer for Canvas2D with Yoga layout",
8
8
  "sideEffects": false,
9
9
  "files": [
10
- "dist"
10
+ "dist",
11
+ "AI_GUIDE.md"
11
12
  ],
12
13
  "repository": {
13
14
  "type": "git",