Skip to content

Commit 6985347

Browse files
committed
chore(website): Home animation code cleaning
1 parent 06591d4 commit 6985347

File tree

1 file changed

+175
-159
lines changed

1 file changed

+175
-159
lines changed

docs/js/home.js

Lines changed: 175 additions & 159 deletions
Original file line numberDiff line numberDiff line change
@@ -1,202 +1,218 @@
1+
/* global TimelineMax, ScrollMagic, Power2, $ */
2+
'use strict';
3+
14
$(function () {
25

36
// init scroll magic
47
var controller = new ScrollMagic.Controller({
5-
globalSceneOptions: {
6-
triggerHook: 'onLeave'
8+
globalSceneOptions:{
9+
triggerHook:'onLeave'
710
}
811
});
912

1013
// get all section window
11-
var windows = $("section.window");
12-
13-
//intro
14+
var windows = $('section.window');
15+
var $anim = $('#anim');
16+
var $overlay = $('#anim-overlay');
17+
var $screens = $('.screen');
18+
var $widgets = $('.widget');
19+
var $widgetTooltips = $('.widget-intro');
20+
var $illusSync = $('.illus-sync');
21+
22+
var $animWidget1 = $anim.find('.widget-1');
23+
var $animWidget2 = $anim.find('.widget-2');
24+
var $animWidget3 = $anim.find('.widget-3');
25+
var $animWidget4 = $anim.find('.widget-4');
26+
var $animWidget5 = $anim.find('.widget-5');
27+
var $animWidget6 = $anim.find('.widget-6');
28+
var $animWidget7 = $anim.find('.widget-7');
29+
30+
// Intro
1431
var intro = new TimelineMax();
1532
intro
16-
.set(".screen", {opacity: 0})
17-
.set("#anim", {opacity: 0, scale: 0})
18-
.set("#anim img", {opacity: 0})
19-
.set("#anim-overlay", {opacity: 0})
20-
.set("#anim-overlay img", {scale: 0})
21-
.set(".widget", {opacity: 0, scale: 0})
22-
.to("#anim", 2, {opacity: 1, scale: 1, rotationX: 30, rotationZ: -6, ease:Power2.easeInOut})
23-
.to("#anim img", 1, {opacity: 1}, "-=1")
24-
.to(".widget-1", .3, {scale:1 , opacity:1 })
25-
.to(".widget-2", .3, {scale:1 , opacity:1 })
26-
.to(".widget-3", .3, {scale:1 , opacity:1 })
27-
.to(".widget-4", .3, {scale:1 , opacity:1 }, "-=.2")
28-
.to(".widget-5", .3, {scale:1 , opacity:1 }, "-=.2")
29-
.to(".widget-6", .3, {scale:1 , opacity:1 }, "-=.2")
30-
.to(".widget-7", .3, {scale:1 , opacity:1 }, "-=.5")
31-
.to("#anim-overlay", .3, {opacity: 1, ease:Power2.easeInOut }, "3.5")
32-
.to("#anim-overlay img", .4, {scale: 1, ease:Bounce.easeOut }, "3.6");
33-
34-
// an array to stock our timelines, relative to each sections
33+
.set($screens, {opacity:0})
34+
.set($anim, {opacity:0, scale:0})
35+
.set('#anim img', {opacity:0})
36+
.set($overlay, {opacity:0})
37+
.set('#anim-overlay img', {scale:0})
38+
.set($widgets, {opacity:0, scale:0})
39+
.to($anim, 2, {opacity:1, scale:1, rotationX:30, rotationZ:-6, ease:Power2.easeInOut})
40+
.to('#anim img', 1, {opacity:1}, '-=1')
41+
.to($animWidget1, 0.3, {scale:1 , opacity:1 })
42+
.to($animWidget2, 0.3, {scale:1 , opacity:1 })
43+
.to($animWidget3, 0.3, {scale:1 , opacity:1 })
44+
.to($animWidget4, 0.3, {scale:1 , opacity:1 }, '-=.2')
45+
.to($animWidget5, 0.3, {scale:1 , opacity:1 }, '-=.2')
46+
.to($animWidget6, 0.3, {scale:1 , opacity:1 }, '-=.2')
47+
.to($animWidget7, 0.3, {scale:1 , opacity:1 }, '-=.5')
48+
.to($overlay, 0.3, {opacity:1, ease:Power2.easeInOut }, '3.5')
49+
.to('#anim-overlay img', 0.4, {scale:1, ease:Bounce.easeInOut }, '3.6');
50+
51+
// An array to stock our timelines, relative to each sections
3552
var tl = [];
36-
// First Section: There is a widget
53+
// First Section
3754
tl[0] = new TimelineMax();
3855
tl[0]
39-
.to(".widget-intro", 2, {opacity: 0})
40-
.to(".widget", 8, {z: 400, opacity: 0})
41-
.to(".widget-1", 8, {y: -350 }, "-=8")
42-
.to(".widget-3", 8, {x: -200, y: -50 }, "-=8")
43-
.to(".widget-4", 8, {x: -200 }, "-=8")
44-
.to(".widget-5", 8, {x: -200, y: 50 }, "-=8")
45-
.to(".widget-6", 8, {x: -200, y: 200 }, "-=8")
46-
.to(".widget-7", 8, {y: 350 }, "-=8")
47-
.to("#anim", 8, {opacity: 1, scale: 1, rotationX: 0, rotationZ: 0, ease:Power2.easeInOut}, "-=8")
48-
.to(".illus-sync", 4, {opacity:1}, "-=4")
49-
.to('#space-overlay', 8, {backgroundColor:'#00B6BA' }, "0");
50-
51-
// First Section: There is a widget
56+
.to($widgetTooltips, 2, {opacity:0})
57+
.to($widgets, 8, {z:400, opacity:0})
58+
.to($animWidget1, 8, {y:-350 }, '-=8')
59+
.to($animWidget3, 8, {x:-200, y:-50 }, '-=8')
60+
.to($animWidget4, 8, {x:-200 }, '-=8')
61+
.to($animWidget5, 8, {x:-200, y:50 }, '-=8')
62+
.to($animWidget6, 8, {x:-200, y:200 }, '-=8')
63+
.to($animWidget7, 8, {y:350 }, '-=8')
64+
.to($anim, 8, {opacity:1, scale:1, rotationX:0, rotationZ:0, ease:Power2.easeInOut}, '-=8')
65+
.to($illusSync, 4, {opacity:1}, '-=4')
66+
.to($overlay, 8, {backgroundColor:'#00B6BA' }, '0');
67+
68+
// Second Section
5269
tl[1] = new TimelineMax();
5370
tl[1]
54-
.to(".widget", .2, {x: 0, y: 0, z: 0, scale: 0 })
55-
.to(".illus-sync", 3, {scale: 0, opacity: 0})
56-
.to("#anim", 5, {scale: .5 })
57-
.to("#anim img", 4, {opacity: 0}, "-=8")
58-
.to("#anim > .widget", 2, {opacity: 1, scale: 1})
59-
.to(".screen-1", 3, {opacity: 1})
60-
.to(".screen-1 .widget", 5, {opacity: 1, scale: 1},"-=2")
61-
.to(".screen-2", 3, {opacity: 1})
62-
.to(".screen-2 .widget", 5, {opacity: 1, scale: 1},"-=2")
63-
.to(".screen-3", 3, {opacity: 1})
64-
.to(".screen-3 .widget", 5, {opacity: 1, scale: 1},"-=2")
65-
.to('#space-overlay', 8, {backgroundColor:'#F6624E' }, "0");
66-
67-
// First Section: There is a widget
71+
.to($widgets, .2, {x:0, y:0, z:0, scale:0 })
72+
.to($illusSync, 3, {scale:0, opacity:0})
73+
.to($anim, 5, {scale:.5 })
74+
.to('#anim img', 4, {opacity:0}, '-=8')
75+
.to('#anim > .widget', 2, {opacity:1, scale:1})
76+
.to('.screen-1', 3, {opacity:1})
77+
.to('.screen-1 .widget', 5, {opacity:1, scale:1},'-=2')
78+
.to('.screen-2', 3, {opacity:1})
79+
.to('.screen-2 .widget', 5, {opacity:1, scale:1},'-=2')
80+
.to('.screen-3', 3, {opacity:1})
81+
.to('.screen-3 .widget', 5, {opacity:1, scale:1},'-=2')
82+
.to($overlay, 8, {backgroundColor:'#F6624E' }, '0');
83+
84+
// Third Section
6885
tl[2] = new TimelineMax();
6986
tl[2]
70-
.to("#anim", 10, {scale: 1, y:100, rotationX: 20, rotationZ: -6, ease:Power2.easeInOut })
71-
.to(".screen", 5, {opacity: 0}, "-=8")
72-
.to(".widget-5", 8, {z: 200})
73-
.to(".widget-5", 4, {rotationY: 180, backgroundColor:'#674492'}, "-=4")
74-
.to(".widget-5", 4, {z: 0})
75-
.to('#space-overlay', 8, {backgroundColor:'#674492' }, "0");
76-
87+
.to($anim, 10, {scale:1, y:100, rotationX:20, rotationZ:-6, ease:Power2.easeInOut })
88+
.to($screens, 5, {opacity:0}, '-=8')
89+
.to($animWidget5, 8, {z:200})
90+
.to($animWidget5, 4, {rotationY:180, backgroundColor:'#674492'}, '-=4')
91+
.to($animWidget5, 4, {z:0}, '-=2')
92+
.to($overlay, 8, {backgroundColor:'#674492' }, '0');
93+
94+
// Fourth Section
7795
tl[3] = new TimelineMax();
7896
tl[3]
79-
.to("#anim", 12, {y: -400, rotationX: 60}, "2s")
80-
.to("#anim", 4, {opacity: 0}, "5");
97+
.to($anim, 12, {y:-400, rotationX:60}, '2s')
98+
.to($anim, 4, {opacity:0}, '5');
8199

82-
// create scene for every window
100+
// Create a scene for every window
83101
var scenes = [];
84102
for (var i=0; i<windows.length; i++) {
85103

86104
scenes[i] = new ScrollMagic.Scene({
87-
triggerElement: windows[i],
88-
duration: 800
105+
triggerElement:windows[i],
106+
duration:800
89107
})
90108
.setTween(tl[i])
91109
.offset(20)
92110
.addIndicators() // add indicators (requires plugin)
93111
.addTo(controller);
94-
};
112+
}
95113

96-
scenes[0].on("start", function (event) {
114+
scenes[0].on('start', function (event) {
97115
intro.seek(20);
98116
});
99117

100-
scenes[3].on("leave", function (event) {
101-
$('body:after').addClass('hide');
118+
scenes[3].on('leave', function (event) {
119+
$('body:after').addClass('hide')
102120
});
103121

122+
});
104123

105124

106-
107-
108-
// Space Canvas
109-
110-
var canvas;
111-
var context;
112-
var screenH;
113-
var screenW;
114-
var stars = [];
115-
var fps = 30;
116-
var numStars = 300;
117-
118-
// Calculate the screen size
119-
screenH = $(window).height();
120-
screenW = $(window).width();
121-
122-
// Get the canvas
123-
canvas = $('#space');
124-
125-
// Fill out the canvas
126-
canvas.attr('height', screenH);
127-
canvas.attr('width', screenW);
128-
context = canvas[0].getContext('2d');
129-
130-
// Create all the stars
131-
for (var i = 0; i < numStars; i++) {
132-
var x = Math.round(Math.random() * screenW);
133-
var y = Math.round(Math.random() * screenH);
134-
var length = 1 + Math.random() * 2;
135-
var opacity = Math.random();
136-
137-
// Create a new star and draw
138-
var star = new Star(x, y, length, opacity);
139-
140-
// Add the the stars array
141-
stars.push(star);
142-
}
143-
setInterval(animate, 1000 / fps);
144-
145-
// Animate the canvas
146-
function animate() {
147-
context.clearRect(0, 0, screenW, screenH);
148-
$.each(stars, function() {
149-
this.draw(context);
150-
})
151-
}
152-
153-
// Star
154-
function Star(x, y, length, opacity) {
155-
this.x = parseInt(x);
156-
this.y = parseInt(y);
157-
this.length = parseInt(length);
158-
this.opacity = opacity;
125+
// Space Canvas
126+
127+
var canvas;
128+
var context;
129+
var screenH;
130+
var screenW;
131+
var stars = [];
132+
var fps = 30;
133+
var numStars = 300;
134+
135+
// Calculate the screen size
136+
screenH = $(window).height();
137+
screenW = $(window).width();
138+
139+
// Get the canvas
140+
canvas = $('#space');
141+
142+
// Fill out the canvas
143+
canvas.attr('height', screenH);
144+
canvas.attr('width', screenW);
145+
context = canvas[0].getContext('2d');
146+
147+
// Create all the stars
148+
for (var i = 0; i < numStars; i++) {
149+
var x = Math.round(Math.random() * screenW);
150+
var y = Math.round(Math.random() * screenH);
151+
var length = 1 + Math.random() * 2;
152+
var opacity = Math.random();
153+
154+
// Create a new star and draw
155+
var star = new Star(x, y, length, opacity);
156+
157+
// Add the the stars array
158+
stars.push(star);
159+
}
160+
161+
setInterval(animate, 1000 / fps);
162+
163+
// Animate the canvas
164+
function animate() {
165+
context.clearRect(0, 0, screenW, screenH);
166+
$.each(stars, function() {
167+
this.draw(context);
168+
})
169+
}
170+
171+
// Star
172+
function Star(x, y, length, opacity) {
173+
this.x = parseInt(x);
174+
this.y = parseInt(y);
175+
this.length = parseInt(length);
176+
this.opacity = opacity;
177+
this.factor = 1;
178+
this.increment = Math.random() * .03;
179+
}
180+
181+
// Draw a star
182+
Star.prototype.draw = function() {
183+
context.rotate((Math.PI * 1 / 10));
184+
185+
// Save the context
186+
context.save();
187+
188+
// move into the middle of the canvas, just to make room
189+
context.translate(this.x, this.y);
190+
191+
// Change the opacity
192+
if (this.opacity > 1) {
193+
this.factor = -1;
194+
} else if (this.opacity <= 0) {
159195
this.factor = 1;
160-
this.increment = Math.random() * .03;
196+
this.x = Math.round(Math.random() * screenW);
197+
this.y = Math.round(Math.random() * screenH);
161198
}
162199

163-
// Draw a star
164-
Star.prototype.draw = function() {
165-
context.rotate((Math.PI * 1 / 10));
166-
167-
// Save the context
168-
context.save();
169-
170-
// move into the middle of the canvas, just to make room
171-
context.translate(this.x, this.y);
200+
this.opacity += this.increment * this.factor;
172201

173-
// Change the opacity
174-
if (this.opacity > 1) {
175-
this.factor = -1;
176-
} else if (this.opacity <= 0) {
177-
this.factor = 1;
178-
this.x = Math.round(Math.random() * screenW);
179-
this.y = Math.round(Math.random() * screenH);
180-
}
181-
182-
this.opacity += this.increment * this.factor;
183-
184-
context.beginPath()
185-
for (var i = 5; i--;) {
186-
context.lineTo(0, this.length);
187-
context.translate(0, this.length);
188-
context.rotate((Math.PI * 2 / 10));
189-
context.lineTo(0, -this.length);
190-
context.translate(0, -this.length);
191-
context.rotate(-(Math.PI * 6 / 10));
192-
}
202+
context.beginPath()
203+
for (var i = 5; i--;) {
193204
context.lineTo(0, this.length);
194-
context.closePath();
195-
context.fillStyle = "rgba(200, 200, 250, " + this.opacity + ")";
196-
// context.shadowBlur = 5;
197-
// context.shadowColor = '#1D96C7';
198-
context.fill();
199-
context.restore();
205+
context.translate(0, this.length);
206+
context.rotate((Math.PI * 2 / 10));
207+
context.lineTo(0, -this.length);
208+
context.translate(0, -this.length);
209+
context.rotate(-(Math.PI * 6 / 10));
200210
}
201-
202-
});
211+
context.lineTo(0, this.length);
212+
context.closePath();
213+
context.fillStyle = 'rgba(200, 200, 250, ' + this.opacity + ')';
214+
// context.shadowBlur = 5;
215+
// context.shadowColor = '#1D96C7';
216+
context.fill();
217+
context.restore();
218+
}

0 commit comments

Comments
 (0)