-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClock.js
106 lines (105 loc) · 4.48 KB
/
Clock.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
/*all credit to Animated Creativity on Code Pen*/
(function() {
var animation = {
newYear: document.querySelector(".new-year"),
range: function(min,max) {
return Math.floor(Math.random() * (max - min + 1) + min);
},
get period() {
/*change timer here*/
var dateFuture = new Date(new Date().getFullYear(), 1, 27);
var dateNow = new Date();
var seconds = Math.floor((dateFuture - (dateNow))/1000);
var minutes = Math.floor(seconds/60);
var hours = Math.floor(minutes/60);
var days = Math.floor(hours/24);
hours = hours-(days*24);
minutes = minutes-(days*24*60)-(hours*60);
seconds = seconds-(days*24*60*60)-(hours*60*60)-(minutes*60);
return {
year: new Date().getFullYear() ,
days: days,
hours: hours,
minutes: minutes,
seconds: seconds
}
},
element: function(parent, type, className, html) {
var element = document.createElement(type);
element.className = className;
if (typeof html !== "undefined") element.innerHTML = html;
parent.appendChild(element);
return element;
},
year: function(className) {
var timeline = new TimelineMax();
var year = animation.element(animation.newYear, "div", className);
for (var i=0; i<=String(animation.period.year).length-1; i++) {
var digit = animation.element(year, "div", "digit", String(animation.period.year).substr(i, 1));
digit.style.top = (0 - (digit.clientHeight * 2)) + "px";
timeline
.to(digit, 0.5, {top: 0, opacity: 1, ease: Bounce.easeOut});
}
return year;
},
animate: function() {
var year1 = animation.year("year year1");
var year2 = animation.year("year year2");
var controls = animation.element(animation.newYear, "div", "controls");
var days = animation.element(controls, "div", "control days");
var hours = animation.element(controls, "div", "control hours");
var minutes = animation.element(controls, "div", "control minutes");
var seconds = animation.element(controls, "div", "control seconds");
animation.controls = {
controls: controls,
days: days,
hours: hours,
minutes: minutes,
seconds: seconds
};
animation.render();
var triangles = animation.element(year1, "div", "triangles");
var fullTimeline = new TimelineMax();
var triangleStorage = [];
for (var i=0; i<=50-1; i++) {
var timeline = new TimelineMax({repeat: -1});
var triangle = animation.element(triangles, "div", "triangle");
triangle.style.top = -50 + "px";
var time = animation.range(0, 100) / 100;
var duration = 1;
var direction = animation.range(1, 2) == 1 ? -1 : 1;
triangleStorage.push(triangle);
}
var previousWidth = 0;
var checkWidth = function() {
if (Math.abs(previousWidth - year1.clientWidth) > 1) {
for (var i=0; i<=triangleStorage.length-1; i++) {
triangleStorage[i].style.left = (-5 + animation.range(0, year1.clientWidth)) + "px";
}
previousWidth = year1.clientWidth;
}
setTimeout(checkWidth, 100);
}
checkWidth();
return new TimelineMax()
.to(days, 0.5, {top: 0, opacity: 1}, 0)
.to(hours, 0.5, {top: 0, opacity: 1}, 0.25)
.to(minutes, 0.5, {top: 0, opacity: 1}, 0.5)
.to(seconds, 0.5, {top: 0, opacity: 1}, 0.75)
.add(fullTimeline, 3);
},
plural: function(property) {
var period = animation.period;
if (String(period[property]).length <= 1) period[property] = "0" + period[property];
return Number(period[property]) > 1 ? period[property] + " " + property : period[property] + " " + property.substr(0, property.length-1);
},
render: function() {
animation.controls.seconds.innerHTML = animation.plural("seconds");
animation.controls.minutes.innerHTML = animation.plural("minutes");
animation.controls.hours.innerHTML = animation.plural("hours");
animation.controls.days.innerHTML = animation.plural("days");
requestAnimationFrame(animation.render);
}
};
animation.animate();
})();