-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
executable file
·333 lines (239 loc) · 8.02 KB
/
script.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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
// Comments char animation, for the home
var commentComponent = document.getElementById("codeCommentContainer");
var commentCharList = [
{
className: 'comment-position-javascript',
charList: ['/*', '<br>', '*/']
},
{
className: 'comment-position-visual-basic',
charList: ["'", '<br>', "'"]
}
]
var charListItem = Math.floor(Math.random() * 2);
commentComponent.innerHTML = commentCharList[charListItem].charList[0] + commentCharList[charListItem].charList[1] + commentCharList[charListItem].charList[2];
commentComponent.className = "show-comment-animated code-comment " + commentCharList[charListItem].className;
/* Arrows animations */
/* Set a fade-in effect to another links who not hover
*/
var _fadeOtherLinks = function (linkList){
return function(hoveredLink){
linkList.forEach(function(link){
if(link != hoveredLink){
if(hoveredLink == undefined)
link.classList.remove("home-fade-to-white");
else
link.classList.add("home-fade-to-white");
}
else
link.classList.remove("home-fade-to-white");
});
}
}
/* Receives a function, set the obj translation.
This must receives the transition value, and must return a {X, Y} object*/
var mainLinkAnimation = function(){
var lastElement = undefined;
var animationLimit = 320;
var transitionValue = 0;
var _transitionComponent = undefined;
var finishTransitionID = undefined;
var animationID = undefined;
return {
start: function (spanElement, transitionComponent){
// Check an occourring animation.
if( animationID != undefined || finishTransitionID != undefined )
return;
lastElement = spanElement;
_transitionComponent = transitionComponent;
// Start of process
var accellFactor = 1;
animationID = setInterval(function(){
if(accellFactor <= 50)
accellFactor = accellFactor + 0.3;
transitionValue = transitionValue - accellFactor;
// Return the arrow to above the link
if(transitionValue < -animationLimit)
transitionValue = animationLimit;
var transitionObj = _transitionComponent(transitionValue);
spanElement.style.transform = "translate( " + transitionObj.X + "%, " + transitionObj.Y + "% )";
}, 15);
},
// At same time the process is rolling, return a function, that allows to stop the process
stop: function(){
// Check an occourring animation.
if( finishTransitionID != undefined || lastElement == undefined )
return;
clearInterval(animationID);
animationID = undefined;
transitionValue = animationLimit;
finishTransitionID = setInterval(function(){
transitionValue = transitionValue - 10;
transitionObj = _transitionComponent(transitionValue);
// Range to end the transition
if(transitionValue > -10 && transitionValue < 10){
lastElement.style.transform = "translate(0%, 0%)";
clearInterval(finishTransitionID);
finishTransitionID = undefined;
lastElement = undefined;
return;
}
lastElement.style.transform = "translate(" + transitionObj.X + "%, " + transitionObj.Y + "%)";
}, 12);
}
}
}
var linksDOMObjects = [
document.getElementById("linkAbout"),
document.getElementById("linkGallery"),
document.getElementById("linkBlog"),
document.getElementById("linkPortfolio")
];
var animationObj = mainLinkAnimation();
var fadeOtherLinks = _fadeOtherLinks(linksDOMObjects);
linksDOMObjects[0].addEventListener("mouseenter", function (mouseEvent){
animationObj.start(mouseEvent.currentTarget.children[0], function(transitionValue){
return {
X: 0, Y: transitionValue
}
});
fadeOtherLinks(linksDOMObjects[0]);
});
linksDOMObjects[0].addEventListener("mouseleave", function (mouseEvent){
animationObj.stop();
fadeOtherLinks();
});
linksDOMObjects[1].addEventListener("mouseenter", function (mouseEvent){
/* The children array changes, because the position in HTML code is different*/
animationObj.start(mouseEvent.currentTarget.children[1], function(transitionValue){
return {
X: -transitionValue, Y: 0
}
});
fadeOtherLinks(linksDOMObjects[1]);
});
linksDOMObjects[1].addEventListener("mouseleave", function (mouseEvent){
animationObj.stop();
fadeOtherLinks();
});
linksDOMObjects[2].addEventListener("mouseenter", function (mouseEvent){
/* The children array changes, because the position in HTML code is different*/
animationObj.start(mouseEvent.currentTarget.children[1], function(transitionValue){
return {
X: 0, Y: -transitionValue
}
});
fadeOtherLinks(linksDOMObjects[2]);
});
linksDOMObjects[2].addEventListener("mouseleave", function (mouseEvent){
animationObj.stop();
fadeOtherLinks();
});
linksDOMObjects[3].addEventListener("mouseenter", function (mouseEvent){
/* The children array changes, because the position in HTML code is different*/
animationObj.start(mouseEvent.currentTarget.children[0], function(transitionValue){
return {
X: transitionValue, Y: 0
}
});
fadeOtherLinks(linksDOMObjects[3]);
});
linksDOMObjects[3].addEventListener("mouseleave", function (mouseEvent){
animationObj.stop();
fadeOtherLinks();
});
/* Canvas animation */
var animatedBG = function(){
var theCanvas = document.getElementById("animatedBG");
var canvasContainer = document.getElementById("animatedBGContainer");
var context = theCanvas.getContext ("2d");
// Resizing the canvas to the container limits
var limits = {
canvasWidth: canvasContainer.offsetWidth,
canvasHeight: canvasContainer.offsetHeight
}
/* Helpers*/
/* Receives: {
context: the context object,
point: the point coordinate
X: coordinate,
Y: coordinate
}*/
var drawPoint = function(paramethers){
// Drawing the shape
paramethers.context.beginPath();
paramethers.context.arc(paramethers.point.X, paramethers.point.Y, 1, 0,2*Math.PI);
paramethers.context.strokeStyle = paramethers.point.color;
paramethers.context.stroke();
};
// The point structure
var pointObj = function (X, Y, direction, color){
var _X = X;
var _Y = Y;
return {
X: _X,
Y: _Y,
direction: direction,
color: color
}
};
// Function to generate a point, outside canvas
var generatePointToStart = function(direction){
/* Generation color */
var color = 'hsl(' + Math.floor( Math.random() * 360 ) + ', 50%, 50%)';
/*var positionIndex = Math.ceil(Math.random() * 3);*/
switch (direction){
case 'up': //Up
var newPoint = pointObj( Math.floor( Math.random() * limits.canvasWidth) , 0, direction, color); break;
case 'down': //Down
var newPoint = pointObj( Math.floor(Math.random() * limits.canvasWidth) , limits.canvasHeight , direction, color); break;
case 'left': //Left
var newPoint = pointObj(0, Math.floor(Math.random() * limits.canvasHeight), direction, color); break;
case 'right': // Right
var newPoint = pointObj(limits.canvasWidth, Math.floor(Math.random() * limits.canvasHeight), direction, color); break;
}
return newPoint;
}
/* Main objects and constants*/
theCanvas.width = limits.canvasWidth;
theCanvas.height = limits.canvasHeight;
// Creating the central point
var centralPoint = pointObj(Math.floor(limits.canvasWidth / 2), Math.floor(limits.canvasHeight / 2));
// Draw it to the center
/*drawPoint({
context: context,
point: centralPoint
});*/
//Generate the points
var pointList = [];
pointList.push(generatePointToStart('up'));
pointList.push(generatePointToStart('down'));
pointList.push(generatePointToStart('left'));
pointList.push(generatePointToStart('right'));
// Start the interaction
animationID = setInterval( function(){
// Verify if the point has come to the destiny
pointList.forEach( function (point){
if (point.X == centralPoint.X && point.Y == centralPoint.Y){
var newPoint = generatePointToStart(point.direction);
point.X = newPoint.X;
point.Y = newPoint.Y;
point.color = newPoint.color;
}
drawPoint({
context: context,
point: point,
});
// Recalculate the point
if(point.X > centralPoint.X)
point.X --;
if(point.X < centralPoint.X)
point.X ++;
if(point.Y > centralPoint.Y)
point.Y --;
if(point.Y < centralPoint.Y)
point.Y ++;
});
}, 5 );
};
animatedBG();