<html>
<head>
<title>Handle Resize</title>
<style>
body { margin: 0; }
canvas { width: 100%; height: 100% }
</style>
</head>
<body onload="init()">
<!--script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r82/three.min.js"></script-->
<!--script src="js/three.min.js"></script-->
<script src="js/three.js"></script>
<script src="js/Detector.js"></script>
<script>
var success = true;
if (!Detector.webgl) {
var warning = Detector.getWebGLErrorMessage();
document.body.appendChild(warning);
success = false;
}
//basic initialization
var aspectRatio;
var camera;
var renderer;
var canvas;
var scene;
function init() {
if (success) {
init3D();
grid();
addCubes();
render();
}
}
//set up 3D
function init3D() {
aspectRatio = window.innerWidth/window.innerHeight;
camera = new THREE.PerspectiveCamera(75, aspectRatio, 0.1, 1000);
renderer = new THREE.WebGLRenderer({antialias: true, alpha: true});
renderer.setSize(window.innerWidth, window.innerHeight);
canvas = renderer.domElement;
document.body.appendChild(canvas); //can also be appended to a DIV
scene = new THREE.Scene();
renderer.setClearColor("#000000",1);
}
var global3DBaseX = 0;
var global3DBaseY = 0;
var global3DBaseZ = 0;
var global3DLookTarget = new THREE.Vector3(global3DBaseX, global3DBaseY, global3DBaseZ);
//define a line in 3D
function define3DLine(x1,y1,z1,x2,y2,z2,lineColor) {
var material = new THREE.LineBasicMaterial({ color: lineColor });
var geometry = new THREE.Geometry();
geometry.vertices.push(
new THREE.Vector3(x1, y1, z1),
new THREE.Vector3(x2, y2, z2)
);
var line = new THREE.Line(geometry, material);
scene.add(line);
return line;
}
function grid() {
//build a grid
var xMin = -3;
var xMax = 3;
var xInc = 0.5;
var zMin = -3;
var zMax = 3;
var zInc = 0.5;
var gridLineColor;
for (var z=zMin; z<=zMax; z+=zInc) {
if (z != 0) {
gridLineColor = "#0000FF";
} else {
gridLineColor = "#FF0000";
}
define3DLine(xMin,0,z,xMax,0,z,gridLineColor);
}
for (var x=xMin; x<=xMax; x+=xInc) {
if (x != 0) {
gridLineColor = "#0000FF";
} else {
gridLineColor = "#FF0000";
}
define3DLine(x,0,zMin,x,0,zMax,gridLineColor);
}
}
var cube1;
var cube2;
var cube3;
function addCubes() {
//add a green cube at 0, 0, 0
var geometry = new THREE.BoxGeometry(1, 1, 1);
var material1 = new THREE.MeshBasicMaterial({ wireframe: true, color: 0x00ff00 });
cube1 = new THREE.Mesh( geometry, material1 );
scene.add(cube1);
//add a yellow cube at 2, 0, -2
var material2 = new THREE.MeshBasicMaterial({ wireframe: true, color: 0xffff00 });
cube2 = new THREE.Mesh( geometry, material2 );
cube2.position.set(2, 0, -2);
scene.add(cube2);
//add a cyan cube at -2, 2, 2
var material3 = new THREE.MeshBasicMaterial({ wireframe: true, color: 0x00ffff });
cube3 = new THREE.Mesh( geometry, material3 );
cube3.position.set(-2, 2, 2);
scene.add(cube3);
}
//initialization for actions
var twoPi = Math.PI * 2.0;
var halfPi = Math.PI * 0.5;
var cRadius = 5; //5;
var cHeight = 4;
var cInc = 0.005;
var cAngle = (1.5 * Math.PI) - cInc;
//do this for every update
function cylRotations() {
cAngle += cInc;
if (cAngle > twoPi) {
cAngle -= twoPi;
} else if (cAngle < 0.0) {
cAngle += twoPi;
}
var x = cRadius * Math.cos(cAngle);
var z = -cRadius * Math.sin(cAngle);
var y = cHeight;
//update location of viewport camera
camera.position.set(x,y,z);
camera.lookAt(global3DLookTarget);
}
var cYIncrement = 0.1;
var cYMax = 10;
var cYMin = -2;
var cRIncrement = 0.1;
var cRMax = 20;
var cRMin = 2;
function moveUp() {
cHeight += cYIncrement;
if (cHeight > cYMax) {
cHeight = cYMax;
}
}
function moveDown() {
cHeight -= cYIncrement;
if (cHeight < cYMin) {
cHeight = cYMin;
}
}
function moveIn() {
cRadius -= cRIncrement;
if (cRadius < cRMin) {
cRadius = cRMin;
}
}
function moveOut() {
cRadius += cRIncrement;
if (cRadius > cRMax) {
cRadius = cRMax;
}
}
function restoreDefaults() {
cHeight = 4;
cRadius = 5;
}
document.onkeydown = checkKey;
function checkKey(e) {
e = e || window.event; //up 38 down 40
if (e.keyCode == '37') { //left arrow
} else if (e.keyCode == '39') { //right arrow
} else if (e.keyCode == '38') { //up arrow
moveUp();
} else if (e.keyCode == '40') { //down arrow
moveDown();
} else if (e.keyCode == '66') { //b key
moveOut();
} else if (e.keyCode == '70') { //f key
moveIn();
} else if (e.keyCode == '36') { //home key
restoreDefaults();
}
}
//from: http://stackoverflow.com/questions/641857/javascript-window-resize-event
/*
function: addEvent
@param: obj (Object)(Required)
- The object which you wish to attach your event to.
@param: type (String)(Required)
- The type of event you wish to establish.
@param: callback (Function)(Required)
- The method you wish to be called by your event listener.
@param: eventReturn (Boolean)(Optional)
- Whether you want the event object returned to your callback method.
*/
var addEvent = function(obj, type, callback, eventReturn) {
if(obj == null || typeof obj === 'undefined')
return;
if(obj.addEventListener)
obj.addEventListener(type, callback, eventReturn ? true : false);
else if(obj.attachEvent)
obj.attachEvent("on" + type, callback);
else
obj["on" + type] = callback;
};
//An example call to the new addEvent function:
var watch = function(evt) {
//Older browser versions may return evt.srcElement
//Newer browser versions should return evt.currentTarget
var dimensions = {
height: (evt.srcElement || evt.currentTarget).innerHeight,
width: (evt.srcElement || evt.currentTarget).innerWidth
};
//https://threejs.org/docs/api/cameras/PerspectiveCamera.html
renderer.setSize(dimensions.width, dimensions.height);
camera.aspect = dimensions.width / dimensions.height;
//var newFov = calcFovAngle(dimensions.height);
//camera.fov = newFov;
camera.updateProjectionMatrix();
};
addEvent(window, 'resize', watch, true);
//animation loop
function render() {
//each cube rotates around a different axis
cube1.rotation.x += 0.01;
cube2.rotation.y += 0.01;
cube3.rotation.z += 0.01;
cylRotations();
renderer.render(scene, camera);
requestAnimationFrame(render);
}
</script>
</body>
</html>