-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathbounds.js
112 lines (98 loc) · 5.36 KB
/
bounds.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
(function () {
var VERSION = 1.1;
var MODULE_PATH = "bounds";
var thisModule = Sky.getUtil(MODULE_PATH);
if( thisModule && thisModule.version >= VERSION) {
return;
};
//--------------------------
// Start bounds class
function moduleClass() {
var bounds = this;
bounds.version = VERSION;
bounds.description = "Some utils for transforming InDesign bounds arrays.";
bounds.getInfo = function( boundsArr ){
// This functions receives boundsArr (y1, x1, y2, x2)
// and returns an object with boundsArr and info as below
var topLeftY = boundsArr[0];
var topLeftX = boundsArr[1];
var botRightY = boundsArr[2];
var botRightX = boundsArr[3];
var height = Math.abs(botRightY - topLeftY);
var width = Math.abs(botRightX - topLeftX);
var halfWidth = 0;
var halfHeight = 0;
if(width > 0) {
halfWidth = width/2;
};
if(height > 0) {
halfHeight = height/2;
};
return { bounds : boundsArr,
height : height,
width : width,
topLeft : {x: topLeftX , y: topLeftY } ,
topCenter : {x: topLeftX + halfWidth , y: topLeftY } ,
topRight : {x: botRightX , y: topLeftY } ,
midLeft : {x: topLeftX , y: topLeftY + halfHeight } ,
midCenter : {x: topLeftX + halfWidth , y: topLeftY + halfHeight } ,
midRight : {x: botRightX , y: topLeftY + halfHeight } ,
botLeft : {x: topLeftX , y: botRightY } ,
botCenter : {x: topLeftX + halfWidth , y: botRightY } ,
botRight : {x: botRightX , y: botRightY } };
};
bounds.getRelativeOfset = function( boundsArr, relativeToBounds ){
// BEWARE: This function expects both bounds to be in the same
// X-Y coordinate space, and use the same measure unit!
//
// X--------X--------X
// | | | | |
// |--X---X---X------|
// | | | | |
// |--X---X---X------|
// X | | x | X
// |--X---X---X------|
// | | | | |
// | | | | |
// | | | | |
// X--------X--------X
//
// Bounds Info
var relBounds = bounds.getInfo(relativeToBounds);
var itemBounds = bounds.getInfo(boundsArr);
return { bounds : itemBounds.bounds,
height : itemBounds.height,
width : itemBounds.width,
topLeft : { x: itemBounds.topLeft.x - relBounds.topLeft.x , y: itemBounds.topLeft.y - relBounds.topLeft.y } ,
topCenter : { x: itemBounds.topCenter.x - relBounds.topCenter.x , y: itemBounds.topCenter.y - relBounds.topCenter.y } ,
topRight : { x: itemBounds.topRight.x - relBounds.topRight.x , y: itemBounds.topRight.y - relBounds.topRight.y } ,
midLeft : { x: itemBounds.midLeft.x - relBounds.midLeft.x , y: itemBounds.midLeft.y - relBounds.midLeft.y } ,
midCenter : { x: itemBounds.midCenter.x - relBounds.midCenter.x , y: itemBounds.midCenter.y - relBounds.midCenter.y } ,
midRight : { x: itemBounds.midRight.x - relBounds.midRight.x , y: itemBounds.midRight.y - relBounds.midRight.y } ,
botLeft : { x: itemBounds.botLeft.x - relBounds.botLeft.x , y: itemBounds.botLeft.y - relBounds.botLeft.y } ,
botCenter : { x: itemBounds.botCenter.x - relBounds.botCenter.x , y: itemBounds.botCenter.y - relBounds.botCenter.y } ,
botRight : { x: itemBounds.botRight.x - relBounds.botRight.x , y: itemBounds.botRight.y - relBounds.botRight.y } };
};
bounds.offset = function( boundsArr, offset ) {
// Param boundsArr: Array in format [y1, x1, y2, x2]
// Param offset: Array or Number (Optional) x_y or [x, y]
if (offset === undefined) offset = [0,0];
if (typeof offset === "number") offset = [offset,offset];
var updatedBounds = [0,0,0,0];
updatedBounds[0] = boundsArr[0] + offset[1];
updatedBounds[1] = boundsArr[1] + offset[0];
updatedBounds[2] = boundsArr[2] + offset[1];
updatedBounds[3] = boundsArr[3] + offset[0];
return updatedBounds;
};
bounds.normalise = function( boundsArr, offset ) {
// Param boundsArr: Array in format [y1, x1, y2, x2]
// Param offset: Array or Number (Optional) x_y or [x, y]
var normalBounds = [0, 0, boundsArr[2]-boundsArr[0], boundsArr[3]-boundsArr[1]];
return bounds.offset( normalBounds, offset);
};
};
//--------------------------
// End bounds class
Sky.setUtil(MODULE_PATH, new moduleClass() );
})();