multiple-scenes-copy-canvas.html 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. <!-- Licensed under a BSD license. See license.html for license -->
  2. <!DOCTYPE html>
  3. <html>
  4. <head>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes">
  7. <title>Three.js - Multiple Scenes - Copy</title>
  8. <style>
  9. canvas {
  10. width: 100%;
  11. height: 100%;
  12. display: block;
  13. }
  14. *[data-diagram] {
  15. display: inline-block;
  16. width: 5em;
  17. height: 3em;
  18. }
  19. .left {
  20. float: left;
  21. margin-right: .25em;
  22. }
  23. .right {
  24. float: right;
  25. margin-left: .25em;
  26. }
  27. p {
  28. margin: 1em auto;
  29. max-width: 500px;
  30. font-size: xx-large;
  31. }
  32. </style>
  33. </head>
  34. <body>
  35. <p>
  36. <span data-diagram="box" class="left"></span>
  37. I love boxes. Presents come in boxes.
  38. When I find a new box I'm always excited to find out what's inside.
  39. </p>
  40. <p>
  41. <span data-diagram="pyramid" class="right"></span>
  42. When I was a kid I dreamed of going on an expedition inside a pyramid
  43. and finding a undiscovered tomb full of mummies and treasure.
  44. </p>
  45. </body>
  46. <!-- Import maps polyfill -->
  47. <!-- Remove this when import maps will be widely supported -->
  48. <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
  49. <script type="importmap">
  50. {
  51. "imports": {
  52. "three": "../../build/three.module.js",
  53. "three/addons/": "../../examples/jsm/"
  54. }
  55. }
  56. </script>
  57. <script type="module">
  58. import * as THREE from 'three';
  59. import { TrackballControls } from 'three/addons/controls/TrackballControls.js';
  60. function main() {
  61. const canvas = document.createElement( 'canvas' );
  62. const renderer = new THREE.WebGLRenderer( { antialias: true, canvas, alpha: true } );
  63. renderer.setScissorTest( true );
  64. const sceneElements = [];
  65. function addScene( elem, fn ) {
  66. const ctx = document.createElement( 'canvas' ).getContext( '2d' );
  67. elem.appendChild( ctx.canvas );
  68. sceneElements.push( { elem, ctx, fn } );
  69. }
  70. function makeScene( elem ) {
  71. const scene = new THREE.Scene();
  72. const fov = 45;
  73. const aspect = 2; // the canvas default
  74. const near = 0.1;
  75. const far = 5;
  76. const camera = new THREE.PerspectiveCamera( fov, aspect, near, far );
  77. camera.position.set( 0, 1, 2 );
  78. camera.lookAt( 0, 0, 0 );
  79. scene.add( camera );
  80. const controls = new TrackballControls( camera, elem );
  81. controls.noZoom = true;
  82. controls.noPan = true;
  83. {
  84. const color = 0xFFFFFF;
  85. const intensity = 3;
  86. const light = new THREE.DirectionalLight( color, intensity );
  87. light.position.set( - 1, 2, 4 );
  88. camera.add( light );
  89. }
  90. return { scene, camera, controls };
  91. }
  92. const sceneInitFunctionsByName = {
  93. 'box': ( elem ) => {
  94. const { scene, camera, controls } = makeScene( elem );
  95. const geometry = new THREE.BoxGeometry( 1, 1, 1 );
  96. const material = new THREE.MeshPhongMaterial( { color: 'red' } );
  97. const mesh = new THREE.Mesh( geometry, material );
  98. scene.add( mesh );
  99. return ( time, rect ) => {
  100. mesh.rotation.y = time * .1;
  101. camera.aspect = rect.width / rect.height;
  102. camera.updateProjectionMatrix();
  103. controls.handleResize();
  104. controls.update();
  105. renderer.render( scene, camera );
  106. };
  107. },
  108. 'pyramid': ( elem ) => {
  109. const { scene, camera, controls } = makeScene( elem );
  110. const radius = .8;
  111. const widthSegments = 4;
  112. const heightSegments = 2;
  113. const geometry = new THREE.SphereGeometry( radius, widthSegments, heightSegments );
  114. const material = new THREE.MeshPhongMaterial( {
  115. color: 'blue',
  116. flatShading: true,
  117. } );
  118. const mesh = new THREE.Mesh( geometry, material );
  119. scene.add( mesh );
  120. return ( time, rect ) => {
  121. mesh.rotation.y = time * .1;
  122. camera.aspect = rect.width / rect.height;
  123. camera.updateProjectionMatrix();
  124. controls.handleResize();
  125. controls.update();
  126. renderer.render( scene, camera );
  127. };
  128. },
  129. };
  130. document.querySelectorAll( '[data-diagram]' ).forEach( ( elem ) => {
  131. const sceneName = elem.dataset.diagram;
  132. const sceneInitFunction = sceneInitFunctionsByName[ sceneName ];
  133. const sceneRenderFunction = sceneInitFunction( elem );
  134. addScene( elem, sceneRenderFunction );
  135. } );
  136. function render( time ) {
  137. time *= 0.001;
  138. for ( const { elem, fn, ctx } of sceneElements ) {
  139. // get the viewport relative position of this element
  140. const rect = elem.getBoundingClientRect();
  141. const { left, right, top, bottom, width, height } = rect;
  142. const rendererCanvas = renderer.domElement;
  143. const isOffscreen =
  144. bottom < 0 ||
  145. top > window.innerHeight ||
  146. right < 0 ||
  147. left > window.innerWidth;
  148. if ( ! isOffscreen ) {
  149. // make sure the renderer's canvas is big enough
  150. if ( rendererCanvas.width < width || rendererCanvas.height < height ) {
  151. renderer.setSize( width, height, false );
  152. }
  153. // make sure the canvas for this area is the same size as the area
  154. if ( ctx.canvas.width !== width || ctx.canvas.height !== height ) {
  155. ctx.canvas.width = width;
  156. ctx.canvas.height = height;
  157. }
  158. renderer.setScissor( 0, 0, width, height );
  159. renderer.setViewport( 0, 0, width, height );
  160. fn( time, rect );
  161. // copy the rendered scene to this element's canvas
  162. ctx.globalCompositeOperation = 'copy';
  163. ctx.drawImage(
  164. rendererCanvas,
  165. 0, rendererCanvas.height - height, width, height, // src rect
  166. 0, 0, width, height ); // dst rect
  167. }
  168. }
  169. requestAnimationFrame( render );
  170. }
  171. requestAnimationFrame( render );
  172. }
  173. main();
  174. </script>
  175. </html>