canvas_ascii_effect.html 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 sphere, plane;
  27. var start = Date.now();
  28. init();
  29. animate();
  30. function init() {
  31. var width = window.innerWidth || 2;
  32. var height = window.innerHeight || 2;
  33. container = document.createElement( 'div' );
  34. document.body.appendChild( container );
  35. var info = document.createElement( 'div' );
  36. info.style.position = 'absolute';
  37. info.style.top = '10px';
  38. info.style.width = '100%';
  39. info.style.textAlign = 'center';
  40. info.innerHTML = 'Drag to change the view';
  41. container.appendChild( info );
  42. camera = new THREE.PerspectiveCamera( 70, width / height, 1, 1000 );
  43. camera.position.y = 150;
  44. camera.position.z = 500;
  45. controls = new THREE.TrackballControls( camera );
  46. scene = new THREE.Scene();
  47. var light = new THREE.PointLight( 0xffffff );
  48. light.position.set( 500, 500, 500 );
  49. scene.add( light );
  50. var light = new THREE.PointLight( 0xffffff, 0.25 );
  51. light.position.set( - 500, - 500, - 500 );
  52. scene.add( light );
  53. sphere = new THREE.Mesh( new THREE.SphereGeometry( 200, 20, 10 ), new THREE.MeshLambertMaterial() );
  54. scene.add( sphere );
  55. // Plane
  56. plane = new THREE.Mesh( new THREE.PlaneBufferGeometry( 400, 400 ), new THREE.MeshBasicMaterial( { color: 0xe0e0e0 } ) );
  57. plane.position.y = - 200;
  58. plane.rotation.x = - Math.PI / 2;
  59. scene.add( plane );
  60. renderer = new THREE.CanvasRenderer();
  61. renderer.setClearColor( 0xf0f0f0 );
  62. renderer.setSize( width, height );
  63. // container.appendChild( renderer.domElement );
  64. effect = new THREE.AsciiEffect( renderer );
  65. effect.setSize( width, height );
  66. container.appendChild( effect.domElement );
  67. stats = new Stats();
  68. container.appendChild( stats.dom );
  69. //
  70. window.addEventListener( 'resize', onWindowResize, false );
  71. }
  72. function onWindowResize() {
  73. camera.aspect = window.innerWidth / window.innerHeight;
  74. camera.updateProjectionMatrix();
  75. renderer.setSize( window.innerWidth, window.innerHeight );
  76. effect.setSize( window.innerWidth, window.innerHeight );
  77. }
  78. //
  79. function animate() {
  80. requestAnimationFrame( animate );
  81. stats.begin();
  82. render();
  83. stats.end();
  84. }
  85. function render() {
  86. var timer = Date.now() - start;
  87. sphere.position.y = Math.abs( Math.sin( timer * 0.002 ) ) * 150;
  88. sphere.rotation.x = timer * 0.0003;
  89. sphere.rotation.z = timer * 0.0002;
  90. controls.update();
  91. effect.render( scene, camera );
  92. }
  93. </script>
  94. </body>
  95. </html>