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
Creates a Size object with the given width and height values.
- width: Number — the width
- height: Number — the height
Parameters:
- Size
Returns:
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); // 5
Creates a Size object using the numbers in the given array as dimensions.
- array: Array
Parameters:
- Size
Returns:
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); // 240
Creates a Size object using the properties in the given object.
- object: Object
Parameters:
- Size
Returns:
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); // 20
Operators
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!
- number: Number — the number to add
Parameters:
- Size — the addition of the size and the value as a new size
Returns:
Example:
var size = new Size(5, 10);
var result = size + 20;
console.log(result); // {width: 25, height: 30}
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!
- size: Size — the size to add
Parameters:
- Size — the addition of the two sizes as a new size
Returns:
Example:
var size1 = new Size(5, 10);
var size2 = new Size(10, 20);
var result = size1 + size2;
console.log(result); // {width: 15, height: 30}
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!
- number: Number — the number to subtract
Parameters:
- Size — the subtraction of the size and the value as a new size
Returns:
Example:
var size = new Size(10, 20);
var result = size - 5;
console.log(result); // {width: 5, height: 15}
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!
- size: Size — the size to subtract
Parameters:
- Size — the subtraction of the two sizes as a new size
Returns:
Example:
var firstSize = new Size(10, 20);
var secondSize = new Size(5, 5);
var result = firstSize - secondSize;
console.log(result); // {width: 5, height: 15}
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!
- number: Number — the number to multiply by
Parameters:
- Size — the multiplication of the size and the value as a new size
Returns:
Example:
var size = new Size(10, 20);
var result = size * 2;
console.log(result); // {width: 20, height: 40}
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!
- size: Size — the size to multiply by
Parameters:
- Size — the multiplication of the two sizes as a new size
Returns:
Example:
var firstSize = new Size(5, 10);
var secondSize = new Size(4, 2);
var result = firstSize * secondSize;
console.log(result); // {width: 20, height: 20}
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!
- number: Number — the number to divide by
Parameters:
- Size — the division of the size and the value as a new size
Returns:
Example:
var size = new Size(10, 20);
var result = size / 2;
console.log(result); // {width: 5, height: 10}
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!
- size: Size — the size to divide by
Parameters:
- Size — the division of the two sizes as a new size
Returns:
Example:
var firstSize = new Size(8, 10);
var secondSize = new Size(2, 5);
var result = firstSize / secondSize;
console.log(result); // {width: 4, height: 2}
The modulo operator returns the integer remainders of dividing the size by the supplied value as a new size.
- value: Number
Parameters:
- Size — the integer remainders of dividing the size by the value as a new size
Returns:
Example:
var size = new Size(12, 6);
console.log(size % 5); // {width: 2, height: 1}
The modulo operator returns the integer remainders of dividing the size by the supplied size as a new size.
- size: Size
Parameters:
- Size — the integer remainders of dividing the sizes by each other as a new size
Returns:
Example:
var size = new Size(12, 6);
console.log(size % new Size(5, 2)); // {width: 2, height: 0}
Methods
Checks whether the width and height of the size are equal to those of the supplied size.
- size: Size — the size to compare to
Parameters:
- Boolean
Returns:
Example:
var size = new Size(5, 10);
console.log(size == new Size(5, 10)); // true
console.log(size == new Size(1, 1)); // false
console.log(size != new Size(1, 1)); // true
- String — a string representation of the size
Returns:
Tests
Checks if this size has both the width and height set to 0.
- Boolean — true if both width and height are 0, false otherwise
Returns:
Checks if the width or the height of the size are NaN.
- Boolean — true if the width or height of the size are NaN, false otherwise
Returns:
Math Functions
Math Operator Functions
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!
- number: Number — the number to add
Parameters:
- Size — the addition of the size and the value as a new size
Returns:
Example:
var size = new Size(5, 10);
var result = size + 20;
console.log(result); // {width: 25, height: 30}
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!
- size: Size — the size to add
Parameters:
- Size — the addition of the two sizes as a new size
Returns:
Example:
var size1 = new Size(5, 10);
var size2 = new Size(10, 20);
var result = size1 + size2;
console.log(result); // {width: 15, height: 30}
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!
- number: Number — the number to subtract
Parameters:
- Size — the subtraction of the size and the value as a new size
Returns:
Example:
var size = new Size(10, 20);
var result = size - 5;
console.log(result); // {width: 5, height: 15}
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!
- size: Size — the size to subtract
Parameters:
- Size — the subtraction of the two sizes as a new size
Returns:
Example:
var firstSize = new Size(10, 20);
var secondSize = new Size(5, 5);
var result = firstSize - secondSize;
console.log(result); // {width: 5, height: 15}
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!
- number: Number — the number to multiply by
Parameters:
- Size — the multiplication of the size and the value as a new size
Returns:
Example:
var size = new Size(10, 20);
var result = size * 2;
console.log(result); // {width: 20, height: 40}
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!
- size: Size — the size to multiply by
Parameters:
- Size — the multiplication of the two sizes as a new size
Returns:
Example:
var firstSize = new Size(5, 10);
var secondSize = new Size(4, 2);
var result = firstSize * secondSize;
console.log(result); // {width: 20, height: 20}
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!
- number: Number — the number to divide by
Parameters:
- Size — the division of the size and the value as a new size
Returns:
Example:
var size = new Size(10, 20);
var result = size / 2;
console.log(result); // {width: 5, height: 10}
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!
- size: Size — the size to divide by
Parameters:
- Size — the division of the two sizes as a new size
Returns:
Example:
var firstSize = new Size(8, 10);
var secondSize = new Size(2, 5);
var result = firstSize / secondSize;
console.log(result); // {width: 4, height: 2}
The modulo operator returns the integer remainders of dividing the size by the supplied value as a new size.
- value: Number
Parameters:
- Size — the integer remainders of dividing the size by the value as a new size
Returns:
Example:
var size = new Size(12, 6);
console.log(size % 5); // {width: 2, height: 1}
The modulo operator returns the integer remainders of dividing the size by the supplied size as a new size.
- size: Size
Parameters:
- Size — the integer remainders of dividing the sizes by each other as a new size
Returns:
Example:
var size = new Size(12, 6);
console.log(size % new Size(5, 2)); // {width: 2, height: 0}
Static Methods
Returns a new size object with the smallest width and height of the supplied sizes.
- Size — the newly created size object
Returns:
Example:
var size1 = new Size(10, 100);
var size2 = new Size(200, 5);
var minSize = Size.min(size1, size2);
console.log(minSize); // {width: 10, height: 5}
Example:Find the minimum of multiple sizes:
var size1 = new Size(60, 100);
var size2 = new Size(200, 5);
var size3 = new Size(250, 35);
[size1, size2, size3].reduce(Size.min) // {width: 60, height: 5}
Returns a new size object with the largest width and height of the supplied sizes.
- Size — the newly created size object
Returns:
Example:
var size1 = new Size(10, 100);
var size2 = new Size(200, 5);
var maxSize = Size.max(size1, size2);
console.log(maxSize); // {width: 200, height: 100}
Example:Find the maximum of multiple sizes:
var size1 = new Size(60, 100);
var size2 = new Size(200, 5);
var size3 = new Size(250, 35);
[size1, size2, size3].reduce(Size.max) // {width: 250, height: 100}