forked from epranka/scrollmagic-plugin-gsap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
315 lines (303 loc) · 12.9 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
/*!
* @file ScrollMagic GSAP Animation Plugin.
*
* requires: GSAP ~1.14
* Powered by the Greensock Animation Platform (GSAP): http://www.greensock.com/js
* Greensock License info at http://www.greensock.com/licensing/
*/
/**
* This plugin is meant to be used in conjunction with the Greensock Animation Plattform.
* It offers an easy API to trigger Tweens or synchronize them to the scrollbar movement.
*
* Both the `lite` and the `max` versions of the GSAP library are supported.
* The most basic requirement is `TweenLite`.
*
* To have access to this extension, please include `plugins/animation.gsap.js`.
* @requires {@link http://greensock.com/gsap|GSAP ~1.14.x}
* @mixin animation.GSAP
*/
var ScrollMagicPluginGsap = function(ScrollMagic, TweenMax, Timeline) {
var Tween = TweenMax;
var NAMESPACE = "animation.gsap";
// (BUILD) - REMOVE IN MINIFY - START
var console = window.console || {},
err = Function.prototype.bind.call(
console.error || console.log || function() {},
console
);
if (!ScrollMagic) {
err(
"(" +
NAMESPACE +
") -> ERROR: The ScrollMagic main module could not be found. Please make sure it's loaded before this plugin or use an asynchronous loader like requirejs."
);
}
if (!Tween) {
err(
"(" +
NAMESPACE +
") -> ERROR: TweenLite or TweenMax could not be found. Please make sure GSAP is loaded before ScrollMagic or use an asynchronous loader like requirejs."
);
}
// (BUILD) - REMOVE IN MINIFY - END
/*
* ----------------------------------------------------------------
* Extensions for Scene
* ----------------------------------------------------------------
*/
/**
* Every instance of ScrollMagic.Scene now accepts an additional option.
* See {@link ScrollMagic.Scene} for a complete list of the standard options.
* @memberof! animation.GSAP#
* @method new ScrollMagic.Scene(options)
* @example
* var scene = new ScrollMagic.Scene({tweenChanges: true});
*
* @param {object} [options] - Options for the Scene. The options can be updated at any time.
* @param {boolean} [options.tweenChanges=false] - Tweens Animation to the progress target instead of setting it.
Does not affect animations where duration is `0`.
*/
/**
* **Get** or **Set** the tweenChanges option value.
* This only affects scenes with a duration. If `tweenChanges` is `true`, the progress update when scrolling will not be immediate, but instead the animation will smoothly animate to the target state.
* For a better understanding, try enabling and disabling this option in the [Scene Manipulation Example](../examples/basic/scene_manipulation.html).
* @memberof! animation.GSAP#
* @method Scene.tweenChanges
*
* @example
* // get the current tweenChanges option
* var tweenChanges = scene.tweenChanges();
*
* // set new tweenChanges option
* scene.tweenChanges(true);
*
* @fires {@link Scene.change}, when used as setter
* @param {boolean} [newTweenChanges] - The new tweenChanges setting of the scene.
* @returns {boolean} `get` - Current tweenChanges option value.
* @returns {Scene} `set` - Parent object for chaining.
*/
// add option (TODO: DOC (private for dev))
ScrollMagic.Scene.addOption(
"tweenChanges", // name
false, // default
function(val) {
// validation callback
return !!val;
}
);
// extend scene
ScrollMagic.Scene.extend(function() {
var Scene = this,
_tween;
// (BUILD) - REMOVE IN MINIFY - START
var log = function() {
if (Scene._log) {
// not available, when main source minified
Array.prototype.splice.call(
arguments,
1,
0,
"(" + NAMESPACE + ")",
"->"
);
Scene._log.apply(this, arguments);
}
};
// (BUILD) - REMOVE IN MINIFY - END
// set listeners
Scene.on("progress.plugin_gsap", function() {
updateTweenProgress();
});
Scene.on("destroy.plugin_gsap", function(e) {
Scene.removeTween(e.reset);
});
/**
* Update the tween progress to current position.
* @private
*/
var updateTweenProgress = function() {
if (_tween) {
var progress = Scene.progress(),
state = Scene.state();
if (_tween.repeat && _tween.repeat() === -1) {
// infinite loop, so not in relation to progress
if (state === "DURING" && _tween.paused()) {
_tween.play();
} else if (state !== "DURING" && !_tween.paused()) {
_tween.pause();
}
} else if (progress != _tween.progress()) {
// do we even need to update the progress?
// no infinite loop - so should we just play or go to a specific point in time?
if (Scene.duration() === 0) {
// play the animation
if (progress > 0) {
// play from 0 to 1
_tween.play();
} else {
// play from 1 to 0
_tween.reverse();
}
} else {
// go to a specific point in time
if (Scene.tweenChanges() && _tween.tweenTo) {
// go smooth
_tween.tweenTo(progress * _tween.duration());
} else {
// just hard set it
_tween.progress(progress).pause();
}
}
}
}
};
/**
* Add a tween to the scene.
* If you want to add multiple tweens, add them into a GSAP Timeline object and supply it instead (see example below).
*
* If the scene has a duration, the tween's duration will be projected to the scroll distance of the scene, meaning its progress will be synced to scrollbar movement.
* For a scene with a duration of `0`, the tween will be triggered when scrolling forward past the scene's trigger position and reversed, when scrolling back.
* To gain better understanding, check out the [Simple Tweening example](../examples/basic/simple_tweening.html).
*
* Instead of supplying a tween this method can also be used as a shorthand for `TweenMax.to()` (see example below).
* @memberof! animation.GSAP#
*
* @example
* // add a single tween directly
* scene.setTween(TweenMax.to("obj"), 1, {x: 100});
*
* // add a single tween via variable
* var tween = TweenMax.to("obj"), 1, {x: 100};
* scene.setTween(tween);
*
* // add multiple tweens, wrapped in a timeline.
* var timeline = new TimelineMax();
* var tween1 = TweenMax.from("obj1", 1, {x: 100});
* var tween2 = TweenMax.to("obj2", 1, {y: 100});
* timeline
* .add(tween1)
* .add(tween2);
* scene.addTween(timeline);
*
* // short hand to add a TweenMax.to() tween
* scene.setTween("obj3", 0.5, {y: 100});
*
* // short hand to add a TweenMax.to() tween for 1 second
* // this is useful, when the scene has a duration and the tween duration isn't important anyway
* scene.setTween("obj3", {y: 100});
*
* @param {(object|string)} TweenObject - A TweenMax, TweenLite, TimelineMax or TimelineLite object that should be animated in the scene. Can also be a Dom Element or Selector, when using direct tween definition (see examples).
* @param {(number|object)} duration - A duration for the tween, or tween parameters. If an object containing parameters are supplied, a default duration of 1 will be used.
* @param {object} params - The parameters for the tween
* @returns {Scene} Parent object for chaining.
*/
Scene.setTween = function(TweenObject, duration, params) {
var newTween;
if (arguments.length > 1) {
if (arguments.length < 3) {
params = duration;
duration = 1;
}
TweenObject = Tween.to(TweenObject, duration, params);
}
try {
// wrap Tween into a Timeline Object if available to include delay and repeats in the duration and standardize methods.
if (Timeline) {
newTween = new Timeline({ smoothChildTiming: true }).add(
TweenObject
);
} else {
newTween = TweenObject;
}
newTween.pause();
} catch (e) {
log(
1,
"ERROR calling method 'setTween()': Supplied argument is not a valid TweenObject"
);
return Scene;
}
if (_tween) {
// kill old tween?
Scene.removeTween();
}
_tween = newTween;
// some properties need to be transferred it to the wrapper, otherwise they would get lost.
if (TweenObject.repeat && TweenObject.repeat() === -1) {
// TweenMax or TimelineMax Object?
_tween.repeat(-1);
_tween.yoyo(TweenObject.yoyo());
}
// (BUILD) - REMOVE IN MINIFY - START
// Some tween validations and debugging helpers
if (Scene.tweenChanges() && !_tween.tweenTo) {
log(
2,
"WARNING: tweenChanges will only work if the TimelineMax object is available for ScrollMagic."
);
}
// check if there are position tweens defined for the trigger and warn about it :)
if (
_tween &&
Scene.controller() &&
Scene.triggerElement() &&
Scene.loglevel() >= 2
) {
// controller is needed to know scroll direction.
var triggerTweens = Tween.getTweensOf(Scene.triggerElement()),
vertical = Scene.controller().info("vertical");
triggerTweens.forEach(function(value, index) {
var tweenvars = value.vars.css || value.vars,
condition = vertical
? tweenvars.top !== undefined ||
tweenvars.bottom !== undefined
: tweenvars.left !== undefined ||
tweenvars.right !== undefined;
if (condition) {
log(
2,
"WARNING: Tweening the position of the trigger element affects the scene timing and should be avoided!"
);
return false;
}
});
}
// (BUILD) - REMOVE IN MINIFY - END
log(3, "added tween");
updateTweenProgress();
return Scene;
};
/**
* Remove the tween from the scene.
* This will terminate the control of the Scene over the tween.
*
* Using the reset option you can decide if the tween should remain in the current state or be rewound to set the target elements back to the state they were in before the tween was added to the scene.
* @memberof! animation.GSAP#
*
* @example
* // remove the tween from the scene without resetting it
* scene.removeTween();
*
* // remove the tween from the scene and reset it to initial position
* scene.removeTween(true);
*
* @param {boolean} [reset=false] - If `true` the tween will be reset to its initial values.
* @returns {Scene} Parent object for chaining.
*/
Scene.removeTween = function(reset) {
if (_tween) {
if (reset) {
_tween.progress(0).pause();
}
_tween.kill();
_tween = undefined;
log(
3,
"removed tween (reset: " + (reset ? "true" : "false") + ")"
);
}
return Scene;
};
});
};
module.exports = { ScrollMagicPluginGsap };