mirror of
https://github.com/niklasvh/html2canvas.git
synced 2023-08-10 21:13:10 +03:00
37 lines
908 B
JavaScript
37 lines
908 B
JavaScript
/* @flow */
|
|
'use strict';
|
|
|
|
export const LENGTH_TYPE = {
|
|
PX: 0,
|
|
PERCENTAGE: 1
|
|
};
|
|
|
|
export type LengthType = $Values<typeof LENGTH_TYPE>;
|
|
|
|
export default class Length {
|
|
type: LengthType;
|
|
value: number;
|
|
|
|
constructor(value: string) {
|
|
this.type =
|
|
value.substr(value.length - 1) === '%' ? LENGTH_TYPE.PERCENTAGE : LENGTH_TYPE.PX;
|
|
const parsedValue = parseFloat(value);
|
|
if (__DEV__ && isNaN(parsedValue)) {
|
|
console.error(`Invalid value given for Length: "${value}"`);
|
|
}
|
|
this.value = isNaN(parsedValue) ? 0 : parsedValue;
|
|
}
|
|
|
|
isPercentage(): boolean {
|
|
return this.type === LENGTH_TYPE.PERCENTAGE;
|
|
}
|
|
|
|
getAbsoluteValue(parentLength: number): number {
|
|
return this.isPercentage() ? parentLength * (this.value / 100) : this.value;
|
|
}
|
|
|
|
static create(v): Length {
|
|
return new Length(v);
|
|
}
|
|
}
|