Skip to content

Commit e954ac7

Browse files
committed
feat: add loadNow func
1 parent 0af802b commit e954ac7

File tree

8 files changed

+144
-96
lines changed

8 files changed

+144
-96
lines changed

build/lottie_canvas.js

Lines changed: 59 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
(function (root, factory) {
2-
if (typeof define === "function" && define.amd) {
2+
if (typeof define === 'function' && define.amd) {
33
define(function () {
44
return factory(root);
55
});
6-
} else if (typeof module === "object" && module.exports) {
6+
} else if (typeof module === 'object' && module.exports) {
77
module.exports = factory(root);
88
} else {
99
root.lottie = factory(root);
1010
root.bodymovin = root.lottie;
1111
}
1212
})(global || {}, function (global) {
13-
"use strict";
13+
'use strict';
1414
var svgNS = 'http://www.w3.org/2000/svg';
1515
var locationHref = '';
1616
var initialDefaultFrame = -999999;
@@ -4150,14 +4150,15 @@ var ImagePreloader = (function () {
41504150
path += assetData.u ? assetData.u : "";
41514151
path += assetData.p;
41524152
}
4153+
41534154
return path;
41544155
}
41554156

41564157
function createImageData(assetData) {
41574158
var path = getAssetsPath(assetData, this.assetsPath, this.path);
41584159
var img = createTag("img");
41594160
img.src = path;
4160-
4161+
41614162
var ob = {
41624163
img: img,
41634164
assetData: assetData,
@@ -8673,10 +8674,12 @@ var animationManager = (function () {
86738674
len += 1;
86748675
}
86758676

8676-
function loadAnimation(params) {
8677+
function loadAnimation(params, isLoadNow = true) {
86778678
var animItem = new AnimationItem();
86788679
setupAnimation(animItem, null);
8680+
animItem.isLoadNow = isLoadNow;
86798681
animItem.setParams(params);
8682+
86808683
return animItem;
86818684
}
86828685

@@ -8797,6 +8800,7 @@ var animationManager = (function () {
87978800
})();
87988801

87998802
const lottieApi = require('lottie-api');
8803+
88008804
var AnimationItem = function () {
88018805
this._cbs = [];
88028806
this.name = '';
@@ -8810,18 +8814,21 @@ var AnimationItem = function () {
88108814
this.playSpeed = 1;
88118815
this.playDirection = 1;
88128816
this.playCount = 0;
8817+
this.isLoadNow = true;
8818+
88138819
this.animationData = {};
88148820
this.assets = [];
8821+
this.segments = [];
88158822
this.isPaused = true;
88168823
this.autoplay = false;
88178824
this.loop = true;
8825+
this.params = null;
88188826
this.renderer = null;
8819-
this.animationID = createElementID();
88208827
this.assetsPath = '';
88218828
this.timeCompleted = 0;
88228829
this.segmentPos = 0;
8830+
this.animationID = createElementID();
88238831
this.subframeEnabled = subframeEnabled;
8824-
this.segments = [];
88258832
this._idle = true;
88268833
this._completedLoop = false;
88278834
this.projectInterface = ProjectInterface();
@@ -8831,13 +8838,10 @@ var AnimationItem = function () {
88318838
extendPrototype([BaseEvent], AnimationItem);
88328839

88338840
AnimationItem.prototype.setParams = function (params) {
8834-
if (params.context) {
8835-
this.context = params.context;
8836-
}
8841+
if (params.context) this.context = params.context;
88378842

8838-
if (params.wrapper || params.container) {
8839-
this.wrapper = params.wrapper || params.container;
8840-
}
8843+
this.wrapper = params.wrapper || params.container || params.canvas;
8844+
this.params = params;
88418845

88428846
var animType = 'canvas';
88438847
this.renderer = new CanvasRenderer(this, params.rendererSettings);
@@ -8879,6 +8883,18 @@ AnimationItem.prototype.setParams = function (params) {
88798883
}
88808884
};
88818885

8886+
AnimationItem.prototype.loadNow = function () {
8887+
try {
8888+
this.preloadImages();
8889+
this.loadSegments();
8890+
this.updaFrameModifier();
8891+
this.waitForFontsLoaded();
8892+
this.setupApi();
8893+
} catch (err) {
8894+
console.error(err);
8895+
}
8896+
};
8897+
88828898
AnimationItem.prototype.includeLayers = function (data) {
88838899
if (data.op > this.animationData.op) {
88848900
this.animationData.op = data.op;
@@ -8962,10 +8978,15 @@ AnimationItem.prototype.preloadImages = function () {
89628978
this.imagePreloader.loadAssets(this.animationData.assets, this.imagesLoaded.bind(this));
89638979
};
89648980

8965-
AnimationItem.prototype.replaceAsset = function (id, path) {
8981+
AnimationItem.prototype.replaceAsset = function (id, path, local = true) {
89668982
const data = this.animationData;
89678983
const asset = data.assets.find((a) => a.id === id);
8968-
if (asset) asset.p = path;
8984+
8985+
if (asset) {
8986+
asset.u = '';
8987+
asset.p = path;
8988+
asset.e = local;
8989+
}
89698990
};
89708991

89718992
AnimationItem.prototype.replaceText = function (target, txt) {
@@ -8986,21 +9007,15 @@ AnimationItem.prototype.configAnimation = function (animData) {
89869007
this.animationData = animData;
89879008
this.totalFrames = Math.floor(this.animationData.op - this.animationData.ip);
89889009
this.renderer.configAnimation(animData);
8989-
if (!animData.assets) {
8990-
animData.assets = [];
8991-
}
9010+
if (!animData.assets) animData.assets = [];
89929011

89939012
this.assets = this.animationData.assets;
89949013
this.frameRate = this.animationData.fr;
89959014
this.firstFrame = Math.round(this.animationData.ip);
89969015
this.frameMult = this.animationData.fr / 1000;
89979016
this.renderer.searchExtraCompositions(animData.assets);
89989017
this.trigger('config_ready');
8999-
this.preloadImages();
9000-
this.loadSegments();
9001-
this.updaFrameModifier();
9002-
this.waitForFontsLoaded();
9003-
this.setupApi();
9018+
if (this.isLoadNow) this.loadNow();
90049019
} catch (error) {
90059020
console.error(error);
90069021
this.triggerConfigError(error);
@@ -9260,12 +9275,17 @@ AnimationItem.prototype.destroy = function (name) {
92609275
if ((name && this.name != name) || !this.renderer) {
92619276
return;
92629277
}
9278+
92639279
this.renderer.destroy();
92649280
this.imagePreloader.destroy();
92659281
this.trigger('destroy');
9266-
this._cbs = null;
9282+
92679283
this.onEnterFrame = this.onLoopComplete = this.onComplete = this.onSegmentStart = this.onDestroy = null;
92689284
this.renderer = null;
9285+
this.imagePreloader = null;
9286+
this.animationData = null;
9287+
this.params = null;
9288+
this._cbs = null;
92699289
};
92709290

92719291
AnimationItem.prototype.setCurrentRawFrameAndGoto = function (value) {
@@ -11899,23 +11919,20 @@ GroupEffect.prototype.init = function (data, element) {
1189911919
subframeEnabled = flag;
1190011920
}
1190111921

11902-
function loadAnimation(params) {
11903-
if (standalone === true) {
11904-
params.animationData = JSON.parse(animationData);
11905-
}
11906-
return animationManager.loadAnimation(params);
11922+
function loadAnimation(params, isLoadNow) {
11923+
return animationManager.loadAnimation(params, isLoadNow);
1190711924
}
1190811925

1190911926
function setQuality(value) {
11910-
if (typeof value === "string") {
11927+
if (typeof value === 'string') {
1191111928
switch (value) {
11912-
case "high":
11929+
case 'high':
1191311930
defaultCurveSegments = 200;
1191411931
break;
11915-
case "medium":
11932+
case 'medium':
1191611933
defaultCurveSegments = 50;
1191711934
break;
11918-
case "low":
11935+
case 'low':
1191911936
defaultCurveSegments = 10;
1192011937
break;
1192111938
}
@@ -11934,18 +11951,18 @@ GroupEffect.prototype.init = function (data, element) {
1193411951
}
1193511952

1193611953
function installPlugin(type, plugin) {
11937-
if (type === "expressions") {
11954+
if (type === 'expressions') {
1193811955
expressionsPlugin = plugin;
1193911956
}
1194011957
}
1194111958

1194211959
function getFactory(name) {
1194311960
switch (name) {
11944-
case "propertyFactory":
11961+
case 'propertyFactory':
1194511962
return PropertyFactory;
11946-
case "shapePropertyFactory":
11963+
case 'shapePropertyFactory':
1194711964
return ShapePropertyFactory;
11948-
case "matrix":
11965+
case 'matrix':
1194911966
return Matrix;
1195011967
}
1195111968
}
@@ -11973,11 +11990,11 @@ GroupEffect.prototype.init = function (data, element) {
1197311990
lottiejs.unfreeze = animationManager.unfreeze;
1197411991
lottiejs.getRegisteredAnimations = animationManager.getRegisteredAnimations;
1197511992
lottiejs.__getFactory = getFactory;
11976-
lottiejs.version = "5.5.95";
11993+
lottiejs.version = '5.5.95';
11994+
11995+
var standalone = '__[STANDALONE]__';
11996+
var animationData = '__[ANIMATIONDATA]__';
11997+
var renderer = '';
1197711998

11978-
var standalone = "__[STANDALONE]__";
11979-
var animationData = "__[ANIMATIONDATA]__";
11980-
var renderer = "";
11981-
1198211999
return lottiejs;
1198312000
});

demo/index.js

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@ const fs = require('fs-extra');
33
const lottie = require('../build/lottie_canvas.js');
44
const { Canvas, Image } = require('canvas');
55

6-
const i = 2; // floder
7-
const j = 9; // file
6+
const i = 4; // floder
7+
const j = 4; // file
88
const num = 50;
99
const delta = 33 * 4;
1010

1111
let id = 0;
1212
let index = 0;
13-
let canvas, ani, now;
13+
let canvas, anim, now;
1414

1515
const saveType = 'image/png';
1616
const file = i == 4 ? `test/data${i}/d${j}/data.json` : `test/data${i}/d${j}.json`;
@@ -25,28 +25,42 @@ const intLottie = () => {
2525
Canvas,
2626
Image,
2727
});
28-
ani = lottie.loadAnimation({
29-
container: canvas, // the dom element that will contain the animation
30-
renderer: 'canvas',
31-
loop: false,
32-
path: path.join(__dirname, file), // the path to the animation json
33-
});
28+
29+
anim = lottie.loadAnimation(
30+
{
31+
container: canvas, // the dom element that will contain the animation
32+
renderer: 'canvas',
33+
loop: false,
34+
path: path.join(__dirname, file), // the path to the animation json
35+
},
36+
false
37+
);
38+
39+
testReplaceImage();
40+
anim.loadNow();
3441

3542
const dir = path.join(__dirname, '__output');
3643
fs.ensureDir(dir);
3744

38-
ani.goToAndStop(1, true);
45+
anim.goToAndStop(1, true);
3946
id = setInterval(renderFrame, 1000 / 15, dir);
4047
};
4148

49+
const testReplaceImage = () => {
50+
if (i === 4 && j === 4) {
51+
const img = path.join(__dirname, './test/data4/image/img.png');
52+
anim.replaceAsset('image_1', img);
53+
}
54+
};
55+
4256
const renderFrame = (dir) => {
4357
if (index >= num) {
4458
clearInterval(id);
4559
index = 0;
4660
return;
4761
}
4862

49-
ani.render(delta);
63+
anim.render(delta);
5064
now = Date.now();
5165
const buffer = canvas.toBuffer(saveType);
5266
const file = path.join(dir, `${index++}.png`);

demo/test/data4/image/img.png

14.2 KB
Loading

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "lottie-nodejs",
3-
"version": "5.5.95",
3+
"version": "5.5.97",
44
"description": "Lottie is a AE render library for Node.js",
55
"main": "./build/lottie_canvas.js",
66
"repository": {

0 commit comments

Comments
 (0)