threejs-multiple-scenes-copy-canvas.html 5.4 KB

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