OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 | 5 |
6 // Namespace object for the utilities. | 6 // Namespace object for the utilities. |
7 function ImageUtil() {} | 7 function ImageUtil() {} |
8 | 8 |
9 // Performance trace. | 9 // Performance trace. |
10 ImageUtil.trace = (function() { | 10 ImageUtil.trace = (function() { |
11 function PerformanceTrace() { | 11 function PerformanceTrace() { |
12 this.lines_ = {}; | 12 this.lines_ = {}; |
13 this.timers_ = {}; | 13 this.timers_ = {}; |
14 this.container_ = null; | 14 this.container_ = null; |
15 } | 15 } |
16 | 16 |
17 PerformanceTrace.prototype.bindToDOM = function(container) { | 17 PerformanceTrace.prototype.bindToDOM = function(container) { |
18 this.container_ = container; | 18 this.container_ = container; |
19 }; | 19 }; |
20 | 20 |
21 PerformanceTrace.prototype.report_ = function(key, value) { | 21 PerformanceTrace.prototype.report = function(key, value) { |
22 if (!this.container_) return; | 22 if (!this.container_) return; |
23 if (!(key in this.lines_)) { | 23 if (!(key in this.lines_)) { |
24 var div = this.lines_[key] = document.createElement('div'); | 24 var div = this.lines_[key] = document.createElement('div'); |
25 this.container_.appendChild(div); | 25 this.container_.appendChild(div); |
26 } | 26 } |
27 this.lines_[key].textContent = key + ': ' + value; | 27 this.lines_[key].textContent = key + ': ' + value; |
28 }; | 28 }; |
29 | 29 |
30 PerformanceTrace.prototype.resetTimer = function(key) { | 30 PerformanceTrace.prototype.resetTimer = function(key) { |
31 this.timers_[key] = Date.now(); | 31 this.timers_[key] = Date.now(); |
32 } | 32 }; |
33 | 33 |
34 PerformanceTrace.prototype.reportTimer = function(key) { | 34 PerformanceTrace.prototype.reportTimer = function(key) { |
35 this.report_(key, (Date.now() - this.timers_[key]) + 'ms'); | 35 this.report(key, (Date.now() - this.timers_[key]) + 'ms'); |
36 }; | 36 }; |
37 | 37 |
38 return new PerformanceTrace(); | 38 return new PerformanceTrace(); |
39 })(); | 39 })(); |
40 | 40 |
41 | 41 |
42 ImageUtil.clip = function(min, value, max) { | 42 ImageUtil.clamp = function(min, value, max) { |
43 return Math.max(min, Math.min(max, value)); | 43 return Math.max(min, Math.min(max, value)); |
44 }; | 44 }; |
45 | 45 |
46 ImageUtil.between = function(min, value, max) { | 46 ImageUtil.between = function(min, value, max) { |
47 return (value - min) * (value - max) <= 0; | 47 return (value - min) * (value - max) <= 0; |
48 }; | 48 }; |
49 | 49 |
50 /** | 50 /** |
51 * Computes the function for every integer value between 0 and max and stores | 51 * Computes the function for every integer value between 0 and max and stores |
52 * the results. Rounds and clips the results to fit the [0..255] range. | 52 * the results. Rounds and clips the results to fit the [0..255] range. |
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
162 }; | 162 }; |
163 | 163 |
164 /** | 164 /** |
165 * @return {Boolean} True if the point lies inside the rectange. | 165 * @return {Boolean} True if the point lies inside the rectange. |
166 */ | 166 */ |
167 Rect.prototype.inside = function(x, y) { | 167 Rect.prototype.inside = function(x, y) { |
168 return this.left <= x && x < this.left + this.width && | 168 return this.left <= x && x < this.left + this.width && |
169 this.top <= y && y < this.top + this.height; | 169 this.top <= y && y < this.top + this.height; |
170 }; | 170 }; |
171 | 171 |
| 172 /** |
| 173 * Clamp the rectangle to the bounds by moving it. |
| 174 * Decrease the size only if necessary. |
| 175 */ |
| 176 Rect.prototype.clamp = function(bounds) { |
| 177 var rect = new Rect(this); |
| 178 |
| 179 if (rect.width > bounds.width) { |
| 180 rect.left = bounds.left; |
| 181 rect.width = bounds.width; |
| 182 } else if (rect.left < bounds.left){ |
| 183 rect.left = bounds.left; |
| 184 } else if (rect.left + rect.width > |
| 185 bounds.left + bounds.width) { |
| 186 rect.left = bounds.left + bounds.width - rect.width; |
| 187 } |
| 188 |
| 189 if (rect.height > bounds.height) { |
| 190 rect.top = bounds.top; |
| 191 rect.height = bounds.height; |
| 192 } else if (rect.top < bounds.top){ |
| 193 rect.top = bounds.top; |
| 194 } else if (rect.top + rect.height > |
| 195 bounds.top + bounds.height) { |
| 196 rect.top = bounds.top + bounds.height - rect.height; |
| 197 } |
| 198 |
| 199 return rect; |
| 200 }; |
| 201 |
172 /* | 202 /* |
173 * Useful shortcuts for drawing (static functions). | 203 * Useful shortcuts for drawing (static functions). |
174 */ | 204 */ |
175 | 205 |
176 /** | 206 /** |
177 * Draws the image in context with appropriate scaling. | 207 * Draws the image in context with appropriate scaling. |
178 */ | 208 */ |
179 Rect.drawImage = function(context, image, dstRect, srcRect) { | 209 Rect.drawImage = function(context, image, opt_dstRect, opt_srcRect) { |
| 210 opt_dstRect = opt_dstRect || new Rect(context.canvas); |
| 211 opt_srcRect = opt_srcRect || new Rect(image); |
180 context.drawImage(image, | 212 context.drawImage(image, |
181 srcRect.left, srcRect.top, srcRect.width, srcRect.height, | 213 opt_srcRect.left, opt_srcRect.top, opt_srcRect.width, opt_srcRect.height, |
182 dstRect.left, dstRect.top, dstRect.width, dstRect.height); | 214 opt_dstRect.left, opt_dstRect.top, opt_dstRect.width, opt_dstRect.height); |
183 }; | 215 }; |
184 | 216 |
185 /** | 217 /** |
186 * Strokes the rectangle. | 218 * Strokes the rectangle. |
187 */ | 219 */ |
188 Rect.stroke = function(context, rect) { | 220 Rect.stroke = function(context, rect) { |
189 context.strokeRect(rect.left, rect.top, rect.width, rect.height); | 221 context.strokeRect(rect.left, rect.top, rect.width, rect.height); |
190 }; | 222 }; |
191 | 223 |
192 /** | 224 /** |
(...skipping 23 matching lines...) Expand all Loading... |
216 }; | 248 }; |
217 | 249 |
218 /** | 250 /** |
219 * Circle class. | 251 * Circle class. |
220 */ | 252 */ |
221 | 253 |
222 function Circle(x, y, R) { | 254 function Circle(x, y, R) { |
223 this.x = x; | 255 this.x = x; |
224 this.y = y; | 256 this.y = y; |
225 this.squaredR = R * R; | 257 this.squaredR = R * R; |
226 }; | 258 } |
227 | 259 |
228 Circle.prototype.inside = function(x, y) { | 260 Circle.prototype.inside = function(x, y) { |
229 x -= this.x; | 261 x -= this.x; |
230 y -= this.y; | 262 y -= this.y; |
231 return x * x + y * y <= this.squaredR; | 263 return x * x + y * y <= this.squaredR; |
232 }; | 264 }; |
OLD | NEW |