The Cube
How do I make an the cube?
Three.js experiment. What is a the cube? How do you make a the cube? This script and codes were developed by François Chazal on 21 September 2022, Wednesday.
The Cube - Script Codes HTML Codes
<!DOCTYPE html>
<html >
<head> <meta charset="UTF-8"> <title>The Cube</title> <script src="https://s.codepen.io/assets/libs/modernizr.js" type="text/javascript"></script> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/normalize/5.0.0/normalize.min.css"> <style> /* NOTE: The styles were added inline because Prefixfree needs access to your styles and they must be inlined if they are on local disk! */ @import url(https://fonts.googleapis.com/css?family=Roboto+Condensed:300italic,400italic,700italic,400,300,700);
html { font-size: 11px;
}
body { overflow: hidden; min-height: 100%; font: 1em/1.5em 'Roboto Condensed', sans-serif;
}
a { text-decoration: inherit; color: inherit;
}
#content { position: fixed; top: 0px; left: 0px; width: 100%; height: 100%;
}
#content #canvas { width: 100%; height: 100%; background: #eee;
}
#about { position: fixed; left: 0px; bottom: 0px; width: 100%; height: 50px; color: #fff; background: rgba(0, 0, 0, 0.8);
}
#about #about-title { position: absolute; left: 25px; bottom: 25px; font-size: 2em; font-weight: 700;
}
#about #about-author { position: absolute; left: 25px; top: 27px; font-size: .9em; font-weight: 300;
}
#console { position: absolute; top: 0px; left: 0px; width: 100%; padding: 5px 10px; color: #000; font-size: 1em; font-style: italic; font-weight: 300;
} </style> <script src="https://cdnjs.cloudflare.com/ajax/libs/prefixfree/1.0.7/prefixfree.min.js"></script>
</head>
<body> <section id="content"></section>
<section id="about"> <div id="about-title">The Cube</div> <div id="about-author">a <b>three.js</b> experimentation by François Chazal (<a href="https://www.twitter.com/fchazal">@fchazal</a>)</div>
</section>
<section id="console">CONSOLE</section> <script src="js/index.js"></script>
</body>
</html>
The Cube - Script Codes CSS Codes
@import url(https://fonts.googleapis.com/css?family=Roboto+Condensed:300italic,400italic,700italic,400,300,700);
html { font-size: 11px;
}
body { overflow: hidden; min-height: 100%; font: 1em/1.5em 'Roboto Condensed', sans-serif;
}
a { text-decoration: inherit; color: inherit;
}
#content { position: fixed; top: 0px; left: 0px; width: 100%; height: 100%;
}
#content #canvas { width: 100%; height: 100%; background: #eee;
}
#about { position: fixed; left: 0px; bottom: 0px; width: 100%; height: 50px; color: #fff; background: rgba(0, 0, 0, 0.8);
}
#about #about-title { position: absolute; left: 25px; bottom: 25px; font-size: 2em; font-weight: 700;
}
#about #about-author { position: absolute; left: 25px; top: 27px; font-size: .9em; font-weight: 300;
}
#console { position: absolute; top: 0px; left: 0px; width: 100%; padding: 5px 10px; color: #000; font-size: 1em; font-style: italic; font-weight: 300;
}
The Cube - Script Codes JS Codes
/* REQUIREMENTS ******************************/
requireScripts( [ 'https://dat-gui.googlecode.com/git/build/dat.gui.min.js', 'https://threejs.org/build/three.min.js' ], load
);
addEvent(window, "DOMContentLoaded", load);
/* BEGINNING OF LOADING BLOCK ****************/
var v = 0;
function load(e) { if (++v != 3) return; // wait for DOM and SCRIPTS Console.init('#console'); Parameters.init(); World.init('#content'); World.add(new Cube()); //World.add(new Plane()); World.render();
}
/* END OF LOADING BLOCK **********************/
/* ELEMENT : Parameters
**********************************************/
var Parameters = { gui: null, init: function() { this.gui = new dat.GUI({ autoPlace: true }); this.gui.domElement.id = 'parameters'; },
};
/* ELEMENT : World
**********************************************/
var World = { domElement: null, scene: null, camera: null, renderer: null, objects: [], light: null, controls: null, init: function(id) { this.domElement = document.querySelector(id);; if (false && window.WebGLRenderingContext) this.renderer = new THREE.WebGLRenderer({ antialias: true }); else this.renderer = new THREE.CanvasRenderer(); this.renderer.setSize(this.domElement.offsetWidth, this.domElement.offsetHeight); this.renderer.setClearColorHex(0xeeeeee); this.domElement.appendChild(this.renderer.domElement); this.scene = new THREE.Scene(); this.camera = new Camera(this.domElement.offsetWidth / this.domElement.offsetHeight); OrbitControls.prototype = Object.create( THREE.EventDispatcher.prototype ); this.controls = new OrbitControls( this.camera.object, this.renderer.domElement ); var light = new THREE.PointLight(0xff0000, 1, 100); light.position.set(5, 0, 0); this.scene.add( light ); var light = new THREE.PointLight(0x00ff00, 1, 100); light.position.set(0, 5, 0); this.scene.add( light ); var light = new THREE.PointLight(0x0000ff, 1, 100); light.position.set(0, 0, 5); this.scene.add( light ); var axes = new THREE.AxisHelper(1); axes.position.set(-0.5,-0.5,-0.5);
/* axes.rotateX(15*Math.PI/180); axes.rotateY(15*Math.PI/180);
*/ this.scene.add(axes); }, add: function(object) { this.objects.push(object); this.scene.add(object.mesh); }, render: function() { window.requestAnimationFrame(function() { World.render(); }); Console.log('rendering'); for (var id in this.objects) { this.objects[id].update(); } //this.camera.update(); this.renderer.render(this.scene, this.camera.object); this.controls.update(); }
};
/* OBJECT : Camera
**********************************************/
var Camera = function(ratio) { this.object = new THREE.PerspectiveCamera(75, ratio, 1, 1000); this.object.position.set(5,5,2); this.object.up.set(0,0,1); this.object.lookAt(new THREE.Vector3(0,0,0)); return this;
}
Camera.prototype = { angle: 0.01, object: null, update: function() { }
}
/* OBJECT : Plane
**********************************************/
var Plane = function() { this.geometry = new THREE.CubeGeometry(10,10,0.01);
// this.geometry = new THREE.PlaneGeometry(20,20,1,1); this.material = new THREE.MeshBasicMaterial({ color: 0xdddddd }); this.mesh = new THREE.Mesh(this.geometry, this.material);
}
Plane.prototype = { geometry: null, material: null, mesh: null, update: function() { }
}
/* OBJECT : Cube
**********************************************/
var Cube = function() { this.geometry = new THREE.CubeGeometry(1,1,1); this.material = new THREE.MeshPhongMaterial({ color: 0xaaaaaa, vertexColors: THREE.FaceColors }); this.mesh = new THREE.Mesh(this.geometry, this.material); this.mesh.position.set(1,1,5);
}
Cube.prototype = { geometry: null, material: null, mesh: null, speed: 0.0, accel: -20, update: function() { this.mesh.rotation.x+=(0.02); this.mesh.rotation.y+=(0.02); this.speed += this.accel * 1e-2; this.mesh.position.z += this.speed * 1e-2; if (this.mesh.position.z < 0) { this.mesh.position.z *= -1; this.speed *= -0.8; } Console.log(this.mesh.rotation.x); }
}
/* ELEMENT : Console
**********************************************/
var Console = { element: null, init: function(id) { this.element = document.querySelector(id); }, log: function(html) { this.element.innerHTML = '<b>LOG:</b> '+html + ' ' + (new Date().getTime()); }, err: function(html) { this.element.innerHTML = '<b>ERROR:</b> '+html; }
}
/* LIBRARY : Animation Framerate
***********************************************/
window.requestAnimationFrame = (function() { return window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || window.oRequestAnimationFrame || window.msRequestAnimationFrame || function (callback) { window.setTimeout(callback, 1000 / 60); // smooth transition };
})();
/* LIBRARY : Requirements Management
***********************************************/
function requireScripts(scripts, callback) { for (var id in scripts) { var script = document.createElement('script'); script.type = 'text/javascript'; script.src = scripts[id] + '?' + (new Date().getTime()); if (callback) { script.onreadystatechange = callback; script.onload = script.onreadystatechange; } document.getElementsByTagName('head')[0].appendChild(script); }
}
/* LIBRARY : Event Management
***********************************************/
function addEvent(eventTrigger, eventName, eventHandler, scopeElement) { var scopedEventHandler = null; if (scopeElement !== undefined) scopedEventHandler = function(e) { eventHandler.apply(scopeElement, [e]); } else scopedEventHandler = eventHandler; if(document.addEventListener) eventTrigger.addEventListener(eventName, scopedEventHandler, false); else if(document.attachEvent) eventTrigger.attachEvent("on"+eventName, scopedEventHandler);
}
function fireEvent(eventTrigger, eventName, eventData) { var evt = null; if (typeof Event !== 'undefined') { evt = new Event(eventName); } else { evt = document.createEvent("Event"); evt.initEvent(eventName, true, true); } evt.data = eventData; eventTrigger.dispatchEvent(evt);
}
/******/
OrbitControls = function ( object, domElement ) { this.object = object; this.domElement = ( domElement !== undefined ) ? domElement : document; // API this.enabled = true; this.center = new THREE.Vector3(); this.userZoom = true; this.userZoomSpeed = 1.0; this.userRotate = true; this.userRotateSpeed = 1.0; this.userPan = true; this.userPanSpeed = 2.0; this.autoRotate = false; this.autoRotateSpeed = 2.0; // 30 seconds per round when fps is 60 this.minPolarAngle = 0; // radians this.maxPolarAngle = Math.PI; // radians this.minDistance = 0; this.maxDistance = Infinity; // 65 /*A*/, 83 /*S*/, 68 /*D*/ this.keys = { LEFT: 37, UP: 38, RIGHT: 39, BOTTOM: 40, ROTATE: 65, ZOOM: 83, PAN: 68 }; // internals var scope = this; var EPS = 0.000001; var PIXELS_PER_ROUND = 1800; var rotateStart = new THREE.Vector2(); var rotateEnd = new THREE.Vector2(); var rotateDelta = new THREE.Vector2(); var zoomStart = new THREE.Vector2(); var zoomEnd = new THREE.Vector2(); var zoomDelta = new THREE.Vector2(); var phiDelta = 0; var thetaDelta = 0; var scale = 1; var lastPosition = new THREE.Vector3(); var STATE = { NONE: -1, ROTATE: 0, ZOOM: 1, PAN: 2 }; var state = STATE.NONE; // events var changeEvent = { type: 'change' }; this.rotateLeft = function ( angle ) { if ( angle === undefined ) { angle = getAutoRotationAngle(); } thetaDelta -= angle; }; this.rotateRight = function ( angle ) { if ( angle === undefined ) { angle = getAutoRotationAngle(); } thetaDelta += angle; }; this.rotateUp = function ( angle ) { if ( angle === undefined ) { angle = getAutoRotationAngle(); } phiDelta -= angle; }; this.rotateDown = function ( angle ) { if ( angle === undefined ) { angle = getAutoRotationAngle(); } phiDelta += angle; }; this.zoomIn = function ( zoomScale ) { if ( zoomScale === undefined ) { zoomScale = getZoomScale(); } scale /= zoomScale; }; this.zoomOut = function ( zoomScale ) { if ( zoomScale === undefined ) { zoomScale = getZoomScale(); } scale *= zoomScale; }; this.pan = function ( distance ) { distance.transformDirection( this.object.matrix ); distance.multiplyScalar( scope.userPanSpeed ); this.object.position.add( distance ); this.center.add( distance ); }; this.update = function () { var position = this.object.position; var offset = position.clone().sub( this.center ); // angle from z-axis around y-axis var theta = Math.atan2( offset.x, offset.z ); // angle from y-axis var phi = Math.atan2( Math.sqrt( offset.x * offset.x + offset.z * offset.z ), offset.y ); if ( this.autoRotate ) { this.rotateLeft( getAutoRotationAngle() ); } theta += thetaDelta; phi += phiDelta; // restrict phi to be between desired limits phi = Math.max( this.minPolarAngle, Math.min( this.maxPolarAngle, phi ) ); // restrict phi to be betwee EPS and PI-EPS phi = Math.max( EPS, Math.min( Math.PI - EPS, phi ) ); var radius = offset.length() * scale; // restrict radius to be between desired limits radius = Math.max( this.minDistance, Math.min( this.maxDistance, radius ) ); offset.x = radius * Math.sin( phi ) * Math.sin( theta ); offset.y = radius * Math.cos( phi ); offset.z = radius * Math.sin( phi ) * Math.cos( theta ); position.copy( this.center ).add( offset ); this.object.lookAt( this.center ); thetaDelta = 0; phiDelta = 0; scale = 1; if ( lastPosition.distanceTo( this.object.position ) > 0 ) { this.dispatchEvent( changeEvent ); lastPosition.copy( this.object.position ); } }; function getAutoRotationAngle() { return 2 * Math.PI / 60 / 60 * scope.autoRotateSpeed; } function getZoomScale() { return Math.pow( 0.95, scope.userZoomSpeed ); } function onMouseDown( event ) { if ( scope.enabled === false ) return; if ( scope.userRotate === false ) return; event.preventDefault(); if ( state === STATE.NONE ) { if ( event.button === 0 ) state = STATE.ROTATE; if ( event.button === 1 ) state = STATE.ZOOM; if ( event.button === 2 ) state = STATE.PAN; } if ( state === STATE.ROTATE ) { //state = STATE.ROTATE; rotateStart.set( event.clientX, event.clientY ); } else if ( state === STATE.ZOOM ) { //state = STATE.ZOOM; zoomStart.set( event.clientX, event.clientY ); } else if ( state === STATE.PAN ) { //state = STATE.PAN; } document.addEventListener( 'mousemove', onMouseMove, false ); document.addEventListener( 'mouseup', onMouseUp, false ); } function onMouseMove( event ) { if ( scope.enabled === false ) return; event.preventDefault(); if ( state === STATE.ROTATE ) { rotateEnd.set( event.clientX, event.clientY ); rotateDelta.subVectors( rotateEnd, rotateStart ); scope.rotateLeft( 2 * Math.PI * rotateDelta.x / PIXELS_PER_ROUND * scope.userRotateSpeed ); scope.rotateUp( 2 * Math.PI * rotateDelta.y / PIXELS_PER_ROUND * scope.userRotateSpeed ); rotateStart.copy( rotateEnd ); } else if ( state === STATE.ZOOM ) { zoomEnd.set( event.clientX, event.clientY ); zoomDelta.subVectors( zoomEnd, zoomStart ); if ( zoomDelta.y > 0 ) { scope.zoomIn(); } else { scope.zoomOut(); } zoomStart.copy( zoomEnd ); } else if ( state === STATE.PAN ) { var movementX = event.movementX || event.mozMovementX || event.webkitMovementX || 0; var movementY = event.movementY || event.mozMovementY || event.webkitMovementY || 0; scope.pan( new THREE.Vector3( - movementX, movementY, 0 ) ); } } function onMouseUp( event ) { if ( scope.enabled === false ) return; if ( scope.userRotate === false ) return; document.removeEventListener( 'mousemove', onMouseMove, false ); document.removeEventListener( 'mouseup', onMouseUp, false ); state = STATE.NONE; } function onMouseWheel( event ) { if ( scope.enabled === false ) return; if ( scope.userZoom === false ) return; var delta = 0; if ( event.wheelDelta ) { // WebKit / Opera / Explorer 9 delta = event.wheelDelta; } else if ( event.detail ) { // Firefox delta = - event.detail; } if ( delta > 0 ) { scope.zoomOut(); } else { scope.zoomIn(); } } function onKeyDown( event ) { if ( scope.enabled === false ) return; if ( scope.userPan === false ) return; switch ( event.keyCode ) { /*case scope.keys.UP:
scope.pan( new THREE.Vector3( 0, 1, 0 ) );
break;
case scope.keys.BOTTOM:
scope.pan( new THREE.Vector3( 0, - 1, 0 ) );
break;
case scope.keys.LEFT:
scope.pan( new THREE.Vector3( - 1, 0, 0 ) );
break;
case scope.keys.RIGHT:
scope.pan( new THREE.Vector3( 1, 0, 0 ) );
break;
*/ case scope.keys.ROTATE: state = STATE.ROTATE; break; case scope.keys.ZOOM: state = STATE.ZOOM; break; case scope.keys.PAN: state = STATE.PAN; break;
}
} function onKeyUp( event ) { switch ( event.keyCode ) { case scope.keys.ROTATE: case scope.keys.ZOOM: case scope.keys.PAN: state = STATE.NONE; break; } } this.domElement.addEventListener( 'contextmenu', function ( event ) { event.preventDefault(); }, false ); this.domElement.addEventListener( 'mousedown', onMouseDown, false ); this.domElement.addEventListener( 'mousewheel', onMouseWheel, false ); this.domElement.addEventListener( 'DOMMouseScroll', onMouseWheel, false ); // firefox window.addEventListener( 'keydown', onKeyDown, false ); window.addEventListener( 'keyup', onKeyUp, false );
};
Developer | François Chazal |
Username | fchazal |
Uploaded | September 21, 2022 |
Rating | 3 |
Size | 6,949 Kb |
Views | 20,240 |
Find the perfect freelance services for your business! Fiverr's mission is to change how the world works together. Fiverr connects businesses with freelancers offering digital services in 500+ categories. Find Developer!
Name | Size |
WebTechs Animated Logo | 2,503 Kb |
Parallax Scrolling | 6,472 Kb |
Obama | 223,882 Kb |
Hexagona | 3,420 Kb |
Logo | 3,497 Kb |
Folding Panel | 6,671 Kb |
World | 5,903 Kb |
Ribbon Animated Effect | 5,909 Kb |
HExa | 2,185 Kb |
Penrose Triangle CSS | 3,283 Kb |
Jasper is the AI Content Generator that helps you and your team break through creative blocks to create amazing, original content 10X faster. Discover all the ways the Jasper AI Content Platform can help streamline your creative workflows. Start For Free!
Name | Username | Size |
Google Chrome Icon using Pure CSS in one DIV | Grssam | 3,627 Kb |
Mobile first social buttons with no iframe | Alistairtweedie | 3,158 Kb |
Border-radius animation | Yukulele | 2,480 Kb |
Price | Catcode | 2,623 Kb |
CardMove | Thompsonemerson | 3,699 Kb |
Cloudy Spiral CSS animation | Hakimel | 6,587 Kb |
Blog Concept 2 | JGallardo | 2,994 Kb |
Print element on a page | Mrs_snow | 2,081 Kb |
Long Shadow Button | Uixcrazy | 3,550 Kb |
CMP5-Opdracht15 | SannevanGastel | 2,733 Kb |
Surf anonymously, prevent hackers from acquiring your IP address, send anonymous email, and encrypt your Internet connection. High speed, ultra secure, and easy to use. Instant setup. Hide Your IP Now!