Implemented JS for #4
This commit is contained in:
30
www/booper.js
Normal file
30
www/booper.js
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
window.addEventListener('load', (e) => {
|
||||||
|
const targets = document.getElementsByClassName('booperTarget');
|
||||||
|
for (let i = 0; i < targets.length; i++) {
|
||||||
|
const target = targets[i];
|
||||||
|
const locators = JSON.parse(target.getAttribute('data-booper'));
|
||||||
|
console.debug(locators);
|
||||||
|
target.addEventListener('click', (e) => {
|
||||||
|
let x = e.offsetX / e.originalTarget.clientWidth;
|
||||||
|
let y = e.offsetY / e.originalTarget.clientHeight;
|
||||||
|
for (const [name, box] of Object.entries(locators)) {
|
||||||
|
const [x1, y1, x2, y2] = box;
|
||||||
|
if (x >= x1 && x <= x2 && y >= y1 && y <= y2) {
|
||||||
|
console.log(`[booper.js] Booped ${name}!`);
|
||||||
|
fetch(`/api/boop/${name}`, {method: 'POST'}).then((r) => {
|
||||||
|
if (r.ok) {
|
||||||
|
r.json().then((json) => {
|
||||||
|
console.debug(json);
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
console.error(`[booper.js] Error whilst booping:`);
|
||||||
|
console.error(r);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log(`[booper.js] Initialised ${targets.length} boop target(s)!`);
|
||||||
|
});
|
||||||
@ -8,6 +8,7 @@
|
|||||||
<title></title>
|
<title></title>
|
||||||
<link rel="stylesheet" href="/styles.css">
|
<link rel="stylesheet" href="/styles.css">
|
||||||
<script src="/main.js"></script>
|
<script src="/main.js"></script>
|
||||||
|
<script src="/booper.js"></script>
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||||
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
|
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
|
||||||
</head>
|
</head>
|
||||||
@ -25,7 +26,7 @@
|
|||||||
<h1 id="helloText"><noscript>Hewwo!</noscript></h1>
|
<h1 id="helloText"><noscript>Hewwo!</noscript></h1>
|
||||||
<h2><span id="imaText"><noscript>I'm a <span class="textHighlight">dumbass</span></noscript></span>.</h2>
|
<h2><span id="imaText"><noscript>I'm a <span class="textHighlight">dumbass</span></noscript></span>.</h2>
|
||||||
<div class="mainContent">
|
<div class="mainContent">
|
||||||
<img src="/fag.png">
|
<img class="booperTarget" data-booper='{"brosef": [0.4752,0.3438,0.5773,0.3907]}' src="/fag.png">
|
||||||
<div>
|
<div>
|
||||||
<h3>Hi there! I'm <b>Brosef</b>, as you can see by the <i>very fancy</i> changing text about this, I'm interested in a lot of things!</h3>
|
<h3>Hi there! I'm <b>Brosef</b>, as you can see by the <i>very fancy</i> changing text about this, I'm interested in a lot of things!</h3>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
Reference in New Issue
Block a user