drawfn 0.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.
- package/README.md +137 -0
- package/dist/index.cjs +1961 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.js +1956 -0
- package/dist/index.js.map +1 -0
- package/package.json +66 -0
package/README.md
ADDED
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
# @21n/drawfn
|
|
2
|
+
|
|
3
|
+
Cross-framework drawing canvas library for brainstorming and simple sketching.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- 🎨 Framework-agnostic core (works with React, Svelte, Vue, vanilla JS)
|
|
8
|
+
- ✏️ Multiple drawing tools: pen, shapes, text, images, and more
|
|
9
|
+
- 📐 Element-based retained-mode architecture
|
|
10
|
+
- 💾 Full scene serialization to JSON
|
|
11
|
+
- ⏮️ Undo/redo support
|
|
12
|
+
- 📤 Export to PNG and SVG
|
|
13
|
+
- 🔍 Pan and zoom with viewport culling
|
|
14
|
+
- 🎯 Selection and transformation tools
|
|
15
|
+
|
|
16
|
+
## Installation
|
|
17
|
+
|
|
18
|
+
```bash
|
|
19
|
+
npm install @21n/drawfn perfect-freehand
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
## Quick Start
|
|
23
|
+
|
|
24
|
+
```typescript
|
|
25
|
+
import { Drawfn } from '@21n/drawfn';
|
|
26
|
+
|
|
27
|
+
const canvas = document.querySelector('canvas');
|
|
28
|
+
const drawfn = new Drawfn({ canvas });
|
|
29
|
+
|
|
30
|
+
// Set a tool
|
|
31
|
+
drawfn.setTool('pen');
|
|
32
|
+
|
|
33
|
+
// Listen to events
|
|
34
|
+
drawfn.on('elementAdded', (element) => {
|
|
35
|
+
console.log('New element added:', element);
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
// Export the scene
|
|
39
|
+
const scene = drawfn.getScene();
|
|
40
|
+
console.log(scene);
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
## Tools
|
|
44
|
+
|
|
45
|
+
- **select**: Select, move, resize, and rotate elements with handles
|
|
46
|
+
- **pan**: Pan the canvas with mouse/trackpad
|
|
47
|
+
- **pen**: Freehand drawing with pressure support (perfect-freehand)
|
|
48
|
+
- **rectangle**: Draw rectangles (hold Shift for squares)
|
|
49
|
+
- **ellipse**: Draw ellipses (hold Shift for circles)
|
|
50
|
+
- **arrow**: Draw arrows with arrowhead
|
|
51
|
+
- **text**: Add text with inline editing overlay
|
|
52
|
+
- **image**: Add images programmatically
|
|
53
|
+
- **node**: Add Nucleus node bookmarks (for integrations)
|
|
54
|
+
|
|
55
|
+
## API
|
|
56
|
+
|
|
57
|
+
### Constructor
|
|
58
|
+
|
|
59
|
+
```typescript
|
|
60
|
+
const drawfn = new Drawfn({
|
|
61
|
+
canvas: HTMLCanvasElement,
|
|
62
|
+
overlayRoot?: HTMLElement, // For text editing overlay
|
|
63
|
+
getImageBitmap?: (src: string) => Promise<ImageBitmap> // Custom image loader
|
|
64
|
+
});
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
### Methods
|
|
68
|
+
|
|
69
|
+
**Scene Management**
|
|
70
|
+
- `load(scene: Scene)`: Load a scene from JSON
|
|
71
|
+
- `getScene(): Scene`: Get current scene as JSON
|
|
72
|
+
- `clear()`: Clear the entire canvas
|
|
73
|
+
|
|
74
|
+
**Element Operations**
|
|
75
|
+
- `add(element: Element): string`: Add an element, returns ID
|
|
76
|
+
- `update(id: string, patch: Partial<Element>)`: Update an element
|
|
77
|
+
- `remove(id: string)`: Remove an element
|
|
78
|
+
- `bringToFront(id: string)`: Move element to top layer
|
|
79
|
+
- `sendToBack(id: string)`: Move element to bottom layer
|
|
80
|
+
- `getElements(): Element[]`: Get all elements
|
|
81
|
+
- `getElementById(id: string): Element | undefined`: Get element by ID
|
|
82
|
+
|
|
83
|
+
**Tool Management**
|
|
84
|
+
- `setTool(tool: ToolName, opts?)`: Set active tool
|
|
85
|
+
- For `image` tool: `setTool('image', { src: 'url', naturalW: 200, naturalH: 150 })`
|
|
86
|
+
- For `node` tool: `setTool('node', { nodeId: 'abc', preview: { title: 'Note' } })`
|
|
87
|
+
- `getTool(): ToolName`: Get current tool
|
|
88
|
+
|
|
89
|
+
**Camera & View**
|
|
90
|
+
- `setCamera(camera: Partial<Camera>)`: Update camera position/zoom
|
|
91
|
+
- `getCamera(): Camera`: Get camera state
|
|
92
|
+
- `screenToCanvas(point: Point): Point`: Convert screen to canvas coordinates
|
|
93
|
+
|
|
94
|
+
**History**
|
|
95
|
+
- `undo()`: Undo last action
|
|
96
|
+
- `redo()`: Redo last undone action
|
|
97
|
+
|
|
98
|
+
**Selection**
|
|
99
|
+
- `setSelection(ids: string[])`: Set selected element IDs
|
|
100
|
+
- `getSelectedIds(): string[]`: Get selected element IDs
|
|
101
|
+
|
|
102
|
+
**Export**
|
|
103
|
+
- `exportPNG(opts?): Promise<Blob>`: Export as PNG
|
|
104
|
+
- Options: `{ padding?: number; scale?: number; transparent?: boolean }`
|
|
105
|
+
- `exportSVG(opts?): Promise<string>`: Export as SVG (includes freedraw, arrows, images)
|
|
106
|
+
- Options: `{ padding?: number; scale?: number }`
|
|
107
|
+
|
|
108
|
+
**Events**
|
|
109
|
+
- `on(event, handler): () => void`: Subscribe to events, returns unsubscribe function
|
|
110
|
+
- Events: `pointerDown`, `pointerMove`, `pointerUp`, `elementAdded`, `elementUpdated`, `elementRemoved`, `selectionChanged`, `historyChanged`, `cameraChanged`
|
|
111
|
+
|
|
112
|
+
**Cleanup**
|
|
113
|
+
- `destroy()`: Cleanup and remove event listeners
|
|
114
|
+
|
|
115
|
+
## Examples
|
|
116
|
+
|
|
117
|
+
See `example.html` for a complete vanilla JS example with toolbar and all tools.
|
|
118
|
+
|
|
119
|
+
For React examples, see `@21n/drawfn-react` package.
|
|
120
|
+
|
|
121
|
+
## Keyboard Shortcuts
|
|
122
|
+
|
|
123
|
+
- **Delete/Backspace**: Delete selected elements
|
|
124
|
+
- **Cmd/Ctrl+Z**: Undo
|
|
125
|
+
- **Cmd/Ctrl+Shift+Z** or **Cmd/Ctrl+Y**: Redo
|
|
126
|
+
- **Space+Drag**: Pan (when not in pan tool mode)
|
|
127
|
+
- **Shift+Drag**: Constrain aspect ratio for shapes
|
|
128
|
+
|
|
129
|
+
## Mouse/Touch Interactions
|
|
130
|
+
|
|
131
|
+
- **Wheel**: Zoom at cursor position
|
|
132
|
+
- **Click+Drag**: Tool-specific interaction
|
|
133
|
+
- **Shift+Click**: Multi-select (in select tool)
|
|
134
|
+
|
|
135
|
+
## License
|
|
136
|
+
|
|
137
|
+
Apache-2.0
|