UNPKG

2.66 kBJavaScriptView Raw
1/*
2* Copyright (C) 1998-2021 by Northwoods Software Corporation. All Rights Reserved.
3*/
4/*
5* This is an extension and not part of the main GoJS library.
6* Note that the API for this class may change with any version, even point releases.
7* If you intend to use an extension in production, you should copy the code to your own source directory.
8* Extensions can be found in the GoJS kit under the extensions or extensionsTS folders.
9* See the Extensions intro page (https://gojs.net/latest/intro/extensions.html) for more information.
10*/
11import * as go from '../release/go-module.js';
12/**
13 * The ResizeMultipleTool class lets the user resize multiple objects at once.
14 *
15 * If you want to experiment with this extension, try the <a href="../../extensionsJSM/ResizeMultiple.html">Resize Multiple</a> sample.
16 * @category Tool Extension
17 */
18export class ResizeMultipleTool extends go.ResizingTool {
19 /**
20 * Constructs a ResizeMultipleTool and sets the name for the tool.
21 */
22 constructor() {
23 super();
24 this.name = 'ResizeMultiple';
25 }
26 /**
27 * Overrides {@link ResizingTool#resize} to resize all selected objects to the same size.
28 * @param {Rect} newr the intended new rectangular bounds for each Part's {@link Part#resizeObject}.
29 */
30 resize(newr) {
31 const diagram = this.diagram;
32 diagram.selection.each(function (part) {
33 if (part instanceof go.Link)
34 return; // only Nodes and simple Parts
35 const obj = part.resizeObject;
36 // calculate new location
37 const pos = part.position.copy();
38 const angle = obj.getDocumentAngle();
39 const sc = obj.getDocumentScale();
40 const radAngle = Math.PI * angle / 180;
41 const angleCos = Math.cos(radAngle);
42 const angleSin = Math.sin(radAngle);
43 const deltaWidth = newr.width - obj.naturalBounds.width;
44 const deltaHeight = newr.height - obj.naturalBounds.height;
45 const angleRight = (angle > 270 || angle < 90) ? 1 : 0;
46 const angleBottom = (angle > 0 && angle < 180) ? 1 : 0;
47 const angleLeft = (angle > 90 && angle < 270) ? 1 : 0;
48 const angleTop = (angle > 180 && angle < 360) ? 1 : 0;
49 pos.x += sc * ((newr.x + deltaWidth * angleLeft) * angleCos - (newr.y + deltaHeight * angleBottom) * angleSin);
50 pos.y += sc * ((newr.x + deltaWidth * angleTop) * angleSin + (newr.y + deltaHeight * angleLeft) * angleCos);
51 obj.desiredSize = newr.size;
52 part.position = pos;
53 });
54 }
55}