[英]javascript find same position in rotated element
我有一个“基本”参考矩形(红色)
在旋转的div(#map)内,我需要一个克隆rect(黄色),它的大小和位置必须与“基本” rect相同,而与父级(#map)的旋转无关。
到目前为止,这是我的所在地,欢迎任何帮助。
http://codepen.io/christianpugliese/pen/oXKOda
var controls = { degrees: 0, rectX:125, rectY:55 };
var wBounds = document.getElementById("wrapper").getBoundingClientRect(),
mapBounds = document.getElementById("map").getBoundingClientRect(),
rectBounds = document.getElementById("rect").getBoundingClientRect();
var _x = ((mapBounds.width - wBounds.width) / 2) + $('#rect').position().left,
_y = ((mapBounds.height - wBounds.height) / 2) + $('#rect').position().top;
$('#rect').css({top: controls.rectY+'px', left:controls.rectX+'px'});
$('#mapRect').width(rectBounds.width);
$('#mapRect').height(rectBounds.height);
$('#mapRect').css({top: _y+'px',
left:_x+'px',
'transform': 'rotate('+ Math.round(-controls.degrees) +'deg)'});
$('#map').css('transform', 'rotate('+ Math.round(controls.degrees) +'deg)');
由于您将#mapRect
沿相反的方向旋转相等的量,因此您将获得正确的旋转/方向而不是原点。 transform-origin
将是#mapBounds
的中心,但相对于#rect
;
笔叉: http : //codepen.io/MisterCurtis/pen/vNBYZJ?editors=101
由于存在一些舍入/子像素定位,因此黄色矩形无法完美对齐像素。
function updateUI(){
var _x = ((mapBounds.width - wBounds.width) / 2) + $('#rect').position().left,
_y = ((mapBounds.height - wBounds.height) / 2) + $('#rect').position().top,
_ox = mapBounds.width/2 - _x, // origin x
_oy = mapBounds.height/2 - _y; // origin y
...
$('#mapRect').css({
'transform-origin': _ox + 'px ' + _oy + 'px', // now it rotates by the bounds
top: _y + 'px',
left: _x + 'px',
'transform': 'rotate(' + Math.round(-controls.degrees) + 'deg)'
});
}
编辑:更新了笔 。 您将不得不使用Rectangle来代替,而使用Polygon。 这样,您可以使用https://github.com/ahmadnassri/google-maps-polygon-rotate这样的插件沿地图中心执行旋转。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.