Path: blob/trunk/third_party/closure/goog/math/size.js
2868 views
// Copyright 2007 The Closure Library Authors. All Rights Reserved.1//2// Licensed under the Apache License, Version 2.0 (the "License");3// you may not use this file except in compliance with the License.4// You may obtain a copy of the License at5//6// http://www.apache.org/licenses/LICENSE-2.07//8// Unless required by applicable law or agreed to in writing, software9// distributed under the License is distributed on an "AS-IS" BASIS,10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.11// See the License for the specific language governing permissions and12// limitations under the License.1314/**15* @fileoverview A utility class for representing two-dimensional sizes.16* @author [email protected] (Shawn Brenneman)17*/181920goog.provide('goog.math.Size');21222324/**25* Class for representing sizes consisting of a width and height. Undefined26* width and height support is deprecated and results in compiler warning.27* @param {number} width Width.28* @param {number} height Height.29* @struct30* @constructor31*/32goog.math.Size = function(width, height) {33/**34* Width35* @type {number}36*/37this.width = width;3839/**40* Height41* @type {number}42*/43this.height = height;44};454647/**48* Compares sizes for equality.49* @param {goog.math.Size} a A Size.50* @param {goog.math.Size} b A Size.51* @return {boolean} True iff the sizes have equal widths and equal52* heights, or if both are null.53*/54goog.math.Size.equals = function(a, b) {55if (a == b) {56return true;57}58if (!a || !b) {59return false;60}61return a.width == b.width && a.height == b.height;62};636465/**66* @return {!goog.math.Size} A new copy of the Size.67*/68goog.math.Size.prototype.clone = function() {69return new goog.math.Size(this.width, this.height);70};717273if (goog.DEBUG) {74/**75* Returns a nice string representing size.76* @return {string} In the form (50 x 73).77* @override78*/79goog.math.Size.prototype.toString = function() {80return '(' + this.width + ' x ' + this.height + ')';81};82}838485/**86* @return {number} The longer of the two dimensions in the size.87*/88goog.math.Size.prototype.getLongest = function() {89return Math.max(this.width, this.height);90};919293/**94* @return {number} The shorter of the two dimensions in the size.95*/96goog.math.Size.prototype.getShortest = function() {97return Math.min(this.width, this.height);98};99100101/**102* @return {number} The area of the size (width * height).103*/104goog.math.Size.prototype.area = function() {105return this.width * this.height;106};107108109/**110* @return {number} The perimeter of the size (width + height) * 2.111*/112goog.math.Size.prototype.perimeter = function() {113return (this.width + this.height) * 2;114};115116117/**118* @return {number} The ratio of the size's width to its height.119*/120goog.math.Size.prototype.aspectRatio = function() {121return this.width / this.height;122};123124125/**126* @return {boolean} True if the size has zero area, false if both dimensions127* are non-zero numbers.128*/129goog.math.Size.prototype.isEmpty = function() {130return !this.area();131};132133134/**135* Clamps the width and height parameters upward to integer values.136* @return {!goog.math.Size} This size with ceil'd components.137*/138goog.math.Size.prototype.ceil = function() {139this.width = Math.ceil(this.width);140this.height = Math.ceil(this.height);141return this;142};143144145/**146* @param {!goog.math.Size} target The target size.147* @return {boolean} True if this Size is the same size or smaller than the148* target size in both dimensions.149*/150goog.math.Size.prototype.fitsInside = function(target) {151return this.width <= target.width && this.height <= target.height;152};153154155/**156* Clamps the width and height parameters downward to integer values.157* @return {!goog.math.Size} This size with floored components.158*/159goog.math.Size.prototype.floor = function() {160this.width = Math.floor(this.width);161this.height = Math.floor(this.height);162return this;163};164165166/**167* Rounds the width and height parameters to integer values.168* @return {!goog.math.Size} This size with rounded components.169*/170goog.math.Size.prototype.round = function() {171this.width = Math.round(this.width);172this.height = Math.round(this.height);173return this;174};175176177/**178* Scales this size by the given scale factors. The width and height are scaled179* by {@code sx} and {@code opt_sy} respectively. If {@code opt_sy} is not180* given, then {@code sx} is used for both the width and height.181* @param {number} sx The scale factor to use for the width.182* @param {number=} opt_sy The scale factor to use for the height.183* @return {!goog.math.Size} This Size object after scaling.184*/185goog.math.Size.prototype.scale = function(sx, opt_sy) {186var sy = goog.isNumber(opt_sy) ? opt_sy : sx;187this.width *= sx;188this.height *= sy;189return this;190};191192193/**194* Uniformly scales the size to perfectly cover the dimensions of a given size.195* If the size is already larger than the target, it will be scaled down to the196* minimum size at which it still covers the entire target. The original aspect197* ratio will be preserved.198*199* This function assumes that both Sizes contain strictly positive dimensions.200* @param {!goog.math.Size} target The target size.201* @return {!goog.math.Size} This Size object, after optional scaling.202*/203goog.math.Size.prototype.scaleToCover = function(target) {204var s = this.aspectRatio() <= target.aspectRatio() ?205target.width / this.width :206target.height / this.height;207208return this.scale(s);209};210211212/**213* Uniformly scales the size to fit inside the dimensions of a given size. The214* original aspect ratio will be preserved.215*216* This function assumes that both Sizes contain strictly positive dimensions.217* @param {!goog.math.Size} target The target size.218* @return {!goog.math.Size} This Size object, after optional scaling.219*/220goog.math.Size.prototype.scaleToFit = function(target) {221var s = this.aspectRatio() > target.aspectRatio() ?222target.width / this.width :223target.height / this.height;224225return this.scale(s);226};227228229