庞利祥 5 years ago
parent
commit
a287754b54

+ 1 - 1
saga-web-graphy/package.json

@@ -1,6 +1,6 @@
 {
     "name": "@saga-web/graphy",
-    "version": "2.1.27",
+    "version": "2.1.28",
     "description": "上格云二维图形引擎。",
     "main": "lib/index.js",
     "types": "lib/index.d.js",

+ 30 - 2
saga-web-graphy/src/SGraphyScene.ts

@@ -68,7 +68,10 @@ export class SGraphyScene {
         // DO NOTHING
     } // Function drawForeground()
 
-    worldRect(): SRect | null {
+    /**
+     * 所有item占用的矩形区域
+     */
+    allItemRect(): SRect | null {
         let rect: SRect | null = null;
 
         // 依次取item列中的所有item。将所有item的边界做并焦处理。
@@ -83,7 +86,32 @@ export class SGraphyScene {
         }
 
         return rect;
-    } // Function worldRect()
+    } // Function allItemRect()
+
+    /**
+     * 被选中item占用的矩形区域
+     */
+    selectedItemRect(): SRect | null {
+        let rect: SRect | null = null;
+
+        // 依次取item列中的所有item。将所有item的边界做并焦处理。
+        for (let item of this.root.children) {
+            // 如果item未被选中,则去选择下一个item
+            if (!item.selected) {
+                continue;
+            }
+
+            if (rect == null) {
+                rect = item.boundingRect().translated(item.pos.x, item.pos.y);
+            } else {
+                rect.union(
+                    item.boundingRect().translated(item.pos.x, item.pos.y)
+                );
+            }
+        }
+
+        return rect;
+    } // Function selectedItemRect()
 
     // =================================================================================================================
     // 事件

+ 35 - 12
saga-web-graphy/src/SGraphyView.ts

@@ -50,6 +50,9 @@ export class SGraphyView extends SCanvasView {
      * @return  URL地址
      */
     sceneSvgData(width: number, height: number): string {
+        if (null == this.scene) {
+            return "";
+        }
         let engine = new SSvgPaintEngine(width, height);
         let painter = new SPainter(engine);
 
@@ -58,7 +61,9 @@ export class SGraphyView extends SCanvasView {
         let x0 = this.origin.x;
         let y0 = this.origin.y;
 
-        this.fitSceneToSize(width, height);
+        // 场景中无对象
+        let rect = this.scene.allItemRect();
+        this.fitRectToSize(width, height, rect);
         this.onDraw(painter);
 
         // 恢复视图缩放比例与原点位置
@@ -73,10 +78,29 @@ export class SGraphyView extends SCanvasView {
      * 适配视图到视图
      */
     fitSceneToView(): void {
-        this.fitSceneToSize(this.width, this.height);
+        if (null == this.scene) {
+            return;
+        }
+
+        // 场景中无对象
+        let rect = this.scene.allItemRect();
+        this.fitRectToSize(this.width, this.height, rect);
     } // Function FitView()
 
     /**
+     * 适配选中的对象在视图中可见
+     */
+    fitSelectedToView(): void {
+        if (null == this.scene) {
+            return;
+        }
+
+        // 场景中无对象
+        let rect = this.scene.selectedItemRect();
+        this.fitRectToSize(this.width, this.height, rect);
+    } // Function fitSelectedToSize()
+
+    /**
      * 将场景中的xy坐标转换成视图坐标。
      *
      * @param   x       场景中的横坐标
@@ -314,20 +338,19 @@ export class SGraphyView extends SCanvasView {
     } // Function onKeyUp()
 
     /**
-     * 适配视图到指定大小
+     * 适配场景在视图中可见
      *
      * @param   width       宽度
      * @param   height      高度
+     * @param   rect        对象的矩阵大小
      */
-    private fitSceneToSize(width: number, height: number): void {
+    private fitRectToSize(
+        width: number,
+        height: number,
+        rect: SRect | null
+    ): void {
         // 未设置场景
-        if (null == this.scene) {
-            return;
-        }
-
-        // 场景中无对象
-        let rect = this.scene.worldRect();
-        if (null == rect) {
+        if (null == rect || !rect.isValid()) {
             return;
         }
 
@@ -337,7 +360,7 @@ export class SGraphyView extends SCanvasView {
         let center = rect.center();
         this.origin.x = width / 2.0 - center.x * this.scale;
         this.origin.y = height / 2.0 - center.y * this.scale;
-    } // Function fitSceneToSize()
+    } // Function fitRectToSize()
 
     /**
      * MouseEvent事件转换成场景SMouseEvent事件