-
Notifications
You must be signed in to change notification settings - Fork 0
/
box.html
156 lines (156 loc) · 4.24 KB
/
box.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<link rel="icon" href="https://barrycap.com/assets/favicon.ico"/>
<script src="https://barrycap.com/googleanalytics.js"></script>
<script async src="https://www.googletagmanager.com/gtag/js?id=G-GY5Q2KLJ19"></script>
<script async src="/script.js"></script>
<title>Barry Cap › Box</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script>
function work() {
var pane = $("#pane"),
box = $("#box"),
maxValue = pane.width() - box.width(),
keysPressed = {},
distancePerIteration = 10
function calculateNewValue(oldValue, keyCode1, keyCode2) {
var newValue =
parseInt(oldValue, 10) -
(keysPressed[keyCode1] ? distancePerIteration : 0) +
(keysPressed[keyCode2] ? distancePerIteration : 0)
return newValue < 0 ? 0 : newValue > maxValue ? maxValue : newValue
}
$(window).keydown(function (event) {
keysPressed[event.which] = true
})
$(window).keyup(function (event) {
keysPressed[event.which] = false
})
setInterval(function () {
box.css({
left: function (index, oldValue) {
return calculateNewValue(oldValue, 37, 39)
},
top: function (index, oldValue) {
return calculateNewValue(oldValue, 38, 40)
}
})
}, 20)
}
</script>
<style>
body {
background-color: #000;
margin: 0;
overflow: hidden;
cursor: none;
}
#pane {
width: 800px;
height: 800px;
border: 4px solid #000;
border-radius: 24px;
border-color: #fff8;
background-color: #222;
margin: auto;
margin-top: calc(50vh - 400px);
position: relative;
}
#pane:hover {
background-color: #220;
border-color: #ff08;
border-radius: 12px;
transition-duration: 0.1s;
}
#pane:active {
background-color: #200;
border-color: #f008;
border-radius: 0;
transition-duration: 1.8s;
}
#box {
background-color: #fff;
border-radius: 24px;
width: 40px;
height: 40px;
position: absolute;
top: Calc(50% - 20px);
left: Calc(50% - 20px);
}
#box:hover {
background-color: #ff0;
border-radius: 8px;
transition-duration: 50ms;
}
#box:active {
background-color: #f00;
border-radius: 0px;
transition-duration: 1s;
}
#follower {
position: absolute;
top: -100%;
left: -100%;
}
#cursor {
position: absolute;
background: #fff;
border-radius: 24px;
width: 40px;
height: 40px;
position: absolute;
top: calc(50% - 16px);
left: calc(10% - 20px);
pointer-events: none;
}
#cursor/*:hover*/ {
background: #ff0;
border-radius: 8px;
transition-duration: 50ms;
}
#cursor:active {
background: #f00;
border-radius: 0;
transition-duration: 1s;
}
</style>
</head>
<body onload="work()">
<div id="pane">
<div id="box"></div>
</div>
<div id="follower">
<div id="cursor"></div>
</div>
<script>
(function cursor() {
var follower, init, mouseX, mouseY, positionElement, printout, timer
follower = document.getElementById('follower')
mouseX = (event) => {
return event.clientX
}
mouseY = (event) => {
return event.clientY
}
positionElement = (event) => {
var mouse
mouse = {
x: mouseX(event),
y: mouseY(event)
}
follower.style.left = mouse.x + 'px'
follower.style.top = mouse.y + 'px'
}
timer = false
window.onmousemove = init = (event) => {
var _event
_event = event
return timer = setTimeout(() => {
return positionElement(_event)
}, 1)
}
}).call(this)
</script>
</body>
</html>