canvas_ascii_effect.html 3.2 KB

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