I know this is an old question, but I faced a similar problem: many lottie animations have large margins, and it is hard to place them correctly. Moreover, the space of the margins is a waste for the application (e.g., I could not put elements near the animated objects because the margins are part of the animation).
I know editing the files using Adobe After Effects and other tools is possible, but I do not want to pay for a license to remove margins for a few Lottie animations.
I investigated a bit in the specification of LottieFiles and I managed to solve this problem by calculating the bounding box for each of the frames in the animation, and once the bounding box is calculated for the whole animation, updating the Lottie animation using a pre-composition layer, which is adjusted to ensure that the part shown in the animation is the window corresponding to the bounding box. The shapes are not modified using this method, and the animation can still be edited in other software such as Adobe After Effects.
A pre-composition layer is a Lottie layer that renders an object from the assets list. The assets list contains other objects that can be self-contained animations. The key is that the pre-composition layer can be translated, scaled, etc.
I created a tool that trims the margins of a Lottie animation and allows you to download it to use in your web or application. The tool is available at https://dealfonso.github.io/simplelottieplayer/lottietrimmer.html
The tool puts the existing layers that create the animation into an asset object:
animation["assets"].push({
"id": "legacy-animation",
"layers": animation["layers"]
})
And then sets the animation to contain a new pre-composition layer that refers to the previous animation:
animation["layers"] = [
{
"ty": 0, // This is the precomposition layer type
"nm": "Precomposition Layer",
"refId": "legacy-animation", // Refers to the asset created before
"ks": {
// The next parameter translates the animation to put the top-left corner of the window
"p": { "a": 0, "k": [ -boundingBoxMinX, -boundingBoxMinY ]},
/** other settings that can be used to manipulate the animation but are not needed for our purpose and are set to the default according to the specification of the Lottie files*/
...
},
}
];
// And now adjust the size of the bounding box
animation["w"] = boundingBoxMaxX - boundingBoxMinX;
animation["h"] = boundingBoxMaxY - boundingBoxMinY;
The whole implementation is available at https://github.com/dealfonso/simplelottieplayer