I'm having trouble centering a div post rotation.
Here's my fiddle: https://jsfiddle.net/cfy2ztdz/
Move between slides with keyboard 1 & 2
You can see that it finds the middle perfectly without rotation, but post rotation, moving back to 1, moves off page - instead of back to the middle.
Where's my math going wrong?
Here's the most relevant code:
var windowWidth = $(window).width();
var windowHeight = $(window).height();
var winOriginX = windowWidth/2;
var winOriginY = windowHeight/2;
document.onkeypress = function(evt) {
evt = evt || window.event;
var charCode = evt.keyCode || evt.which;
var charStr = String.fromCharCode(charCode);
var slideWidth = $('#slide'+charStr).width();
var slideHeight = $('#slide'+charStr).height();
var slideXPos = $('#slide'+charStr).position().left;
var slideYPos = $('#slide'+charStr).position().top;
var originX = slideXPos + slideWidth/2;
var originY = slideYPos + slideHeight/2;
var xPos = winOriginX - slideXPos - slideWidth/2;
var yPos = winOriginY - slideYPos - slideHeight/2;
var rotation = 0;
if($('#slide'+charStr).attr('slider-rotate')) {
rotation = $('#slide'+charStr).attr('slider-rotate');
}
else {
rotation = 0;
}
var nextRotation = rotation;
$('#viewport').velocity({scaleX: "50%", scaleY: "50%", translateZ:0, rotateZ: nextRotation + "deg"},{duration: 1000, easing: 'easeInOutSine'});
slideXPos = $('#slide'+charStr).position().left;
slideYPos = $('#slide'+charStr).position().top;
xPos = winOriginX - slideXPos - slideWidth/2;
yPos = winOriginY - slideYPos - slideHeight/2;
$('#slides').velocity({left: xPos + 'px', top: yPos + 'px', translateZ:0},{duration: 1000, easing: 'easeInOutSine'});
$('#viewport').velocity({scaleX: "100%", scaleY: "100%", translateZ:0},{duration: 1000, easing: 'easeInOutSine'});
}
And the HTML
<div id="slides">
<div id="slide1" class="slide" style="top: 300px; left: 100px; background: url('Andy.jpg');"></div>
<div id="slide2" class="slide" style="top: 300px; left: 600px; background: transparent" slider-rotate="-75">
<div style="width: 300px; height: 300px; background: url('Woody.jpg'); transform-origin: 50% 50%; transform: rotate(75deg)"></div>
</div>
</div>
The problem is that after the rotation the new target x/y values are calculated based on the rotated positions. You could buffer the target centering x/y positions of the viewport inside each slide at startup, and reuse those while animating towards the centerpoint. In this example I used jquery data to store the values:
Fiddle
Script: