Click-Center
by velo_ninja
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Click Element Center</title>
<style>
#myButton {
margin: 50px;
padding: 20px;
}
.point {
width: 10px;
height: 10px;
border-radius: 50%;
position: absolute;
transform: translate(-50%, -50%);
pointer-events: none; /* Prevents the point from interfering with clicks */
}
.red-point {
background-color: red;
}
.blue-point {
background-color: blue;
}
</style>
</head>
<body>
<button id="myButton">Click me!</button>
<div id="output"></div>
<script>
function getElementCenter(elementOrId) {
let element;
if (typeof elementOrId === 'string') {
element = document.getElementById(elementOrId);
} else {
element = elementOrId;
}
const rect = element.getBoundingClientRect();
const centerX = rect.left + (rect.width / 2);
const centerY = rect.top + (rect.height / 2);
return { x: centerX, y: centerY };
}
function setPoints(centerX, centerY, bottomX, bottomY) {
// Create red point for the center
const redPoint = document.createElement('div');
redPoint.classList.add('point', 'red-point');
redPoint.style.left = `${centerX}px`;
redPoint.style.top = `${centerY}px`;
document.body.appendChild(redPoint);
// Create blue point for the bottom center
const bluePoint = document.createElement('div');
bluePoint.classList.add('point', 'blue-point');
bluePoint.style.left = `${bottomX}px`;
bluePoint.style.top = `${bottomY}px`;
...