Skip to content

Commit

Permalink
feat: Repeater shape
Browse files Browse the repository at this point in the history
  • Loading branch information
mbasaglia committed Oct 11, 2021
1 parent ba6cdad commit 6b42c08
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/constants/index.ts
Expand Up @@ -10,3 +10,4 @@ export * from './mask-mode';
export * from './property-type';
export * from './shape-type';
export * from './trim-mode';
export * from './repeater-composite';
4 changes: 4 additions & 0 deletions src/constants/repeater-composite.ts
@@ -0,0 +1,4 @@
export enum RepeaterComposite {
ABOVE = 1,
BELOW = 2,
}
1 change: 1 addition & 0 deletions src/shapes/index.ts
Expand Up @@ -8,3 +8,4 @@ export * from './shape';
export * from './stroke-shape';
export * from './trim-shape';
export * from './gradient-stroke-shape';
export * from './repeater-shape';
110 changes: 110 additions & 0 deletions src/shapes/repeater-shape.ts
@@ -0,0 +1,110 @@
import { PropertyType, ShapeType, RepeaterComposite } from '../constants';
import { Property } from '../properties';
import { Shape } from './shape';

/**
* Repeater shape type.
*/
export class RepeaterShape extends Shape {
/**
* Repeater shape type: rp
*/
public readonly type = ShapeType.REPEATER;

public anchor: Property = new Property(this, PropertyType.ANCHOR);

public startOpacity: Property = new Property(this, PropertyType.OPACITY);

public endOpacity: Property = new Property(this, PropertyType.OPACITY);

public position: Property = new Property(this, PropertyType.POSITION);

public rotation: Property = new Property(this, PropertyType.ROTATION);

public scale: Property = new Property(this, PropertyType.SCALE);

public shapes: Shape[] = [];

public skew: Property = new Property(this, PropertyType.SKEW);

public skewAxis: Property = new Property(this, PropertyType.SKEW_AXIS);

public copies: Property = new Property(this, PropertyType.NUMBER);

public offset: Property = new Property(this, PropertyType.NUMBER);

public composition: RepeaterComposite = RepeaterComposite.ABOVE;

/**
* Convert the Lottie JSON object to class instance.
*
* @param json JSON object
* @returns GroupShape instance
*/
public fromJSON(json: Record<string, any>): RepeaterShape {
// 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.copies.fromJSON(json.c);
this.composition = json.m;
this.offset.fromJSON(json.o);

this.anchor.fromJSON(json.tr.a);
this.startOpacity.fromJSON(json.tr.so);
this.endOpacity.fromJSON(json.tr.eo);
this.position.fromJSON(json.tr.p);
this.rotation.fromJSON(json.tr.r);
this.scale.fromJSON(json.tr.s);

if (json.tr.sk) {
this.skew.fromJSON(json.tr.sk);
}

if (json.tr.sa) {
this.skewAxis.fromJSON(json.tr.sa);
}

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<string, any> {

return {
ty: this.type,

// Base shape
cl: this.classNames,
hd: this.isHidden,
ln: this.id,
mn: this.matchName,
nm: this.name,

// This shape
m: this.composition,
c: this.copies,
o: this.offset,
tr: {
a: this.anchor,
so: this.startOpacity,
eo: this.endOpacity,
p: this.position,
r: this.rotation,
s: this.scale,
sk: this.skew,
sa: this.skewAxis,
}
};
}
}

0 comments on commit 6b42c08

Please sign in to comment.