I need to exactly position a DIV (DIV.selector) on top of another div which has been rotate (DIV.target).
In the following xample: http://jsfiddle.net/egjnd9bp/5/ please click 'rotate' and after 'create selector'.
The desired result would be the yellow box (DIV.selector) which exact match( position and rotation) the cyan div (DIV.target)
At the moment I am using getBoundingClientRect(), but I am not able to get the right x.y coordinates as rect
include the boundary of the object rotated.
Notes: A possible solution would be to remove the rotation, using getBoundingClientRect() and reapply the rotation after, but I am not sure if it is a proper solution, I would be glad to have a feedback, possibly on a more math/geometric solution instead.
<div id="example" style="display: none;"></div>
<div id="target"></div>
<div id="trace"></div>
<button id="btn-rotate" type="button">rotate</button>
<button id="btn-coordinate" type="button">get coordinate x,y</button>
<button id="btn-selector" type="button">create selector</button>
document.getElementById('btn-rotate').addEventListener('click', function (event) {
document.getElementById('target').classList.add('rotation');
document.getElementById('example').style.display = '';
}.bind(this));
document.getElementById('btn-coordinate').addEventListener('click', function (event) {
alert(JSON.stringify(document.getElementById('target').getBoundingClientRect()));
}.bind(this));
document.getElementById('btn-selector').addEventListener('click', function (event) {
var rect = document.getElementById('target').getBoundingClientRect();
document.body.innerHTML += '<div style="position:absolute; top:' + rect.top + 'px; left:' + rect.left + 'px; width:' + rect.width + 'px; height:' + rect.height + 'px; background:yellow; opacity:0.5;"></div>';
}.bind(this));
#target {
position:absolute;
top:100px;
left:100px;
width:200px;
height:200px;
background-color: cyan;
transform-origin: 50% 50% 0;
}
#trace {
position:absolute;
top:100px;
left:100px;
width:200px;
height:200px;
border: 1px solid gray;
opacity: 0.1;
}
#example {
position:absolute;
top:100px;
left:100px;
width:5px;
height:5px;
background-color: red;
z-index: 100;
}
.rotation {
-ms-transform: rotate(10deg);
-webkit-transform: rotate(10deg);
transform: rotate(10deg);
}