JSFiddle - React, Tailwind, and code Playground
HTML
<body>
<p style="background:white">body</p>
<div class="myDiv">
<p style="background:white">myDiv</p>
<canvas id="c" width="200" height="200"> </canvas>
</div>
</body>
CSS
body {
margin: 1em;
background: #8888ff;
padding: 1em;
}
.myDiv {
position: absolute;
left: 20px;
top: 40px;
border: solid #88ff88 10px;
background: green;
margin: 1em;
padding: 0.5em;
}
canvas {
border: solid red 20px;
position: relative;
left: 20px;
top: 0px;
padding: 10px;
background: brown;
margin: 1em
}
JavaScript
function getNumericStyleProperty(style, prop){
return parseInt(style.getPropertyValue(prop),10) ;
}
function element_position(e) {
var x = 0, y = 0;
var inner = true ;
do {
x += e.offsetLeft;
y += e.offsetTop;
var style = getComputedStyle(e,null) ;
var borderTop = getNumericStyleProperty(style,"border-top-width") ;
var borderLeft = getNumericStyleProperty(style,"border-left-width") ;
y += borderTop ;
x += borderLeft ;
if (inner){
var paddingTop = getNumericStyleProperty(style,"padding-top") ;
var paddingLeft = getNumericStyleProperty(style,"padding-left") ;
y += paddingTop ;
x += paddingLeft ;
}
inner = false ;
} while (e = e.offsetParent);
return { x: x, y: y };
}
var c = document.getElementById('c');
var t = c.getContext('2d');
t.font = '10px monospace';
c.addEventListener('mousemove', function(e) {
t.fillStyle = "white"
t.fillRect(0, 0, c.width, c.height);
t.fillStyle = "black"
var p = element_position(c);
t.fillText('pageX: ' + e.pageX, 16, 16);
t.fillText('pageY: ' + e.pageX, 16, 32);
t.fillText('offset x: ' + p.x, 16, 48);
t.fillText('offset y: ' + p.y, 16, 64);
t.fillText('canvas x: ' + (e.pageX - p.x), 16, 80);
t.fillText('canvas y: ' + (e.pageY - p.y), 16, 96);
}, false);