@cs-open/react-fabric 1.0.0 → 1.0.1

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/README.md +810 -76
  2. package/package.json +4 -4
package/README.md CHANGED
@@ -1,10 +1,57 @@
1
+ <div align="right">
2
+
3
+ [<kbd>中文</kbd>](#chinese) | [<kbd>English</kbd>](#english)
4
+
5
+ </div>
6
+
1
7
  # @cs-open/react-fabric
2
8
 
3
- React 组件库,基于 Fabric.js 构建,提供强大的 Canvas 绘图功能。
9
+ [![npm version](https://badge.fury.io/js/@cs-open%2Freact-fabric.svg)](https://badge.fury.io/js/@cs-open%2Freact-fabric)
10
+ [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
11
+
12
+
13
+ ---
14
+
15
+ <div id="chinese">
16
+
17
+ 一个基于 Fabric.js 构建的现代化 React Canvas 绘图组件库,提供强大的 2D 图形绘制、交互和动画功能。
18
+
19
+ ![React Fabric](https://github.com/user-attachments/assets/02ff8152-bad3-4e99-82db-13eefc5413b0)
20
+
21
+ ## ✨ 核心特性
22
+
23
+ ### 🎯 丰富的图形组件
24
+
25
+ - **基础图形**: 矩形、圆形、椭圆、线条、多边形、路径
26
+ - **文本组件**: 文本、可编辑文本、文本框
27
+ - **图像组件**: 背景图片、普通图片
28
+ - **组合组件**: 分组、对象集合
29
+ - **自定义控件**: 可拖拽控制点、工具栏
30
+
31
+ ### 🖱️ 强大的交互功能
32
+
33
+ - **自动缩放**: 支持鼠标滚轮缩放,自动适应容器大小
34
+ - **平移操作**: 支持拖拽平移画布视图
35
+ - **触摸支持**: 完整的触摸设备支持,包括双指缩放和拖拽
36
+ - **选择系统**: 多选、框选、键盘快捷键支持
37
+ - **拖拽操作**: 对象拖拽、批量操作
38
+
39
+ ### 📦 响应式设计
40
+
41
+ - **自动适配**: 画布自动撑满父容器,响应式调整
42
+ - **触摸优化**: 专为移动设备优化的触摸交互
43
+ - **跨平台**: 支持桌面端和移动端浏览器
44
+
45
+ ### 💻 开发者友好
46
+
47
+ - **TypeScript**: 完整的 TypeScript 类型支持
48
+ - **React 风格**: 声明式 API,符合 React 开发习惯
49
+ - **事件系统**: 完整的事件回调,支持所有 Fabric.js 事件
50
+ - **状态管理**: 内置状态管理,支持受控和非受控模式
4
51
 
5
- <img width="1770" height="1478" alt="react-fabric demo" src="https://github.com/user-attachments/assets/02ff8152-bad3-4e99-82db-13eefc5413b0" />
52
+ ## 快速开始
6
53
 
7
- ## 安装
54
+ ### 安装
8
55
 
9
56
  ```bash
10
57
  npm install @cs-open/react-fabric
@@ -14,124 +61,811 @@ yarn add @cs-open/react-fabric
14
61
  pnpm add @cs-open/react-fabric
15
62
  ```
16
63
 
17
- ### 可选依赖
64
+ ### 基础用法
65
+
66
+ ```tsx
67
+ import React from 'react'
68
+ import { ReactFabric, Rect, Text, } from '@cs-open/react-fabric'
69
+
70
+ function App() {
71
+ return (
72
+ <div style={{ width: '100%', height: '500px' }}>
73
+ <ReactFabric>
74
+ <Rect left={100} top={100} width={200} height={100} fill="red" stroke="blue" strokeWidth={2} />
75
+ <Text left={150} top={250} text="Hello Fabric!" fontSize={20} fill="white" />
76
+ </ReactFabric>
77
+ </div>
78
+ )
79
+ }
80
+
81
+ export default App
82
+ ```
83
+
84
+ ## 🎯 核心功能
85
+
86
+ ### 自动缩放与平移
87
+
88
+ ```tsx
89
+ import { ReactFabric, useReactFabric } from '@cs-open/react-fabric'
90
+
91
+ function CanvasWithControls() {
92
+ const { zoomIn, zoomOut, resetViewport, zoom } = useReactFabric()
93
+
94
+ return (
95
+ <div>
96
+ <div className="toolbar">
97
+ <button onClick={zoomIn}>放大</button>
98
+ <button onClick={zoomOut}>缩小</button>
99
+ <button onClick={() => resetViewport()}>重置</button>
100
+ <span>缩放: {Math.round(zoom * 100)}%</span>
101
+ </div>
102
+
103
+ <ReactFabric zoomable={true} panAble={true} minManualZoom={0.1} maxManualZoom={5}>
104
+ {/* 你的画布内容 */}
105
+ </ReactFabric>
106
+ </div>
107
+ )
108
+ }
109
+ ```
110
+
111
+ ### 触摸设备支持
112
+
113
+ ```tsx
114
+ import { ReactFabric, PluginPinch } from '@cs-open/react-fabric'
115
+
116
+ function TouchCanvas() {
117
+ return (
118
+ <ReactFabric>
119
+ {/* 你的画布内容 */}
120
+ <PluginPinch />
121
+ </ReactFabric>
122
+ )
123
+ }
124
+ ```
125
+
126
+ ### 背景图片
127
+
128
+ ```tsx
129
+ import { ReactFabric, BackgroundImage } from '@cs-open/react-fabric'
130
+
131
+ function CanvasWithBackground() {
132
+ return (
133
+ <ReactFabric defaultCentered>
134
+ <BackgroundImage src="/path/to/image.jpg" scaleToFit />
135
+ {/* 其他图形元素 */}
136
+ </ReactFabric>
137
+ )
138
+ }
139
+ ```
140
+
141
+ ## 🔌 插件系统
142
+
143
+ ### 内置插件
144
+
145
+ | 插件 | 功能 | 描述 |
146
+ | ---------------- | -------- | ---------------------- |
147
+ | `PluginPinch` | 触摸缩放 | 支持双指缩放和拖拽操作 |
148
+ | `PluginFreeDraw` | 自由绘制 | 手绘路径和涂鸦功能 |
149
+ | `PluginFreeRect` | 矩形绘制 | 交互式矩形绘制工具 |
150
+ | `PluginFreeText` | 文本工具 | 点击添加可编辑文本 |
151
+ | `PluginGrid` | 网格辅助 | 显示网格线辅助对齐 |
152
+ | `PluginMask` | 遮罩效果 | 创建遮罩和裁剪效果 |
153
+
154
+ ### 使用插件
155
+
156
+ ```tsx
157
+ import { ReactFabric,PluginPinch, PluginFreeDraw, PluginFreeRect, PluginGrid } from '@cs-open/react-fabric'
158
+
159
+ function AdvancedCanvas() {
160
+ return (
161
+ <ReactFabric>
162
+ {/* 触摸支持 */}
163
+ <PluginPinch />
164
+
165
+ {/* 自由绘制 */}
166
+ <PluginFreeDraw
167
+ onComplete={(path, { canvas }) => {
168
+ console.log('绘制完成:', path)
169
+ }}
170
+ />
171
+
172
+ {/* 矩形绘制工具 */}
173
+ <PluginFreeRect
174
+ fill={'red'}
175
+ onComplete={(rect, { canvas }) => {
176
+ console.log('矩形绘制完成:', rect)
177
+ }}
178
+ />
179
+
180
+ {/* 网格线 */}
181
+ <PluginGrid />
182
+ </ReactFabric>
183
+ )
184
+ }
185
+ ```
186
+
187
+ ## 📦 组件 API
188
+
189
+ ### ReactFabric 组件
190
+
191
+ 主要的画布容器组件,支持以下属性:
192
+
193
+ ```tsx
194
+ interface ReactFabricProps {
195
+ // 基础属性
196
+ width?: number
197
+ height?: number
198
+ className?: string
199
+ style?: CSSProperties
200
+
201
+ // 交互控制
202
+ zoomable?: boolean // 是否可缩放
203
+ panAble?: boolean // 是否可平移
204
+ selection?: boolean // 是否可选择
205
+ defaultSelection?: boolean // 默认选择状态
206
+ defaultDraggable?: boolean // 默认拖拽状态
207
+
208
+ // 缩放控制
209
+ manualZoom?: number // 手动缩放倍数
210
+ minManualZoom?: number // 最小缩放倍数
211
+ maxManualZoom?: number // 最大缩放倍数
212
+ defaultCentered?: boolean // 背景图是否居中
213
+
214
+ // 事件回调
215
+ onMouseDown?: (e: FabricPublicEvent) => void
216
+ onMouseMove?: (e: FabricPublicEvent) => void
217
+ onMouseUp?: (e: FabricPublicEvent) => void
218
+ onMouseWheel?: (e: FabricPublicEvent) => void
219
+ }
220
+ ```
221
+
222
+ ### 图形组件
223
+
224
+ 所有图形组件都支持对应的 Fabric.js 对象的所有属性和事件:
225
+
226
+ ```tsx
227
+ // 矩形
228
+ <Rect
229
+ left={100}
230
+ top={100}
231
+ width={200}
232
+ height={100}
233
+ fill="red"
234
+ stroke="blue"
235
+ strokeWidth={2}
236
+ onModified={(e) => console.log('矩形被修改', e.target)}
237
+ />
238
+
239
+ // 圆形
240
+ <Circle
241
+ left={200}
242
+ top={200}
243
+ radius={50}
244
+ fill="green"
245
+ onSelected={() => console.log('圆形被选中')}
246
+ />
247
+
248
+ // 文本
249
+ <Text
250
+ left={100}
251
+ top={300}
252
+ text="Hello World"
253
+ fontSize={24}
254
+ fill="black"
255
+ fontFamily="Arial"
256
+ />
257
+
258
+ // 图片
259
+ <Image
260
+ left={300}
261
+ top={300}
262
+ src="/path/to/image.jpg"
263
+ width={200}
264
+ height={150}
265
+ />
266
+ ```
267
+
268
+ ## 🎮 状态管理
269
+
270
+ ### useReactFabric Hook
271
+
272
+ ```tsx
273
+ import { useReactFabric } from '@cs-open/react-fabric'
274
+
275
+ function Toolbar() {
276
+ const {
277
+ // 状态
278
+ canvas,
279
+ zoom,
280
+ manualZoom,
281
+ isDragging,
282
+ selection,
283
+
284
+ // 方法
285
+ zoomIn,
286
+ zoomOut,
287
+ resetViewport,
288
+ setZoomable,
289
+ setSelection,
290
+ setDraggable,
291
+ } = useReactFabric()
292
+
293
+ return (
294
+ <div className="toolbar">
295
+ <button onClick={zoomIn}>放大</button>
296
+ <button onClick={zoomOut}>缩小</button>
297
+ <button onClick={() => resetViewport()}>重置</button>
298
+ <span>缩放: {Math.round(zoom * 100)}%</span>
299
+ </div>
300
+ )
301
+ }
302
+ ```
303
+
304
+ ### 跨组件状态访问
305
+
306
+ ReactFabricProvider 是一个上下文提供程序,允许您从组件树中的任何位置访问流的内部状态,例如子组件,甚至在 ReactFabric 之外 元件。它通常用于应用程序的顶层。
307
+ 在这种情况下,您可能需要使用 ReactFabricProvider 组件
308
+
309
+ ```tsx
310
+ import { ReactFabricProvider, useReactFabric } from '@cs-open/react-fabric'
311
+
312
+ function App() {
313
+ return (
314
+ <ReactFabricProvider>
315
+ <Toolbar />
316
+ <ReactFabric>{/* 画布内容 */}</ReactFabric>
317
+ </ReactFabricProvider>
318
+ )
319
+ }
320
+
321
+ function Toolbar() {
322
+ const { zoomIn, zoomOut, resetViewport } = useReactFabric()
323
+ // 可以在 ReactFabric 外部访问状态
324
+ }
325
+ ```
326
+
327
+ ## 🎨 高级用法
328
+
329
+ ### 受控模式
330
+
331
+ ```tsx
332
+ import { useState } from 'react'
333
+ import { ReactFabric, Rect } from '@cs-open/react-fabric'
334
+
335
+ function ControlledCanvas() {
336
+ const [rect, setRect] = useState({
337
+ left: 100,
338
+ top: 100,
339
+ width: 200,
340
+ height: 100,
341
+ fill: 'red',
342
+ })
343
+
344
+ return (
345
+ <ReactFabric>
346
+ <Rect {...rect} onModified={e => setRect(e.target)} />
347
+ </ReactFabric>
348
+ )
349
+ }
350
+ ```
351
+
352
+ ### 非受控模式
353
+
354
+ ```tsx
355
+ import { ReactFabric, Rect, Group } from '@cs-open/react-fabric'
356
+
357
+ function UncontrolledCanvas() {
358
+ return (
359
+ <ReactFabric>
360
+ <Group>
361
+ <Rect defaultLeft={100} defaultTop={100} defaultWidth={100} defaultHeight={100} fill="blue" />
362
+ </Group>
363
+ </ReactFabric>
364
+ )
365
+ }
366
+ ```
367
+
368
+ ### DOM 集成
369
+
370
+ ```tsx
371
+ import { ReactFabric, Rect } from '@cs-open/react-fabric'
372
+
373
+ function CanvasWithDOM() {
374
+ return (
375
+ <ReactFabric>
376
+ <Rect left={100} top={100} width={200} height={100}>
377
+ <div className="tooltip">这是一个提示框</div>
378
+ </Rect>
379
+ </ReactFabric>
380
+ )
381
+ }
382
+ ```
383
+
384
+ ## 📋 依赖要求
385
+
386
+ ### 必需依赖
387
+
388
+ ```json
389
+ {
390
+ "react": ">=17.0.0",
391
+ "react-dom": ">=17.0.0",
392
+ "fabric": "^6.6.1",
393
+ "zustand": "^4.0.0 || ^5.0.0"
394
+ }
395
+ ```
18
396
 
19
- 某些插件需要额外的依赖才能正常工作:
397
+ ### 可选依赖
20
398
 
21
- #### Pinch 插件(触摸手势支持)
399
+ 某些插件需要额外的依赖:
22
400
 
23
401
  ```bash
402
+ # 触摸手势支持
24
403
  npm install hammerjs
25
- # 如果使用 TypeScript,还需要安装类型定义
26
- npm install @types/hammerjs
404
+ npm install @types/hammerjs # TypeScript 用户
405
+
406
+ # 浮动 UI 支持(用于 DOM 集成)
407
+ npm install @floating-ui/react
27
408
  ```
28
409
 
29
- **注意**:如果没有安装 `hammerjs`,Pinch 插件会自动禁用,不会影响其他功能。
410
+ ## 🤝 贡献
411
+
412
+ 欢迎提交 Issue 和 Pull Request!
413
+
414
+ 1. Fork 本仓库
415
+ 2. 创建特性分支 (`git checkout -b feature/AmazingFeature`)
416
+ 3. 提交更改 (`git commit -m 'Add some AmazingFeature'`)
417
+ 4. 推送到分支 (`git push origin feature/AmazingFeature`)
418
+ 5. 开启 Pull Request
419
+
420
+ ## 📄 许可证
421
+
422
+ 本项目基于 [MIT](LICENSE) 许可证开源。
423
+
424
+ ## 🔗 相关链接
425
+
426
+ - [Fabric.js 官方文档](http://fabricjs.com/)
427
+ - [React 官方文档](https://reactjs.org/)
428
+ - [项目主页](https://cs-open.github.io/react-fabric/)
429
+ - [GitHub 仓库](https://github.com/cs-open/react-fabric)
430
+
431
+ ---
432
+
433
+ **Made with ❤️ by the CS-Open team**
434
+
435
+ <div align="center">
436
+
437
+ **[⬆ Back to Top](#-cs-openreact-fabric) | [English](#english)**
438
+
439
+ </div>
440
+
441
+ </div>
442
+
30
443
 
31
- ## 使用示例
444
+ <div id="english">
445
+
446
+ A modern React Canvas drawing component library built on Fabric.js, providing powerful 2D graphics rendering, interaction, and animation capabilities.
447
+
448
+ ## ✨ Core Features
449
+
450
+ ### 🎯 Rich Graphic Components
451
+
452
+ - **Basic Shapes**: Rectangle, Circle, Ellipse, Line, Polygon, Path
453
+ - **Text Components**: Text, Editable Text, Textbox
454
+ - **Image Components**: Background Image, Regular Image
455
+ - **Group Components**: Group, Object Collection
456
+ - **Custom Controls**: Draggable Control Points, Toolbar
457
+
458
+ ### 🖱️ Powerful Interaction Features
459
+
460
+ - **Auto Zoom**: Supports mouse wheel zooming, automatically adapts to container size
461
+ - **Pan Operation**: Supports drag to pan canvas view
462
+ - **Touch Support**: Full touch device support, including pinch zoom and drag
463
+ - **Selection System**: Multi-select, box selection, keyboard shortcut support
464
+ - **Drag Operation**: Object dragging, batch operations
465
+
466
+ ### 📦 Responsive Design
467
+
468
+ - **Auto Adapt**: Canvas automatically fills parent container, responsive adjustment
469
+ - **Touch Optimization**: Touch interactions optimized for mobile devices
470
+ - **Cross-platform**: Supports desktop and mobile browsers
471
+
472
+ ### 💻 Developer Friendly
473
+
474
+ - **TypeScript**: Full TypeScript type support
475
+ - **React Style**: Declarative API, conforms to React development practices
476
+ - **Event System**: Complete event callbacks, supports all Fabric.js events
477
+ - **State Management**: Built-in state management, supports controlled and uncontrolled modes
478
+
479
+ ## ✨ Quick Start
480
+
481
+ ### Installation
482
+
483
+ ```bash
484
+ npm install @cs-open/react-fabric
485
+ # or
486
+ yarn add @cs-open/react-fabric
487
+ # or
488
+ pnpm add @cs-open/react-fabric
489
+ ```
490
+
491
+ ### Basic Usage
32
492
 
33
493
  ```tsx
34
494
  import React from 'react'
35
- import { Canvas, Rect, Text } from '@cs-open/react-fabric'
495
+ import { ReactFabric, Rect, Text, Circle } from '@cs-open/react-fabric'
36
496
 
37
497
  function App() {
38
498
  return (
39
- <Canvas width={800} height={600}>
40
- <Rect left={100} top={100} width={200} height={100} fill="red" />
41
- <Text left={150} top={150} text="Hello Fabric!" fontSize={20} fill="white" />
42
- </Canvas>
499
+ <div style={{ width: '100%', height: '500px' }}>
500
+ <ReactFabric>
501
+ <Rect left={100} top={100} width={200} height={100} fill="red" stroke="blue" strokeWidth={2} />
502
+ <Circle left={300} top={150} radius={50} fill="green" />
503
+ <Text left={150} top={250} text="Hello Fabric!" fontSize={20} fill="white" />
504
+ </ReactFabric>
505
+ </div>
43
506
  )
44
507
  }
45
508
 
46
509
  export default App
47
510
  ```
48
511
 
49
- ## 插件系统
512
+ ## 🎯 Core Features
50
513
 
51
- ### 内置插件
514
+ ### Auto Zoom and Pan
52
515
 
53
- - **Pinch**: 触摸设备双指缩放支持
54
- - **FreeDraw**: 自由绘制工具
55
- - **FreeRect**: 自由矩形绘制工具
56
- - **FreeText**: 自由文本工具
57
- - **GridLine**: 网格线辅助工具
58
- - **Mask**: 遮罩效果
59
- - **Pinch**: 触摸手势支持
516
+ ```tsx
517
+ import { ReactFabric, useReactFabric } from '@cs-open/react-fabric'
60
518
 
61
- ### 使用插件
519
+ function CanvasWithControls() {
520
+ const { zoomIn, zoomOut, resetViewport, zoom } = useReactFabric()
521
+
522
+ return (
523
+ <div>
524
+ <div className="toolbar">
525
+ <button onClick={zoomIn}>Zoom In</button>
526
+ <button onClick={zoomOut}>Zoom Out</button>
527
+ <button onClick={() => resetViewport()}>Reset</button>
528
+ <span>Zoom: {Math.round(zoom * 100)}%</span>
529
+ </div>
530
+
531
+ <ReactFabric zoomable={true} panAble={true} minManualZoom={0.1} maxManualZoom={5}>
532
+ {/* Your canvas content */}
533
+ </ReactFabric>
534
+ </div>
535
+ )
536
+ }
537
+ ```
538
+
539
+ ### Touch Device Support
540
+
541
+ ```tsx
542
+ import { ReactFabric, PluginPinch } from '@cs-open/react-fabric'
543
+ import { PluginPinch } from '@cs-open/react-fabric/plugins'
544
+
545
+ function TouchCanvas() {
546
+ return (
547
+ <ReactFabric>
548
+ {/* Your canvas content */}
549
+ <PluginPinch />
550
+ </ReactFabric>
551
+ )
552
+ }
553
+ ```
554
+
555
+ ### Background Image
556
+
557
+ ```tsx
558
+ import { ReactFabric, BackgroundImage } from '@cs-open/react-fabric'
559
+
560
+ function CanvasWithBackground() {
561
+ return (
562
+ <ReactFabric defaultCentered>
563
+ <BackgroundImage src="/path/to/image.jpg" scaleToFit />
564
+ {/* Other graphic elements */}
565
+ </ReactFabric>
566
+ )
567
+ }
568
+ ```
569
+
570
+ ## 🔌 Plugin System
571
+
572
+ ### Built-in Plugins
573
+
574
+ | Plugin | Function | Description |
575
+ | ---------------- | -------------- | ------------------------------------------- |
576
+ | `PluginPinch` | Touch Zoom | Supports pinch zoom and drag operations |
577
+ | `PluginFreeDraw` | Free Draw | Hand-drawn paths and doodle features |
578
+ | `PluginFreeRect` | Rectangle Draw | Interactive rectangle drawing tool |
579
+ | `PluginFreeText` | Text Tool | Click to add editable text |
580
+ | `PluginGridLine` | Grid Guide | Display grid lines for alignment assistance |
581
+ | `PluginMask` | Mask Effect | Create mask and crop effects |
582
+
583
+ ### Using Plugins
584
+
585
+ ```tsx
586
+ import { ReactFabric } from '@cs-open/react-fabric'
587
+ import { PluginPinch, PluginFreeDraw, PluginFreeRect, PluginGridLine } from '@cs-open/react-fabric/plugins'
588
+
589
+ function AdvancedCanvas() {
590
+ return (
591
+ <ReactFabric>
592
+ {/* Touch support */}
593
+ <PluginPinch />
594
+
595
+ {/* Free draw */}
596
+ <PluginFreeDraw
597
+ onComplete={(path, { canvas }) => {
598
+ console.log('Drawing completed:', path)
599
+ }}
600
+ />
601
+
602
+ {/* Rectangle drawing tool */}
603
+ <PluginFreeRect
604
+ fill={'red'}
605
+ onComplete={(rect, { canvas }) => {
606
+ console.log('Rectangle drawing completed:', rect)
607
+ }}
608
+ />
609
+
610
+ {/* Grid lines */}
611
+ <PluginGridLine />
612
+ </ReactFabric>
613
+ )
614
+ }
615
+ ```
616
+
617
+ ## 📦 Component API
618
+
619
+ ### ReactFabric Component
620
+
621
+ The main canvas container component, supports the following properties:
62
622
 
63
623
  ```tsx
64
- import { Canvas } from '@cs-open/react-fabric'
65
- import { Pinch, FreeDraw } from '@cs-open/react-fabric/plugins'
624
+ interface ReactFabricProps {
625
+ // Basic Properties
626
+ width?: number
627
+ height?: number
628
+ className?: string
629
+ style?: CSSProperties
630
+
631
+ // Interaction Control
632
+ zoomable?: boolean // Whether zoomable
633
+ panAble?: boolean // Whether panable
634
+ selection?: boolean // Whether selectable
635
+ defaultSelection?: boolean // Default selection state
636
+ defaultDraggable?: boolean // Default draggable state
637
+
638
+ // Zoom Control
639
+ manualZoom?: number // Manual zoom level
640
+ minManualZoom?: number // Minimum zoom level
641
+ maxManualZoom?: number // Maximum zoom level
642
+ defaultCentered?: boolean // Whether background image is centered
643
+
644
+ // Event Callbacks
645
+ onMouseDown?: (e: FabricPublicEvent) => void
646
+ onMouseMove?: (e: FabricPublicEvent) => void
647
+ onMouseUp?: (e: FabricPublicEvent) => void
648
+ onMouseWheel?: (e: FabricPublicEvent) => void
649
+ }
650
+ ```
651
+
652
+ ### Graphic Components
653
+
654
+ All graphic components support all properties and events of the corresponding Fabric.js objects:
655
+
656
+ ```tsx
657
+ // Rectangle
658
+ <Rect
659
+ left={100}
660
+ top={100}
661
+ width={200}
662
+ height={100}
663
+ fill="red"
664
+ stroke="blue"
665
+ strokeWidth={2}
666
+ onModified={(e) => console.log('Rectangle modified', e.target)}
667
+ />
668
+
669
+ // Circle
670
+ <Circle
671
+ left={200}
672
+ top={200}
673
+ radius={50}
674
+ fill="green"
675
+ onSelected={() => console.log('Circle selected')}
676
+ />
677
+
678
+ // Text
679
+ <Text
680
+ left={100}
681
+ top={300}
682
+ text="Hello World"
683
+ fontSize={24}
684
+ fill="black"
685
+ fontFamily="Arial"
686
+ />
687
+
688
+ // Image
689
+ <Image
690
+ left={300}
691
+ top={300}
692
+ src="/path/to/image.jpg"
693
+ width={200}
694
+ height={150}
695
+ />
696
+ ```
697
+
698
+ ## 🎮 State Management
699
+
700
+ ### useReactFabric Hook
701
+
702
+ ```tsx
703
+ import { useReactFabric } from '@cs-open/react-fabric'
704
+
705
+ function Toolbar() {
706
+ const {
707
+ // State
708
+ canvas,
709
+ zoom,
710
+ manualZoom,
711
+ isDragging,
712
+ selection,
713
+
714
+ // Methods
715
+ zoomIn,
716
+ zoomOut,
717
+ resetViewport,
718
+ setZoomable,
719
+ setSelection,
720
+ setDraggable,
721
+ } = useReactFabric()
722
+
723
+ return (
724
+ <div className="toolbar">
725
+ <button onClick={zoomIn}>Zoom In</button>
726
+ <button onClick={zoomOut}>Zoom Out</button>
727
+ <button onClick={() => resetViewport()}>Reset</button>
728
+ <span>Zoom: {Math.round(zoom * 100)}%</span>
729
+ </div>
730
+ )
731
+ }
732
+ ```
733
+
734
+ ### Cross-component State Access
735
+
736
+ ReactFabricProvider is a context provider that allows you to access the internal state of the flow from anywhere in the component tree, such as child components, or even outside the ReactFabric component. It is typically used at the top level of the application.
737
+ In this case, you may need to use the ReactFabricProvider component
738
+
739
+ ```tsx
740
+ import { ReactFabricProvider, useReactFabric } from '@cs-open/react-fabric'
66
741
 
67
742
  function App() {
68
743
  return (
69
- <Canvas width={800} height={600}>
70
- {/* 你的画布内容 */}
71
- <Pinch />
72
- <FreeDraw />
73
- </Canvas>
744
+ <ReactFabricProvider>
745
+ <Toolbar />
746
+ <ReactFabric>{/* Canvas content */}</ReactFabric>
747
+ </ReactFabricProvider>
74
748
  )
75
749
  }
750
+
751
+ function Toolbar() {
752
+ const { zoomIn, zoomOut, resetViewport } = useReactFabric()
753
+ // Can access state outside ReactFabric
754
+ }
76
755
  ```
77
756
 
78
- ## 故障排除
757
+ ## 🎨 Advanced Usage
79
758
 
80
- ### Pinch 插件不工作
759
+ ### Controlled Mode
81
760
 
82
- 如果触摸手势功能不工作,请确保:
761
+ ```tsx
762
+ import { useState } from 'react'
763
+ import { ReactFabric, Rect } from '@cs-open/react-fabric'
764
+
765
+ function ControlledCanvas() {
766
+ const [rect, setRect] = useState({
767
+ left: 100,
768
+ top: 100,
769
+ width: 200,
770
+ height: 100,
771
+ fill: 'red',
772
+ })
83
773
 
84
- 1. 已安装 `hammerjs`:
774
+ return (
775
+ <ReactFabric>
776
+ <Rect {...rect} onModified={e => setRect(e.target)} />
777
+ </ReactFabric>
778
+ )
779
+ }
780
+ ```
85
781
 
86
- ```bash
87
- npm install hammerjs
88
- ```
782
+ ### Uncontrolled Mode
89
783
 
90
- 2. 检查控制台是否有相关警告信息
784
+ ```tsx
785
+ import { ReactFabric, Rect, Group } from '@cs-open/react-fabric'
786
+
787
+ function UncontrolledCanvas() {
788
+ return (
789
+ <ReactFabric>
790
+ <Group>
791
+ <Rect defaultLeft={100} defaultTop={100} defaultWidth={100} defaultHeight={100} fill="blue" />
792
+ </Group>
793
+ </ReactFabric>
794
+ )
795
+ }
796
+ ```
91
797
 
92
- 3. 确保在触摸设备上测试
798
+ ### DOM Integration
93
799
 
94
- ### 其他问题
800
+ ```tsx
801
+ import { ReactFabric, Rect } from '@cs-open/react-fabric'
95
802
 
96
- 如果遇到其他问题,请检查:
803
+ function CanvasWithDOM() {
804
+ return (
805
+ <ReactFabric>
806
+ <Rect left={100} top={100} width={200} height={100}>
807
+ <div className="tooltip">This is a tooltip</div>
808
+ </Rect>
809
+ </ReactFabric>
810
+ )
811
+ }
812
+ ```
97
813
 
98
- 1. 所有必需依赖是否正确安装
99
- 2. 浏览器控制台是否有错误信息
100
- 3. 确保使用兼容的浏览器版本
814
+ ## 📋 Dependency Requirements
101
815
 
102
- DOM control
816
+ ### Required Dependencies
103
817
 
104
- ```jsx
105
- <Rect>
106
- <div className="w-full h-full bg-red-500">
107
- <EvaluatesHtml
108
- type="right"
109
- pageSize={pageSize}
110
- allSentences={allSentences}
111
- isPaperFront={isPaperFront}
112
- fontSizeMemo={fontSizeMemo / 1.2}
113
- singleColumnLeft={singleColumnLeft}
114
- />
115
- </div>
116
- </Rect>
818
+ ```json
819
+ {
820
+ "react": ">=17.0.0",
821
+ "react-dom": ">=17.0.0",
822
+ "fabric": "^6.6.1",
823
+ "zustand": "^4.0.0 || ^5.0.0"
824
+ }
117
825
  ```
118
826
 
119
- ## 许可证
827
+ ### Optional Dependencies
120
828
 
121
- MIT
829
+ Some plugins require additional dependencies:
122
830
 
123
- ```jsx
124
- pnpm run changeset // 修改 changeset.md
125
- 全选 changed & unchanged enter
126
- 不选 enter major
127
- 不选 enter minor
128
- Summary 随便写
129
- desired changeset ? true
130
- pnpm run version-packages // 修改 package.json 的 version
131
- pnpm install // 修改 lock 文件
132
- git add .
133
- git commit -m 'chore(core): version bump'
134
- git push
135
- 提交pr到main
136
- 通过 github action 自动发布到 npm
831
+ ```bash
832
+ # Touch gesture support
833
+ npm install hammerjs
834
+ npm install @types/hammerjs # TypeScript users
835
+
836
+ # Floating UI support (for DOM integration)
837
+ npm install @floating-ui/react
137
838
  ```
839
+
840
+ ## 🤝 Contributing
841
+
842
+ Welcome to submit Issues and Pull Requests!
843
+
844
+ 1. Fork this repository
845
+ 2. Create feature branch (`git checkout -b feature/AmazingFeature`)
846
+ 3. Commit changes (`git commit -m 'Add some AmazingFeature'`)
847
+ 4. Push to branch (`git push origin feature/AmazingFeature`)
848
+ 5. Open Pull Request
849
+
850
+ ## 📄 License
851
+
852
+ This project is open source under the [MIT](LICENSE) license.
853
+
854
+ ## 🔗 Related Links
855
+
856
+ - [Fabric.js Official Documentation](http://fabricjs.com/)
857
+ - [React Official Documentation](https://reactjs.org/)
858
+ - [Project Homepage](https://cs-open.github.io/react-fabric/)
859
+ - [GitHub Repository](https://github.com/cs-open/react-fabric)
860
+
861
+ ---
862
+
863
+ **Made with ❤️ by the CS-Open team**
864
+
865
+ </div>
866
+
867
+ <div align="center">
868
+
869
+ **[⬆ Back to Top](#-cs-openreact-fabric) | [中文](#chinese)**
870
+
871
+ </div>
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@cs-open/react-fabric",
3
3
  "type": "module",
4
- "version": "1.0.0",
4
+ "version": "1.0.1",
5
5
  "description": "React Fabric",
6
6
  "keywords": [
7
7
  "@cs-open/react-fabric",
@@ -88,9 +88,9 @@
88
88
  "typescript-eslint": "^8.17.0",
89
89
  "use-sync-external-store": "^1.4.0",
90
90
  "zustand": "^4.0.0 || ^5.0.0",
91
- "@cs-open/eslint-config": "1.0.0",
92
- "@cs-open/rollup-config": "1.0.0",
93
- "@cs-open/tsconfig": "1.0.0"
91
+ "@cs-open/eslint-config": "1.0.1",
92
+ "@cs-open/rollup-config": "1.0.1",
93
+ "@cs-open/tsconfig": "1.0.1"
94
94
  },
95
95
  "dependencies": {
96
96
  "es-toolkit": "^1.39.10",