-
Notifications
You must be signed in to change notification settings - Fork 0
/
animation.js
247 lines (222 loc) · 10.9 KB
/
animation.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
// Change style of navbar on scroll
window.onscroll = function() {myFunction()};
function myFunction() {
var navbar = document.getElementById("myNavbar");
if (document.body.scrollTop > 100 || document.documentElement.scrollTop > 100) {
navbar.className = "w3-bar" + " w3-card-2" + " w3-animate-top" + " w3-white";
} else {
navbar.className = navbar.className.replace(" w3-card-2 w3-animate-top w3-white", "");
}
}
// Used to toggle the menu on small screens when clicking on the menu button
function toggleFunction() {
var x = document.getElementById("navDemo");
if (x.className.indexOf("w3-show") == -1) {
x.className += " w3-show";
} else {
x.className = x.className.replace(" w3-show", "");
}
}
//below source are from https://codepen.io/MarcoGuglielmelli/pen/lLCxy
/*
(function() {
var width, height, largeHeader, canvas, ctx, points, target, animateHeader = true;
// Main
initHeader();
initAnimation();
addListeners();
function initHeader() {
width = window.innerWidth;
height = window.innerHeight;
target = {x: width/2, y: height/2};
largeHeader = document.getElementById('home');
largeHeader.style.height = height+'px';
canvas = document.getElementById('demo-canvas');
canvas.width = width;
canvas.height = height;
ctx = canvas.getContext('2d');
// create points
points = [];
for(var x = 0; x < width; x = x + width/20) {
for(var y = 0; y < height; y = y + height/20) {
var px = x + Math.random()*width/20;
var py = y + Math.random()*height/20;
var p = {x: px, originX: px, y: py, originY: py };
points.push(p);
}
}
// for each point find the 5 closest points
for(var i = 0; i < points.length; i++) {
var closest = [];
var p1 = points[i];
for(var j = 0; j < points.length; j++) {
var p2 = points[j]
if(!(p1 == p2)) {
var placed = false;
for(var k = 0; k < 5; k++) {
if(!placed) {
if(closest[k] == undefined) {
closest[k] = p2;
placed = true;
}
}
}
for(var k = 0; k < 5; k++) {
if(!placed) {
if(getDistance(p1, p2) < getDistance(p1, closest[k])) {
closest[k] = p2;
placed = true;
}
}
}
}
}
p1.closest = closest;
}
// assign a circle to each point
for(var i in points) {
var c = new Circle(points[i], 2+Math.random()*2, 'rgba(255,255,255,0.3)');
points[i].circle = c;
}
}
// Event handling
function addListeners() {
if(!('ontouchstart' in window)) {
window.addEventListener('mousemove', mouseMove);
}
//window.addEventListener('touchstart', touchEvent);
window.addEventListener('scroll', scrollCheck);
window.addEventListener('resize', resize);
}
function mouseMove(e) {
var posx = posy = 0;
if (e.pageX || e.pageY) {
posx = e.pageX;
posy = e.pageY;
}
else if (e.clientX || e.clientY) {
posx = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
posy = e.clientY + document.body.scrollTop + document.documentElement.scrollTop;
}
target.x = posx;
target.y = posy;
}
function scrollCheck() {
if(document.body.scrollTop > height) animateHeader = false;
else animateHeader = true;
}
function touchEvent(e){
var posx = posy = 0;
if (e.pageX || e.pageY) {
posx = e.pageX;
posy = e.pageY;
}
else if (e.clientX || e.clientY) {
posx = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
posy = e.clientY + document.body.scrollTop + document.documentElement.scrollTop;
}
target.x = posx;
target.y = posy;
}
function resize() {
width = window.innerWidth;
height = window.innerHeight;
largeHeader.style.height = height+'px';
canvas.width = width;
canvas.height = height;
}
// animation
function initAnimation() {
animate();
for(var i in points) {
shiftPoint(points[i]);
}
}
function animate() {
if(animateHeader) {
ctx.clearRect(0,0,width,height);
for(var i in points) {
// detect points in range
if(Math.abs(getDistance(target, points[i])) < 4000) {
points[i].active = 0.3;
points[i].circle.active = 0.6;
} else if(Math.abs(getDistance(target, points[i])) < 20000) {
points[i].active = 0.1;
points[i].circle.active = 0.3;
} else if(Math.abs(getDistance(target, points[i])) < 40000) {
points[i].active = 0.02;
points[i].circle.active = 0.1;
} else {
points[i].active = 0;
points[i].circle.active = 0;
}
drawLines(points[i]);
points[i].circle.draw();
}
}
requestAnimationFrame(animate);
}
function shiftPoint(p) {
TweenLite.to(p, 1+1*Math.random(), {x:p.originX-50+Math.random()*100,
y: p.originY-50+Math.random()*100, ease:Circ.easeInOut,
onComplete: function() {
shiftPoint(p);
}});
}
// Canvas manipulation
function drawLines(p) {
if(!p.active) return;
for(var i in p.closest) {
ctx.beginPath();
ctx.moveTo(p.x, p.y);
ctx.lineTo(p.closest[i].x, p.closest[i].y);
ctx.strokeStyle = 'rgba(156,217,249,'+ p.active+')';
ctx.stroke();
}
}
function Circle(pos,rad,color) {
var _this = this;
// constructor
(function() {
_this.pos = pos || null;
_this.radius = rad || null;
_this.color = color || null;
})();
this.draw = function() {
if(!_this.active) return;
ctx.beginPath();
ctx.arc(_this.pos.x, _this.pos.y, _this.radius, 0, 2 * Math.PI, false);
ctx.fillStyle = 'rgba(156,217,249,'+ _this.active+')';
ctx.fill();
};
}
// Util
function getDistance(p1, p2) {
return Math.pow(p1.x - p2.x, 2) + Math.pow(p1.y - p2.y, 2);
}
})();*/
window.onscroll=function(){myFunction()};function myFunction(){var navbar=document.getElementById("myNavbar");if(document.body.scrollTop>100||document.documentElement.scrollTop>100){navbar.className="w3-bar"+" w3-card-2"+" w3-animate-top"+" w3-white"}else{navbar.className=navbar.className.replace(" w3-card-2 w3-animate-top w3-white","")}}
function toggleFunction(){var x=document.getElementById("navDemo");if(x.className.indexOf("w3-show")==-1){x.className+=" w3-show"}else{x.className=x.className.replace(" w3-show","")}}(function(){var width,height,largeHeader,canvas,ctx,points,target,animateHeader=!0;initHeader();initAnimation();addListeners();function initHeader(){width=window.innerWidth;height=window.innerHeight;target={x:width/2,y:height/2};largeHeader=document.getElementById('home');largeHeader.style.height=height+'px';canvas=document.getElementById('demo-canvas');canvas.width=width;canvas.height=height;ctx=canvas.getContext('2d');points=[];for(var x=0;x<width;x=x+width/20){for(var y=0;y<height;y=y+height/20){var px=x+Math.random()*width/20;var py=y+Math.random()*height/20;var p={x:px,originX:px,y:py,originY:py};points.push(p)}}
for(var i=0;i<points.length;i++){var closest=[];var p1=points[i];for(var j=0;j<points.length;j++){var p2=points[j]
if(!(p1==p2)){var placed=!1;for(var k=0;k<5;k++){if(!placed){if(closest[k]==undefined){closest[k]=p2;placed=!0}}}
for(var k=0;k<5;k++){if(!placed){if(getDistance(p1,p2)<getDistance(p1,closest[k])){closest[k]=p2;placed=!0}}}}}
p1.closest=closest}
for(var i in points){var c=new Circle(points[i],2+Math.random()*2,'rgba(255,255,255,0.3)');points[i].circle=c}}
function addListeners(){if(!('ontouchstart' in window)){window.addEventListener('mousemove',mouseMove)}
window.addEventListener('scroll',scrollCheck);window.addEventListener('resize',resize)}
function mouseMove(e){var posx=posy=0;if(e.pageX||e.pageY){posx=e.pageX;posy=e.pageY}
else if(e.clientX||e.clientY){posx=e.clientX+document.body.scrollLeft+document.documentElement.scrollLeft;posy=e.clientY+document.body.scrollTop+document.documentElement.scrollTop}
target.x=posx;target.y=posy}
function scrollCheck(){if(document.body.scrollTop>height)animateHeader=!1;else animateHeader=!0}
function touchEvent(e){var posx=posy=0;if(e.pageX||e.pageY){posx=e.pageX;posy=e.pageY}
else if(e.clientX||e.clientY){posx=e.clientX+document.body.scrollLeft+document.documentElement.scrollLeft;posy=e.clientY+document.body.scrollTop+document.documentElement.scrollTop}
target.x=posx;target.y=posy}
function resize(){width=window.innerWidth;height=window.innerHeight;largeHeader.style.height=height+'px';canvas.width=width;canvas.height=height}
function initAnimation(){animate();for(var i in points){shiftPoint(points[i])}}
function animate(){if(animateHeader){ctx.clearRect(0,0,width,height);for(var i in points){if(Math.abs(getDistance(target,points[i]))<4000){points[i].active=0.3;points[i].circle.active=0.6}else if(Math.abs(getDistance(target,points[i]))<20000){points[i].active=0.1;points[i].circle.active=0.3}else if(Math.abs(getDistance(target,points[i]))<40000){points[i].active=0.02;points[i].circle.active=0.1}else{points[i].active=0;points[i].circle.active=0}
drawLines(points[i]);points[i].circle.draw()}}
requestAnimationFrame(animate)}
function shiftPoint(p){TweenLite.to(p,1+1*Math.random(),{x:p.originX-50+Math.random()*100,y:p.originY-50+Math.random()*100,ease:Circ.easeInOut,onComplete:function(){shiftPoint(p)}})}
function drawLines(p){if(!p.active)return;for(var i in p.closest){ctx.beginPath();ctx.moveTo(p.x,p.y);ctx.lineTo(p.closest[i].x,p.closest[i].y);ctx.strokeStyle='rgba(156,217,249,'+p.active+')';ctx.stroke()}}
function Circle(pos,rad,color){var _this=this;(function(){_this.pos=pos||null;_this.radius=rad||null;_this.color=color||null})();this.draw=function(){if(!_this.active)return;ctx.beginPath();ctx.arc(_this.pos.x,_this.pos.y,_this.radius,0,2*Math.PI,!1);ctx.fillStyle='rgba(156,217,249,'+_this.active+')';ctx.fill()}}
function getDistance(p1,p2){return Math.pow(p1.x-p2.x,2)+Math.pow(p1.y-p2.y,2)}})()