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() { |
(...skipping 30 matching lines...) Expand all Loading... |
41 | 41 |
42 ImageUtil.clamp = 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 | |
52 * the results. Rounds and clips the results to fit the [0..255] range. | |
53 * Used to speed up pixel manipulations. | |
54 * @param {Function} func Function returning a number. | |
55 * @param {Number} max Maximum argument value (inclusive). | |
56 * @return {Array<Number>} Computed results | |
57 */ | |
58 | |
59 ImageUtil.precomputeByteFunction = function(func, max) { | |
60 var results = []; | |
61 for (var arg = 0; arg <= max; arg ++) { | |
62 results.push(Math.max(0, Math.min(0xFF, Math.round(func(arg))))); | |
63 } | |
64 return results; | |
65 } | |
66 | |
67 /** | |
68 * Rectangle class. | 51 * Rectangle class. |
69 */ | 52 */ |
70 | 53 |
71 /** | 54 /** |
72 * Rectange constructor takes 0, 1, 2 or 4 arguments. | 55 * Rectange constructor takes 0, 1, 2 or 4 arguments. |
73 * Supports following variants: | 56 * Supports following variants: |
74 * new Rect(left, top, width, height) | 57 * new Rect(left, top, width, height) |
75 * new Rect(width, height) | 58 * new Rect(width, height) |
76 * new Rect(rect) // anything with left, top, width, height properties | 59 * new Rect(rect) // anything with left, top, width, height properties |
77 * new Rect(bounds) // anything with left, top, right, bottom properties | 60 * new Rect(bounds) // anything with left, top, right, bottom properties |
(...skipping 12 matching lines...) Expand all Loading... |
90 | 73 |
91 case 2: | 74 case 2: |
92 this.left = 0; | 75 this.left = 0; |
93 this.top = 0; | 76 this.top = 0; |
94 this.width = arguments[0]; | 77 this.width = arguments[0]; |
95 this.height = arguments[1]; | 78 this.height = arguments[1]; |
96 return; | 79 return; |
97 | 80 |
98 case 1: { | 81 case 1: { |
99 var source = arguments[0]; | 82 var source = arguments[0]; |
100 if (source.hasOwnProperty('left') && source.hasOwnProperty('top')) { | 83 if ('left' in source && 'top' in source) { |
101 this.left = source.left; | 84 this.left = source.left; |
102 this.top = source.top; | 85 this.top = source.top; |
103 if (source.hasOwnProperty('right') && source.hasOwnProperty('bottom')) { | 86 if ('right' in source && 'bottom' in source) { |
104 this.width = source.right - source.left; | 87 this.width = source.right - source.left; |
105 this.height = source.bottom - source.top; | 88 this.height = source.bottom - source.top; |
106 return; | 89 return; |
107 } | 90 } |
108 } else { | 91 } else { |
109 this.left = 0; | 92 this.left = 0; |
110 this.top = 0; | 93 this.top = 0; |
111 } | 94 } |
112 if (source.hasOwnProperty('width') && source.hasOwnProperty('height')) { | 95 if ('width' in source && 'height' in source) { |
113 this.width = source.width; | 96 this.width = source.width; |
114 this.height = source.height; | 97 this.height = source.height; |
115 return; | 98 return; |
116 } | 99 } |
117 break; // Fall through to the error message. | 100 break; // Fall through to the error message. |
118 } | 101 } |
119 | 102 |
120 case 0: | 103 case 0: |
121 this.left = 0; | 104 this.left = 0; |
122 this.top = 0; | 105 this.top = 0; |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
197 } | 180 } |
198 | 181 |
199 return rect; | 182 return rect; |
200 }; | 183 }; |
201 | 184 |
202 /* | 185 /* |
203 * Useful shortcuts for drawing (static functions). | 186 * Useful shortcuts for drawing (static functions). |
204 */ | 187 */ |
205 | 188 |
206 /** | 189 /** |
207 * Draws the image in context with appropriate scaling. | 190 * Draw the image in context with appropriate scaling. |
208 */ | 191 */ |
209 Rect.drawImage = function(context, image, opt_dstRect, opt_srcRect) { | 192 Rect.drawImage = function(context, image, opt_dstRect, opt_srcRect) { |
210 opt_dstRect = opt_dstRect || new Rect(context.canvas); | 193 opt_dstRect = opt_dstRect || new Rect(context.canvas); |
211 opt_srcRect = opt_srcRect || new Rect(image); | 194 opt_srcRect = opt_srcRect || new Rect(image); |
212 context.drawImage(image, | 195 context.drawImage(image, |
213 opt_srcRect.left, opt_srcRect.top, opt_srcRect.width, opt_srcRect.height, | 196 opt_srcRect.left, opt_srcRect.top, opt_srcRect.width, opt_srcRect.height, |
214 opt_dstRect.left, opt_dstRect.top, opt_dstRect.width, opt_dstRect.height); | 197 opt_dstRect.left, opt_dstRect.top, opt_dstRect.width, opt_dstRect.height); |
215 }; | 198 }; |
216 | 199 |
217 /** | 200 /** |
218 * Strokes the rectangle. | 201 * Draw a box around the rectangle. |
219 */ | 202 */ |
220 Rect.stroke = function(context, rect) { | 203 Rect.outline = function(context, rect) { |
221 context.strokeRect(rect.left, rect.top, rect.width, rect.height); | 204 context.strokeRect( |
| 205 rect.left - 0.5, rect.top - 0.5, rect.width + 1, rect.height + 1); |
222 }; | 206 }; |
223 | 207 |
224 /** | 208 /** |
| 209 * Fill the rectangle. |
| 210 */ |
| 211 Rect.fill = function(context, rect) { |
| 212 context.fillRect(rect.left, rect.top, rect.width, rect.height); |
| 213 }; |
| 214 |
| 215 /** |
225 * Fills the space between the two rectangles. | 216 * Fills the space between the two rectangles. |
226 */ | 217 */ |
227 Rect.fillBetween= function(context, inner, outer) { | 218 Rect.fillBetween= function(context, inner, outer) { |
228 var inner_right = inner.left + inner.width; | 219 var inner_right = inner.left + inner.width; |
229 var inner_bottom = inner.top + inner.height; | 220 var inner_bottom = inner.top + inner.height; |
230 var outer_right = outer.left + outer.width; | 221 var outer_right = outer.left + outer.width; |
231 var outer_bottom = outer.top + outer.height; | 222 var outer_bottom = outer.top + outer.height; |
232 if (inner.top > outer.top) { | 223 if (inner.top > outer.top) { |
233 context.fillRect( | 224 context.fillRect( |
234 outer.left, outer.top, outer.width, inner.top - outer.top); | 225 outer.left, outer.top, outer.width, inner.top - outer.top); |
(...skipping 20 matching lines...) Expand all Loading... |
255 this.x = x; | 246 this.x = x; |
256 this.y = y; | 247 this.y = y; |
257 this.squaredR = R * R; | 248 this.squaredR = R * R; |
258 } | 249 } |
259 | 250 |
260 Circle.prototype.inside = function(x, y) { | 251 Circle.prototype.inside = function(x, y) { |
261 x -= this.x; | 252 x -= this.x; |
262 y -= this.y; | 253 y -= this.y; |
263 return x * x + y * y <= this.squaredR; | 254 return x * x + y * y <= this.squaredR; |
264 }; | 255 }; |
OLD | NEW |