Skip to content

Instantly share code, notes, and snippets.

@smagch
Created June 20, 2012 19:43
Show Gist options
  • Save smagch/2961782 to your computer and use it in GitHub Desktop.
Save smagch/2961782 to your computer and use it in GitHub Desktop.
A web page created at CodePen.io
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>CSS3 3D transform mouse animation with centering demo &middot; CodePen</title>
<style>
body {
background-color: black;
color: white;
font-family: 'Lucida Grande', Verdana, Arial;
font-size: 12px;
background-image: -webkit-gradient(radial,
50% 500, 1,
50% 500, 400,
from(rgba(255, 255, 255, 0.3)),
to(rgba(255, 255, 255, 0)));
background-repeat: no-repeat;
}
fieldset {
width: 200px;
}
#discription {
position: absolute;
}
#stage {
margin: 0;
width: 0;
height: 0;
-webkit-perspective: 800;
-webkit-transform-style: preserve-3d;
}
#world {
margin: 0;
position: absolute;
-webkit-transform-style: preserve-3d;
}
.box {
margin: 0;
position: absolute;
-webkit-transform-style: preserve-3d;
}
.plane {
margin: 0;
position: absolute;
background-color: blue;
width: 100px;
height: 100px;
opacity: 0.6;
border: thin solid red;
-webkit-transform-style: flat;
-webkit-backface-visibility: hidden;
}
.plane:hover {
opacity: 1;
}
</style>
<style>
#codepen-footer, #codepen-footer * {
-webkit-box-sizing: border-box !important;
-moz-box-sizing: border-box !important;
box-sizing: border-box !important;
}
#codepen-footer {
display: block !important;
position: fixed !important;
bottom: 0 !important;
left: 0 !important;
width: 100% !important;
padding: 0 10px !important;
margin: 0 !important;
height: 30px !important;
line-height: 30px !important;
font-size: 12px !important;
color: #eeeeee !important;
background-color: #505050 !important;
text-align: left !important;
background: -webkit-linear-gradient(top, #505050, #383838) !important;
background: -moz-linear-gradient(top, #505050, #383838) !important;
background: -ms-linear-gradient(top, #505050, #383838) !important;
background: -o-linear-gradient(top, #505050, #383838) !important;
border-top: 1px solid black !important;
border-bottom: 1px solid black !important;
border-radius: 0 !important;
box-shadow: inset 0 1px 0 #6e6e6e, 0 2px 2px rgba(0, 0, 0, 0.4) !important;
z-index: 300 !important;
font-family: "Lucida Grande", "Lucida Sans Unicode", Tahoma, sans-serif !important;
letter-spacing: 0 !important;
word-spacing: 0 !important;
}
#codepen-footer a {
color: #a7a7a7 !important;
text-decoration: none !important;
}
#codepen-footer a:hover {
color: white !important;
}
</style>
<script src="http://codepen.io/javascripts/libs/prefixfree.min.js"></script>
</head>
<body>
<div id='discription'>
<h1>CSS3 3D transfrom, mouse animation with centering demo</h1>
</div>
<div id='stage'>
<div id='world'>
</div>
</div>
<script src="http://codepen.io/javascripts/libs/modernizr.js"></script>
<script>
(function() {
// requestAnim shim layer by Paul Irish
var requestAnimFrame = (function() {
return window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.oRequestAnimationFrame ||
window.msRequestAnimationFrame ||
function(/* function */ callback, /* DOMElement */ element){
window.setTimeout(callback, 1000 / 60);
};
})();
var Util = (function() {
var mat = new WebKitCSSMatrix();
mats = [ mat,
mat.rotateAxisAngle(0,1,0,90),
mat.rotateAxisAngle(0,1,0,180),
mat.rotateAxisAngle(0,1,0,-90),
mat.rotateAxisAngle(1,0,0,90),
mat.rotateAxisAngle(1,0,0,-90) ];
return {
createBox : function(size, parentId) { // width = height = size;
var parent = document.getElementById(parentId);
var box = document.createElement('div');
box.className = 'box';
for(var i = 0; i < 6 ; ++i) {
var div = document.createElement('div');
div.className = 'plane';
var m = mats[i];
div.style.webkitTransform = m.translate(0,0,size/2);
div.style.width = div.style.height = size + 'px';
box.appendChild(div);
}
parent.appendChild(box);
return box;
},
createBigBox : function( cubeNum, parentId, cubeSize, span ) {
var size = cubeSize + span;
var offset = (size * cubeNum - span) * -0.5;
var offsetZ = -size * (cubeNum - 1) * 0.5;
for(var i = 0; i < cubeNum; i++ ) {
for(var j = 0; j < cubeNum; j++) {
for(var k = 0; k < cubeNum; k++) {
var box = Util.createBox(cubeSize, parentId );
box.style.webkitTransform = 'translateX(' + ( offset + size * i ) + 'px) ' +
'translateY(' + ( offset + size * j ) + 'px) ' +
'translateZ(' + ( offsetZ + size * k ) + 'px)';
}
}
}
}
};
})();
var Handler = (function() {
function handler(worldId) {
var self = this;
this.resize();
world = document.getElementById(worldId);
}
var mouseX = 0,
mouseY = 0,
mouseXBuffer = 0,
mouseYBuffer = 0,
halfW = 0,
halfH = 0,
world,
worldView = new WebKitCSSMatrix();
handler.prototype.update = function() {
mouseXBuffer += ( mouseX - mouseXBuffer ) * 0.1;
mouseYBuffer += ( mouseY - mouseYBuffer ) * 0.1;
var rotateY = ( mouseXBuffer - halfW ) / halfW * 180,
rotateX = ( mouseYBuffer - halfH ) / halfH * 180;
world.style.WebkitTransform = worldView.translate(halfW,halfH ,0)
.rotateAxisAngle(0,1,0, rotateY)
.rotateAxisAngle(1,0,0, rotateX);
}
handler.prototype.resize = function() {
halfW = window.innerWidth / 2;
halfH = window.innerHeight / 2;
var stage = document.getElementById('stage');
stage.style.webkitPerspectiveOrigin = halfW + 'px ' + halfH + 'px';
}
window.onresize = function(event) {
handler.prototype.resize();
}
window.onmousemove = function (event) {
mouseX = event.clientX;
mouseY = event.clientY;
}
return handler;
})();
// init scripts
Util.createBigBox(3, 'world', 40, 30);
var handler = new Handler('world');
(function animloop() {
handler.update();
requestAnimFrame(animloop);
})();
})();
</script>
<div id="codepen-footer">
<a style="color: #f73535 !important;" href="https://codepen.wufoo.com/forms/m7x3r3/def/field14=" onclick="window.open(this.href, null, 'height=517, width=680, toolbar=0, location=0, status=1, scrollbars=1, resizable=1'); return false">Report Abuse</a>
&nbsp;
<a href="/smagch/pen/centering-demo/30">Edit this Pen</a>
</div>
</body>
</html>
// requestAnim shim layer by Paul Irish
var requestAnimFrame = (function() {
return window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.oRequestAnimationFrame ||
window.msRequestAnimationFrame ||
function(/* function */ callback, /* DOMElement */ element){
window.setTimeout(callback, 1000 / 60);
};
})();
var Util = (function() {
var mat = new WebKitCSSMatrix();
mats = [ mat,
mat.rotateAxisAngle(0,1,0,90),
mat.rotateAxisAngle(0,1,0,180),
mat.rotateAxisAngle(0,1,0,-90),
mat.rotateAxisAngle(1,0,0,90),
mat.rotateAxisAngle(1,0,0,-90) ];
return {
createBox : function(size, parentId) { // width = height = size;
var parent = document.getElementById(parentId);
var box = document.createElement('div');
box.className = 'box';
for(var i = 0; i < 6 ; ++i) {
var div = document.createElement('div');
div.className = 'plane';
var m = mats[i];
div.style.webkitTransform = m.translate(0,0,size/2);
div.style.width = div.style.height = size + 'px';
box.appendChild(div);
}
parent.appendChild(box);
return box;
},
createBigBox : function( cubeNum, parentId, cubeSize, span ) {
var size = cubeSize + span;
var offset = (size * cubeNum - span) * -0.5;
var offsetZ = -size * (cubeNum - 1) * 0.5;
for(var i = 0; i < cubeNum; i++ ) {
for(var j = 0; j < cubeNum; j++) {
for(var k = 0; k < cubeNum; k++) {
var box = Util.createBox(cubeSize, parentId );
box.style.webkitTransform = 'translateX(' + ( offset + size * i ) + 'px) ' +
'translateY(' + ( offset + size * j ) + 'px) ' +
'translateZ(' + ( offsetZ + size * k ) + 'px)';
}
}
}
}
};
})();
var Handler = (function() {
function handler(worldId) {
var self = this;
this.resize();
world = document.getElementById(worldId);
}
var mouseX = 0,
mouseY = 0,
mouseXBuffer = 0,
mouseYBuffer = 0,
halfW = 0,
halfH = 0,
world,
worldView = new WebKitCSSMatrix();
handler.prototype.update = function() {
mouseXBuffer += ( mouseX - mouseXBuffer ) * 0.1;
mouseYBuffer += ( mouseY - mouseYBuffer ) * 0.1;
var rotateY = ( mouseXBuffer - halfW ) / halfW * 180,
rotateX = ( mouseYBuffer - halfH ) / halfH * 180;
world.style.WebkitTransform = worldView.translate(halfW,halfH ,0)
.rotateAxisAngle(0,1,0, rotateY)
.rotateAxisAngle(1,0,0, rotateX);
}
handler.prototype.resize = function() {
halfW = window.innerWidth / 2;
halfH = window.innerHeight / 2;
var stage = document.getElementById('stage');
stage.style.webkitPerspectiveOrigin = halfW + 'px ' + halfH + 'px';
}
window.onresize = function(event) {
handler.prototype.resize();
}
window.onmousemove = function (event) {
mouseX = event.clientX;
mouseY = event.clientY;
}
return handler;
})();
// init scripts
Util.createBigBox(3, 'world', 40, 30);
var handler = new Handler('world');
(function animloop() {
handler.update();
requestAnimFrame(animloop);
})();
<div id='discription'>
<h1>CSS3 3D transfrom, mouse animation with centering demo</h1>
</div>
<div id='stage'>
<div id='world'>
</div>
</div>
body {
background-color: black;
color: white;
font-family: 'Lucida Grande', Verdana, Arial;
font-size: 12px;
background-image: -webkit-gradient(radial,
50% 500, 1,
50% 500, 400,
from(rgba(255, 255, 255, 0.3)),
to(rgba(255, 255, 255, 0)));
background-repeat: no-repeat;
}
fieldset {
width: 200px;
}
#discription {
position: absolute;
}
#stage {
margin: 0;
width: 0;
height: 0;
-webkit-perspective: 800;
-webkit-transform-style: preserve-3d;
}
#world {
margin: 0;
position: absolute;
-webkit-transform-style: preserve-3d;
}
.box {
margin: 0;
position: absolute;
-webkit-transform-style: preserve-3d;
}
.plane {
margin: 0;
position: absolute;
background-color: blue;
width: 100px;
height: 100px;
opacity: 0.6;
border: thin solid red;
-webkit-transform-style: flat;
-webkit-backface-visibility: hidden;
}
.plane:hover {
opacity: 1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment