MCPcopy
hub / github.com/greensock/GSAP / constructor

Method constructor

src/gsap-core.js:2322–2418  ·  view source on GitHub ↗
(targets, vars, position, skipInherit)

Source from the content-addressed store, hash-verified

2320export class Tween extends Animation {
2321
2322 constructor(targets, vars, position, skipInherit) {
2323 if (typeof(vars) === "number") {
2324 position.duration = vars;
2325 vars = position;
2326 position = null;
2327 }
2328 super(skipInherit ? vars : _inheritDefaults(vars));
2329 let { duration, delay, immediateRender, stagger, overwrite, keyframes, defaults, scrollTrigger } = this.vars,
2330 parent = vars.parent || _globalTimeline,
2331 parsedTargets = (_isArray(targets) || _isTypedArray(targets) ? _isNumber(targets[0]) : ("length" in vars)) ? [targets] : toArray(targets), // edge case: someone might try animating the "length" of an object with a "length" property that's initially set to 0 so don't interpret that as an empty Array-like object.
2332 tl, i, copy, l, p, curTarget, staggerFunc, staggerVarsToMerge;
2333 this._targets = parsedTargets.length ? _harness(parsedTargets) : _warn("GSAP target " + targets + " not found. https://gsap.com", !_config.nullTargetWarn) || [];
2334 this._ptLookup = []; //PropTween lookup. An array containing an object for each target, having keys for each tweening property
2335 this._overwrite = overwrite;
2336 if (keyframes || stagger || _isFuncOrString(duration) || _isFuncOrString(delay)) {
2337 vars = this.vars;
2338 let easeReverse = vars.easeReverse || vars.yoyoEase;
2339 tl = this.timeline = new Timeline({data: "nested", defaults: defaults || {}, targets: parent && parent.data === "nested" ? parent.vars.targets : parsedTargets}); // we need to store the targets because for staggers and keyframes, we end up creating an individual tween for each but function-based values need to know the index and the whole Array of targets.
2340 tl.kill();
2341 tl.parent = tl._dp = this;
2342 tl._start = 0;
2343 if (stagger || _isFuncOrString(duration) || _isFuncOrString(delay)) {
2344 l = parsedTargets.length;
2345 staggerFunc = stagger && distribute(stagger);
2346 if (_isObject(stagger)) { //users can pass in callbacks like onStart/onComplete in the stagger object. These should fire with each individual tween.
2347 for (p in stagger) {
2348 if (~_staggerTweenProps.indexOf(p)) {
2349 staggerVarsToMerge || (staggerVarsToMerge = {});
2350 staggerVarsToMerge[p] = stagger[p];
2351 }
2352 }
2353 }
2354 for (i = 0; i < l; i++) {
2355 copy = _copyExcluding(vars, _staggerPropsToSkip);
2356 copy.stagger = 0;
2357 easeReverse && (copy.easeReverse = easeReverse);
2358 staggerVarsToMerge && _merge(copy, staggerVarsToMerge);
2359 curTarget = parsedTargets[i];
2360 //don't just copy duration or delay because if they're a string or function, we'd end up in an infinite loop because _isFuncOrString() would evaluate as true in the child tweens, entering this loop, etc. So we parse the value straight from vars and default to 0.
2361 copy.duration = +_parseFuncOrString(duration, this, i, curTarget, parsedTargets);
2362 copy.delay = (+_parseFuncOrString(delay, this, i, curTarget, parsedTargets) || 0) - this._delay;
2363 if (!stagger && l === 1 && copy.delay) { // if someone does delay:"random(1, 5)", repeat:-1, for example, the delay shouldn't be inside the repeat.
2364 this._delay = delay = copy.delay;
2365 this._start += delay;
2366 copy.delay = 0;
2367 }
2368 tl.to(curTarget, copy, staggerFunc ? staggerFunc(i, curTarget, parsedTargets) : 0);
2369 tl._ease = _easeMap.none;
2370 }
2371 tl.duration() ? (duration = delay = 0) : (this.timeline = 0); // if the timeline's duration is 0, we don't need a timeline internally!
2372 } else if (keyframes) {
2373 _inheritDefaults(_setDefaults(tl.vars.defaults, {ease:"none"}));
2374 tl._ease = _parseEase(keyframes.ease || vars.ease || "none");
2375 let time = 0,
2376 a, kf, v;
2377 if (_isArray(keyframes)) {
2378 keyframes.forEach(frame => tl.to(parsedTargets, frame, ">"));
2379 tl.duration(); // to ensure tl._dur is cached because we tap into it for performance purposes in the render() method.

Callers

nothing calls this directly

Calls 15

renderMethod · 0.95
durationMethod · 0.80
reverseMethod · 0.80
pausedMethod · 0.80
_inheritDefaultsFunction · 0.70
_isNumberFunction · 0.70
toArrayFunction · 0.70
_harnessFunction · 0.70
_warnFunction · 0.70
_isFuncOrStringFunction · 0.70
distributeFunction · 0.70
_isObjectFunction · 0.70

Tested by

no test coverage detected