canvas_ascii_effect.html 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js - ASCII Effect</title>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
  7. <style>
  8. body {
  9. font-family: Monospace;
  10. background-color: #f0f0f0;
  11. margin: 0px;
  12. overflow: hidden;
  13. }
  14. </style>
  15. </head>
  16. <body>
  17. <script src="../build/three.js"></script>
  18. <script src="js/controls/TrackballControls.js"></script>
  19. <script src="js/effects/AsciiEffect.js"></script>
  20. <script src="js/renderers/Projector.js"></script>
  21. <script src="js/renderers/CanvasRenderer.js"></script>
  22. <script src="js/libs/stats.min.js"></script>
  23. <script>
  24. var container, stats;
  25. var camera, controls, scene, renderer;
  26. var effect;
  27. var sphere, plane;
  28. var start = Date.now();
  29. init();
  30. animate();
  31. function init() {
  32. var width = window.innerWidth || 2;
  33. var height = window.innerHeight || 2;
  34. container = document.createElement( 'div' );
  35. document.body.appendChild( container );
  36. var info = document.createElement( 'div' );
  37. info.style.position = 'absolute';
  38. info.style.top = '10px';
  39. info.style.width = '100%';
  40. info.style.textAlign = 'center';
  41. info.innerHTML = 'Drag to change the view';
  42. container.appendChild( info );
  43. camera = new THREE.PerspectiveCamera( 70, width / height, 1, 1000 );
  44. camera.position.y = 150;
  45. camera.position.z = 500;
  46. controls = new THREE.TrackballControls( camera );
  47. scene = new THREE.Scene();
  48. scene.background = new THREE.Color( 0xf0f0f0 );
  49. var light = new THREE.PointLight( 0xffffff );
  50. light.position.set( 500, 500, 500 );
  51. scene.add( light );
  52. var light = new THREE.PointLight( 0xffffff, 0.25 );
  53. light.position.set( - 500, - 500, - 500 );
  54. scene.add( light );
  55. sphere = new THREE.Mesh( new THREE.SphereBufferGeometry( 200, 20, 10 ), new THREE.MeshLambertMaterial() );
  56. scene.add( sphere );
  57. // Plane
  58. plane = new THREE.Mesh( new THREE.PlaneBufferGeometry( 400, 400 ), new THREE.MeshBasicMaterial( { color: 0xe0e0e0 } ) );
  59. plane.position.y = - 200;
  60. plane.rotation.x = - Math.PI / 2;
  61. scene.add( plane );
  62. renderer = new THREE.CanvasRenderer();
  63. renderer.setSize( width, height );
  64. // container.appendChild( renderer.domElement );
  65. effect = new THREE.AsciiEffect( renderer );
  66. effect.setSize( width, height );
  67. container.appendChild( effect.domElement );
  68. stats = new Stats();
  69. container.appendChild( stats.dom );
  70. //
  71. window.addEventListener( 'resize', onWindowResize, false );
  72. }
  73. function onWindowResize() {
  74. camera.aspect = window.innerWidth / window.innerHeight;
  75. camera.updateProjectionMatrix();
  76. renderer.setSize( window.innerWidth, window.innerHeight );
  77. effect.setSize( window.innerWidth, window.innerHeight );
  78. }
  79. //
  80. function animate() {
  81. requestAnimationFrame( animate );
  82. stats.begin();
  83. render();
  84. stats.end();
  85. }
  86. function render() {
  87. var timer = Date.now() - start;
  88. sphere.position.y = Math.abs( Math.sin( timer * 0.002 ) ) * 150;
  89. sphere.rotation.x = timer * 0.0003;
  90. sphere.rotation.z = timer * 0.0002;
  91. controls.update();
  92. effect.render( scene, camera );
  93. }
  94. </script>
  95. </body>
  96. </html>