Skip to content

Commit edc9d42

Browse files
committed
Add intro/outro, initial, final, and better during animations
1 parent 5427ea5 commit edc9d42

File tree

11 files changed

+320
-33
lines changed

11 files changed

+320
-33
lines changed

README.md

Lines changed: 52 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,14 @@
88
- Node: `npm install anim8js-scrollmagic`
99
- Download: [anim8js-scrollmagic](https://raw.githubusercontent.com/anim8js/anim8js-scrollmagic/master/build/anim8js-scrollmagic.js)
1010

11+
### Index
12+
- [Examples](#examples)
13+
- [Documentation](#documentation)
14+
1115
### Examples
1216
```javascript
1317
new ScrollMagic.Scene({triggerElement: '#trigger', duration: '100%'})
1418
.addTo( controller )
15-
// Generic Transitioning (any state to AFTER scene)
16-
.transition('*', 'AFTER', function() {
17-
this.animator('#element', function() {
18-
this.queue('wiggle inf');
19-
});
20-
})
2119
// Triggered when transitioning from outside to inside scene
2220
.enter(function() {
2321
this.animator('#element', function() {
@@ -51,6 +49,12 @@ new ScrollMagic.Scene({triggerElement: '#trigger', duration: '100%'})
5149
this.play('wiggle');
5250
});
5351
})
52+
// Generic Transitioning (any state to AFTER scene)
53+
.transition('*', 'AFTER', function() {
54+
this.animator('#element', function() {
55+
this.queue('wiggle inf');
56+
});
57+
})
5458
// Multiple
5559
listen('enter before *>AFTER', function() {
5660
// Execute on enter, when we go before the scene, and when we go after the scene
@@ -61,3 +65,45 @@ new ScrollMagic.Scene({triggerElement: '#trigger', duration: '100%'})
6165
.setBackwards( true )
6266
;
6367
```
68+
69+
### Documentation
70+
71+
This plugin works by detecting events relative to the current scene, and invoking
72+
a function which builds commands to play when the event is triggered.
73+
74+
**Events (occurs when you...)**
75+
- `after`: go from inside the scene to AFTER, or if you land on the page AFTER the scene.
76+
- `fromAfter`: go from AFTER the scene to anywhere else
77+
- `before`: go from inside the scene to BEFORE or if you land on the page BEFORE the scene.
78+
- `fromBefore`: go from BEFORE the scene to anywhere else
79+
- `enter`: go from outside the scene to inside or if you land on the page in the scene
80+
- `exit`: go from inside the scene to outside
81+
- `any`: go from any state to another
82+
- `start`: land on the page
83+
- `startBefore`: land on the page before the scene
84+
- `startAfter`: land on the page after the scene
85+
- `startDuring`: land on the page inside the scene
86+
- `intro`: same as enter except it applies the initial state of the animations if the user lands on the page BEFORE the scene
87+
- `outro`: same as exit except it applies the final state of the animations if the user lands on the page AFTER the scene
88+
- `during`: are scrolling through the scene. animations are interpolated from start to finish over the duration of the scene
89+
90+
You can listen for events by using the event name as a function and passing a function, or by using the following functions:
91+
92+
- `listen`: animate on multiple events specified in a space-delimited string or array of strings
93+
- `transition`: animate when from one state to another - the events above use this function
94+
95+
**Build Commands**
96+
- `animator( subject or query, function which makes animator calls )`
97+
- `animators( subjects or query, function which makes animators calls )`
98+
- `movie( movie, function which makes movie player calls )`
99+
- `player( player, function which makes movie player calls )`
100+
101+
**Quick Animate**
102+
This allows you to add animations in a shorthand manner:
103+
- `animate( event, build, subject, subject method, subject arguments... )`
104+
105+
**Backwards**
106+
ScrollMagic works by having the user scroll down or to the right of a page. If
107+
you set a scene to backwards with `setBackwards( true )` then the BEFORE/AFTER and
108+
progress through a scene is swapped for anim8js functions. This allows you to have
109+
scroll up and scroll left pages (make sure to set the scrollbar the whole way to the bottom/right when you initialize your scenes)

bower.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "anim8js-scrollmagic",
3-
"version": "1.0.2",
3+
"version": "1.0.3",
44
"homepage": "http://anim8js.github.io/anim8js-scrollmagic",
55
"authors": [
66
"Philip Diffenderfer <pdiffenderfer@gmail.com>"

build/anim8js-scrollmagic.js

Lines changed: 130 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* anim8js-scrollmagic 1.0.2 - anim8 ScrollMagic by Philip Diffenderfer */
1+
/* anim8js-scrollmagic 1.0.3 - anim8 ScrollMagic by Philip Diffenderfer */
22
// UMD (Universal Module Definition)
33
(function (root, factory)
44
{
@@ -116,7 +116,7 @@ Scene.getInvokeCallback = function(callback)
116116
// .transition('*', 'AFTER') "enter"
117117
// .transition(null, 'DURING') "start on during"
118118
// .transition('AFTER', 'DURING') "enter from after"
119-
Scene.transition = function(expectedPrevious, expectedCurrent, getCalls)
119+
Scene.transition = function(expectedPrevious, expectedCurrent, getCalls, onStateChange)
120120
{
121121
var builder = new CallEventBuilder(getCalls);
122122
var previous = Events.INITIAL;
@@ -126,6 +126,11 @@ Scene.transition = function(expectedPrevious, expectedCurrent, getCalls)
126126
{
127127
if (previous !== current)
128128
{
129+
if (onStateChange)
130+
{
131+
onStateChange.call( this, current, previous, progress, builder );
132+
}
133+
129134
if (this.isEventMatch( previous, expectedPrevious ) &&
130135
this.isEventMatch( current, expectedCurrent ))
131136
{
@@ -192,6 +197,33 @@ Scene.startDuring = function(getCalls)
192197
return this.transition( Events.INITIAL, Events.DURING, getCalls );
193198
};
194199

200+
// Special Enter / Exit Events
201+
Scene.intro = function(getCalls)
202+
{
203+
var onStateChange = function(current, previous, progress, builder)
204+
{
205+
if ((previous === Events.INITIAL && current === this.getBefore()) || current === Events.DURING)
206+
{
207+
builder.executeInitials( current, progress );
208+
}
209+
};
210+
211+
return this.transition( Events.ANY, Events.DURING, getCalls, onStateChange );
212+
};
213+
214+
Scene.outro = function(getCalls)
215+
{
216+
var onStateChange = function(current, previous, progress, builder)
217+
{
218+
if (previous === Events.INITIAL && current === this.getAfter())
219+
{
220+
builder.executeFinals( current, progress );
221+
}
222+
};
223+
224+
return this.transition( Events.DURING, Events.ANY, getCalls, onStateChange );
225+
};
226+
195227
// Special During Event
196228
Scene.during = function(getCalls)
197229
{
@@ -311,16 +343,28 @@ Class.define( CallBuilder,
311343
init: function(getCalls)
312344
{
313345
this.calls = [];
346+
this.initials = [];
347+
this.finals = [];
314348

315349
getCalls.call( this, this );
316350
},
317351
execute: function()
318352
{
319-
var calls = this.calls;
320-
321-
for (var i = 0; i < calls.length; i++)
353+
this.executeList( this.calls, arguments );
354+
},
355+
executeInitials: function()
356+
{
357+
this.executeList( this.initials, arguments );
358+
},
359+
executeFinals: function()
360+
{
361+
this.executeList( this.finals, arguments );
362+
},
363+
executeList: function(list, args)
364+
{
365+
for (var i = 0; i < list.length; i++)
322366
{
323-
calls[ i ].apply( this, arguments );
367+
list[ i ].apply( this, args );
324368
}
325369
},
326370
call: function(callback)
@@ -329,14 +373,42 @@ Class.define( CallBuilder,
329373

330374
return this;
331375
},
332-
callWith: function(context, getCalls)
376+
initial: function(callback)
377+
{
378+
this.initials.push( callback );
379+
380+
return this;
381+
},
382+
final: function(callback)
383+
{
384+
this.finals.push( callback );
385+
386+
return this;
387+
},
388+
callWith: function(context, getCalls, getInitial, getFinal)
333389
{
334390
if (context)
335391
{
336392
this.call(function()
337393
{
338394
getCalls.apply( context, arguments );
339395
});
396+
397+
if (getInitial)
398+
{
399+
this.initial(function()
400+
{
401+
getInitial.apply( context, arguments );
402+
});
403+
}
404+
405+
if (getFinal)
406+
{
407+
this.final(function()
408+
{
409+
getFinal.apply( context, arguments );
410+
});
411+
}
340412
}
341413

342414
return this;
@@ -353,15 +425,15 @@ Class.extend( CallEventBuilder, CallBuilder,
353425
{
354426
animator: function(query, getCalls)
355427
{
356-
return this.callWith( ScrollMagic.getAnimator( query ), getCalls );
428+
return this.callWith( ScrollMagic.getAnimator( query ), getCalls, createAnimatorInitial( getCalls ), createAnimatorFinal( getCalls ) );
357429
},
358430
animators: function(query, getCalls)
359431
{
360-
return this.callWith( ScrollMagic.getAnimators( query ), getCalls );
432+
return this.callWith( ScrollMagic.getAnimators( query ), getCalls, createAnimatorInitial( getCalls ), createAnimatorFinal( getCalls ) );
361433
},
362434
player: function(player, getCalls)
363435
{
364-
return this.callWith( player, getCalls );
436+
return this.callWith( player, getCalls, createMovieInitial( getCalls ), createMovieFinal() );
365437
},
366438
movie: function(movie, getCalls)
367439
{
@@ -370,6 +442,45 @@ Class.extend( CallEventBuilder, CallBuilder,
370442
});
371443

372444

445+
function createAnimatorInitial(getCalls)
446+
{
447+
return function()
448+
{
449+
this.stop().restore();
450+
getCalls.apply( this, arguments );
451+
this.preupdate( 0 ).update( 0 ).apply().stop();
452+
};
453+
}
454+
455+
function createAnimatorFinal(getCalls)
456+
{
457+
return function()
458+
{
459+
this.stop().restore();
460+
getCalls.apply( this, arguments );
461+
this.preupdate( 0 ).update( 0 ).end().apply();
462+
};
463+
}
464+
465+
function createMovieInitial(getCalls)
466+
{
467+
return function()
468+
{
469+
getCalls.apply( this, arguments );
470+
471+
this.pause().apply( this.time, true );
472+
};
473+
}
474+
475+
function createMovieFinal()
476+
{
477+
return function()
478+
{
479+
this.end( true, true );
480+
};
481+
}
482+
483+
373484
function CallDuringBuilder(getCalls)
374485
{
375486
this.init( getCalls );
@@ -409,6 +520,15 @@ Class.extend( CallDuringBuilder, CallBuilder,
409520
var attrimator = attrimators[ i ];
410521
var prop = properties[ i ];
411522
var value = attrimator.valueAtSearch( now, animator.frame[ prop ] );
523+
if (value === false) {
524+
var last = attrimator;
525+
var lastTime = now;
526+
while (last.next) {
527+
lastTime -= last.totalTime();
528+
last = last.next;
529+
}
530+
value = last.valueAt( lastTime, animator.frame[ prop ] );
531+
}
412532
if (value !== false) {
413533
animator.updated[ prop ] = true;
414534
animator.frame[ prop ] = value;

build/anim8js-scrollmagic.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

build/anim8js-scrollmagic.min.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "anim8js-scrollmagic",
3-
"version": "1.0.2",
3+
"version": "1.0.3",
44
"description": "anim8 ScrollMagic",
55
"author": "Philip Diffenderfer",
66
"license": "MIT",

0 commit comments

Comments
 (0)