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