@twick/canvas 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 ADDED
@@ -0,0 +1,130 @@
1
+ # @twick/canvas
2
+
3
+ A React-based canvas library built with Fabric.js for video and image manipulation.
4
+
5
+ ## Requirements
6
+
7
+ - Browser environment with Canvas and Video support
8
+ - React 18 or higher
9
+ - Fabric.js 6.6.2 or higher
10
+
11
+ ## Installation
12
+
13
+ ```bash
14
+ pnpm install @twick/canvas
15
+ ```
16
+
17
+ ## Usage
18
+
19
+ ### Basic Canvas Setup
20
+
21
+ ```tsx
22
+ import { useTwickCanvas } from '@twick/canvas';
23
+ import { useRef, useEffect } from 'react';
24
+
25
+ function CustomCanvas() {
26
+ const canvasRef = useRef<HTMLCanvasElement>(null);
27
+ const containerRef = useRef<HTMLDivElement>(null);
28
+
29
+ const { twickCanvas, buildCanvas, addElementToCanvas } = useTwickCanvas({
30
+ onCanvasReady: (canvas) => {
31
+ console.log("Canvas ready", canvas);
32
+ },
33
+ onCanvasOperation: (operation, data) => {
34
+ console.log("Canvas operation", operation, data);
35
+ }
36
+ });
37
+
38
+ useEffect(() => {
39
+ const container = containerRef.current;
40
+ const canvasSize = {
41
+ width: container?.clientWidth,
42
+ height: container?.clientHeight,
43
+ };
44
+
45
+ buildCanvas({
46
+ videoSize: {
47
+ width: 720,
48
+ height: 1280,
49
+ },
50
+ canvasSize,
51
+ canvasRef: canvasRef.current,
52
+ });
53
+ }, []);
54
+
55
+ return (
56
+ <div ref={containerRef}>
57
+ <canvas ref={canvasRef} />
58
+ </div>
59
+ );
60
+ }
61
+ ```
62
+
63
+ ### Adding Elements
64
+
65
+ ```tsx
66
+ // Add an image
67
+ const imageElement = {
68
+ type: "image",
69
+ id: "image-1",
70
+ frame: {
71
+ size: [400, 300],
72
+ },
73
+ props: {
74
+ src: "https://example.com/image.jpg",
75
+ }
76
+ };
77
+
78
+ addElementToCanvas({ element: imageElement, index: 0 });
79
+
80
+ // Add text
81
+ const textElement = {
82
+ type: "text",
83
+ id: "text-1",
84
+ props: {
85
+ x: 100,
86
+ y: 100,
87
+ text: "Hello World",
88
+ fontSize: 64,
89
+ fill: "#FFFFFF",
90
+ }
91
+ };
92
+
93
+ addElementToCanvas({ element: textElement, index: 1 });
94
+ ```
95
+
96
+ ## API Reference
97
+
98
+ ### Hooks
99
+
100
+ - `useTwickCanvas`: Custom hook for canvas manipulation
101
+
102
+ ### Helpers
103
+
104
+ - `createCanvas`: Create a new Fabric.js canvas instance
105
+ - `reorderElementsByZIndex`: Reorder canvas elements by z-index
106
+ - `getCurrentFrameEffect`: Get current frame effect
107
+ - `convertToCanvasPosition`: Convert video coordinates to canvas coordinates
108
+ - `convertToVideoPosition`: Convert canvas coordinates to video coordinates
109
+
110
+ ### Types
111
+
112
+ - `CanvasProps`: Canvas configuration props
113
+ - `CanvasMetadata`: Canvas metadata interface
114
+ - `FrameEffect`: Frame effect interface
115
+ - `CanvasElement`: Canvas element interface
116
+ - `CanvasElementProps`: Canvas element props interface
117
+ - `CaptionProps`: Caption configuration props
118
+
119
+ ## Browser Support
120
+
121
+ This library requires a browser environment with support for:
122
+ - HTML5 Canvas
123
+ - HTML5 Video
124
+ - Modern JavaScript features (ES2020+)
125
+
126
+ The library will throw appropriate errors if used in an unsupported environment.
127
+
128
+ ## License
129
+
130
+ Apache-2.0
@@ -0,0 +1,73 @@
1
+ import { addBackgroundColor } from './components/elements';
2
+ import { addCaptionElement } from './components/elements';
3
+ import { addImageElement } from './components/elements';
4
+ import { addRectElement } from './components/elements';
5
+ import { addTextElement } from './components/elements';
6
+ import { addVideoElement } from './components/elements';
7
+ import { CANVAS_OPERATIONS } from './helpers/constants';
8
+ import { CanvasElement } from './types';
9
+ import { CanvasElementProps } from './types';
10
+ import { CanvasMetadata } from './types';
11
+ import { CanvasProps } from './types';
12
+ import { CaptionProps } from './types';
13
+ import { convertToCanvasPosition } from './helpers/canvas.util';
14
+ import { convertToVideoPosition } from './helpers/canvas.util';
15
+ import { createCanvas } from './helpers/canvas.util';
16
+ import { disabledControl } from './components/element-controls';
17
+ import { FrameEffect } from './types';
18
+ import { getCurrentFrameEffect } from './helpers/canvas.util';
19
+ import { reorderElementsByZIndex } from './helpers/canvas.util';
20
+ import { rotateControl } from './components/element-controls';
21
+ import { useTwickCanvas } from './hooks/use-twick-canvas';
22
+
23
+ export { addBackgroundColor }
24
+
25
+ export { addCaptionElement }
26
+
27
+ export { addImageElement }
28
+
29
+ export { addRectElement }
30
+
31
+ export { addTextElement }
32
+
33
+ export { addVideoElement }
34
+
35
+ export { CANVAS_OPERATIONS }
36
+
37
+ export { CanvasElement }
38
+
39
+ export { CanvasElementProps }
40
+
41
+ export { CanvasMetadata }
42
+
43
+ export { CanvasProps }
44
+
45
+ export { CaptionProps }
46
+
47
+ export { convertToCanvasPosition }
48
+
49
+ export { convertToVideoPosition }
50
+
51
+ export { createCanvas }
52
+
53
+ export { disabledControl }
54
+
55
+ export { FrameEffect }
56
+
57
+ export { getCurrentFrameEffect }
58
+
59
+ export { reorderElementsByZIndex }
60
+
61
+ export { rotateControl }
62
+
63
+ export { useTwickCanvas }
64
+
65
+ export { }
66
+
67
+
68
+ declare module 'fabric' {
69
+ interface FabricObject {
70
+ zIndex?: number;
71
+ }
72
+ }
73
+