1
1
import { BaseService } from '@pictode/utils' ;
2
2
import { fabric } from 'fabric' ;
3
3
4
- import { Rect } from './customs/rect ' ;
5
- import { AppOption , ControlsOption , EventArgs , Model , Plugin , ToolStrategy } from './types' ;
4
+ import { MouseService } from './services/mouse ' ;
5
+ import { AppOption , ControlsOption , EventArgs , Plugin , ToolStrategy } from './types' ;
6
6
import { DEFAULT_APP_OPTION } from './utils' ;
7
7
8
8
export class App extends BaseService < EventArgs > {
9
9
public canvas : fabric . Canvas ;
10
+ public mouseService : MouseService ;
11
+ public currentTool : ToolStrategy | null = null ;
10
12
11
- private currentTool : ToolStrategy | null = null ;
12
13
private option : AppOption & { controls : ControlsOption } ;
13
14
private installedPlugins : Map < string , Plugin > = new Map ( ) ;
14
15
private canvasEl : HTMLCanvasElement ;
15
- private curPointer : fabric . Point = new fabric . Point ( 0 , 0 ) ;
16
16
17
17
constructor ( option ?: AppOption ) {
18
18
super ( ) ;
@@ -22,11 +22,11 @@ export class App extends BaseService<EventArgs> {
22
22
backgroundColor : option ?. backgroundColor ,
23
23
} ) ;
24
24
this . setControls ( this . option . controls ) ;
25
- this . canvas . on ( 'mouse:move' , ( { e } : fabric . IEvent < MouseEvent > ) => {
26
- const { x , y } = this . canvas . getPointer ( e ) ;
27
- this . curPointer . setXY ( x , y ) ;
28
- console . log ( '===>' , x , y ) ;
29
- } ) ;
25
+ this . mouseService = new MouseService ( this ) ;
26
+ }
27
+
28
+ public get pointer ( ) : fabric . Point {
29
+ return this . mouseService . pointer ;
30
30
}
31
31
32
32
public mount ( element : HTMLElement ) {
@@ -35,10 +35,9 @@ export class App extends BaseService<EventArgs> {
35
35
width : element . clientWidth ,
36
36
height : element . clientHeight ,
37
37
} ) ;
38
- this . setModel ( 'select' ) ;
39
38
}
40
39
41
- public setControls ( controls : ControlsOption | boolean ) : App {
40
+ public setControls ( controls : ControlsOption | boolean ) : void {
42
41
this . option . controls = controls ;
43
42
if ( typeof controls === 'boolean' ) {
44
43
this . option . controls . hasControls = controls ;
@@ -52,45 +51,10 @@ export class App extends BaseService<EventArgs> {
52
51
}
53
52
} ) ;
54
53
this . render ( true ) ;
55
- return this ;
56
54
}
57
55
58
- public setTool ( tool : ToolStrategy ) : App {
56
+ public setTool ( tool : ToolStrategy ) : void {
59
57
this . currentTool = tool ;
60
- return this ;
61
- }
62
-
63
- public setModel ( model : Model ) : App {
64
- const rect = new Rect ( {
65
- width : 200 ,
66
- height : 100 ,
67
- top : 20 ,
68
- left : 20 ,
69
- fill : 'transparent' ,
70
- stroke : 'blue' ,
71
- strokeWidth : 5 ,
72
- rx : 5 ,
73
- ry : 5 ,
74
- } ) ;
75
- switch ( model ) {
76
- case 'select' :
77
- this . canvas . isDrawingMode = false ;
78
- this . canvas . selection = true ;
79
- this . canvas . selectionColor = 'rgba(157, 157, 231, 0.5)' ;
80
- this . canvas . selectionBorderColor = 'rgb(157, 157, 231)' ;
81
- this . canvas . selectionLineWidth = 2 ;
82
- break ;
83
- case 'drawing' :
84
- this . canvas . isDrawingMode = true ;
85
- this . canvas . selection = false ;
86
- this . canvas . freeDrawingBrush . color = 'red' ;
87
- this . canvas . freeDrawingBrush . width = 20 ;
88
- break ;
89
- case 'rect' :
90
- this . canvas . add ( rect ) ;
91
- break ;
92
- }
93
- return this ;
94
58
}
95
59
96
60
public render ( asyncRedraw ?: boolean ) : void {
0 commit comments