-
Notifications
You must be signed in to change notification settings - Fork 0
/
weightforit.html
136 lines (128 loc) · 3.38 KB
/
weightforit.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
---
layout: iframe
---
<div
id="weightForIt"
class="container container-sm module"
style="overflow:visible"
>
<div id="canvas">
<div class="pulsating-circle"></div>
<div id="dollars"></div>
<div class="label">CASL</div>
<div id="xAxis">
<div class="float-left">864</div>
<div class="float-right">864</div>
</div>
<div id="yAxis">
<div>864</div>
<div>864</div>
</div>
</div>
<div class="label-bottom clearfix">
<div class="float-left">Weight:300</div>
<div class="float-right" style="margin-left:800px">1000</div>
</div>
</div>
<script type="text/javascript">
const body = document.querySelector("body");
let lastX = 0,
lastY = 0,
i = 0;
const mousePos = {
x: 0,
y: 0,
cx: 0,
cy: 0
};
let dollarCount = 0;
if (body.classList.contains("touch-input")) {
var offset = 0;
} else {
var offset = 42;
}
const pulse = document.getElementsByClassName("pulsating-circle")[0];
const xAxisEl = document.getElementById("xAxis");
const yAxisEl = document.getElementById("yAxis");
// Only keep track of mouse position, do nothing else
document.getElementById("canvas").onmousemove = function(e) {
mousePos.x = e.offsetX;
mousePos.y = e.offsetY;
mousePos.cx = e.clientX;
mousePos.cy = e.clientY;
if (this != e.target) {
mousePos.x = mousePos.x + e.target.offsetLeft;
mousePos.y = mousePos.y + e.target.offsetTop;
}
// Ask browser to repaint at earliest convenience instead
// of forcing a repaint
requestAnimationFrame(draw);
};
function draw() {
pulse.style.display = "none";
const x = mousePos.x;
const y = mousePos.y;
const cx = mousePos.cx;
const cy = mousePos.cy;
const dollarsContainer = document.getElementById("dollars");
let bounds = dollarsContainer.getBoundingClientRect();
let newX = interpolate(cx, bounds.left, bounds.right, 0, bounds.width);
let newY = interpolate(cy, bounds.top, bounds.bottom, 0, bounds.height);
let xPos = interpolate(
cx,
bounds.left,
bounds.right,
0,
dollarsContainer.clientWidth
);
let yPos = interpolate(
cy,
bounds.top,
bounds.bottom,
0,
dollarsContainer.clientHeight
);
let yAxisLabel = interpolate(newY, bounds.top, bounds.bottom, 0, 1);
if (xPos > 0) {
yAxisEl.style.transform = `translateX(${xPos}px) translateX(-100%)`;
yAxisEl.innerHTML =
"<div>" +
Math.floor(newX) +
"</div><div>" +
Math.floor(newX) +
"</div>";
}
xAxisEl.style.transform = `translateY(${yPos}px) translateY(-100%)`;
xAxisEl.innerHTML =
'<div class="float-left">' +
yAxisLabel.toFixed(2) +
'</div><div class="float-right">' +
yAxisLabel.toFixed(2) +
"</div>";
if (
x > lastX + offset ||
x < lastX - offset ||
y > lastY + offset ||
y < lastY - offset
) {
if (yPos < 400 && yPos > 60 && xPos < 933 && xPos > 60) {
document.getElementById(
"dollars"
).innerHTML += `<div class='dollar' style='transform:translate(${xPos}px, ${newY}px) translate(-50%, -50%); font-variation-settings: "CASL" ${yAxisLabel.toFixed(
2
)}, "wght" ${Math.floor(newX)}'>$</div>`;
[lastX, lastY] = [x, y];
if (dollarCount++ >= 8) {
// Clean up!
const dead = dollarsContainer.querySelector(
".dollar:first-child"
);
dead.parentNode.removeChild(dead);
}
}
}
}
function interpolate(value, low1, high1, low2, high2) {
return low2 + ((high2 - low2) * (value - low1)) / (high1 - low1);
}
</script>