multiple-scenes-copy-canvas.html 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. <script type="module">
  47. import * as THREE from '../../build/three.module.js';
  48. import {TrackballControls} from '../../examples/jsm/controls/TrackballControls.js';
  49. function main() {
  50. const canvas = document.createElement('canvas');
  51. const renderer = new THREE.WebGLRenderer({canvas, alpha: true});
  52. renderer.setScissorTest(true);
  53. const sceneElements = [];
  54. function addScene(elem, fn) {
  55. const ctx = document.createElement('canvas').getContext('2d');
  56. elem.appendChild(ctx.canvas);
  57. sceneElements.push({elem, ctx, fn});
  58. }
  59. function makeScene(elem) {
  60. const scene = new THREE.Scene();
  61. const fov = 45;
  62. const aspect = 2; // the canvas default
  63. const near = 0.1;
  64. const far = 5;
  65. const camera = new THREE.PerspectiveCamera(fov, aspect, near, far);
  66. camera.position.set(0, 1, 2);
  67. camera.lookAt(0, 0, 0);
  68. scene.add(camera);
  69. const controls = new TrackballControls(camera, elem);
  70. controls.noZoom = true;
  71. controls.noPan = true;
  72. {
  73. const color = 0xFFFFFF;
  74. const intensity = 1;
  75. const light = new THREE.DirectionalLight(color, intensity);
  76. light.position.set(-1, 2, 4);
  77. camera.add(light);
  78. }
  79. return {scene, camera, controls};
  80. }
  81. const sceneInitFunctionsByName = {
  82. 'box': (elem) => {
  83. const {scene, camera, controls} = makeScene(elem);
  84. const geometry = new THREE.BoxGeometry(1, 1, 1);
  85. const material = new THREE.MeshPhongMaterial({color: 'red'});
  86. const mesh = new THREE.Mesh(geometry, material);
  87. scene.add(mesh);
  88. return (time, rect) => {
  89. mesh.rotation.y = time * .1;
  90. camera.aspect = rect.width / rect.height;
  91. camera.updateProjectionMatrix();
  92. controls.handleResize();
  93. controls.update();
  94. renderer.render(scene, camera);
  95. };
  96. },
  97. 'pyramid': (elem) => {
  98. const {scene, camera, controls} = makeScene(elem);
  99. const radius = .8;
  100. const widthSegments = 4;
  101. const heightSegments = 2;
  102. const geometry = new THREE.SphereGeometry(radius, widthSegments, heightSegments);
  103. const material = new THREE.MeshPhongMaterial({
  104. color: 'blue',
  105. flatShading: true,
  106. });
  107. const mesh = new THREE.Mesh(geometry, material);
  108. scene.add(mesh);
  109. return (time, rect) => {
  110. mesh.rotation.y = time * .1;
  111. camera.aspect = rect.width / rect.height;
  112. camera.updateProjectionMatrix();
  113. controls.handleResize();
  114. controls.update();
  115. renderer.render(scene, camera);
  116. };
  117. },
  118. };
  119. document.querySelectorAll('[data-diagram]').forEach((elem) => {
  120. const sceneName = elem.dataset.diagram;
  121. const sceneInitFunction = sceneInitFunctionsByName[sceneName];
  122. const sceneRenderFunction = sceneInitFunction(elem);
  123. addScene(elem, sceneRenderFunction);
  124. });
  125. function render(time) {
  126. time *= 0.001;
  127. for (const {elem, fn, ctx} of sceneElements) {
  128. // get the viewport relative position of this element
  129. const rect = elem.getBoundingClientRect();
  130. const {left, right, top, bottom, width, height} = rect;
  131. const rendererCanvas = renderer.domElement;
  132. const isOffscreen =
  133. bottom < 0 ||
  134. top > window.innerHeight ||
  135. right < 0 ||
  136. left > window.innerWidth;
  137. if (!isOffscreen) {
  138. // make sure the renderer's canvas is big enough
  139. if (rendererCanvas.width < width || rendererCanvas.height < height) {
  140. renderer.setSize(width, height, false);
  141. }
  142. // make sure the canvas for this area is the same size as the area
  143. if (ctx.canvas.width !== width || ctx.canvas.height !== height) {
  144. ctx.canvas.width = width;
  145. ctx.canvas.height = height;
  146. }
  147. renderer.setScissor(0, 0, width, height);
  148. renderer.setViewport(0, 0, width, height);
  149. fn(time, rect);
  150. // copy the rendered scene to this element's canvas
  151. ctx.globalCompositeOperation = 'copy';
  152. ctx.drawImage(
  153. rendererCanvas,
  154. 0, rendererCanvas.height - height, width, height, // src rect
  155. 0, 0, width, height); // dst rect
  156. }
  157. }
  158. requestAnimationFrame(render);
  159. }
  160. requestAnimationFrame(render);
  161. }
  162. main();
  163. </script>
  164. </html>