Browse Source

feat: update line marker resize

wxyyxc1992 5 years ago
parent
commit
a55f4f8079

+ 12
- 3
src/board/Drawboard/index.ts View File

94
   private scale = 1.0;
94
   private scale = 1.0;
95
 
95
 
96
   constructor(
96
   constructor(
97
-    page: WhitePage,
98
     target: HTMLImageElement,
97
     target: HTMLImageElement,
99
-    { onChange }: { onChange?: onChangeFunc } = {}
98
+    { page, onChange }: { page?: WhitePage; onChange?: onChangeFunc } = {}
100
   ) {
99
   ) {
101
-    this.page = page;
100
+    if (page) {
101
+      this.page = page;
102
+    }
103
+
102
     this.target = target;
104
     this.target = target;
105
+
106
+    // 如果仅传入图片地址或者 Blob,则必须为全屏模式
103
     this.width = target.clientWidth;
107
     this.width = target.clientWidth;
104
     this.height = target.clientHeight;
108
     this.height = target.clientHeight;
105
 
109
 
158
   public addMarker = (markerType: typeof BaseMarker, { id }: { id?: string } = {}) => {
162
   public addMarker = (markerType: typeof BaseMarker, { id }: { id?: string } = {}) => {
159
     const marker = markerType.createMarker();
163
     const marker = markerType.createMarker();
160
 
164
 
165
+    // 假如 Drawboard 存在 Page 引用,则传导给 Marker
166
+    if (this.page) {
167
+      marker.page = this.page;
168
+    }
169
+
161
     if (id) {
170
     if (id) {
162
       marker.id = id;
171
       marker.id = id;
163
     }
172
     }

+ 4
- 3
src/board/WhitePage/index.ts View File

23
 
23
 
24
     if (this.mode === 'master') {
24
     if (this.mode === 'master') {
25
       if (eventHub) {
25
       if (eventHub) {
26
-        this.drawboard = new Drawboard(this, target, {
26
+        this.drawboard = new Drawboard(target, {
27
+          page: this,
27
           onChange: ev => eventHub.emit('change', ev)
28
           onChange: ev => eventHub.emit('change', ev)
28
         });
29
         });
29
       } else {
30
       } else {
30
-        this.drawboard = new Drawboard(this, target);
31
+        this.drawboard = new Drawboard(target, { page: this });
31
       }
32
       }
32
     }
33
     }
33
 
34
 
36
         throw new Error('Invalid eventHub');
37
         throw new Error('Invalid eventHub');
37
       }
38
       }
38
 
39
 
39
-      this.drawboard = new Drawboard(this, target);
40
+      this.drawboard = new Drawboard(target, { page: this });
40
 
41
 
41
       eventHub.on('change', (ev: ChangeEvent) => {
42
       eventHub.on('change', (ev: ChangeEvent) => {
42
         if (ev.event === 'add') {
43
         if (ev.event === 'add') {

+ 2
- 0
src/event/Event.ts View File

1
 export type TargetType = 'page' | 'drawboard' | 'marker';
1
 export type TargetType = 'page' | 'drawboard' | 'marker';
2
 export type EventType = 'add' | 'resize' | 'move' | 'remove';
2
 export type EventType = 'add' | 'resize' | 'move' | 'remove';
3
 export type PositionType =
3
 export type PositionType =
4
+  | 'left'
5
+  | 'right'
4
   | 'topLeft'
6
   | 'topLeft'
5
   | 'bottomLeft'
7
   | 'bottomLeft'
6
   | 'topRight'
8
   | 'topRight'

+ 4
- 0
src/markers/BaseMarker/index.ts View File

1
+import { WhitePage } from './../../board/WhitePage/index';
1
 import { PositionType } from 'fc-whiteboard/src/event/Event';
2
 import { PositionType } from 'fc-whiteboard/src/event/Event';
2
 import { onChangeFunc, EventType } from '../../event/Event';
3
 import { onChangeFunc, EventType } from '../../event/Event';
3
 import { MarkerType } from '../types';
4
 import { MarkerType } from '../types';
7
 export class BaseMarker {
8
 export class BaseMarker {
8
   id: string = uuid();
9
   id: string = uuid();
9
   type: MarkerType = 'base';
10
   type: MarkerType = 'base';
11
+  // 归属的
12
+  page: WhitePage;
13
+  // Marker 的属性发生变化后的回调
10
   onChange: onChangeFunc = () => {};
14
   onChange: onChangeFunc = () => {};
11
 
15
 
12
   public static createMarker = (): BaseMarker => {
16
   public static createMarker = (): BaseMarker => {

+ 31
- 13
src/markers/LinearMarker/index.ts View File

1
 import { BaseMarker } from '../BaseMarker';
1
 import { BaseMarker } from '../BaseMarker';
2
 import { ResizeGrip } from '../BaseMarker/ResizeGrip';
2
 import { ResizeGrip } from '../BaseMarker/ResizeGrip';
3
 import { SvgHelper } from '../../renderer/SvgHelper';
3
 import { SvgHelper } from '../../renderer/SvgHelper';
4
+import { PositionType } from 'fc-whiteboard/src/event/Event';
4
 
5
 
5
 export class LinearMarker extends BaseMarker {
6
 export class LinearMarker extends BaseMarker {
6
   public static createMarker = (): LinearMarker => {
7
   public static createMarker = (): LinearMarker => {
17
 
18
 
18
   private controlBox: SVGGElement;
19
   private controlBox: SVGGElement;
19
 
20
 
20
-  private controlGrip1: ResizeGrip;
21
-  private controlGrip2: ResizeGrip;
21
+  private controlGrips: { left: ResizeGrip; right: ResizeGrip };
22
   private activeGrip: ResizeGrip | null;
22
   private activeGrip: ResizeGrip | null;
23
 
23
 
24
   private x1: number = 0;
24
   private x1: number = 0;
56
     this.addControlBox();
56
     this.addControlBox();
57
   }
57
   }
58
 
58
 
59
-  protected resize(x: number, y: number) {
59
+  protected resize(x: number, y: number, onPosition?: (pos: PositionType) => void) {
60
     if (this.activeGrip) {
60
     if (this.activeGrip) {
61
       if (
61
       if (
62
-        this.activeGrip === this.controlGrip1 &&
62
+        this.activeGrip === this.controlGrips.left &&
63
         this.getLineLength(this.x1 + x, this.y1 + 1, this.x2, this.y2) >= this.MIN_LENGTH
63
         this.getLineLength(this.x1 + x, this.y1 + 1, this.x2, this.y2) >= this.MIN_LENGTH
64
       ) {
64
       ) {
65
         this.x1 += x;
65
         this.x1 += x;
68
         this.markerBgLine.setAttribute('y1', this.y1.toString());
68
         this.markerBgLine.setAttribute('y1', this.y1.toString());
69
         this.markerLine.setAttribute('x1', this.x1.toString());
69
         this.markerLine.setAttribute('x1', this.x1.toString());
70
         this.markerLine.setAttribute('y1', this.y1.toString());
70
         this.markerLine.setAttribute('y1', this.y1.toString());
71
+        if (onPosition) {
72
+          onPosition('left');
73
+        }
71
       } else if (
74
       } else if (
72
-        this.activeGrip === this.controlGrip2 &&
75
+        this.activeGrip === this.controlGrips.right &&
73
         this.getLineLength(this.x1, this.y1, this.x2 + x, this.y2 + y) >= this.MIN_LENGTH
76
         this.getLineLength(this.x1, this.y1, this.x2 + x, this.y2 + y) >= this.MIN_LENGTH
74
       ) {
77
       ) {
75
         this.x2 += x;
78
         this.x2 += x;
78
         this.markerBgLine.setAttribute('y2', this.y2.toString());
81
         this.markerBgLine.setAttribute('y2', this.y2.toString());
79
         this.markerLine.setAttribute('x2', this.x2.toString());
82
         this.markerLine.setAttribute('x2', this.x2.toString());
80
         this.markerLine.setAttribute('y2', this.y2.toString());
83
         this.markerLine.setAttribute('y2', this.y2.toString());
84
+        if (onPosition) {
85
+          onPosition('right');
86
+        }
81
       }
87
       }
82
     }
88
     }
83
 
89
 
84
     this.adjustControlBox();
90
     this.adjustControlBox();
85
   }
91
   }
86
 
92
 
93
+  protected resizeByEvent(x: number, y: number, pos?: PositionType) {
94
+    if (pos === 'left') {
95
+      this.activeGrip = this.controlGrips.left;
96
+    } else {
97
+      this.activeGrip = this.controlGrips.right;
98
+    }
99
+
100
+    this.resize(x, y);
101
+  }
102
+
87
   private getLineLength = (x1: number, y1: number, x2: number, y2: number): number => {
103
   private getLineLength = (x1: number, y1: number, x2: number, y2: number): number => {
88
     const dx = Math.abs(x1 - x2);
104
     const dx = Math.abs(x1 - x2);
89
     const dy = Math.abs(y1 - y2);
105
     const dy = Math.abs(y1 - y2);
103
   };
119
   };
104
 
120
 
105
   private addControlGrips = () => {
121
   private addControlGrips = () => {
106
-    this.controlGrip1 = this.createGrip();
107
-    this.controlGrip2 = this.createGrip();
122
+    this.controlGrips = {
123
+      left: this.createGrip(),
124
+      right: this.createGrip()
125
+    };
108
 
126
 
109
     this.positionGrips();
127
     this.positionGrips();
110
   };
128
   };
126
   };
144
   };
127
 
145
 
128
   private positionGrips = () => {
146
   private positionGrips = () => {
129
-    const gripSize = this.controlGrip1.GRIP_SIZE;
147
+    const gripSize = this.controlGrips.left.GRIP_SIZE;
130
 
148
 
131
     const x1 = this.x1 - gripSize / 2;
149
     const x1 = this.x1 - gripSize / 2;
132
     const y1 = this.y1 - gripSize / 2;
150
     const y1 = this.y1 - gripSize / 2;
133
     const x2 = this.x2 - gripSize / 2;
151
     const x2 = this.x2 - gripSize / 2;
134
     const y2 = this.y2 - gripSize / 2;
152
     const y2 = this.y2 - gripSize / 2;
135
 
153
 
136
-    this.positionGrip(this.controlGrip1.visual, x1, y1);
137
-    this.positionGrip(this.controlGrip2.visual, x2, y2);
154
+    this.positionGrip(this.controlGrips.left.visual, x1, y1);
155
+    this.positionGrip(this.controlGrips.right.visual, x2, y2);
138
   };
156
   };
139
 
157
 
140
   private positionGrip = (grip: SVGGraphicsElement, x: number, y: number) => {
158
   private positionGrip = (grip: SVGGraphicsElement, x: number, y: number) => {
146
   private gripMouseDown = (ev: MouseEvent) => {
164
   private gripMouseDown = (ev: MouseEvent) => {
147
     this.isResizing = true;
165
     this.isResizing = true;
148
     this.activeGrip =
166
     this.activeGrip =
149
-      (ev.target as SVGGraphicsElement) === this.controlGrip1.visual
150
-        ? this.controlGrip1
151
-        : this.controlGrip2;
167
+      (ev.target as SVGGraphicsElement) === this.controlGrips.left.visual
168
+        ? this.controlGrips.left
169
+        : this.controlGrips.right;
152
     this.previousMouseX = ev.screenX;
170
     this.previousMouseX = ev.screenX;
153
     this.previousMouseY = ev.screenY;
171
     this.previousMouseY = ev.screenY;
154
     ev.stopPropagation();
172
     ev.stopPropagation();

+ 3
- 3
src/markers/RectangularMarker/index.ts View File

1
-import { SvgHelper } from '../../renderer/SvgHelper';
2
-import { BaseMarker } from '../BaseMarker';
3
 import { RectangularMarkerGrips } from './RectangularMarkerGrips';
1
 import { RectangularMarkerGrips } from './RectangularMarkerGrips';
2
+import { BaseMarker } from '../BaseMarker';
4
 import { ResizeGrip } from '../BaseMarker/ResizeGrip';
3
 import { ResizeGrip } from '../BaseMarker/ResizeGrip';
5
-import { PositionType } from 'fc-whiteboard/src/event/Event';
4
+import { PositionType } from '../../event/Event';
5
+import { SvgHelper } from '../../renderer/SvgHelper';
6
 
6
 
7
 export class RectangularMarker extends BaseMarker {
7
 export class RectangularMarker extends BaseMarker {
8
   public static createMarker = (): RectangularMarker => {
8
   public static createMarker = (): RectangularMarker => {