-
Notifications
You must be signed in to change notification settings - Fork 0
/
cursor.js
73 lines (65 loc) · 1.73 KB
/
cursor.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
// dots is an array of Dot objects,
// mouse is an object used to track the X and Y position
// of the mouse, set with a mousemove event listener below
var dots = [],
mouse = {
x: 0,
y: 0,
};
// The Dot object used to scaffold the dots
var Dot = function () {
this.x = 0;
this.y = 0;
this.node = (function () {
var n = document.createElement("div");
n.className = "trail";
document.body.appendChild(n);
return n;
})();
};
// The Dot.prototype.draw() method sets the position of
// the object's <div> node
Dot.prototype.draw = function () {
this.node.style.left = this.x + 35 + "px";
this.node.style.top = this.y + 35 + "px";
};
// Creates the Dot objects, populates the dots array
for (var i = 0; i < 18; i++) {
var d = new Dot();
if (i == 0) {
var span = document.createElement("span");
span.className = "my-name-cursor";
span.textContent = "daphnechiu";
d.node.appendChild(span);
}
dots.push(d);
}
// This is the screen redraw function
function draw() {
// Make sure the mouse position is set everytime
// draw() is called.
var x = mouse.x,
y = mouse.y;
// This loop is where all the 90s magic happens
dots.forEach(function (dot, index, dots) {
var nextDot = dots[index + 1] || dots[0];
dot.x = x;
dot.y = y;
dot.draw();
x += (nextDot.x - dot.x) * 0.6;
y += (nextDot.y - dot.y) * 0.6;
});
}
addEventListener("mousemove", function (event) {
//event.preventDefault();
mouse.x = event.pageX;
mouse.y = event.pageY;
});
// animate() calls draw() then recursively calls itself
// everytime the screen repaints via requestAnimationFrame().
function animate() {
draw();
requestAnimationFrame(animate);
}
// And get it started by calling animate().
animate();