forked from ampproject/amphtml
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlayout-rect.js
139 lines (123 loc) · 3.54 KB
/
layout-rect.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/**
* Copyright 2015 The AMP HTML Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS-IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* The structure that combines position and size for an element. The exact
* interpretation of position and size depends on the use case.
*
* @typedef {{
* top: number,
* bottom: number,
* left: number,
* right: number,
* width: number,
* height: number
* }}
*/
let LayoutRectDef;
/**
* Creates a layout rect based on the left, top, width and height parameters
* in that order.
* @param {number} left
* @param {number} top
* @param {number} width
* @param {number} height
* @return {!LayoutRectDef}
*/
export function layoutRectLtwh(left, top, width, height) {
return {
left: left,
top: top,
width: width,
height: height,
bottom: top + height,
right: left + width
};
}
/**
* Creates a layout rect based on the DOMRect, e.g. obtained from calling
* getBoundingClientRect.
* @param {!DOMRect} rect
* @return {!LayoutRectDef}
*/
export function layoutRectFromDomRect(rect) {
return {
left: rect.left,
top: rect.top,
width: rect.width,
height: rect.height,
bottom: rect.top + rect.height,
right: rect.left + rect.width
};
}
/**
* Returns true if the specified two rects overlap by a single pixel.
* @param {!LayoutRectDef} r1
* @param {!LayoutRectDef} r2
* @return {boolean}
*/
export function layoutRectsOverlap(r1, r2) {
return (r1.top <= r2.bottom && r2.top <= r1.bottom &&
r1.left <= r2.right && r2.left <= r1.right);
}
/**
* Returns the intersection between a, b or null if there is none.
* @param {!LayoutRectDef} a
* @param {!LayoutRectDef} b
* @return {?LayoutRectDef}
*/
export function rectIntersection(a, b) {
const x0 = Math.max(a.left, b.left);
const x1 = Math.min(a.left + a.width, b.left + b.width);
if (x0 <= x1) {
const y0 = Math.max(a.top, b.top);
const y1 = Math.min(a.top + a.height, b.top + b.height);
if (y0 <= y1) {
return layoutRectLtwh(x0, y0, x1 - x0, y1 - y0);
}
}
return null;
}
/**
* Expand the layout rect using multiples of width and height.
* @param {!LayoutRectDef} rect Original rect.
* @param {number} dw Expansion in width, specified as a multiple of width.
* @param {number} dh Expansion in height, specified as a multiple of height.
* @return {!LayoutRectDef}
*/
export function expandLayoutRect(rect, dw, dh) {
return {
top: rect.top - rect.height * dh,
bottom: rect.bottom + rect.height * dh,
left: rect.left - rect.width * dw,
right: rect.right + rect.width * dw,
width: rect.width * (1 + dw * 2),
height: rect.height * (1 + dh * 2)
};
}
/**
* Moves the layout rect using dx and dy.
* @param {!LayoutRectDef} rect Original rect.
* @param {number} dx Move horizontally with this value.
* @param {number} dy Move vertically with this value.
* @return {!LayoutRectDef}
*/
export function moveLayoutRect(rect, dx, dy) {
if (dx == 0 && dy == 0) {
return rect;
}
return layoutRectLtwh(rect.left + dx, rect.top + dy,
rect.width, rect.height);
}