forked from johnwun/js4ai
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ZoomAndCenterSelection.js
87 lines (76 loc) · 2.73 KB
/
ZoomAndCenterSelection.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/////////////////////////////////////////////////////////////////
//Zoom and Center to Selection v2. -- CS, CS2
//>=--------------------------------------
//
// Zooms active view to selected object(s).
//
// New in v.2:
// If nothing is selected; sets view to 100% at current location.
//
// Simple but REALLY cool!
//
//>=--------------------------------------
// JS code (c) copyright: John Wundes ( [email protected] ) www.wundes.com
//copyright full text here: http://www.wundes.com/js4ai/copyright.txt
//////////////////////////////////////////////////////////////////
if ( documents.length > 0)
{
if(activeDocument.selection.length >0){
mySelection = activeDocument.selection;
//if object is a (collection of) object(s) not a text field.
if (mySelection instanceof Array) {
//initialize vars
initBounds = mySelection[0].visibleBounds;
ul_x = initBounds[0];
ul_y = initBounds[1];
lr_x = initBounds[2];
lr_y = initBounds[3];
//check rest of group if any
for (i=1; i<mySelection.length; i++) {
groupBounds = mySelection[i].visibleBounds;
if (groupBounds[0]<ul_x){ul_x=groupBounds[0]}
if (groupBounds[1]>ul_y){ul_y=groupBounds[1]}
if (groupBounds[2]>lr_x){lr_x=groupBounds[2]}
if (groupBounds[3]<lr_y){lr_y=groupBounds[3]}
}
}
//get x,y/x,y Matrix for 100% view
activeDocument.views[0].zoom = 1;
ScreenSize = activeDocument.views[0].bounds;
ScreenWidth= ScreenSize[2] - ScreenSize[0];
ScreenHeight=ScreenSize[1] - ScreenSize[3];
screenProportion =ScreenHeight/ScreenWidth;
//Determine upperLeft position of object(s)
cntrPos = [ul_x,ul_y];
//mySelection[0].position;
//cntrPos[0] += (mySelection[0].width /2);
//cntrPos[1] -= (mySelection[0].height /2);
//offset by half width and height
mySelWidth=(lr_x-ul_x);
mySelHeight=(ul_y-lr_y);
cntrPos[0] = ul_x + (mySelWidth/2);
cntrPos[1] = ul_y - (mySelHeight/2);
//alert("ul point is "+cntrPos);
//center to screen to the object
activeDocument.views[0].centerPoint = cntrPos;
//alert("objWidth="+mySelection[0].width+", actualWidth="+ActualWidth);
//alert("objHeight="+mySelection[0].height+", actualHeight="+ActualHeight);
//set zoom for height and width
zoomFactorW = ScreenWidth/mySelWidth;
zoomFactorH = ScreenHeight/mySelHeight;
//alert("zoomFactorW = "+zoomFactorW+"\r zoomFactorH = "+zoomFactorH);
//decide which proportion is larger...
if((mySelWidth*screenProportion) >= mySelHeight){
zF = zoomFactorW;
//alert("zoomFactorW = "+zoomFactorW);
}else{
zF = zoomFactorH;
//alert("zoomFactorH = "+zoomFactorH);
}
//and scale to that proportion minus a little bit.
activeDocument.views[0].zoom = zF *.85;
}else{
//alert("Please select an object on the page.");
activeDocument.activeView.zoom=1;
}
}