diff --git a/src/constants/index.ts b/src/constants/index.ts index b09958a..f972034 100644 --- a/src/constants/index.ts +++ b/src/constants/index.ts @@ -11,3 +11,4 @@ export * from './property-type'; export * from './shape-type'; export * from './trim-mode'; export * from './repeater-composite'; +export * from './star-type'; diff --git a/src/constants/star-type.ts b/src/constants/star-type.ts new file mode 100644 index 0000000..c65e9f9 --- /dev/null +++ b/src/constants/star-type.ts @@ -0,0 +1,4 @@ +export enum StarType { + STAR = 1, + POLYGON = 2, +} diff --git a/src/shapes/index.ts b/src/shapes/index.ts index bda4a0d..48bfe76 100644 --- a/src/shapes/index.ts +++ b/src/shapes/index.ts @@ -9,3 +9,4 @@ export * from './stroke-shape'; export * from './trim-shape'; export * from './gradient-stroke-shape'; export * from './repeater-shape'; +export * from './star-shape'; diff --git a/src/shapes/star-shape.ts b/src/shapes/star-shape.ts new file mode 100644 index 0000000..f79a22c --- /dev/null +++ b/src/shapes/star-shape.ts @@ -0,0 +1,86 @@ +import { PropertyType, ShapeType, StarType } from '../constants'; +import { Property } from '../properties'; +import { Shape } from './shape'; + +/** + * Star shape type. + */ +export class StarShape extends Shape { + /** + * Shape type + */ + public readonly type = ShapeType.STAR; + + public position: Property = new Property(this, PropertyType.POSITION); + + public innerRadius: Property = new Property(this, PropertyType.NUMBER); + + public innerRoundness: Property = new Property(this, PropertyType.NUMBER); + + public outerRadius: Property = new Property(this, PropertyType.NUMBER); + + public outerRoundness: Property = new Property(this, PropertyType.NUMBER); + + public rotation: Property = new Property(this, PropertyType.ROTATION); + + public points: Property = new Property(this, PropertyType.NUMBER); + + public starType: StarType = StarType.STAR; + + /** + * Convert the Lottie JSON object to class instance. + * + * @param json JSON object + * @returns StarShape instance + */ + public fromJSON(json: Record): StarShape { + // Base shape + this.classNames = json.cl; + this.id = json.ln; + this.isHidden = json.hd; + this.matchName = json.mn; + this.name = json.nm; + + // This shape + this.position.fromJSON(json.p); + this.innerRadius.fromJSON(json.ir); + this.innerRoundness.fromJSON(json.is); + this.outerRadius.fromJSON(json.or); + this.outerRoundness.fromJSON(json.os); + this.rotation.fromJSON(json.r); + this.points.fromJSON(json.pt); + this.starType = json.sy; + + return this; + } + + /** + * Convert the class instance to Lottie JSON object. + * + * Called by Javascript when serializing object with JSON.stringify() + * + * @returns JSON object + */ + public toJSON(): Record { + return { + ty: this.type, + + // Base shape + cl: this.classNames, + hd: this.isHidden, + ln: this.id, + mn: this.matchName, + nm: this.name, + + // This shape + p: this.position, + ir: this.innerRadius, + is: this.innerRoundness, + or: this.outerRadius, + os: this.outerRoundness, + r: this.rotation, + pt: this.points, + sy: this.starType, + }; + } +}