Skip to content

Commit 9371f4c

Browse files
committed
Generic Transitions, Multiple Transitions, More Events
1 parent 65ae444 commit 9371f4c

File tree

9 files changed

+284
-106
lines changed

9 files changed

+284
-106
lines changed

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@
1212
```javascript
1313
new ScrollMagic.Scene({triggerElement: '#trigger', duration: '100%'})
1414
.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+
})
1521
// Triggered when transitioning from outside to inside scene
1622
.enter(function() {
1723
this.animator('#element', function() {
@@ -45,6 +51,10 @@ new ScrollMagic.Scene({triggerElement: '#trigger', duration: '100%'})
4551
this.play('wiggle');
4652
});
4753
})
54+
// Multiple
55+
listen('enter before *>AFTER', function() {
56+
// Execute on enter, when we go before the scene, and when we go after the scene
57+
})
4858
// A short cut to a single call
4959
.animate('during', 'animator', '#box', 'play', 'wiggle')
5060
// Switch the direction of the scene

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.0",
3+
"version": "1.0.1",
44
"homepage": "http://anim8js.github.io/anim8js-scrollmagic",
55
"authors": [
66
"Philip Diffenderfer <[email protected]>"

build/anim8js-scrollmagic.js

Lines changed: 135 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* anim8js-scrollmagic 1.0.0 - anim8 ScrollMagic by Philip Diffenderfer */
1+
/* anim8js-scrollmagic 1.0.1 - anim8 ScrollMagic by Philip Diffenderfer */
22
// UMD (Universal Module Definition)
33
(function (root, factory)
44
{
@@ -36,7 +36,9 @@
3636
var Events = {
3737
BEFORE: 'BEFORE',
3838
DURING: 'DURING',
39-
AFTER: 'AFTER'
39+
AFTER: 'AFTER',
40+
ANY: '*',
41+
INITIAL: null
4042
};
4143

4244
Scene.setBackwards = function(backwards)
@@ -56,10 +58,48 @@ Scene.getBefore = function()
5658
return this.backwards ? Events.AFTER : Events.BEFORE;
5759
};
5860

61+
Scene.isEventMatch = function(actual, expected)
62+
{
63+
switch (expected)
64+
{
65+
case Events.ANY:
66+
return true;
67+
case Events.BEFORE:
68+
expected = this.getBefore();
69+
break;
70+
case Events.AFTER:
71+
expected = this.getAfter();
72+
break;
73+
}
74+
75+
return actual == expected; // jshint ignore:line
76+
};
77+
5978
Scene.onProgress = function(callback)
79+
{
80+
var invokeCallback = this.getInvokeCallback( callback );
81+
82+
this.on( 'progress.anim8js', invokeCallback );
83+
84+
anim8.requestRun( invokeCallback );
85+
86+
return this;
87+
};
88+
89+
Scene.onStart = function(callback)
90+
{
91+
var invokeCallback = this.getInvokeCallback( callback );
92+
93+
anim8.requestRun( invokeCallback );
94+
95+
return this;
96+
};
97+
98+
Scene.getInvokeCallback = function(callback)
6099
{
61100
var instance = this;
62-
var invokeCallback = function()
101+
102+
return function()
63103
{
64104
var state = instance.state();
65105
var progress = instance.progress();
@@ -71,79 +111,90 @@ Scene.onProgress = function(callback)
71111

72112
callback.call( instance, state, progress );
73113
};
74-
75-
instance.on( 'progress.anim8js', invokeCallback );
76-
77-
anim8.requestRun( invokeCallback );
78-
79-
return this;
80114
};
81115

82116

83-
Scene.after = function(getCalls)
117+
// .transition('*', 'DURING') "enter"
118+
// .transition('*', 'AFTER') "enter"
119+
// .transition(null, 'DURING') "start on during"
120+
// .transition('AFTER', 'DURING') "enter from after"
121+
Scene.transition = function(expectedPrevious, expectedCurrent, getCalls)
84122
{
85123
var builder = new CallEventBuilder(getCalls);
86-
var last = null;
124+
var previous = null;
125+
var listener = expectedPrevious === Events.INITIAL ? 'onStart' : 'onProgress';
87126

88-
return this.onProgress(function(state, progress)
127+
return this[ listener ](function(current, progress)
89128
{
90-
if (state !== last) {
91-
if (state === this.getAfter()) {
92-
builder.execute( state, last );
129+
if (previous !== current)
130+
{
131+
if (this.isEventMatch( previous, expectedPrevious ) &&
132+
this.isEventMatch( current, expectedCurrent ))
133+
{
134+
builder.execute( current, progress );
93135
}
94-
last = state;
136+
137+
previous = current;
95138
}
96139
});
97140
};
98141

142+
Scene.after = function(getCalls)
143+
{
144+
return this.transition( Events.ANY, Events.AFTER, getCalls );
145+
};
146+
147+
Scene.fromAfter = function(getCalls)
148+
{
149+
return this.transition( Events.AFTER, Events.ANY, getCalls );
150+
};
151+
99152
Scene.before = function(getCalls)
100153
{
101-
var builder = new CallEventBuilder(getCalls);
102-
var last = null;
154+
return this.transition( Events.ANY, Events.BEFORE, getCalls );
155+
};
103156

104-
return this.onProgress(function(state, progress)
105-
{
106-
if (state !== last) {
107-
if (state === this.getBefore()) {
108-
builder.execute( state, last );
109-
}
110-
last = state;
111-
}
112-
});
157+
Scene.fromBefore = function(getCalls)
158+
{
159+
return this.transition( Events.BEFORE, Events.ANY, getCalls );
113160
};
114161

115162
Scene.enter = function(getCalls)
116163
{
117-
var builder = new CallEventBuilder(getCalls);
118-
var last = null;
119-
120-
return this.onProgress(function(state, progress)
121-
{
122-
if (state !== last) {
123-
if (state === Events.DURING) {
124-
builder.execute( state, last );
125-
}
126-
last = state;
127-
}
128-
});
164+
return this.transition( Events.ANY, Events.DURING, getCalls );
129165
};
130166

131167
Scene.exit = function(getCalls)
132168
{
133-
var builder = new CallEventBuilder(getCalls);
134-
var last = null;
169+
return this.transition( Events.DURING, Events.ANY, getCalls );
170+
};
135171

136-
return this.onProgress(function(state, progress)
137-
{
138-
if (state !== last) {
139-
if (last && state !== Events.DURING) {
140-
builder.execute( state, last );
141-
}
142-
last = state;
143-
}
144-
});
172+
Scene.any = function(getCalls)
173+
{
174+
return this.transition( Events.ANY, Events.ANY, getCalls );
175+
};
176+
177+
Scene.start = function(getCalls)
178+
{
179+
return this.transition( Events.INITIAL, Events.ANY, getCalls );
180+
};
181+
182+
Scene.startBefore = function(getCalls)
183+
{
184+
return this.transition( Events.INITIAL, Events.BEFORE, getCalls );
185+
};
186+
187+
Scene.startAfter = function(getCalls)
188+
{
189+
return this.transition( Events.INITIAL, Events.AFTER, getCalls );
190+
};
191+
192+
Scene.startDuring = function(getCalls)
193+
{
194+
return this.transition( Events.INITIAL, Events.DURING, getCalls );
145195
};
146196

197+
// Special During Event
147198
Scene.during = function(getCalls)
148199
{
149200
var builder = new CallDuringBuilder(getCalls);
@@ -156,6 +207,39 @@ Scene.during = function(getCalls)
156207
});
157208
};
158209

210+
// Multiple Events
211+
Scene.REGEX_SPLIT = /\s+/g;
212+
Scene.REGEX_TRANSITION = /(|DURING|AFTER|\*)>(|DURING|AFTER|\*)/i;
213+
214+
Scene.listen = function(events, getCalls)
215+
{
216+
if (events.split)
217+
{
218+
events = events.split( this.REGEX_SPLIT );
219+
}
220+
221+
for (var i = 0; i < events.length; i++)
222+
{
223+
var eventMethod = events[ i ];
224+
225+
if (eventMethod in this)
226+
{
227+
this[ eventMethod ]( getCalls );
228+
}
229+
else
230+
{
231+
var matches = this.REGEX_TRANSITION.exec( eventMethod.toUpperCase() );
232+
233+
if (matches)
234+
{
235+
this.transition( matches[1], matches[2], getCalls );
236+
}
237+
}
238+
}
239+
240+
return this;
241+
};
242+
159243

160244
Scene.animate = function(type, target, subject, method, a1, a2, a3, a4)
161245
{
@@ -230,7 +314,7 @@ Class.define( CallBuilder,
230314
{
231315
this.calls = [];
232316

233-
getCalls.call( this );
317+
getCalls.call( this, this );
234318
},
235319
execute: function()
236320
{

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.

0 commit comments

Comments
 (0)