Length

Vecta.Length

new Vecta.Length(length) Returns: Vecta.Length

Vecta length object. Used to convert a length to any other units.

NameTypeDescription
lengthstring|number

Accepts valid unit length, in numbers (px) or string, eg: 1, 1in, 1mm, 1 cm.

Examples:
//Use Vecta.Length to test if a string is a valid length string
var len = new Vecta.Length('xxx');

console.log(len.value); //null

//Convert values from inch to mm
len = new Vecta.Length('1in');

console.log(len.mm()); //25.399999293486296

Properties

.hasUnitboolean|null

NameTypeDescription
hasUnitboolean|null

Indicates if a length has units. Null if length parameter is invalid.

Examples:
var len = new Vecta.Length('10in');

len.hasUnit ? console.log('Have units') : console.log('No units'); //"Have units"

len = new Vecta.Length('10');

len.hasUnit ? console.log('Have units') : console.log('No units'); //"No units"

.lengthstring|number

NameTypeDescription
lengthstring|number

The original length parameter passed when the object is created.

Examples:
var len = new Vecta.Length('1in');

console.log(len.length); //"1in"

.resultUnitstring|null

NameTypeDescription
resultUnitstring|null

Get the resulting unit only if the unit is not pixels. Resulting units can be any of the following:

in, mm, cm

If the resulting unit is pixels, "" or an empty string will be returned.

Examples:
var len = new Vecta.Length('10in');

console.log(len.resultUnit); //"in"

len = new Vecta.Length('10px');

console.log(len.resultUnit); //"" or empty string

.unitstring|null

NameTypeDescription
unitstring|null

The length unit, can be px, in, mm, cm. Null if length parameter is invalid.

Examples:
var len = new Vecta.Length('10in');

console.log(len.unit); //"in"

.valuenumber|null

NameTypeDescription
valuenumber|null

The length value in numbers (no units), if length parameter contains valid length, null otherwise.

Examples:
var len = new Vecta.Length('10in');

console.log(len.value); //10

Methods

.cm([format]) Returns: number|string

Convert value to centimeters.

NameTypeAttributesDescription
formatstringoptional

Specifies the format of the returned value.

Returns:

Returns a number if no format is specified, and a formatted string if format is specified.

Examples:
var len = new Vecta.Length('1in');

console.log(len.cm()); //2.5399999293486295

console.log(len.cm('0.00')); //"2.54"

console.log(len.cm('0.0000 cm')); //"2.5400 cm"

.in([format]) Returns: number|string

Convert value to inches.

NameTypeAttributesDescription
formatstringoptional

Specifies the format of the returned value.

Returns:

Returns a number if no format is specified, and a formatted string if format is specified.

Examples:
var len = new Vecta.Length('10mm');

console.log(len.in()); //0.3937007983525594

console.log(len.in('0.00')); //"0.39"

console.log(len.in('0.0000 in')); //"0.3937 in"

.m([format]) Returns: number|string

Convert value to meters.

NameTypeAttributesDescription
formatstringoptional

Specifies the format of the returned value.

Returns:

Returns a number if no format is specified, and a formatted string if format is specified.

Examples:
var len = new Vecta.Length('1in');

console.log(len.m()); //0.025399999293486297

console.log(len.m('0.00')); //"0.03"

console.log(len.m('0.0000 m')); //"0.0254 m"

.mm([format]) Returns: number|string

Convert value to millimeters.

NameTypeAttributesDescription
formatstringoptional

Specifies the format of the returned value.

Returns:

Returns a number if no format is specified, and a formatted string if format is specified.

Examples:
var len = new Vecta.Length('1in');

console.log(len.mm()); //25.399999293486295

console.log(len.mm('0.00')); //"25.40"

console.log(len.mm('0.0000 mm')); //"25.4000 mm"

.pt([format]) Returns: number|string

Convert value to points.

NameTypeAttributesDescription
formatstringoptional

Specifies the format of the returned value.

Returns:

Returns a number if no format is specified, and a formatted string if format is specified.

Examples:
var len = new Vecta.Length('10mm');

console.log(len.pt()); //28.346456636594038

console.log(len.pt('0.00')); //"28.35"

console.log(len.pt('0.0000 pt')); //"28.3465 pt"

.px([format]) Returns: number|string

Convert value to pixels.

NameTypeAttributesDescription
formatstringoptional

Specifies the format of the returned value.

Returns:

Returns a number if no format is specified, and a formatted string if format is specified.

Examples:
var len = new Vecta.Length('1in');

console.log(len.px()); //96

console.log(len.px('0.00')); //"96.00"

console.log(len.px('0.0000 px')); //"96.0000 px"

.toSvgUnit() Returns: string

Convert value to respective SVG supported unit.

Returns:

Returns a formatted string.

Examples:
var len = new Vecta.Length('10px');

console.log(len.toSvgUnit()); //"10"

len = new Vecta.Length('10mm');

console.log(len.toSvgUnit()); //"10mm"

len = new Vecta.Length('10m');

console.log(len.toSvgUnit()); //"1000cm"
vecta.io Early Access