OLD | NEW |
| (Empty) |
1 <!DOCTYPE html> | |
2 <!-- | |
3 Copyright (c) 2014 The Chromium Authors. All rights reserved. | |
4 Use of this source code is governed by a BSD-style license that can be | |
5 found in the LICENSE file. | |
6 --> | |
7 <link rel="import" href="/tracing/base/base.html"> | |
8 <link rel="import" href="/tracing/base/math.html"> | |
9 <script> | |
10 'use strict'; | |
11 | |
12 tr.exportTo('tr.b', function() { | |
13 /** | |
14 * Tracks a 2D bounding box. | |
15 * @constructor | |
16 */ | |
17 function Rect() { | |
18 this.x = 0; | |
19 this.y = 0; | |
20 this.width = 0; | |
21 this.height = 0; | |
22 } | |
23 | |
24 Rect.fromXYWH = function(x, y, w, h) { | |
25 var rect = new Rect(); | |
26 rect.x = x; | |
27 rect.y = y; | |
28 rect.width = w; | |
29 rect.height = h; | |
30 return rect; | |
31 }; | |
32 | |
33 Rect.fromArray = function(ary) { | |
34 if (ary.length !== 4) | |
35 throw new Error('ary.length must be 4'); | |
36 var rect = new Rect(); | |
37 rect.x = ary[0]; | |
38 rect.y = ary[1]; | |
39 rect.width = ary[2]; | |
40 rect.height = ary[3]; | |
41 return rect; | |
42 }; | |
43 | |
44 Rect.prototype = { | |
45 __proto__: Object.prototype, | |
46 | |
47 get left() { | |
48 return this.x; | |
49 }, | |
50 | |
51 get top() { | |
52 return this.y; | |
53 }, | |
54 | |
55 get right() { | |
56 return this.x + this.width; | |
57 }, | |
58 | |
59 get bottom() { | |
60 return this.y + this.height; | |
61 }, | |
62 | |
63 toString: function() { | |
64 return 'Rect(' + this.x + ', ' + this.y + ', ' + | |
65 this.width + ', ' + this.height + ')'; | |
66 }, | |
67 | |
68 toArray: function() { | |
69 return [this.x, this.y, this.width, this.height]; | |
70 }, | |
71 | |
72 clone: function() { | |
73 var rect = new Rect(); | |
74 rect.x = this.x; | |
75 rect.y = this.y; | |
76 rect.width = this.width; | |
77 rect.height = this.height; | |
78 return rect; | |
79 }, | |
80 | |
81 enlarge: function(pad) { | |
82 var rect = new Rect(); | |
83 this.enlargeFast(rect, pad); | |
84 return rect; | |
85 }, | |
86 | |
87 enlargeFast: function(out, pad) { | |
88 out.x = this.x - pad; | |
89 out.y = this.y - pad; | |
90 out.width = this.width + 2 * pad; | |
91 out.height = this.height + 2 * pad; | |
92 return out; | |
93 }, | |
94 | |
95 size: function() { | |
96 return {width: this.width, height: this.height}; | |
97 }, | |
98 | |
99 scale: function(s) { | |
100 var rect = new Rect(); | |
101 this.scaleFast(rect, s); | |
102 return rect; | |
103 }, | |
104 | |
105 scaleSize: function(s) { | |
106 return Rect.fromXYWH(this.x, this.y, this.width * s, this.height * s); | |
107 }, | |
108 | |
109 scaleFast: function(out, s) { | |
110 out.x = this.x * s; | |
111 out.y = this.y * s; | |
112 out.width = this.width * s; | |
113 out.height = this.height * s; | |
114 return out; | |
115 }, | |
116 | |
117 translate: function(v) { | |
118 var rect = new Rect(); | |
119 this.translateFast(rect, v); | |
120 return rect; | |
121 }, | |
122 | |
123 translateFast: function(out, v) { | |
124 out.x = this.x + v[0]; | |
125 out.y = this.x + v[1]; | |
126 out.width = this.width; | |
127 out.height = this.height; | |
128 return out; | |
129 }, | |
130 | |
131 asUVRectInside: function(containingRect) { | |
132 var rect = new Rect(); | |
133 rect.x = (this.x - containingRect.x) / containingRect.width; | |
134 rect.y = (this.y - containingRect.y) / containingRect.height; | |
135 rect.width = this.width / containingRect.width; | |
136 rect.height = this.height / containingRect.height; | |
137 return rect; | |
138 }, | |
139 | |
140 intersects: function(that) { | |
141 var ok = true; | |
142 ok &= this.x < that.right; | |
143 ok &= this.right > that.x; | |
144 ok &= this.y < that.bottom; | |
145 ok &= this.bottom > that.y; | |
146 return ok; | |
147 }, | |
148 | |
149 equalTo: function(rect) { | |
150 return rect && | |
151 (this.x === rect.x) && | |
152 (this.y === rect.y) && | |
153 (this.width === rect.width) && | |
154 (this.height === rect.height); | |
155 } | |
156 }; | |
157 | |
158 return { | |
159 Rect, | |
160 }; | |
161 }); | |
162 </script> | |
OLD | NEW |