-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
114 lines (90 loc) · 2.52 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
'use strict';
// shim layer with setTimeout fallback
window.requestAnimFrame = (function(){
return window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
function( callback ){
window.setTimeout(callback, 1000 / 60);
};
})();
// Set up the canvas
var canvas = document.querySelector('canvas')
, ctx = canvas.getContext('2d')
, width = window.innerWidth
, height = window.innerHeight;
canvas.height = height;
canvas.width = width;
// Config
var maxArrows = 10
, directions = ['topRight', 'bottomLeft'];
// Arrow class
var Arrow = function (direction) {
this.x = Math.round(20 + (Math.random() * (width - 20)));
this.y = Math.round(20 + (Math.random() * (height - 20)));
this.opacity = 0; // Invisible at first
this.direction = direction;
this.hasAppeared = false;
this.velocity = 0.5
this.img = new Image();
this.img.src = (this.direction === 'topRight') ? 'images/arrow-top-right.png' : 'images/arrow-bottom-left.png';
this.draw = function () {
ctx.save();
ctx.globalAlpha = this.opacity;
ctx.drawImage(this.img, this.x, this.y, 15, 15);
ctx.restore();
};
this.animate = function () {
this.draw();
// Make it appear (fade in)
if (this.direction === 'topRight') {
this.x += this.velocity;
this.y -= this.velocity;
}
if (this.direction === 'bottomLeft') {
this.x -= this.velocity;
this.y += this.velocity;
}
if (!this.hasAppeared) {
this.opacity += 0.02;
if (this.opacity >= 1) {
this.hasAppeared = true;
}
}
if (this.hasAppeared) {
this.opacity -= 0.005
}
if (this.opacity <= 0)
return true;
else
return false;
}
}
var arrows = [];
var index = 0;
var pusher = setInterval(function () {
var direction = (Math.random() < 0.5) ? directions[0] : directions[1];
arrows.push(new Arrow(direction));
index++;
if (index === maxArrows) clearInterval(pusher);
}, 200)
var render = function () {
// console.log(arrows.length);
ctx.clearRect(0, 0, width, height);
for (var i = 0; i < arrows.length; i++) {
var arrow = arrows[i];
var hasEnded = arrow.animate();
if (hasEnded) {
arrows[i] = new Arrow((Math.random() < 0.5) ? directions[0] : directions[1]);
}
}
}
function animloop(){
requestAnimFrame(animloop);
render();
};
animloop();
// Show content after 1.5s
setTimeout(function () {
document.querySelector('.main').className = 'main';
}, 1500)