Size
The Size object is used to describe the size or dimensions of something, through its width and height properties.
Example: Create a size that is 10pt wide and 5pt high, and use it to define a rectangle:
var size = new Size(10, 5);
console.log(size.width); // 10
console.log(size.height); // 5
var rect = new Rectangle(new Point(20, 15), size);
console.log(rect); // { x: 20, y: 15, width: 10, height: 5 }Constructors
Size(width, height)Creates a Size object with the given width and height values.
Parameters:
width:Number— the widthheight:Number— the heightReturns:
Size
Example:Create a size that is 10pt wide and 5pt high
var size = new Size(10, 5); console.log(size.width); // 10 console.log(size.height); // 5Size(array)Creates a Size object using the numbers in the given array as dimensions.
Parameters:
array:ArrayReturns:
Size
Example:Creating a size of width: 320, height: 240 using an array of numbers:
var array = [320, 240]; var size = new Size(array); console.log(size.width); // 320 console.log(size.height); // 240Size(object)Creates a Size object using the properties in the given object.
Parameters:
object:ObjectReturns:
Size
Example:Creating a size of width: 10, height: 20 using an object literal:
var size = new Size({ width: 10, height: 20 }); console.log(size.width); // 10 console.log(size.height); // 20Size(size)Creates a Size object using the coordinates of the given Size object.
Parameters:
size:SizeReturns:
Size
Size(point)Creates a Size object using the
point.xandpoint.yvalues of the given Point object.Parameters:
point:PointReturns:
Size
Example:
var point = new Point(50, 50); var size = new Size(point); console.log(size.width); // 50 console.log(size.height); // 50
Operators
+number,+sizeReturns the addition of the supplied value to the width and height of the size as a new size. The object itself is not modified!
Parameters:
number:Number— the number to addReturns:
Size— the addition of the size and the value as a new size
Example:
Returns the addition of the width and height of the supplied size to the size as a new size. The object itself is not modified!
Parameters:
size:Size— the size to addReturns:
Size— the addition of the two sizes as a new size
Example:
-number,-sizeReturns the subtraction of the supplied value from the width and height of the size as a new size. The object itself is not modified! The object itself is not modified!
Parameters:
number:Number— the number to subtractReturns:
Size— the subtraction of the size and the value as a new size
Example:
Returns the subtraction of the width and height of the supplied size from the size as a new size. The object itself is not modified!
Parameters:
size:Size— the size to subtractReturns:
Size— the subtraction of the two sizes as a new size
Example:
*number,*sizeReturns the multiplication of the supplied value with the width and height of the size as a new size. The object itself is not modified!
Parameters:
number:Number— the number to multiply byReturns:
Size— the multiplication of the size and the value as a new size
Example:
Returns the multiplication of the width and height of the supplied size with the size as a new size. The object itself is not modified!
Parameters:
size:Size— the size to multiply byReturns:
Size— the multiplication of the two sizes as a new size
Example:
/number,/sizeReturns the division of the supplied value by the width and height of the size as a new size. The object itself is not modified!
Parameters:
number:Number— the number to divide byReturns:
Size— the division of the size and the value as a new size
Example:
Returns the division of the width and height of the supplied size by the size as a new size. The object itself is not modified!
Parameters:
size:Size— the size to divide byReturns:
Size— the division of the two sizes as a new size
Example:
%number,%sizeThe modulo operator returns the integer remainders of dividing the size by the supplied value as a new size.
Parameters:
value:NumberReturns:
Size— the integer remainders of dividing the size by the value as a new size
Example:
The modulo operator returns the integer remainders of dividing the size by the supplied size as a new size.
Parameters:
size:SizeReturns:
Size— the integer remainders of dividing the sizes by each other as a new size
Example:
Properties
widthThe width of the size
Type:
Number
heightThe height of the size
Type:
Number
Methods
set(...values)Sets the size to the passed values. Note that any sequence of parameters that is supported by the various
Size() constructors also work for calls ofset().Parameters:
values:any valueReturns:
Size
equals(size)Checks whether the width and height of the size are equal to those of the supplied size.
Parameters:
size:Size— the size to compare toReturns:
Boolean
Example:
clone()Returns a copy of the size.
Returns:
Size
toString()Returns:
String— a string representation of the size
Tests
isZero()Checks if this size has both the width and height set to 0.
Returns:
Boolean—trueif both width and height are 0,falseotherwise
isNaN()Checks if the width or the height of the size are NaN.
Returns:
Boolean—trueif the width or height of the size are NaN,falseotherwise
Math Functions
round()Returns a new size with rounded
widthandheightvalues. The object itself is not modified!Returns:
Size
Example:
ceil()Returns a new size with the nearest greater non-fractional values to the specified
widthandheightvalues. The object itself is not modified!Returns:
Size
Example:
floor()Returns a new size with the nearest smaller non-fractional values to the specified
widthandheightvalues. The object itself is not modified!Returns:
Size
Example:
abs()Returns a new size with the absolute values of the specified
widthandheightvalues. The object itself is not modified!Returns:
Size
Example:
Math Operator Functions
add(number)Returns the addition of the supplied value to the width and height of the size as a new size. The object itself is not modified!
Parameters:
number:Number— the number to addReturns:
Size— the addition of the size and the value as a new size
Example:
add(size)Returns the addition of the width and height of the supplied size to the size as a new size. The object itself is not modified!
Parameters:
size:Size— the size to addReturns:
Size— the addition of the two sizes as a new size
Example:
subtract(number)Returns the subtraction of the supplied value from the width and height of the size as a new size. The object itself is not modified! The object itself is not modified!
Parameters:
number:Number— the number to subtractReturns:
Size— the subtraction of the size and the value as a new size
Example:
subtract(size)Returns the subtraction of the width and height of the supplied size from the size as a new size. The object itself is not modified!
Parameters:
size:Size— the size to subtractReturns:
Size— the subtraction of the two sizes as a new size
Example:
multiply(number)Returns the multiplication of the supplied value with the width and height of the size as a new size. The object itself is not modified!
Parameters:
number:Number— the number to multiply byReturns:
Size— the multiplication of the size and the value as a new size
Example:
multiply(size)Returns the multiplication of the width and height of the supplied size with the size as a new size. The object itself is not modified!
Parameters:
size:Size— the size to multiply byReturns:
Size— the multiplication of the two sizes as a new size
Example:
divide(number)Returns the division of the supplied value by the width and height of the size as a new size. The object itself is not modified!
Parameters:
number:Number— the number to divide byReturns:
Size— the division of the size and the value as a new size
Example:
divide(size)Returns the division of the width and height of the supplied size by the size as a new size. The object itself is not modified!
Parameters:
size:Size— the size to divide byReturns:
Size— the division of the two sizes as a new size
Example:
modulo(value)The modulo operator returns the integer remainders of dividing the size by the supplied value as a new size.
Parameters:
value:NumberReturns:
Size— the integer remainders of dividing the size by the value as a new size
Example:
modulo(size)The modulo operator returns the integer remainders of dividing the size by the supplied size as a new size.
Parameters:
size:SizeReturns:
Size— the integer remainders of dividing the sizes by each other as a new size
Example:
Static Methods
Size.min(size1, size2)Returns a new size object with the smallest
widthandheightof the supplied sizes.Parameters:
size1:Sizesize2:SizeReturns:
Size— the newly created size object
Example:
Example:Find the minimum of multiple sizes:
Size.max(size1, size2)Returns a new size object with the largest
widthandheightof the supplied sizes.Parameters:
size1:Sizesize2:SizeReturns:
Size— the newly created size object
Example:
Example:Find the maximum of multiple sizes:
Size.random()Returns a size object with random
widthandheightvalues between0and1.Returns:
Size— the newly created size object
Example:
Was this helpful?