JSFiddle - React, Tailwind, and code Playground
by kent93
HTML
<!doctype html>
<html>
<head>
<style>
.wrapper {
position: relative;
width: 800px;
height: 800px;
background-color: yellow;
}
.threedee {
position: absolute;
width: 150px;
height: 150px;
margin: 100px;
background-color: red;
}
</style>
</head>
<body>
<div class='wrapper'>
drag in this yellow container
<div class='threedee'>
</div>
</div>
<script type="text/javascript">
/* Modernizr 2.5.3 (Custom Build) | MIT & BSD
* Build: http://www.modernizr.com/download/#-csstransforms3d-csstransitions-shiv-cssclasses-prefixed-teststyles-testprop-testallprops-prefixes-domprefixes-load
*/
;window.Modernizr=function(a,b,c){function z(a){j.cssText=a}function A(a,b){return z(m.join(a+";")+(b||""))}function B(a,b){return typeof a===b}function C(a,b){return!!~(""+a).indexOf(b)}function D(a,b){for(var d in a)if(j[a[d]]!==c)return b=="pfx"?a[d]:!0;return!1}function E(a,b,d){for(var e in a){var f=b[a[e]];if(f!==c)return d===!1?a[e]:B(f,"function")?f.bind(d||b):f}return!1}function F(a,b,c){var d=a.charAt(0).toUpperCase()+a.substr(1),e=(a+" "+o.join(d+" ")+d).split(" ");return B(b,"string")||B(b,"undefined")?D(e,b):(e=(a+" "+p.join(d+" ")+d).split(" "),E(e,b,c))}var d="2.5.3",e={},f=!0,g=b.documentElement,h="modernizr",i=b.createElement(h),j=i.style,k,l={}.toString,m=" -webkit- -moz- -o- -ms- ".split(" "),n="Webkit Moz O ms",o=n.split(" "),p=n.toLowerCase().split(" "),q={},r={},s={},t=[],u=t.slice,v,w=function(a,c,d,e){var f,i,j,k=b.createElement("div"),l=b.body,m=l?l:b.createElement("body");if(parseInt(d,10))while(d--)j=b.createElement("div"),j.id=e?e[d]:h+(d+1),k.appendChild(j);return f=["­","<style>",a,"</style>"].join(""),k.id=h,m.innerHTML+=f,m.appendChild(k),l||(m.style.background="",g.appendChild(m)),i=c(k,a),l?k.parentNode.removeChild(k):m.parentNode.removeChild(m),!!i},x={}.hasOwnProperty,y;!B(x,"undefined")&&!B(x.call,"undefined")?y=function(a,b){return...
JavaScript
var transformString = Modernizr.prefixed('transform');
var inputDown, inputMove, inputUp;
if (window.Touch) {
inputDown = "touchstart";
inputMove = "touchmove";
inputUp = "touchend";
}
else {
inputDown = "mousedown";
inputMove = "mousemove";
inputUp = "mouseup";
}
var mouse = { startX: 0, startY: 0 };
var innerElement = $('.threedee');
function normalizedX(event){
return window.Touch ? event.originalEvent.touches[0].pageX : event.pageX;
}
function normalizedY(event){
return window.Touch ? event.originalEvent.touches[0].pageY : event.pageY;
}
$(document).bind(inputDown, function(event){
event.preventDefault();
if(event.button === 2) return true; // right-click
mouse.startX = normalizedX(event);
mouse.startY = normalizedY(event);
// store the transform state
// globalMatrix.setMatrixValue($inner.css('-webkit-transform'));
$(document)
.bind(inputMove, move)
.one(inputUp, function(){ $(document).unbind(inputMove); });
});
function move(event){
event.preventDefault();
var offsetX = normalizedX(event) - mouse.startX;
var offsetY = normalizedY(event) - mouse.startY;
if(event.shiftKey){
offsetX = roundToMultiple(offsetX, 15);
offsetY = roundToMultiple(offsetY, 15);
}
innerElement.css(transformString, 'rotateY('+offsetX+'deg) rotateX('+-offsetY+'deg)');
/* move relative to the initial position
$inner.css('-webkit-transform', globalMatrix.rotate(-offsetY, offsetX, 0));//*/
}
function roundToMultiple(number, multiple){
var value = number/multiple
, integer = Math.floor(value)
, rest = value - integer;
return rest > 0.5 ? (integer+1)*multiple : integer*multiple;
}