Created
June 20, 2012 20:05
-
-
Save smagch/2961886 to your computer and use it in GitHub Desktop.
A web page created at CodePen.io
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="UTF-8"> | |
<title>CSS3 3D the first person view demo using glmatrix · 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; | |
overflow: hidden; | |
} | |
#stage { | |
margin: 0 auto; | |
position: relative; | |
-webkit-perspective: 800px; | |
-webkit-transform-style: preserve-3d; | |
} | |
#world { | |
margin: 0 auto; | |
position: absolute; | |
-webkit-transform-style: preserve-3d; | |
} | |
.box { | |
margin: 0 auto; | |
position: absolute; | |
-webkit-transform-style: preserve-3d; | |
} | |
.plane { | |
margin: 0 auto; | |
position: absolute; | |
background-color:blue; | |
opacity: 0.6; | |
border: thin solid red; | |
-webkit-transform-style: flat; | |
-webkit-backface-visibility: hidden; | |
} | |
.plane:hover { | |
background-color:green; | |
} | |
@-webkit-keyframes y-spin { | |
0% { -webkit-transform: rotateY(0deg); } | |
50% { -webkit-transform: rotateY(180deg); } | |
100% { -webkit-transform: rotateY(360deg); } | |
} | |
</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='stage'> | |
<div id='world'> | |
</div> | |
</div> | |
<script src="http://codepen.io/javascripts/libs/modernizr.js"></script> | |
<script src="https://raw.github.com/toji/gl-matrix/master/gl-matrix-min.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) ]; | |
var exports = {}; | |
var createPlane = exports.createPlane = function( size, parent) { | |
if(typeof parent === 'string') { | |
parent = document.getElementById(parent); | |
} | |
var div = document.createElement(div); | |
div.className = 'plane'; | |
div.style.height = div.style.width = size + 'px'; | |
parent.appendChild(div); | |
return div; | |
}; | |
var createBox = exports.createBox = function(size, parent) { // width = height = size; | |
if( typeof parent === 'string') { | |
parent = document.getElementById(parent); | |
} | |
var box = document.createElement('div'); | |
box.className = 'box'; | |
for(var i = 0; i < 6 ; ++i) { | |
var div = createPlane(size, box); | |
var m = mats[i]; | |
div.style.webkitTransform = m.translate(0,0,size/2); | |
} | |
parent.appendChild(box); | |
return box; | |
}; | |
exports.createBigBox = function( cubeNum, parent, cubeSize, span ) { | |
if(typeof parent === 'string') { | |
parent = document.getElementById(parent); | |
} | |
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 = createBox(cubeSize, parent ); | |
box.style.webkitTransform = 'translateX(' + ( offset + size * i ) + 'px) ' + | |
'translateY(' + ( offset + size * j ) + 'px) ' + | |
'translateZ(' + ( offsetZ + size * k ) + 'px)'; | |
} | |
} | |
} | |
}; | |
return exports; | |
})(); | |
var Handler = (function() { | |
var handler = function(worldId) { | |
this.resize(); | |
world = document.getElementById(worldId); | |
worldView = new WebKitCSSMatrix(); | |
} | |
var mouseX = 0, | |
mouseY = 0, | |
mouseXBuffer = 0, | |
mouseYBuffer = 0, | |
world, | |
worldView, | |
pos=[0,0,-800], | |
TO_RADIAN = Math.PI / 180, | |
modelView = mat4.create(); | |
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; | |
var rotateY = -( mouseXBuffer - halfW ) / halfW * 30, | |
rotateX = ( mouseYBuffer - halfH ) / halfH * 30; | |
mat4.identity(modelView); | |
mat4.rotate(modelView, TO_RADIAN * rotateY, [0,1,0]); | |
mat4.rotate(modelView, TO_RADIAN * rotateX, [1,0,0]); | |
mat4.translate(modelView, [0,0,-800]);//stage's -webkit-perspective: 800px; | |
var newPos = [0,0,0]; | |
mat4.multiplyVec3(modelView, [0,0,0], newPos); | |
world.style.webkitTransform = worldView | |
.translate(halfW,halfH,0) | |
.rotateAxisAngle(1,0,0, -rotateX) | |
.rotateAxisAngle(0,1,0, -rotateY) | |
.translate(-newPos[0],-newPos[1],-newPos[2]) | |
.translate(pos[0],pos[1],pos[2]) | |
; | |
} | |
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; | |
}; | |
window.onmousedown = function(event) { | |
pos[2] -= 100; | |
}; | |
return handler; | |
})(); | |
Util.createBigBox(3, 'world', 140, 10); | |
var world = document.getElementById('world'); | |
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> | |
| |
<a href="/smagch/pen/5/2">Edit this Pen</a> | |
</div> | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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) ]; | |
var exports = {}; | |
var createPlane = exports.createPlane = function( size, parent) { | |
if(typeof parent === 'string') { | |
parent = document.getElementById(parent); | |
} | |
var div = document.createElement(div); | |
div.className = 'plane'; | |
div.style.height = div.style.width = size + 'px'; | |
parent.appendChild(div); | |
return div; | |
}; | |
var createBox = exports.createBox = function(size, parent) { // width = height = size; | |
if( typeof parent === 'string') { | |
parent = document.getElementById(parent); | |
} | |
var box = document.createElement('div'); | |
box.className = 'box'; | |
for(var i = 0; i < 6 ; ++i) { | |
var div = createPlane(size, box); | |
var m = mats[i]; | |
div.style.webkitTransform = m.translate(0,0,size/2); | |
} | |
parent.appendChild(box); | |
return box; | |
}; | |
exports.createBigBox = function( cubeNum, parent, cubeSize, span ) { | |
if(typeof parent === 'string') { | |
parent = document.getElementById(parent); | |
} | |
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 = createBox(cubeSize, parent ); | |
box.style.webkitTransform = 'translateX(' + ( offset + size * i ) + 'px) ' + | |
'translateY(' + ( offset + size * j ) + 'px) ' + | |
'translateZ(' + ( offsetZ + size * k ) + 'px)'; | |
} | |
} | |
} | |
}; | |
return exports; | |
})(); | |
var Handler = (function() { | |
var handler = function(worldId) { | |
this.resize(); | |
world = document.getElementById(worldId); | |
worldView = new WebKitCSSMatrix(); | |
} | |
var mouseX = 0, | |
mouseY = 0, | |
mouseXBuffer = 0, | |
mouseYBuffer = 0, | |
world, | |
worldView, | |
pos=[0,0,-800], | |
TO_RADIAN = Math.PI / 180, | |
modelView = mat4.create(); | |
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; | |
var rotateY = -( mouseXBuffer - halfW ) / halfW * 30, | |
rotateX = ( mouseYBuffer - halfH ) / halfH * 30; | |
mat4.identity(modelView); | |
mat4.rotate(modelView, TO_RADIAN * rotateY, [0,1,0]); | |
mat4.rotate(modelView, TO_RADIAN * rotateX, [1,0,0]); | |
mat4.translate(modelView, [0,0,-800]);//stage's -webkit-perspective: 800px; | |
var newPos = [0,0,0]; | |
mat4.multiplyVec3(modelView, [0,0,0], newPos); | |
world.style.webkitTransform = worldView | |
.translate(halfW,halfH,0) | |
.rotateAxisAngle(1,0,0, -rotateX) | |
.rotateAxisAngle(0,1,0, -rotateY) | |
.translate(-newPos[0],-newPos[1],-newPos[2]) | |
.translate(pos[0],pos[1],pos[2]) | |
; | |
} | |
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; | |
}; | |
window.onmousedown = function(event) { | |
pos[2] -= 100; | |
}; | |
return handler; | |
})(); | |
Util.createBigBox(3, 'world', 140, 10); | |
var world = document.getElementById('world'); | |
var handler = new Handler('world'); | |
(function animloop() { | |
handler.update(); | |
requestAnimFrame(animloop); | |
})(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div id='stage'> | |
<div id='world'> | |
</div> | |
</div> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
overflow: hidden; | |
} | |
#stage { | |
margin: 0 auto; | |
position: relative; | |
-webkit-perspective: 800px; | |
-webkit-transform-style: preserve-3d; | |
} | |
#world { | |
margin: 0 auto; | |
position: absolute; | |
-webkit-transform-style: preserve-3d; | |
} | |
.box { | |
margin: 0 auto; | |
position: absolute; | |
-webkit-transform-style: preserve-3d; | |
} | |
.plane { | |
margin: 0 auto; | |
position: absolute; | |
background-color:blue; | |
opacity: 0.6; | |
border: thin solid red; | |
-webkit-transform-style: flat; | |
-webkit-backface-visibility: hidden; | |
} | |
.plane:hover { | |
background-color:green; | |
} | |
@-webkit-keyframes y-spin { | |
0% { -webkit-transform: rotateY(0deg); } | |
50% { -webkit-transform: rotateY(180deg); } | |
100% { -webkit-transform: rotateY(360deg); } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment