Browse Source

feat: update line marker resize

wxyyxc1992 5 years ago
parent
commit
a55f4f8079

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

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

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

@@ -23,11 +23,12 @@ export class WhitePage {
23 23
 
24 24
     if (this.mode === 'master') {
25 25
       if (eventHub) {
26
-        this.drawboard = new Drawboard(this, target, {
26
+        this.drawboard = new Drawboard(target, {
27
+          page: this,
27 28
           onChange: ev => eventHub.emit('change', ev)
28 29
         });
29 30
       } else {
30
-        this.drawboard = new Drawboard(this, target);
31
+        this.drawboard = new Drawboard(target, { page: this });
31 32
       }
32 33
     }
33 34
 
@@ -36,7 +37,7 @@ export class WhitePage {
36 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 42
       eventHub.on('change', (ev: ChangeEvent) => {
42 43
         if (ev.event === 'add') {

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

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

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

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

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

@@ -1,6 +1,7 @@
1 1
 import { BaseMarker } from '../BaseMarker';
2 2
 import { ResizeGrip } from '../BaseMarker/ResizeGrip';
3 3
 import { SvgHelper } from '../../renderer/SvgHelper';
4
+import { PositionType } from 'fc-whiteboard/src/event/Event';
4 5
 
5 6
 export class LinearMarker extends BaseMarker {
6 7
   public static createMarker = (): LinearMarker => {
@@ -17,8 +18,7 @@ export class LinearMarker extends BaseMarker {
17 18
 
18 19
   private controlBox: SVGGElement;
19 20
 
20
-  private controlGrip1: ResizeGrip;
21
-  private controlGrip2: ResizeGrip;
21
+  private controlGrips: { left: ResizeGrip; right: ResizeGrip };
22 22
   private activeGrip: ResizeGrip | null;
23 23
 
24 24
   private x1: number = 0;
@@ -56,10 +56,10 @@ export class LinearMarker extends BaseMarker {
56 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 60
     if (this.activeGrip) {
61 61
       if (
62
-        this.activeGrip === this.controlGrip1 &&
62
+        this.activeGrip === this.controlGrips.left &&
63 63
         this.getLineLength(this.x1 + x, this.y1 + 1, this.x2, this.y2) >= this.MIN_LENGTH
64 64
       ) {
65 65
         this.x1 += x;
@@ -68,8 +68,11 @@ export class LinearMarker extends BaseMarker {
68 68
         this.markerBgLine.setAttribute('y1', this.y1.toString());
69 69
         this.markerLine.setAttribute('x1', this.x1.toString());
70 70
         this.markerLine.setAttribute('y1', this.y1.toString());
71
+        if (onPosition) {
72
+          onPosition('left');
73
+        }
71 74
       } else if (
72
-        this.activeGrip === this.controlGrip2 &&
75
+        this.activeGrip === this.controlGrips.right &&
73 76
         this.getLineLength(this.x1, this.y1, this.x2 + x, this.y2 + y) >= this.MIN_LENGTH
74 77
       ) {
75 78
         this.x2 += x;
@@ -78,12 +81,25 @@ export class LinearMarker extends BaseMarker {
78 81
         this.markerBgLine.setAttribute('y2', this.y2.toString());
79 82
         this.markerLine.setAttribute('x2', this.x2.toString());
80 83
         this.markerLine.setAttribute('y2', this.y2.toString());
84
+        if (onPosition) {
85
+          onPosition('right');
86
+        }
81 87
       }
82 88
     }
83 89
 
84 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 103
   private getLineLength = (x1: number, y1: number, x2: number, y2: number): number => {
88 104
     const dx = Math.abs(x1 - x2);
89 105
     const dy = Math.abs(y1 - y2);
@@ -103,8 +119,10 @@ export class LinearMarker extends BaseMarker {
103 119
   };
104 120
 
105 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 127
     this.positionGrips();
110 128
   };
@@ -126,15 +144,15 @@ export class LinearMarker extends BaseMarker {
126 144
   };
127 145
 
128 146
   private positionGrips = () => {
129
-    const gripSize = this.controlGrip1.GRIP_SIZE;
147
+    const gripSize = this.controlGrips.left.GRIP_SIZE;
130 148
 
131 149
     const x1 = this.x1 - gripSize / 2;
132 150
     const y1 = this.y1 - gripSize / 2;
133 151
     const x2 = this.x2 - gripSize / 2;
134 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 158
   private positionGrip = (grip: SVGGraphicsElement, x: number, y: number) => {
@@ -146,9 +164,9 @@ export class LinearMarker extends BaseMarker {
146 164
   private gripMouseDown = (ev: MouseEvent) => {
147 165
     this.isResizing = true;
148 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 170
     this.previousMouseX = ev.screenX;
153 171
     this.previousMouseY = ev.screenY;
154 172
     ev.stopPropagation();

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

@@ -1,8 +1,8 @@
1
-import { SvgHelper } from '../../renderer/SvgHelper';
2
-import { BaseMarker } from '../BaseMarker';
3 1
 import { RectangularMarkerGrips } from './RectangularMarkerGrips';
2
+import { BaseMarker } from '../BaseMarker';
4 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 7
 export class RectangularMarker extends BaseMarker {
8 8
   public static createMarker = (): RectangularMarker => {