Skip to content

Commit

Permalink
feat: Add constructors for animated properties and keyframes
Browse files Browse the repository at this point in the history
  • Loading branch information
mbasaglia committed Oct 11, 2021
1 parent 3c57583 commit be30ed4
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
4 changes: 3 additions & 1 deletion src/properties/property.ts
Expand Up @@ -37,10 +37,12 @@ export class Property {
* @param parent Parent instance the property belongs to.
* @param type Property type.
*/
constructor(parent: any, type: PropertyType) {
constructor(parent: any, type: PropertyType, values: Array<KeyFrame> = []) {
this._parent = parent;

this.type = type;
this.values = values;
this.isAnimated = values.length > 1;

useRegistry().set(this, parent);
}
Expand Down
12 changes: 11 additions & 1 deletion src/timeline/key-frame.ts
Expand Up @@ -8,6 +8,12 @@ export class KeyFrame {
public frameOutTangent?: [number, number];
public valueInTangent?: [number, number];
public valueOutTangent?: [number, number];
public hold = false;

public constructor(frame = 0, value: number | number[] = 0) {
this.frame = frame;
this.value = value;
}

/**
* Convert the Lottie JSON object to class instance.
Expand All @@ -32,6 +38,8 @@ export class KeyFrame {
? ['x' in json.to ? json.to.x : json.to[0], 'y' in json.to ? json.to.y : json.to[1]]
: undefined;

this.hold = 'h' in json && json.h;

return this;
}

Expand All @@ -49,7 +57,9 @@ export class KeyFrame {
s: this.value,
};

if (this.frameInTangent && this.frameOutTangent) {
if (this.hold) {
json.h = 1;
} else if (this.frameInTangent && this.frameOutTangent) {
json.i = { x: this.frameInTangent[0], y: this.frameInTangent[1] };
json.o = { x: this.frameOutTangent[0], y: this.frameOutTangent[1] };
}
Expand Down

0 comments on commit be30ed4

Please sign in to comment.