canvas_ascii_effect.html 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js ASCII - geometry - cube</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/Stats.js"></script>
  19. <script src="js/renderers/AsciiRenderer.js"></script>
  20. <script>
  21. // Just swap out the default CanvasRenderer -> AsciiEffect and things should work
  22. // Let's start with the class spinning cube example from three.js
  23. var container, stats;
  24. var camera, scene, renderer;
  25. var cube, plane;
  26. var targetYRotation = targetXRotation = 0;
  27. var targetYRotationOnMouseDown = targetXRotationOnMouseDown = 0;
  28. var mouseX = 0, mouseY = 0;
  29. var mouseYOnMouseDown = mouseXOnMouseDown = 0;
  30. var windowHalfX = window.innerWidth / 2;
  31. var windowHalfY = window.innerHeight / 2;
  32. init();
  33. animate();
  34. function init() {
  35. container = document.createElement( 'div' );
  36. document.body.appendChild( container );
  37. var info = document.createElement( 'div' );
  38. info.style.position = 'absolute';
  39. info.style.top = '10px';
  40. info.style.width = '100%';
  41. info.style.textAlign = 'center';
  42. info.innerHTML = 'Drag to spin the cube';
  43. container.appendChild( info );
  44. scene = new THREE.Scene();
  45. camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 1, 1000 );
  46. camera.position.y = 150;
  47. camera.position.z = 500;
  48. scene.add( camera );
  49. // Cube
  50. var materials = [];
  51. for ( var i = 0; i < 6; i ++ ) {
  52. materials.push( new THREE.MeshBasicMaterial( { color: Math.random() * 0xffffff } ) );
  53. }
  54. cube = new THREE.Mesh( new THREE.CubeGeometry( 200, 200, 200, 1, 1, 1, materials ), new THREE.MeshFaceMaterial() );
  55. cube.position.y = 150;
  56. scene.add( cube );
  57. // Plane
  58. plane = new THREE.Mesh( new THREE.PlaneGeometry( 400, 400 ), new THREE.MeshBasicMaterial( { color: 0xe0e0e0 } ) );
  59. scene.add( plane );
  60. renderer = new THREE.AsciiEffect(); //THREE.CanvasRenderer
  61. renderer.setSize( window.innerWidth, window.innerHeight );
  62. container.appendChild( renderer.domElement );
  63. stats = new Stats();
  64. stats.domElement.style.position = 'absolute';
  65. stats.domElement.style.top = '0px';
  66. container.appendChild( stats.domElement );
  67. document.addEventListener( 'mousedown', onDocumentMouseDown, false );
  68. document.addEventListener( 'touchstart', onDocumentTouchStart, false );
  69. document.addEventListener( 'touchmove', onDocumentTouchMove, false );
  70. }
  71. //
  72. function onDocumentMouseDown( event ) {
  73. event.preventDefault();
  74. document.addEventListener( 'mousemove', onDocumentMouseMove, false );
  75. document.addEventListener( 'mouseup', onDocumentMouseUp, false );
  76. document.addEventListener( 'mouseout', onDocumentMouseOut, false );
  77. mouseXOnMouseDown = event.clientX - windowHalfX;
  78. mouseYOnMouseDown = event.clientY - windowHalfY;
  79. targetYRotationOnMouseDown = targetYRotation;
  80. targetXRotationOnMouseDown = targetXRotation;
  81. }
  82. function onDocumentMouseMove( event ) {
  83. mouseX = event.clientX - windowHalfX;
  84. targetYRotation = targetYRotationOnMouseDown + ( mouseX - mouseXOnMouseDown ) * 0.02;
  85. targetXRotation = targetXRotationOnMouseDown + ( mouseY - mouseYOnMouseDown ) * 0.02;
  86. }
  87. function onDocumentMouseUp( event ) {
  88. document.removeEventListener( 'mousemove', onDocumentMouseMove, false );
  89. document.removeEventListener( 'mouseup', onDocumentMouseUp, false );
  90. document.removeEventListener( 'mouseout', onDocumentMouseOut, false );
  91. }
  92. function onDocumentMouseOut( event ) {
  93. document.removeEventListener( 'mousemove', onDocumentMouseMove, false );
  94. document.removeEventListener( 'mouseup', onDocumentMouseUp, false );
  95. document.removeEventListener( 'mouseout', onDocumentMouseOut, false );
  96. }
  97. function onDocumentTouchStart( event ) {
  98. if ( event.touches.length == 1 ) {
  99. event.preventDefault();
  100. mouseXOnMouseDown = event.touches[ 0 ].pageX - windowHalfX;
  101. targetRotationOnMouseDown = targetRotation;
  102. }
  103. }
  104. function onDocumentTouchMove( event ) {
  105. if ( event.touches.length == 1 ) {
  106. event.preventDefault();
  107. mouseX = event.touches[ 0 ].pageX - windowHalfX;
  108. targetRotation = targetRotationOnMouseDown + ( mouseX - mouseXOnMouseDown ) * 0.05;;
  109. }
  110. }
  111. //
  112. function animate() {
  113. requestAnimationFrame( animate );
  114. render();
  115. stats.update();
  116. }
  117. function render() {
  118. cube.rotation.x += ( targetXRotation - cube.rotation.x ) * 0.05;
  119. plane.rotation.y = cube.rotation.y += ( targetYRotation - cube.rotation.y ) * 0.05;
  120. renderer.render( scene, camera );
  121. }
  122. </script>
  123. </body>
  124. </html>