Skip to content

Commit 731a0da

Browse files
committed
test: on progress
1 parent 5c87456 commit 731a0da

File tree

10 files changed

+255
-203
lines changed

10 files changed

+255
-203
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ chore
77
ci
88
docs
99
feat
10-
fix
1110
perf
11+
fix
1212
refactor
1313
revert
1414
style

apps/react-demo/src/App.tsx

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ import './App.css';
66

77
function App() {
88
const [count, setCount] = useState(0);
9-
const canvasContainerRef = useRef<HTMLDivElement>(null);
9+
const canvasRef = useRef<HTMLCanvasElement>(null);
1010

1111
useEffect(() => {
12-
if (canvasContainerRef.current) {
13-
const imageEditor = new ImageEditor(canvasContainerRef.current);
14-
imageEditor.init();
12+
if (canvasRef.current) {
13+
const imageEditor = new ImageEditor(canvasRef.current);
14+
1515
return () => {
1616
imageEditor.destroy();
1717
};
@@ -33,14 +33,19 @@ function App() {
3333
<button onClick={() => setCount((count) => count + 1)}>
3434
count is {count}
3535
</button>
36+
37+
<button onClick={() => {}}>init</button>
38+
3639
<p>
3740
Edit <code>src/App.tsx</code> and save to test HMR
3841
</p>
3942
</div>
4043
<p className="read-the-docs">
4144
Click on the Vite and React logos to learn more
4245
</p>
43-
<div className="canvas-container" ref={canvasContainerRef}></div>
46+
<div className="canvas-container">
47+
<canvas ref={canvasRef}></canvas>
48+
</div>
4449
</>
4550
);
4651
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import * as fabric from 'fabric';
2+
export declare class FabricJSCanvas {
3+
private fabricCanvas;
4+
private container;
5+
private width;
6+
private height;
7+
constructor(width: number, height: number, container: HTMLElement);
8+
initializeFabricCanvas(): void;
9+
initEvents(): void;
10+
getCanvas(): fabric.Canvas | null;
11+
destroy(): void;
12+
}
Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
1-
import { Canvas } from 'fabric';
21
export declare class ImageEditor {
3-
private readonly container;
42
private fabricCanvas;
5-
constructor(container: HTMLElement);
3+
constructor(canvasElement: HTMLElement);
64
init(): void;
7-
getCanvas(): Canvas | null;
85
destroy(): void;
96
}

packages/image-editor/dist/main.js

Lines changed: 140 additions & 152 deletions
Large diffs are not rendered by default.
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import * as fabric from 'fabric';
2+
3+
export class FabricJSCanvas {
4+
private fabricCanvas: fabric.Canvas | null = null;
5+
private container: HTMLElement;
6+
private width: number;
7+
private height: number;
8+
constructor(width: number, height: number, container: HTMLElement) {
9+
this.container = container;
10+
this.width = width;
11+
this.height = height;
12+
}
13+
14+
public initializeFabricCanvas() {
15+
if (!this.container.contains(document.querySelector('canvas'))) {
16+
const canvas = document.createElement('canvas');
17+
canvas.id = 'fabric-canvas';
18+
this.container.appendChild(canvas);
19+
this.fabricCanvas = new fabric.Canvas(canvas);
20+
this.fabricCanvas.setDimensions({
21+
width: this.width,
22+
height: this.height,
23+
});
24+
const add = () => {
25+
const red = new fabric.Rect({
26+
top: Math.random() * (this.height - 25),
27+
left: Math.random() * (this.width - 40),
28+
width: 80,
29+
height: 50,
30+
fill: 'red',
31+
});
32+
const blue = new fabric.Rect({
33+
top: Math.random() * (this.height - 35),
34+
left: Math.random() * (this.width - 25),
35+
width: 50,
36+
height: 70,
37+
fill: 'blue',
38+
});
39+
const green = new fabric.Rect({
40+
top: Math.random() * (this.height - 30),
41+
left: Math.random() * (this.width - 30),
42+
width: 60,
43+
height: 60,
44+
fill: 'green',
45+
});
46+
return [red, blue, green];
47+
};
48+
this.fabricCanvas.add(...add());
49+
this.fabricCanvas.setActiveObject(this.fabricCanvas.getObjects()[0]);
50+
this.fabricCanvas.renderAll();
51+
}
52+
53+
// this.initEvents();
54+
}
55+
56+
public initEvents() {
57+
this.fabricCanvas?.on('selection:created', (e) => {
58+
console.log('selection:created', e);
59+
});
60+
this.fabricCanvas?.on('selection:updated', (e) => {
61+
console.log('selection:updated', e);
62+
});
63+
this.fabricCanvas?.on('selection:cleared', (e) => {
64+
console.log('selection:cleared', e);
65+
});
66+
}
67+
68+
public getCanvas() {
69+
return this.fabricCanvas;
70+
}
71+
72+
public destroy() {
73+
this.fabricCanvas?.destroy();
74+
}
75+
}

packages/image-editor/src/image-editor.ts

Lines changed: 16 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,27 @@
1-
import type { Canvas } from 'fabric';
2-
3-
import { Canvas as FabricCanvas, FabricText } from 'fabric';
1+
import * as fabric from 'fabric';
42

53
export class ImageEditor {
64
// private static instance: ImageEditor;
7-
private readonly container: HTMLElement;
8-
// private canvas: HTMLCanvasElement | null = null;
9-
private fabricCanvas: Canvas | null = null;
10-
11-
constructor(container: HTMLElement) {
12-
this.container = container;
13-
}
14-
15-
public init() {
16-
console.log('init');
17-
console.log(this.fabricCanvas);
18-
console.log('@@@@ zzzz');
19-
if (!this.fabricCanvas) {
20-
this.fabricCanvas = new FabricCanvas();
21-
if (!this.container.contains(this.fabricCanvas.getElement())) {
22-
console.log('append');
235

24-
console.log(this.fabricCanvas);
6+
private fabricCanvas: fabric.Canvas | null = null;
257

26-
this.fabricCanvas.add(new FabricText('Hello World'));
27-
this.fabricCanvas.renderAll();
28-
29-
this.container.appendChild(this.fabricCanvas.getElement());
30-
}
31-
32-
// this.fabricCanvas.add(new FabricText('Hello World'));
33-
// this.fabricCanvas.renderAll();
34-
// this.container.appendChild(this.fabricCanvas.getElement());
8+
constructor(canvasElement: HTMLElement) {
9+
if (
10+
!canvasElement ||
11+
!(canvasElement instanceof HTMLCanvasElement) ||
12+
canvasElement instanceof fabric.Canvas
13+
) {
14+
throw new Error('canvasElement is required');
3515
}
16+
this.fabricCanvas = new fabric.Canvas(canvasElement);
17+
// this.fabricCanvas.setDimensions({
18+
// width: canvasElement.width,
19+
// height: canvasElement.height,
20+
// });
21+
// this.fabricCanvas.renderAll();
3622
}
3723

38-
// public initCanvas() {
39-
// this.canvas = document.createElement('canvas');
40-
// this.container.appendChild(this.canvas);
41-
// this.canvas.width = 100;
42-
// this.canvas.height = 100;
43-
// this.canvas.style.border = '1px solid #000';
44-
// this.canvas.style.backgroundColor = 'red';
45-
// }
46-
47-
public getCanvas() {
48-
return this.fabricCanvas;
49-
}
24+
public init() {}
5025

5126
public destroy() {
5227
this.fabricCanvas?.destroy();

packages/image-editor/src/tools/drawing-line.ts

Whitespace-only changes.

packages/image-editor/src/tools/drawing-path.ts

Whitespace-only changes.

packages/image-editor/src/tools/drawing-text.ts

Whitespace-only changes.

0 commit comments

Comments
 (0)