Rectangle

Rectangle shape can draw a rectangles (if width and height are different) and a square (when width and height are same.

Rectangle class extends Shape class.

Constructor

new Rectangle(strokeopt, backgroundopt, opacityopt, xopt, yopt, widthopt, heightopt)

Creates an instance of Rectangle shape.

Parameters:
NameTypeAttributesDefaultDescription
strokeStroke<optional>

A border of the shape.

backgroundstring<optional>

Color, gradient, or image background, similar to CSS. Use an empty string to remove the background.

opacitynumber<optional>
1

Opacity of the shape (0 to 1).

xnumber<optional>
0

Horizontal position from the left edge.

ynumber<optional>
0

Vertical position from the top edge.

widthnumber<optional>
100

Width of the ellipse (horizontal diameter).

heightnumber<optional>

Height of the ellipse (vertical diameter). If omitted, height will be same as width.

Classes

Rectangle

Methods

cornerArc(width, heightopt) → {Object|Rectangle}

Sets corner radius if only one argument passed, or arc width and height if two arguments or size object supplied. The arc size is applied to all corners.

This a way to create all rounded corners with arc of same or different width and height. If you want to specify corner radius for specific corners, use Rectangle#cornerRadius method: cornerRadius(...).

Parameters:
NameTypeAttributesDescription
widthnumber | Object

Width of the corner arc, or a size object (e.g., {width:300,height:300}).

heightnumber<optional>

Height of the corner arc.

Returns:

If called without parameters, current arc size is returned; otherwise this Rectangle object is returned.

Type: 
Object | Rectangle
Example
<h2>Valid ways to set arc size:</h2>
rectangle.cornerArc(radius);
rectangle.cornerArc(width,height);
rectangle.cornerArc({width:5,height:10});

cornerRadius(topLeft_or_all, topRight, bottomRight, bottomLeft) → {Object|Rectangle}

Sets or gets a corner radius for each corner separately or for all corners at once.

This method sets same width and height of the arc. If you need to set arc with different width and height, you can use Rectangle#cornerArc method: cornerArc(...).

Parameters:
NameTypeDescription
topLeft_or_allnumber | string | object | undefined

Radius for top-left corner, a string radii for all corners (e.g., "15 15 0 0"), or an object containing each corner in the same format as this method returns. If this is the only passed parameter, it will be used for all corners.

topRightnumber | undefined

Radius for top-right corner. If omitted, it defaults to topLeft corner.

bottomRightnumber | undefined

Radius for bottom-right corner. If omitted, it defaults to topRight corner.

bottomLeftnumber | undefined

Radius for bottom-left corner. If omitted, it defaults to bottomRight corner.

Returns:

If called without parameters or topLeft is undefined, current corner radius object is returned. If called with arguments, this Rectangle shape is returned for chaining.

Type: 
Object | Rectangle
Examples
<h2>3 ways to set radius for each corner separately:</h2>
rectangle.cornerRadius(10, 10, 0, 0); // as separate parameters
rectangle.cornerRadius("10 10 0 0"); // in a string (CSS style)
rectangle.cornerRadius({tl:10, tr:10, br:0, bl:0}); // In same object format as this method returns
<h2>Setting radius for all corners at once:</h2>
rectangle.cornerRadius(10);

size(width, heightopt) → {Object|Rectangle}

Sets or gets the size of the Rectangle.

Parameters:
NameTypeAttributesDescription
widthnumber | Object

Width of the Rectangle, or a size object (e.g., {width:300,height:300}).

heightnumber<optional>

Height of the Rectangle.

Returns:

If called without parameters, current size is returned; otherwise this Rectangle object is returned.

Type: 
Object | Rectangle

toObject(includeopt) → {object}

Returns a JSON object that contains this shape properties. It may be converted back to this shape.

Parameters:
NameTypeAttributesDefaultDescription
includestring<optional>
"all"

Specified what to include. "all" is default. Valid values are: "all", "stroke", "background", "points", "basic".

Returns:

Returns an object containing this shape's properties.

Type: 
object