Path: blob/trunk/third_party/closure/goog/math/size.js
4527 views
/**1* @license2* Copyright The Closure Library Authors.3* SPDX-License-Identifier: Apache-2.04*/56/**7* @fileoverview A utility class for representing two-dimensional sizes.8*/91011goog.provide('goog.math.Size');12131415/**16* Class for representing sizes consisting of a width and height. Undefined17* width and height support is deprecated and results in compiler warning.18* @param {number} width Width.19* @param {number} height Height.20* @struct21* @constructor22*/23goog.math.Size = function(width, height) {24'use strict';25/**26* Width27* @type {number}28*/29this.width = width;3031/**32* Height33* @type {number}34*/35this.height = height;36};373839/**40* Compares sizes for equality.41* @param {goog.math.Size} a A Size.42* @param {goog.math.Size} b A Size.43* @return {boolean} True iff the sizes have equal widths and equal44* heights, or if both are null.45*/46goog.math.Size.equals = function(a, b) {47'use strict';48if (a == b) {49return true;50}51if (!a || !b) {52return false;53}54return a.width == b.width && a.height == b.height;55};565758/**59* @return {!goog.math.Size} A new copy of the Size.60*/61goog.math.Size.prototype.clone = function() {62'use strict';63return new goog.math.Size(this.width, this.height);64};656667if (goog.DEBUG) {68/**69* Returns a nice string representing size.70* @return {string} In the form (50 x 73).71* @override72*/73goog.math.Size.prototype.toString = function() {74'use strict';75return '(' + this.width + ' x ' + this.height + ')';76};77}787980/**81* @return {number} The longer of the two dimensions in the size.82*/83goog.math.Size.prototype.getLongest = function() {84'use strict';85return Math.max(this.width, this.height);86};878889/**90* @return {number} The shorter of the two dimensions in the size.91*/92goog.math.Size.prototype.getShortest = function() {93'use strict';94return Math.min(this.width, this.height);95};969798/**99* @return {number} The area of the size (width * height).100*/101goog.math.Size.prototype.area = function() {102'use strict';103return this.width * this.height;104};105106107/**108* @return {number} The perimeter of the size (width + height) * 2.109*/110goog.math.Size.prototype.perimeter = function() {111'use strict';112return (this.width + this.height) * 2;113};114115116/**117* @return {number} The ratio of the size's width to its height.118*/119goog.math.Size.prototype.aspectRatio = function() {120'use strict';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() {130'use strict';131return !this.area();132};133134135/**136* Clamps the width and height parameters upward to integer values.137* @return {!goog.math.Size} This size with ceil'd components.138*/139goog.math.Size.prototype.ceil = function() {140'use strict';141this.width = Math.ceil(this.width);142this.height = Math.ceil(this.height);143return this;144};145146147/**148* @param {!goog.math.Size} target The target size.149* @return {boolean} True if this Size is the same size or smaller than the150* target size in both dimensions.151*/152goog.math.Size.prototype.fitsInside = function(target) {153'use strict';154return this.width <= target.width && this.height <= target.height;155};156157158/**159* Clamps the width and height parameters downward to integer values.160* @return {!goog.math.Size} This size with floored components.161*/162goog.math.Size.prototype.floor = function() {163'use strict';164this.width = Math.floor(this.width);165this.height = Math.floor(this.height);166return this;167};168169170/**171* Rounds the width and height parameters to integer values.172* @return {!goog.math.Size} This size with rounded components.173*/174goog.math.Size.prototype.round = function() {175'use strict';176this.width = Math.round(this.width);177this.height = Math.round(this.height);178return this;179};180181182/**183* Scales this size by the given scale factors. The width and height are scaled184* by `sx` and `opt_sy` respectively. If `opt_sy` is not185* given, then `sx` is used for both the width and height.186* @param {number} sx The scale factor to use for the width.187* @param {number=} opt_sy The scale factor to use for the height.188* @return {!goog.math.Size} This Size object after scaling.189*/190goog.math.Size.prototype.scale = function(sx, opt_sy) {191'use strict';192const sy = (typeof opt_sy === 'number') ? opt_sy : sx;193this.width *= sx;194this.height *= sy;195return this;196};197198199/**200* Uniformly scales the size to perfectly cover the dimensions of a given size.201* If the size is already larger than the target, it will be scaled down to the202* minimum size at which it still covers the entire target. The original aspect203* ratio will be preserved.204*205* This function assumes that both Sizes contain strictly positive dimensions.206* @param {!goog.math.Size} target The target size.207* @return {!goog.math.Size} This Size object, after optional scaling.208*/209goog.math.Size.prototype.scaleToCover = function(target) {210'use strict';211const s = this.aspectRatio() <= target.aspectRatio() ?212target.width / this.width :213target.height / this.height;214215return this.scale(s);216};217218219/**220* Uniformly scales the size to fit inside the dimensions of a given size. The221* original aspect ratio will be preserved.222*223* This function assumes that both Sizes contain strictly positive dimensions.224* @param {!goog.math.Size} target The target size.225* @return {!goog.math.Size} This Size object, after optional scaling.226*/227goog.math.Size.prototype.scaleToFit = function(target) {228'use strict';229const s = this.aspectRatio() > target.aspectRatio() ?230target.width / this.width :231target.height / this.height;232233return this.scale(s);234};235236237